The Boost Library; Overview, Code, Applications

Postby Cuchulainn » Fri Apr 17, 2009 12:42 pm

Maths Toolkit, Part I: Statistical Distributions

///



4. The Boost Math Toolkit contains a number of template classes for a wide range of univariate continuous and discrete probability distributions. We can define a probability distribution by giving its defining parameters and using them in the constructor of the corresponding template class. In general, each distribution has member functions to compute mean and standard deviation while the most extensive functionality is to be found in free (that is, non-member) functions. The library supports the following categories of functions:



. Essential functions (pdf, cdf, cdf complement)

. Measures of central tendency (mean, median, mode, quantile)

. Measures of dispersion (standard deviation, variance)

. Kurtosis, kurtosis excess, hazard functions



The library contains many of the most popular discrete and continuous probability distribution functions that we can use in computational finance. It is worth mentioning that it has now support for the Students t-distribution, Gamma distribution, Chi Squared distribution and the Noncentral Chi Squared distribution.

What are the advantages of this library in our opinion?



. Standardisation (the code has been peer-reviewed and conforms to the boost design standard)

. Quality: the code is efficient, robust and portable. As developer, you use the library without having to be concerned with its maintenance

. Building applications: you can use the classes in the library as part of large software systems

. No more pseudo-code needed: instead of discussing non-runnable code in books, articles and other finance literature, we could use code from Boost.Math directly, thus allowing readers to check the validity of numerical results.
Attachments
Test101.cpp
(2.54 KiB) Downloaded 972 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Fri Apr 17, 2009 12:57 pm

Maths Toolkit, Part II: Building a lookup table for the Noncentral Chi^2 distribution using assocative matrices and Excel coupling

///



Word 2007 (docx) and 2003(doc) versions!!!!
Attachments
LookupTableNonCentralChiSquaredDistribution.docx
(19.22 KiB) Downloaded 875 times
LookupTableNonCentralChiSquaredDistribution.doc
(47.5 KiB) Downloaded 912 times
TestNonCentralChiSquared.cpp
(5.37 KiB) Downloaded 1015 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Apr 19, 2009 11:48 am

Special Functions

//



We introduce a number of software modules from the boost Math Toolkit for the so-called special functions:



. Gamma and beta functions

. Error function

. Bessel functions

. Elliptic integrals

. Orthogonal polynomials, for example Legendre, Laguerre and Hermite polynomials



These functions have many applications in mathematical physics, statistics and engineering. For example, we see that a number of statistical distributions are defined in terms of gamma and beta functions. Other applications include the solution of differential equations, signal processing and eigenvalue analysis. The library supports many of the functionality that these applications need. Using the library in your applications is easy and an advantage is the code is portable and you do not have to write your own library suite or use proprietary class libraries.
Attachments
TestZetaFunction.cpp
(857 Bytes) Downloaded 900 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu Apr 23, 2009 5:32 pm

A very simple one this time: 3-valued logic
Attachments
TestTribool101.cpp
(704 Bytes) Downloaded 906 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Tue Apr 28, 2009 3:28 pm

boost Maths supports the robust Brent function minimisation procedure. Useful in more complex optimisation routines.
Attachments
TestBrent101_A.cpp
(1.17 KiB) Downloaded 1017 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun May 03, 2009 12:19 pm

Smart pointers in Boost.

///////////////////////////



We give a short review of the potential problems that we can experience using standard C++ pointers. To this end, we sketch a number of erroneous scenarios that show what can go wrong when memory is not properly allocated and deallocated. The problem is that it is the responsibility of the programmer to ensure that all memory lifecycle issues are correctly implemented. Of course, this is not always realized in practice, especially in large, complex or badly-designed application.



We give a general introduction to a number of techniques to manage the memory in a

running application. This is called garbage collection in general and it encompasses a spectrum of scenarios:



S1. automatic garbage collection: a collector attempts to reclaim memory (garbage) that will never be accessed again in an application. No intervention on the part of the developer is necessary as the system frees the developer from manually dealing with memory allocation and deallocation.

S2. manual garbage collection: the developer is responsible for all memory allocation and deallocation. This is the regime when we use C and C++ pointers to allocate heap memory.

S3. reference counting: a technique to store the number of references, handles or pointers to a resource (such as memory). The main use of this technique is allow the deallocation of objects that are no longer referenced.



Languages such as Java and C# require automatic garbage collection and are sometimes called garbage-collected languages. C++, on the other hand does not have such automatic garbage collection features and thus it is the responsibility of the developer to ensure that all resources are allocated and deallocated.



We give examples of scoped and shared smart pointers.
Attachments
TestScoped001.cpp
(1.19 KiB) Downloaded 869 times
TestSharedPtr001.cpp
(2.1 KiB) Downloaded 853 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sat May 09, 2009 12:00 pm

Interlude: get smart: test your knowledge of smart pointers
Attachments
exercisesmemorymanagement_138.doc
(49.5 KiB) Downloaded 860 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu May 14, 2009 5:11 pm

In this chapter we introduce the Boost Multidimensional Array Library (MAL) that provides a template class to allow developers create generalized data structures in n-dimensional space, for example matrices (two-dimensional) and tensors (three-dimensional). The Standard Library is lacking in support for these structures and the usual workaround is to create nested vector classes. This approach leads to code that takes time to write, debug and integrate into applications.

