The approach I describe here is somewhat in the middle. It requires some manual work comparing and judging results and utilises automation to obtain the data to compare.

While working my way through the book: C++ Design Patterns and Derivatives Pricing, I needed to establish a performance baseline to compare the different implementation. A nice implementation should not cost anything or as close to that as possible.

To do this we must establish a test scenario.

The example used throughout the book is a Monte Carlo simulation. One of the main parameters here is the number of iterations done, which we will call n. Experimenting a little bit we see that the accuracy goes up with the number of iterations. We also see that the average time pr. iteration levels of as the number of iterations increase.

We fix the number of iterations at powers of 2 and set the scale of the x-axis to be logarithmic.

We do two different measures, an absolute time and a time pr iteration.

First the absolute time with the y-axis on a logarithmic scale. We expect that time grow linearly with the number of iterations, i.e. time is O(n). This is shown nicely in the graph below.

Then we have the time pr. iteration, also with the y-axis on a logarithmic scale. We expect the time of the iterations to approach some constant, i.e. time pr. iteration is O(1). Which we also see in the graph below.

Finally we graph the price output at the end of each simulation. This gives us an idea about how fast the price converges.

This establish our performance baseline.

]]>The book “C++ Design Patterns and Derivatives Pricing” by Mark S. Joshi, takes us on a tour of applying design patterns in the financial domain.

The book is well written, its target audience is for people with a non computer science background who write C++ programs for modelling financial products. The book takes you from writing a procedural program for monte carlo simulation of a simple financial product, to a well organised Object Oriented program that makes use of Design Patterns.

I wanted to not just read the book, I wanted to get a practical feel for the examples. Implementing all the examples from the book gives insight into where and how the C++ and the Design Patterns used work together. I also implemented some testing that was outside the scope of the book. This makes it possible to judge the performance of the choices made based on timings and profiling of the code.

Mark makes the point about code reuse many times in the book and gives good arguments for why that is a good idea. I think he could have done more using templates, and following the ideas from Andrei Alexandrescu’s book “Modern C++ Design”

The book is well worth reading, and I recommend it.

]]>