Geeks With Blogs

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

Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.

improve my => 'code'

I just started playing around with 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 C# , Software Design , Statistics , Software Development , Financial Engineering | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: LINQ and Financial Simulation

# re: LINQ and Financial Simulation
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.
Left by Skup on Jan 28, 2008 4:31 AM

# re: LINQ and Financial Simulation
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 !
Left by Skup on Jan 28, 2008 12:54 PM

# re: LINQ and Financial Simulation
Nice. Did you check out out Linq to Financial Markets? We include lots of portfolio quant/optimization work post efficient frontier (I'm glad you didn't post a NORMAL distribution - inside joke). This is from work I started way back as CTO of Monetaire and before that at the Citi global private bank.

Damon Wilder Carr
Left by Damon Wilder Carr on Nov 27, 2008 1:22 PM

# re: LINQ and Financial Simulation
CTO Good luck with that quant work, it will be as slow as that dive last winter using LINQ 'tech'..
Left by qtqw on May 04, 2009 5:33 PM