954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

normal distribution formula in c# ?

Hi All,
I'm trying to implement the NORMSDIST function of MS excel in the C#. I found its implementation from the following link.
http://office.microsoft.com/en-us/excel/HP052091941033.aspx

and I've written the following code for this i.e.

static double NORMSDIST(double z_score) 
        {            
            double z_ = - ( (z_score * z_score) / 2 ); 
            double normDist = (1 / ((Math.Sqrt(2 * Math.PI)))) * ( Math.Exp(z_) );

            return normDist;
        }


unfortunately this isn't giving me the correct result, can someone point out for me what m i missing in here?

thanks in advance.

afr0
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Your code is correct but it gives the value if cumulative is false.
The excel example gives the value when cumulative is true.
I don't have your function in my Excel version, just simlated it with a similar function and setting mu to zero and sigma to one.
Hope this helps a bit.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Forgot to notice this was your first post!
Welcome to Daniweb afr0!
Try to use code tags when you post code.
I use underscores sometimes but, I would never use them in math formulas. It is confusing, I did not notice your unary minus at first.:|
You also use a few parenthesis to many.
This code (with code tags!) shows it(I think it is much more clearer to read):

static double NORMSDIST(double Zscore)
        {
            double Z = -(Zscore * Zscore) / 2;
            double normDist = (1 / Math.Sqrt(2 * Math.PI)) * (Math.Exp(Z));

            return normDist;
        }
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

I'm using 2007 office suite. but can you tell me how can i set cumulative to true. From the given formula that i provided the link i couldn't see any such flag. can provide that formula ?

thanks.

afr0
Newbie Poster
2 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

I have a 2003 version of Excel with this : NORMDIST(x,mean,stddev,cumulative) Cumulative can be true or false.
Your C# function returns the y value for x under the bell curve, so it is not cumulative.
If you want a cumulative function, you have to calculate the area under the bell curve from minus infinity up to the value of x. Perhaps you already have an algoritm to do that?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Perhaps you can do something(as a start) with this snippet
Just plug in your function and play with some parameters to get the results you want.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: