I need to use NORMSINV
formula in Excel in my C# code. I dont have any contact with E开发者_开发问答xcel sheet and I am not getting any values from excel. Simply need to use the formula in C# code.
How to acheive this?
You could use the GSL (GNU Scientific Library, written in C). The function which corresponds to the one you're after would be "gsl_cdf_ugaussian_Pinv" I believe.
You can get the GSL here:
http://ftp.heanet.ie/mirrors/gnu/gsl
Details of the function from Excel:
http://support.microsoft.com/kb/826772
And the corresponding details from the GSL:
http://www.gnu.org/s/gsl/manual/html_node/The-Gaussian-Distribution.html
(Note: The Standard Normal distribution is also known as the Unit Gaussian distribution)
I believe this should calculate an accurate approximation of what you're after.
(Note: I don't program in C# so there might be some syntax errors)
public double pdf_norm(double x)
{
return (1 / Math.Sqrt(Math.PI)) * Math.Exp(-1 * (Math.Pow(x, 2) / 2));
}
public double cdf_norm(double x)
{
double[] bValues = [0.2316419, 0.319381530, −0.356563782, 1.781477937, −1.821255978, 1.330274429];
double t = 1 / (1 + bValues[0]*x);
return 1 - pdf_norm(x)*(bValues[1]*t + bValues[2]*Math.Pow(t,2) + bValues[3]*Math.Pow(t,3) + bValues[4]*Math.Pow(t,4) + bValues[5]*Math.Pow(t,5));
}
You'd then want to call the function cdf_norm to return the number you want.
Please test it works and let me know. :)
精彩评论