The arrays in the MAL are accessed using the same syntax as with native C++ arrays. The library has a number of template classes to model n-dimensional data of arbitrary type and wrapper classes for standard C arrays. It is possible to create what are in fact n-dimensional hypercubes and then access their elements using the familiar operator []. Some of the other functionality is:



. Creating views and slices in a multi-array

. Storage order: C-style (row-major), Fortran (column-order) or user-defined

. Changing an array?s shape

. Resizing an array



The library is suitable when we model volatility surfaces, for example. In this case we can model the underlying data as a boost Tuple.
Attachments
MultiArrayTuple.cpp
(1.77 KiB) Downloaded 940 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon May 18, 2009 4:58 pm

Boost and Design Patterns

////////////////////////////////



The boost library is not actually a book, but a number of its libraries are in fact generic implementations of GOF patterns. The following are well worth looking into





Flyweight (==> GOF flyweight and factories)

Function (C and OO callbacks, i.e. Observer, Mediator and Command)

Signals and Slots (Observer+++!)

Spirit (Interpreter)

Any (use in Chain of Responsibility in combination with Signals)



These libraries are so good that there is almost no need to program all the plumbing that GOF does (and no OO hierarchies needed because boost uses parametric polymorphism).
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Tue Jun 02, 2009 4:18 pm

The Math Common Factor library has support for computing gcd (greatest comon divisor) and lcm (least common multiple) in the form of template function objects, function templates and functions that take unsigned long arguments. They are parametrized by a numeric type representing integers. Here is code that shows the use of each option:





#include <boost/math/common_factor_rt.hpp> // for gcd and lcm, run time

#include <boost/math/common_factor_ct.hpp> // compile time version







int main ()

{



int n1 = 42;

int n2 = 56;



// Using function templates

int g = boost::math::gcd<int>(n1, n2);

cout << "gcd: " << g << endl;



int least = boost::math::lcm<int>(n1, n2);

cout << "lcm: " << least << endl;



// Using function objects

boost::math::gcd_evaluator<int> gcdVal;

cout << gcdVal(n1, n2) << endl;



boost::math::lcm_evaluator<int> lcmVal;

cout << lcmVal(n1, n2) << endl;





// Using unsigned long (compile time)

cout << "GCD and LCM of " << n1 << " and " << n2 << "are "

<< boost::math::static_gcd<42, 56>::value << " and "

<< boost::math::static_lcm<42, 56>::value << endl;



return 0;

}
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon Jun 22, 2009 2:44 pm

Some examples of the use of orthogonal polynomials in boost, in this case Legendre polynomials.
Attachments
OrthogonalPolynomial.jpg
OrthogonalPolynomial.jpg (233.87 KiB) Viewed 35781 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon Jun 22, 2009 4:13 pm

Hermite polynomial in boost (using the Visualizer from my book).
Attachments
Hermite.jpg
Hermite.jpg (183.23 KiB) Viewed 36856 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Jun 28, 2009 12:52 pm

This example combines boost::Function with template template parameters to create flexible s/w, in this case a Command pattern. It is like a plug and socket architecture.
Attachments
TemplateTemplate101.cpp
(1.68 KiB) Downloaded 902 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu Aug 13, 2009 1:07 pm

Here is code that interoperates between BOOST and C#!!
Attachments
BoostMath.hpp
(536 Bytes) Downloaded 1031 times
BoostMath.cpp
(372 Bytes) Downloaded 907 times
UniformDistribution.hpp
(940 Bytes) Downloaded 983 times
UniformDistribution.cpp
(1 KiB) Downloaded 923 times
Main.cs
(804 Bytes) Downloaded 957 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sat Aug 15, 2009 10:38 am

Here is the next example; it calls functions for non central chi^2 that boosts supports (writing the code anew in C# would take forever..). Note that I use replicated data structures in C# and C++ and this has positive(!) consequences. No wrapping of vectors, just a narrow channel to the distribution. Couple hours work, at most.



The code is a snippet to create lookup tables based on the 2 defining parameters.

// Make the matrix and fill it

NumericMatrix<double> mat=new NumericMatrix<double>(nRows, nColumns);

for (int r=mat.MinRowIndex; r<=mat.MaxRowIndex; ++r)

{

c1=nonCentralParameterColumns.Start;

for (int c=mat.MinColumnIndex; c<=mat.MaxColumnIndex; ++c)

{

// THIS IS WHERE WE CALL THE BOOST CODE

cs=BoostMath.Quantile(new ChiSquaredDistribution(r1), 0.05);

mat[r, c]=BoostMath.Cdf(new NonCentralChiSquaredDistribution(r1, c1), cs);

c1+=incrementColumn;

}

r1+=incrementRow;

}



// Make it into an associative matrix and print it

AssocMatrix<double, double, double> myAssocMat=new AssocMatrix<double,double,double>(dofSet, nonCentralParameterSet, mat);

Print(myAssocMat);



// Send associative matrix to Excel

ExcelMechanisms excel=new ExcelMechanisms();

excel.printAssocMatrixInExcel(myAssocMat, "NCCQT");

}
Attachments
SnapShot.jpg
SnapShot.jpg (333.31 KiB) Viewed 39644 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

PreviousNext

Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron