My current task is to optimise a Monte Carlo Simulation that calculates Capital Adequacy figures by region for a set of Obligors.

It is running about 10 x too slow for where it will need to be in production and number or daily runs required. Additionally the granularity of the result figures will need to be improved down to desk possibly book level at some stage, the code I've been given is basically a prototype which is used by business units in a semi production capacity.

The app is currently single threaded so I'll need to make it multi-threaded, may look at ThreadPool or the Microsoft Parallels library but I'm constrained to .Net 2 on the server at this bank so may have to consider this guy's port http://www.codeproject.com/KB/cs/aforge_parallel.aspx am trying my best to get them to upgrade to .net 3.5 SP1 but it's a major exercise in an organisation of this size and might not be possible in my contract time frames.

I've profiled the app using the trial of dottrace (http://www.jetbrains.com/profiler) any other good profilers people know of? free ones?

A lot of the execution time is spent generating uniform random numbers and then translating this to a normally distributed random number, they are using a c# mersenne twister implementation not sure where they got it or if it's the best way to go about this (or best implementation) to generate the uniform random numbers, then this is translated to a normally distributed version for use in the calculation (haven't delved into the translation code yet).

Also has anyone used:

http://quantlib.org

http://www.qlnet.org (c# port of quantlib)

http://www.boost.org

Any alternatives you know of?

I'm a c# developer so would prefer c#, but a wrapper to c++ shouldn't be a problem should it? maybe even faster leveraging the the c++ implementations. Am thinking some of these libraries will have the fastest method to directly generate normally distributed random numbers, without the translation step. Also they may have some other functions that will be helpful in the subsequent calculations.

Also the box this is on is a quad core Opteron 275, 8GB memory but Windows Server 2003 Enterprise 32 bit, should I advise then to upgrade to a 64 bit OS ? any links to articles supporting this decision would really be appreciated.

Anyway any advice, help you may have is really appreciated.

I'm looking for experts to weigh in on the best way to:

- profile this

- take this from single threaded to multithreaded, whether to go for ThreadPool, some Parallel library...

- best way to generate normally distributed random numbers enmass and consume by the monte carlo simulation

- any other architechural considerations I should be thinking about

Thanks

It is running about 10 x too slow for where it will need to be in production and number or daily runs required. Additionally the granularity of the result figures will need to be improved down to desk possibly book level at some stage, the code I've been given is basically a prototype which is used by business units in a semi production capacity.

The app is currently single threaded so I'll need to make it multi-threaded, may look at ThreadPool or the Microsoft Parallels library but I'm constrained to .Net 2 on the server at this bank so may have to consider this guy's port http://www.codeproject.com/KB/cs/aforge_parallel.aspx am trying my best to get them to upgrade to .net 3.5 SP1 but it's a major exercise in an organisation of this size and might not be possible in my contract time frames.

I've profiled the app using the trial of dottrace (http://www.jetbrains.com/profiler) any other good profilers people know of? free ones?

A lot of the execution time is spent generating uniform random numbers and then translating this to a normally distributed random number, they are using a c# mersenne twister implementation not sure where they got it or if it's the best way to go about this (or best implementation) to generate the uniform random numbers, then this is translated to a normally distributed version for use in the calculation (haven't delved into the translation code yet).

Also has anyone used:

http://quantlib.org

http://www.qlnet.org (c# port of quantlib)

http://www.boost.org

Any alternatives you know of?

I'm a c# developer so would prefer c#, but a wrapper to c++ shouldn't be a problem should it? maybe even faster leveraging the the c++ implementations. Am thinking some of these libraries will have the fastest method to directly generate normally distributed random numbers, without the translation step. Also they may have some other functions that will be helpful in the subsequent calculations.

Also the box this is on is a quad core Opteron 275, 8GB memory but Windows Server 2003 Enterprise 32 bit, should I advise then to upgrade to a 64 bit OS ? any links to articles supporting this decision would really be appreciated.

Anyway any advice, help you may have is really appreciated.

I'm looking for experts to weigh in on the best way to:

- profile this

- take this from single threaded to multithreaded, whether to go for ThreadPool, some Parallel library...

- best way to generate normally distributed random numbers enmass and consume by the monte carlo simulation

- any other architechural considerations I should be thinking about

Thanks

```
Is being called like this millions of times for example:
IDistribution dist = new DiscreteNormalDistribution(1048576);
...
SimMat[i, j] = dist.ICDF(rnd.nextUniform());
class DiscreteNormalDistribution : IDistribution
{
private long msize;
private double[] vals;
public DiscreteNormalDistribution(long size)
{
NormalDistribution dist = new NormalDistribution();
msize = size;
vals = new double[size+1];
double z;
for (long i = 1; i < size-1; i++)
{
z = (1.0/size)*i;
vals[i] = dist.ICDF(z);
}
vals[0] = vals[1];
vals[size] = vals[size - 1];
}
public double PDF(double q)
{
return 0;
}
public double CDF(double q)
{
return 0;
}
public double ICDF(double q)
{
long z = Convert.ToInt64(q * (msize));
if (z < 0 || z > msize)
{
return 0;
}
else return vals[z];
}
public double Generate(IRandomGenerator x)
{
return ICDF(x.nextUniform());
}
}
```

```
class NormalDistribution: IDistribution
{
public double PDF(double q)
{
return 0;
}
public double CDF(double q)
{
return 0;
}
public double ICDF(double q)
{
if (q == .5)
return 0;
q = 1.0 - q;
double p = (q > 0.0 && q < 0.5) ? q : (1.0 - q);
double t = Math.Sqrt(Math.Log(1.0 / Math.Pow(p, 2.0)));
double c0 = 2.515517;
double c1 = 0.802853;
double c2 = 0.010328;
double d1 = 1.432788;
double d2 = 0.189269;
double d3 = 0.001308;
// double x = t - (c0 + c1 * t + c2 * Math.Pow(t, 2.0)) /
// (1.0 + d1 * t + d2 * Math.Pow(t, 2.0) + d3 * Math.Pow(t, 3.0));
double x = t - (c0 + c1 * t + c2 *(t*t)) /
(1.0 + d1 * t + d2 * (t*t) + d3 * (t*t*t));
if (q > .5)
x *= -1.0;
return x;
}
public double Generate(IRandomGenerator x)
{
return ICDF(x.nextUniform());
}
}
```

ct/cuda_ho ) designed to work with stream computation engines in nVidia video cards (8800GT and above). It is already in use in some financial institutions that need parallel processing in existing servers (you install a dedicated computation card). 10x speed increases are not unheard of.me.html#

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

The referenced article also gives links to other methods, if you prefer one of them,