You can try this. I got the cumulative normal code from
http://www.cs.princeton.edu/introcs/...Math.java.html
using System;
public class blah {
public static double NORMDIST(double x, double mean, double std,
bool cumulative){
if ( cumulative ) {
return Phi( x, mean, std );
} else {
double tmp = 1/((Math.Sqrt(2*Math.PI)*std));
return tmp * Math.Exp(-.5 * Math.Pow((x-mean)/std,2));
}
}
//from
http://www.cs.princeton.edu/introcs/...Math.java.html
// fractional error less than 1.2 * 10 ^ -7.
static double erf(double z) {
double t = 1.0 / (1.0 + 0.5 * Math.Abs(z));
// use Horner's method
double ans = 1 - t * Math.Exp( -z*z - 1.26551223 +
t * ( 1.00002368 +
t * ( 0.37409196 +
t * ( 0.09678418 +
t * (-0.18628806 +
t * ( 0.27886807 +
t * (-1.13520398 +
t * ( 1.48851587 +
t * (-0.82215223 +
t * ( 0.17087277))))))))));
if (z >= 0) return ans;
else return -ans;
}
// cumulative normal distribution
static double Phi(double z) {
return 0.5 * (1.0 + erf(z / (Math.Sqrt(2.0))));
}
// cumulative normal distribution with mean mu and std deviation sigma
static double Phi(double z, double mu, double sigma) {
return Phi((z - mu) / sigma);
}
}
littlecharva wrote:
Hi,
I need to convert a complicated Excel document that's been created by some Maths boffin into a program in C#. I've been doing really well (despite my Mathematical handicap) until I hit the Excel NORMDIST(x,mean,stdDev,cumulative) function. System.Math doesn't have any functions to help me out here, and I just can't seem to work out how to convert the formula into C# code. I need a function to work the same as the Excel Function with cumulative set to TRUE.
Any takers?
LittleC