Standard Normal Distribution z-value function in C#

Viewed 29719

I been looking at the recent blog post by Jeff Atwood on Alternate Sorting Orders. I tried to convert the code in the post to C# but I ran into an issue. There is no function in .NET that I know of that will return the z-value, given the percentage of area under the standard normal curve. The recommended values to use for the algorithm are 95% and 97.5% which you can look up on the z-value table in any statistics book.

Does anyone know how to implement such a function for all values of z or at least to 6 standard deviations from the mean. One way would be to hard code the values into a dictionary and use a look up but there has to be a way of calculating the exact value. My attempt at solving this was to take a definite integral of the standard normal curve function.

y = (1 / (sqrt(2 * PI))) * e^(-(1/2) * x^2)

This gives me the area under the curve between two x values but then I am stuck… Maybe I am way of base and this is not how you would do it?

Thanks.

5 Answers

For a newer version of MathNet

    //standard normal cumulative distribution function
    static double F(double x)
    {
        MathNet.Numerics.Distributions.Normal result = new MathNet.Numerics.Distributions.Normal();
        return result.CumulativeDistribution(x);
    }

if you are using Math.Net then you can just use the InverseCumulativeDistribution function.

So if you want the Z-value, where 80% of the Standard Normal curve is covered the code would look something like this

var curve = new MathNet.Numerics.Distributions.Normal();
var z_value = curve.InverseCumulativeDistribution(0.8);
Console.WriteLine(z_value);

Ensure that you have installed the MathNet.Numerics NuGet package.

Related