Compiling LevmarSharp (Visual Studio 2010)

Prerequisites:

-Visual Studio 2010

-levmar 2.6 (http://users.ics.forth.gr/~lourakis/levmar/)

-levmarsharp (https://github.com/AvengerDr/LevmarSharp)


For a recent research project we needed to solve an optimization problem. In specific we were trying to reproduce the results in the paper “A Generalized Procedure for Building Trees for the Short Rate and its Application to Determining Market Implied Volatility Functions” by Hull and White. In the paper it is described how a lattice can be constructed and calibrated to market. The calibration is essentially an optimization problem where the difference between the discount factors (or interest rates) observed in the market and the discounts generated in the model is made as small as possible by varying the model parameters.

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.

The easiest is to grab Windows pre-build packages available here:

http://www.netlib.org/clapack/LIB_WINDOWS/prebuilt_libraries_windows.html

Download:

  • clapack.lib
  • blas.lib
  • BLAS_nowrap.lib
  • libf2c.lib

Save the packages in C:\code\LevmarSharp\LevmarSharp

 

3 - Make the Solution VS2010 ready

If you open the Visual Studio solution you will notice that the project does not load. The project is currently configured for .Net 4.5, while VS2010 uses .Net 4.0.

image05

To fix this open the project files

  • LevmarSharp.vcxproj
  • LevmarSharpTest.csproj

in a text editor (Notepad is fine) and change the line:

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>

to

<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

After closing and opening the solution both projects should load.

image04

Next we need to set the correct Toolset. Goto LevmarSharp Project Properties > Configuration Properties > General and set the “Platform Toolset” to v100.

image03

Finally the app.config needs to be updated. Go the the LevmarSharpTest project. Open the App.config and delete [sku=".NETFramework,Version=v4.5"]. The resulting config file should read:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

   <startup>

       <supportedRuntime version="v4.0"/>

   </startup>

</configuration>

4 - Link the Libraries

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.

image01

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.

image00

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:

C:\Users\dev>cd \code\LevmarSharp
C:\code\LevmarSharp>cd LevmarSharpTest
C:\code\LevmarSharp\LevmarSharpTest>cd bin\Debug
C:\code\LevmarSharp\LevmarSharpTest\bin\Debug>LevmarSharpTest.exe

Results for Wood's function

Levenberg-Marquardt returned 113 in 113 iterations, reason: 6

Solution:
1 1 1 1

Minimization Info:

19192 2.7328403951087e-24 5.27060562231998e-10 1.44737827768744e-18 6.8508259131

0522e-07 113 6 158 11 113

image02

This is it. Your LevmarSharp is configured and compiling!


All the files used here are available for download under GPL:

Leave a Reply

Your email address will not be published. Required fields are marked *