http://www.bnikolic.co.uk/blog/blog Bojan's Blog A blog on various computing related items by Bojan Nikolic. See http://www.bnikolic.co.uk/blog/index.html . 2010-08-20T01:00:09Z Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-boost-bind-not-struct-error.html Errors like "is not a class, struct, or union type" when using <paragraph>Decoding compile-time errors when using complex template libraries (like <reference name="Boost" refuri="http://www.boost.org/doc/libs/1_35_0">Boost</reference>) can be a bit involved with the current generation of c++ compilers. Hopefully this will improve as the compilers are improved but in the meantime it is useful to keep track of the type of errors that you get and why. Here is an example of one of those non-obvious errors.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2010-05-24T17:20:34Z http://www.bnikolic.co.uk/blog/cpp-khachiyan-min-cov-ellipsoid.html C++ implementation of Khachiyan algorithm for the minimum enclosing (or covering) ellipsoid <paragraph>The problem in this case consists of computing the hyper-ellipsoid (potentially in a space of high-dimensionality) such that it has the minimum volume but still encloses all of the points in a supplied set. The ellipsoid is specified by it's centre and a matrix describing it's axes.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-class-hirearchy.html RR: Avoiding Artificial Class Hierarchies <paragraph>Polymorphism is a cornerstone of object oriented programing. It allows run-time resolution of the class function (method) to be called, depending on concrete type of the object. This is of course extremely useful, but requirement for run-time resolution of function called should <strong>not</strong> be by itself the reason for deciding on a deep and complex class hierarchy. If run-time resolution is all that is required, then much simpler solutions based on function pointers, or <literal>boost::function</literal> object are much better suited.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/python-running.html Running Python Programs <paragraph>Answering the question "How do I run Python programs?" is the obvious first step in learning how to use and program python, but often people do not spend enough time thinking about this.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-rand.html RR: Basic random number generation <paragraph>Many, many codes and books use the standard C function <literal>rand()</literal> to generate uniformly distributed random numbers. What are the potential pitfalls of using this function? What are the alternatives?</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/python-tips-switch.html Python: To Switch or not to Switch <paragraph>It is easy to fall into the trap of writing <literal>switch</literal> like statements in any programing language. (<literal>switch</literal> is a construct in C and C++; in python it can be replicated by a string of <literal>elif</literal> statements.) Often, for example, this occurs at the interface between applications and libraries. In Python these look something like the following frivolous example:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-heston-simple.html Pricing options using the Heston model using QuantLib <paragraph>Bellow is a simple command line program which uses the Heston model implementation in QuantLib to price equity options. The various parameters of the Heston model can be adjusted through the command line. For a list of these, run the program with the ''--help'' option:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-boost-rand-normal.html Generating Normally Distributed Random Numbers Using Boost <paragraph>One of my <reference name="previous" refuri="cpp-rr-rand-normal.html">previous</reference> posts describes a very minimal program that generates a normally distributed random number using <reference name="Boost" refuri="http://www.boost.org/doc/libs">Boost</reference>. However, I've realised from the number of people visiting that this example may indeed be a bit too short and simple, and lead people to not exactly the best practises. So here is a longer post with more pointers.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-rand-normal.html Simple normally distributed random number generation <paragraph>Here is a very simple program that illustrates how to generate normally distributed random numbers using boost.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/website-simple.html Creating simple websites <paragraph>This website is written primarily in <reference name="re-structured text" refuri="http://docutils.sourceforge.net/rst.html">re-structured text</reference><target ids="re-structured-text" names="re-structured\ text" refuri="http://docutils.sourceforge.net/rst.html" />, a simple text markup language. It is produced to final html form using the following tools:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-boost-uniform01.html A potential pitfall when using boost::uniform_01 <paragraph>The <reference name="Boost" refuri="http://www.boost.org/doc/libs/1_35_0">Boost</reference> library collection has a highly useful <reference name="random number library (Boost.Random)" refuri="http://www.boost.org/doc/libs/1_35_0/libs/random/index.html">random number library (Boost.Random)</reference>. One of the classes in this library, <literal>boost::uniform_01</literal>, does however present a potential pitfall which I will briefly describe in this post. This pitfall is mentioned briefly in the library documentation but this post expands on this substantially.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-about.html RR: About the Random Review Series <paragraph>The aim of the <emphasis>random review</emphasis> series articles is to do a sort of public code-review of C++ code, generally in the field of numerical algorithms. I do not publish the names of original codes to which review articles relate because the hope is that they will be applicable much more widely than the original codes.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/gprs-e61-linux-greece.html Accessing Internet in Greece using Linux, Nokia E61, and a Vodafone data plan contract <paragraph>The aim is to be able to connect my laptop to the Internet over the cellular/wireless networks in Greece at a reasonable price and speed.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2010-05-25T20:51:39Z http://www.bnikolic.co.uk/blog/ql-handle-1-ex.html Relinking Handles <paragraph>Very simple example showing how to relink a Handle object in QuantLib:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-brownian-bridge.html Using QuantLib to generate a Brownian Bridge <paragraph>This is a very short article illustrating how to use the QuantLib library Brownian bridge code. The reason there can be some confusion is that the code takes a sequence of random variates as input for the bridge construction code. The first variate in this sequence is taken to represent the global step of the bridge, i.e., how much the value of the underlying changes over the entire period (for bridge of length of <strong>unit</strong> time only). If the first variate is set to zero, then the standard Brownian bridge that begins and ends at zero will be created.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-checked-delete.html Private implementation using smart pointers and deletion trouble <paragraph>It is often desirable to hide excess implementation detail from users of libraries by using forward declarations and private pointers. There is a lot written on the Internet on this idiom. If you are using it you may very well want to use the <literal>boost::scoped_ptr&lt;&gt;</literal> smart pointer. There is however one simple trap you can fall into doing this and then you may get error messages related to incomplete type such as this:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rand-parallel.html Generating Random in Parallel <paragraph>The classic requirement for this a large Monte-Carlo simulation which you'd like to be able to run across many threads in production, but at the same time would always like to get exactly the same answer regardless of the number of threads that are used. In other words, the exact numerical answer should be exactly the same across multiple runs and independently of the number of threads.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2010-08-20T00:52:47Z http://www.bnikolic.co.uk/blog/ql-heston-implementation.html Implementation of the Heston model in QuantLib <paragraph>The <reference name="QuantLib" refuri="http://quantlib.org/index.shtml">QuantLib</reference> derivatives pricing library provides an algorithm for "analytic" pricing of European-style options under the Heston model. The description "analytic" is conventional but not very precise as the algorithm in fact involves numerical evaluation of an integral. There is however no stepping in time in this algorithm, and so it is certainly distinct from the finite-difference methods that are used to price path-dependent options.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-fitting-curves.html Fitting a Curve in QuantLib <paragraph>Here is a simple example of fitting a curve in QuantLib, reworked from the original CurveFitting.cpp file and adjusted to compile with the current versions of QuantLib:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/python-running-emacs.html Running Python Programs from Emacs <paragraph>Tips on running python from Emacs.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-boost-simple-parse.html Simple input parsing using Boost <paragraph>Inexperienced programmers often get stuck getting data into their C++ program and with output out of their program. Somewhat more experienced programmers often use clunky routines from the Standard C library such as <literal>strtok</literal>, <literal>atof</literal> and friends. I think pretty much everybody agrees that plain C++ does not provide for elegant parsing of simple input data. Fortunately the Boost libraries provide lots of tools to tackle this, and here is a very simple example.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-fx-option-simple.html A simple FX-Option Example in QuantLib <paragraph>Here is a simple, bare-bones example of pricing an American FX vanilla option in QuantLib:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-equity-option-ex.html A minor rework of the Equity Option example in QuantLib <paragraph>To enable some experiments with QuantLib I've reorganised the EquityOption example so that each of the pricing methods sits in a separate function. It is a pretty trivial change but I think it makes the example a bit more readable. Feel free to use in accordance with the QuantLib licence.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/python-windows-setup.html Setting up Python on Win32 platform <paragraph>Here are just the links... this is work in progress:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/website-rst-atom.html Converting a Directory of ReSTructured files to Atom feed <paragraph>In one of the previous posts I sketched out how to <reference name="generate an Atom feed using Python and Kid" refuri="website-atom.html">generate an Atom feed using Python and Kid</reference>. Here I show a complete script that can convert a directory of restructured text files into an Atom feed -- great for writing blogs using restructured text.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/python-running-cline.html Running Python Programs from the Command-line <paragraph>Typical ways of running python programs are discussed on the <reference name="Running Python" refuri="python-running.html">Running Python</reference> page, including introduction to running from the command line. Here are some tips on more efficiently running python from the command line.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2010-08-20T01:00:09Z http://www.bnikolic.co.uk/blog/indexplain.html Computing Blog -- Complete Index and permanent links <paragraph>This blog is linked on <reference name="technorati" refuri="http://technorati.com/blogs/www.bnikolic.co.uk/blog">technorati</reference>.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-lm-lourakis.html Using the Lourakis constrained Levenberg-Marquardt library <paragraph>Here is a very quickly prepared example of how to use the Lourakis library to do constrained minimisation in n-dimensions. This shows simple fitting of a second degree polynomial to a handful of observed points. The box constraint is used and it is set so that it does not contain the maximum likelihood point -- this shows off the constraint feature.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-ontop-bzr.html Managing QuantLib source using Bazaar <paragraph><reference name="QuantLib" refuri="http://quantlib.org">QuantLib</reference> is an open source project providing a comprehensive software framework for quantitative finance. It is developed in an open way by discussion on mailing lists and the source code is manged in a centralised <reference name="QuantLib SVN repository" refuri="http://quantlib.svn.sourceforge.net/viewvc/quantlib/">QuantLib SVN repository</reference>. In this article I discuss how to efficiently manage non-public modifications to the source code tree of this project. These modifications could, for example, be patches that are not yet ready for a public review and integration in the central source code tree; or they could be modifications intended as permanently private extensions of the QuantLib functionality.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-epsilon.html RR: Small numerical values in C++ <paragraph>In numerical programs, financial codes included, there is often a requirement to use very small numerical values in the algorithm, perhaps for ensuring all operations are in a valid domain. In a code published as an accompaniment to a book there is a line:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2010-05-13T15:02:26Z http://www.bnikolic.co.uk/blog/ql-date-io-simple.html Simple examples of Date input/output in QuantLib <paragraph>Following a discussion on the QuantLib users mailing list, here is a quick and very short program illustrating input and output of Date objects in QuantLib:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-10-25T18:42:54Z http://www.bnikolic.co.uk/blog/nqm-project.html Numerical and Quantitative methods in C++ project <paragraph>I've started on a new long-term project which not only by its nature can not be finished, but it is likely to progress quite slowly: a web-book which gives solutions to some popular numerical/quantitative problems in C++.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-derived-class-assignment.html Derived classes still have a default assignment operator <paragraph>If a class does not declare an explicit assignment operator (<literal>operator=()</literal>), a default operator will be created by the C++ compiler which will copy over each of the data members of the class. A class can (and often they do) override this behaviour by declaring the assignment operator. If a class derives from a base class with a non-default assignment operator, but does not have an assignment operator <emphasis>itself</emphasis>, the C++ compiler will still create a default operator which copies the members of the derived (but <emphasis>not</emphasis> the base) class.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/emacs-environment-vars.html Setting environment variables from within Emacs <paragraph>If you have a long-running <literal>emacs</literal> session it is sometimes convenient to change or set the values of the UNIX "environment" variables of the <literal>emacs</literal> process. This is easy to do using the "setenv" function.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-memory-vector.html RR: std::vector<> instead of new[] <paragraph>One of the trickiest concepts for beginners in C++ (and other languages with low-level capability) is memory management, i.e., correct allocation and de-allocation of memory. I already discussed that in some cases memory management can be <reference name="avoided" refuri="cpp-rr-memory-alloc1.html">avoided</reference>, but of course at some point in the program memory allocation does need to be made. Here I discuss using the standard library <literal>std::vector&lt;&gt;</literal> container instead of the plain <literal>new[]</literal> and the advantages this approach gives.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/cpp-rr-memory-alloc1.html RR: Avoiding Memory Allocation <paragraph>The C++ language has a range of excellent tools for managing allocation and de-allocation of memory. They are an essential part of writing effective and efficient programs. The first strategy, however, for dealing with memory allocation should be to avoid it. If that is not possible, you should try to separate the logic of what you are trying to achieve from the memory allocation routines.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/ql-american-disc-dividend.html Example of Pricing an American Option on a Dividend Paying Stock in QuantLib <paragraph>Here is a quick, bare-bones, example on how to price an option on a stock that pays dividend on arbitrary but known dates:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-11-16T07:47:28Z http://www.bnikolic.co.uk/blog/cpp-constructor-template.html Template constructor in a non-template class <paragraph>Sometimes it is required to derive a new class from a base class with constructors that can take many types and perform internal conversion. An easy way to avoid errors due to by-hand writing forwarding constructors for each of the types is to use a constructor in the derived class with a template parameter. This works fine even in a non-template class, i.e., non of the <emphasis>data members</emphasis> of the class are dependent on template parameters. This is an example:</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/website-atom.html Generating Atom feeds using Python and KID <paragraph>The <reference name="Atom" refuri="http://tools.ietf.org/html/rfc4287">Atom</reference> syndication format is the standard way of making others aware of changes to your web-site, in particular to new content on the website. <reference name="Atom" refuri="http://tools.ietf.org/html/rfc4287">Atom</reference> is based on XML and <reference name="Atom" refuri="http://tools.ietf.org/html/rfc4287">Atom</reference> feeds can be produced very simply and conveniently using <reference name="Kid" refuri="http://www.kid-templating.org/index.html">Kid</reference> and of course Python. Here is on outline on how to do it.</paragraph> Bojan Nikolic bojan@bnikolic.co.uk 2009-09-23T09:44:55Z http://www.bnikolic.co.uk/blog/heston-calibration-s.html Heston Model Calibration <paragraph>Here are some quick graphs from recent experiments in calibration of a Heston model to observed option prices (in this case options on the DAX index). They are offered with only the briefest of explanations below for the time being, but there should be more coming soon; or if you wish, get in touch with me at <reference refuri="mailto:webs@bnikolic.co.uk">webs@bnikolic.co.uk</reference>.</paragraph>