March 25, 2008

Happy Easter...

...once again, a few days late. Also, it is somewhat telling that my latest post was to wish you a happy new year.

Oh well. As usual with these posts, I bring news on my Implementing QuantLib drafts. If you follow the link, you'll find a new version of chapter 3 in which I completed the section on yield term structures. As usual, I'll be grateful for any comments and/or corrections.

Luigi

January 03, 2008

Happy New Year!

I'm a few days late, I know. I was on vacation after the flurry of activity at the end of 2007; we had a new QuantLib release on December 24th (if you're interested, you can get it while it's hot at quantlib.org). My drafts of Implementing QuantLib got updated, too; there's no new content, but the existing material was modified to reflect changes in the library.

Again, happy 2008.

Luigi

November 05, 2007

Luigi Ballabio: implementing QuantLib (again)

I really have to come up with a catchier title for these posts.

However: the Implementing QuantLib page now links (thanks to a suggestion from Comp++'s own Daniel Duffy) to a tentative table of contents. Also new is a small appendix describing the code conventions we use in QuantLib---and I use in the book.

As usual, I'll be grateful for any comments and corrections.

Luigi

October 31, 2007

Luigi Ballabio: Implementing QuantLib

Hi all, Another half chapter of Implementing QuantLib is available. As promised, it describes a few classes I glossed over in the previous chapters. You can find the draft here.

As usual, comments are welcome.

Luigi

October 15, 2007

NYC Quantitative Modeling & Financial Market Dynamics

World renowned experts will be speaking at Quantitative Modeling & Financial Market Dynamics , to be held from 4-8 pm Oct. 31 at 7city Learning, 55 Broad Street. This first annual QuantDay event will be hosted by Numerical Algorithms Group (NAG), in conjunction with Wilmott magazine, 7city Learning and Quantstar.

Speakers at the QuantDay event include: Dr. Robert Tong – "Numerical Software, Market Data and Extreme Events."

Dr. Mike Giles (Risk Magazine’s Quant of the Year 2007 – “Multilevel Monte Carlo Path Simulation”

Dr. John Birge – “Dynamic Portfolio Optimization Using Decomposition and Finite-Element Methods”

Financial quantitative analysts and other financial industry managers can find more details on QuantDay seminars and/or register to attend this QuantDay event at http://www.nag.com/market/quantday2007.asp or by contacting Kurt Peckman, kpeckman@nag.com, 630-598-5216.

QuantDay is the first public Numerical Algorithms Group event for financial quantitative analysts to be hosted in North America, and is modeled after the popular NAG series for financial quantitative analysts in The City area of London.

Tom

September 26, 2007

Implementing QuantLib Part 2

Hi all,

Just a short post to inform you that I've uploaded another half chapter of my ongoing book---as well as a revision of the previous chapter. You can find the draft here.

As usual, comments are welcome.

Luigi

September 24, 2007

Interested in Computational Finance? Join the C(omp) Community and start Blogging!

                           

Contributing to the C(omp)++ blog couldn't be simpler.

  1. Allow us to build a profile for you alongside the community experts and professionals already taking part.  Please complete this form and return it to us at comp@wiley.co.uk.  Please also send a headshot of yourself for the profile.
  2. Next, to start blogging, simply send us your blog posts as .txt (e.g. Notepad) or .doc (e.g. Word) files and we will upload and publish them for you.  Feel secure in the knowledge that we will ensure your post appears properly formatted for the blog. 
    NB: Posts can include equations, code and diagrams - please send these as individual image files for easier formatting.
  3. Finally, include a selection of keywords by which you would like your post categorized (around 4-5 is optimum), to help people find posts on the topics they are most interested in.
  4. And that's it!  Sit back, relax and watch your blogging career take flight from the comfort of your own desk without the complications of having to manage your own blog.  C(omp)++ is a thriving and constantly growing community that we want YOU to be a part of. 

    With visitors from over 140 countries worldwide, this is THE Computational Finance community to join.

    Remember, ANY questions, send us an email.

    Best wishes from your C(omp)++ moderator team.

September 14, 2007

IOStreams & custom formatting objects

by Thijs van den Berg

Recently I had to add support for multiple output formats of some C++ data structures that I was generating. Something along the lines of writing a matrix data structure to a file in either

  • binary format,
  • comma separate file (csv),
  • HTML tables,
  • ...

My idea  was to implement a stream syntax ala setw()  which is used to set the field width for the next stream insertion operation.   

std::cout << std::setw(2) << d;

The nice thing about this approach is that the formatting details are stored in the stream, -not in all of the objects-. For specifying file types, that would be the best place to store that information. All object can query the formatting information from the stream, and serialize themselves in the corresponding format. The solution I ended up with looks like this

std::cout << my_format::HTML << some_object;

std::cout << another_object;

Attaching custom meta data to a stream

Central to this approach is to use ios_base::xalloc() to get a index to a storage location which will be available in all streams to write and read data (an integer) to/from..

static const int iword_index = std::ios_base::xalloc();

The above statement gives us a index that we name iword_index, and which can be user to store...

std::ostream os1,os2;

os1.iword(iword_index) = 3;

os2.iword(iword_index) = 17;

and retrieve values that are attached to streams

switch ( os1.iword(iword_index) )
{
   
case 3: os1 << "format number 3:" << ...
}

September 07, 2007

An Introduction to the Boost Library

by Daniel Duffy

C++ continues to evolve and already STL is part of the official standard. I would like to discuss some new developments that are taking place and in particular we give an overview of the boost library (www.boost.org), a suite of useful C++ libraries containing functionality in the form of template classes:

  • Multiarray: defines a generic interface to multidimensional containers
  • Random numbers: contains a number of classes for generators and statistical distributions
  • Property map: classes that embody key-value pairs and definition of the corresponding access (for example read and write)
  • Smart pointers: objects that store pointers to dynamically allocated (heap) objects
  • Graph library: a C++ library implementing graphs, networks and related graph algorithms

The authors (as well as many other software developers) have developed a subset of the functionality contained in these libraries. For example, in Duffy 2004 and Duffy 2006 we have implemented a template Property Map class with properties similar to that in boost; furthermore, we have created classes for two-dimensional matrices and three-dimensional tensors using nested STL classes. If we were to build such classes again, we would choose to use the boost library as underlying substrate.
We give a short description of the functionality in the above libraries. At this stage we avoid dealing with the C++ details on how to use these libraries in an application (they will be discussed later). We summarise each library as a list of features:

   

Multiarray
    Array classes for n-dimensional data
    Accessing the elements of array using () and [] operators
    Creating views of an array having the same or less dimensions
    Determining storage ordering of data (C-style, Fortran-style or user-defined)
    Defining or changing array index (zero is default)
    Changing an array’s shape
    Resizing an array (increasing or decreasing the extent of a dimension in an array)

   

Random Numbers
    Linear congruential generators
    Mersenne Twister generator
    Lagged Fibonacci generators
    Classes for continuous statistical distributions
    Classes for discrete statistical distributions

   

Property Maps
    Key-value pair concept
    Readable and writable data
    Support for built-in C++ data types
    Applicability to Boost Graph Library (BGL)

   

Smart Pointers
    Sole ownership to single objects and arrays
    Shared ownership of objects and arrays
    Shared ownership of objects with embedded reference counting

The gradual introduction of these features in applications will promote the reliability, maintainability and efficiency of your software. We shall see next how to use these libraries and to apply them to the Monte Carlo method and other applications in finance.

In the next blogs we shall give some examples in C++ and applications to the Monte Carlo method.

July 03, 2007

C++ Parallel Programming in Computational Finance, Part II: A “Hello World, 101” Example using OpenMP

Daniel J. Duffy

In the previous blog (Part I) I gave a high-level overview of the OpenMP Application Programming Interface (API). OpenMP consists of library functions, directives and environment variables that allow developers to create multi-threaded code on shared-memory architectures.

In this blog I would like to give a concrete example of using OpenMP C++ code. The example is simple but it does show how serial code can be made parallel. We concentrate on a problem that creates two STL vectors, calculates their inner product and then prints them on the console. The code was compiled under Microsoft VS2005 that supports OpenMP. We have created a Win32 console application and all code is placed in one file. The first statements tell the compiler that we are using STL and OpenMP:

 

#include <vector>

#include <iostream>

#include <omp.h>

      using namespace std;

After having done this we can then use the OpenMP API library functions to parallelise the code. The main program is very simple:


int main()
{

// Preprocessing: Input

cout << "Give size of the arrays: ";
      int N; cin >> N;
      cout << "Give value in the first array: ";

      double val1; cin >> val1;
      cout << "Give value in the second array: ";

      double val2; cin >> val2;

      // Processing; Data and algorithms
      vector<double> v1(N, val1);
      vector<double> v2(N, val2);

      double result = InnerProduct(v1, v2); // Sum of products

      // Postprocessing: Output
      print(v1);
      print(v2);

      cout << endl << "Inner product is: " << result << endl;

      return 0;

}

This program prompts for input and then creates two STL vectors. It then calculates their inner product and prints both of them on the console. This program is serial but the functions for calculating the inner product and printing use loop-level parallel pragmas. First, the code for the inner product is:

 

double InnerProduct(const vector<double>& v1, const vector<double>& v2)

{

      double result = v1[0] * v2[0];

      // Assume sizes of v1 and v2 are equal

      // Perform a reduction
      #pragma omp parallel for reduction (+: result)
      for (int j = 1; j < (int)v1.size(); ++j)
      {
          result += v1[j]*v2[j];
      }

// implicit barrier here

      return result;

}

The presence of an OpenMP directive ensures that the master thread forks a number of child threads. Each thread is allocated parts of the work to calculate the inner product. Each thread’s contribution is added to a global variable result. We use a special keyword reduction in order to add the individual contributions and to avoid race conditions at the same time.

The code for printing a vector is given by:

void print(const vector<double>& vec)

{

     cout << endl;

     // Since we only read the values of vec, the default shared
     // variable access is OK
     #pragma omp parallel for
     for(int j=0; j < (int)vec.size(); ++j)
     {
          cout << "vec[" << j << "] = " << vec[j] << endl;
     }

     // implicit barrier here

     cout << endl;

}

In this case, multiple threads are created and each thread is responsible for printing one block of the vector. We mention that a so-called implicit barrier is defined in both functions at the end of loop. This implies that the threads are removed and the code goes back into serial mode.

We can draw some conclusions: first, we see that it is easy to incorporate parallel commands in serial code in order to improve speedup. Second, OpenMP API takes care of thread creation and destruction and this fact lessens the burden on the developer. Finally, the loop-level code can be used to improve the performance of matrix-based computations in finance. For example, in some cases speedup of 80% is possible on duo-core machines.

The full source code for the test program can be found at www.datasimfinancial.com (where you can register and log into the forum, see the OpenMP thread).

In the next blog I shall discuss the application of coarse-grained techniques to the development of efficient code for Monte Carlo applications.


C(omp) Search


WWW
compplusplus.com

C(omp) Community

Could this be you? Thijs van den Berg Dr. Jörg Kienitz Bjarne Stroustrup Dr. Egor Kraev Daniel Duffy Andrea Germani Umberto Cherubini Luigi Ballabio

More Members

Meet the Editorial Team



C(omp) Feeds


Want to know when new posts and features are made available? Sign up to receive email notifications by entering your email address:

Delivered by FeedBurner



Any Comments?

Send in questions for our authors and bloggers: comp@wiley.co.uk



C(omp) Events

1) 13-15 November: Quant Invest 2007
Russell Hotel, London
Key speakers include Sushil Wadhhwani, Paul Wilmott and Deborah Fuhr...

2) 30 November: CCCP Mathematical Finance Conference
Princeton University
Speakers include Paul Glasserman, Peter Carr and Rama Cont.

3) 10-14 December: Risk Minds 2007
President Wilson, Geneva

4) 12-15 December: Quantitative Methods in Finance
Manly Pacific Hotel
Sydney, Australia
Speakers include Mark Joshi

5) January 2008: Distance Learning for Financial Engineers
Computational and Quantitative Finance in C++
Datasim Education BV


Recent Forum Discussions


C(omp) Calendar

March 2008
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31