Connecting Tech Pros Worldwide Forums | Help | Site Map

Normal Distribution

littlecharva
Guest
 
Posts: n/a
#1: Nov 16 '05
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

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Normal Distribution


LittleC,

If you know that Excel will be installed on the machine that the app
will be on, why not just use interop and call the function yourself?
Surely, the function has a representation in the object model of Excel.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"littlecharva" <anonymous@discussions.microsoft.com> wrote in message
news:4ED2F1DC-421C-4495-ABE9-E6B316FD4194@microsoft.com...[color=blue]
> Hi,
>
> I need to convert a complicated Excel document that's been created by some[/color]
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.[color=blue]
>
> Any takers?
>
> LittleC[/color]


LittleC
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Normal Distribution


Hi,

The code is eventually going to be run as an ASP.NET page, so that's out of the question.

LittleC
Marcus
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Normal Distribution


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:
[color=blue]
> 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[/color]
NaraendiraKumar R. R.
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Normal Distribution


Have you tried NAG? http://www.nag.com/statistical_software.asp

-Naraen
------------
"littlecharva" <anonymous@discussions.microsoft.com> wrote in message
news:4ED2F1DC-421C-4495-ABE9-E6B316FD4194@microsoft.com...[color=blue]
> Hi,
>
> I need to convert a complicated Excel document that's been created by some[/color]
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.
[color=blue]
> Any takers?
>
> LittleC[/color]


Closed Thread