Posts
71
Comments
64
Trackbacks
0
LINQ and Financial Simulation

improve my => 'code' Add to Google

I just started playing around with LINQ seriously, and I really love some of the features incorporated, like the Enumerable.Range() function and how it can be used for integer programming.

Here's a simple function for generating lognormal distributions (could be useful for financial engineering).

Hope you're enjoying the samples,

Jonathan Starr

 public List<double> GenerateLogNormalDistribution(int numberOfTimes, double mean, double standardDeviation)

{

    Random randomGenerator = new Random();

    var randomNumbers = from theNumbers in Enumerable.Range(1,

        numberOfTimes).Select(x => randomGenerator.Next(1,

  1000)/1000.0)

        select theNumbers;

 

    List<double> results = new List<double>();

    randomNumbers.ToList().ForEach

        (x => results.Add(mean + (standardDeviation * (Math.Sqrt(-2 * Math.Log(x)) * Math.Cos(6.28 * x)))));

 

    return results;

}

posted on Friday, January 25, 2008 8:00 PM Print
Comments
Gravatar
# re: LINQ and Financial Simulation
Skup
1/28/2008 4:31 AM
Hi,
to be even more Linq style, you can totally skip the list :

public IEnumerable<double> GenerateLogNormalDistribution(int numberOfTimes, double mean, double standardDeviation)
{
Random random = new Random();
return from number in Enumerable.Rage(1, count)
let x = random.Next(1,1000)/1000.0)
select mean + (standardDeviation * (Math.Sqrt(-2*Math.Log(x)) * Math.Cos(2.28*x)));
}

I didn't check this code, so there are surely parentheses mismatchs, but you can get an idea.

This way, you don't put the value in a list until you need it.
Gravatar
# re: LINQ and Financial Simulation
Jonathan Starr
1/28/2008 10:48 AM
This works! I was afraid I would be using the same random number through the IEnumerable (so if I wanted 100 results, they would all be the same) and that is why I coded it the way I did. Apparently the let statement does not work as I first thought (it is much more flexible). Here's the cleaned up version of your code (there was a missing parenthesis and I changed count to numberOfTimes).

public IEnumerable<double> GenerateLogNormalDistribution(int numberOfTimes, double mean, double standardDeviation)
{
Random random = new Random();
return from number in Enumerable.Range(1, numberOfTimes)
let x = (random.Next(1,1000)/1000.0)
select mean + (standardDeviation * (Math.Sqrt(-2*Math.Log(x)) * Math.Cos(2.28*x)));
}


Thanks again,

Jonathan Starr
Gravatar
# re: LINQ and Financial Simulation
Skup
1/28/2008 12:54 PM
Yes, the let statement is re-evaluated for each item in the collection. It is the same as defining a local variable in a foreach loop when using the imperative syntax.

With Linq (and functional programming) you almost never need to loop through a list to fill another one !

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 1 and 3 and type the answer here:
News
Jonathan Starr is a developer in Saint Louis, MO. He holds an MBA in Finance from Columbia Business School and earned his MCSD from Microsoft.


All statements in this blog are personal opinions and do not reflect the opinions of his employer.





Related Sites
Join My Community at MyBloglog!

Tag Cloud