Consider there are m discounts observed and n model parameters. The model in this case is a function of taking vector of n parameters and returning an m vector of model discounts. The difference between this m vector and the observed value (market discounts) need to be as small as possible. This is exactly what the Levenberg Marquardt algorithm does.

After a couple of searches we decided to go for the levmar by Lourakis. Some preliminary test showed it to be very stable. To integrate the project in our C# solution we used a LevarSharp by Adal and Visual Studio 2010. To get everything configured and compiling required a few steps. Those steps will be listed here.

Setup

In this walkthrough we consider that all code files are in C:\code\LevmarSharp.

1 - Get LevmarSharp

Download the LevmarSharp code and unpack. Move everything into the C:\code\LevmarSharp folder.

2 - levmar

The LevmarSharp solution already ships with a levmar.lib file inside. You can use this one or compile your own. Probably you want to do the latter to make sure that you are working with the correct version of the lemvar package. In addition in some cases you will have to do so to make Visual Studio play nice. In our case, using Visual Studio 2010, the LevmarSharp will not work out of the box. Place the levmar lib in the folder C:\code\LevmarSharp\LevmarSharp.

Instructions on how to compile levmar can be found in a previous blog post.

2 - Get CLAPACK (required if your levmar uses CLAPACK)

The website of levmar notes that it’s strongly recommended to use lapack for QR factorization. The code can do without, but here the recommendation is followed so lapack needs to be included. For Windows there is the free clapack which is described in LAPACK Users' Guide Third Edition. These packages will be used here. Depending on your machine setup, you may have some or all of these libraries as part of Visual Studio or other installations. To be safe we recommend you to get all the CLAPACK lib.

Right click on the Libraries folder in the LevmarSharp project. Select add > existing item and add the following libraries:

levmar.lib

clapack.lib

blas.lib

BLAS_nowrap.lib

libf2c.lib

The libraries should be in C:\code\LevmarSharp\LevmarSharp as results of steps 2 and 3.

5 - Conflicting Libraries

The solution now has conflicting library call, hence it will not compile. To resolve this we must tell the compiler to ignore the libcmt.lib. We can do so by right click the LevmarSharp solution and selecting properties. Here select properties > Configuration Properties > Linker > Input > Ignore Specific Default Libraries enter the libcmt.lib in the textbox.

6 - Optional : Telling x64 installation to use x86

The steps presented here are for a x86 (32 bit) installation. If you have a x64 installation you can tell the compiler to target only this platform. That can be done by LevmarSharpTest > Properties > Build > Platform target : x86.

Note if you need x64 version you can repeat all the steps here, but using the x64 CLAPACK and configuring Visual Studio to compile the levmar code to x64.

7 - Test if the DLL is working

To test if the DLL is working we can use the test project included in the solution. After building the solution, open the command prompt and go to the folder C:\code\LevmarSharp\LevmarSharpTest. Then run the LevmarSharpTest.exe. This should give you something like this:

Topics

What We Do

Modeling

We build financial models. We do this both within the Ugly Duckling team and embedded in your organization. When building models we often use the existing Ugly Duckling code base and research. This makes the modeling faster and more efficient.

Validation

We love details. When given the chance, we deep dive into a model and validate its mathematical, financial and implementation details. We also find it a rich experience to explore the models of others and learn for their approach. In most cases we contribute to make an even better model thanks to the feedback we provide as part of the process.

Research

Ugly Duckling is dedicated to expand the body of mathematical finance knowledge. To do so we invest in a continuous research effort. These efforts keep us up-to-dated on the new developments in finance. So when you encounter a challenge for which no solution exists in the literature, please call us. We can help.