normal distribution formula in c# ?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 2
Reputation: afr0 is an unknown quantity at this point 
Solved Threads: 0
afr0 afr0 is offline Offline
Newbie Poster

normal distribution formula in c# ?

 
0
  #1
Aug 20th, 2009
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/ex...091941033.aspx

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

  1. static double NORMSDIST(double z_score)
  2. {
  3. double z_ = - ( (z_score * z_score) / 2 );
  4. double normDist = (1 / ((Math.Sqrt(2 * Math.PI)))) * ( Math.Exp(z_) );
  5.  
  6. return normDist;
  7. }

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

thanks in advance.
Last edited by John A; Aug 21st, 2009 at 12:57 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: normal distribution formula in c# ?

 
0
  #2
Aug 20th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: normal distribution formula in c# ?

 
0
  #3
Aug 20th, 2009
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):
  1. static double NORMSDIST(double Zscore)
  2. {
  3. double Z = -(Zscore * Zscore) / 2;
  4. double normDist = (1 / Math.Sqrt(2 * Math.PI)) * (Math.Exp(Z));
  5.  
  6. return normDist;
  7. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: afr0 is an unknown quantity at this point 
Solved Threads: 0
afr0 afr0 is offline Offline
Newbie Poster

Re: normal distribution formula in c# ?

 
0
  #4
Aug 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: normal distribution formula in c# ?

 
0
  #5
Aug 21st, 2009
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?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: normal distribution formula in c# ?

 
0
  #6
Aug 22nd, 2009
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.
Last edited by ddanbe; Aug 22nd, 2009 at 10:29 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

Tags
math, statistics

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC