Question

I have interprated the formula in wikipedia in c# code, i do get a nice normal curve, but is it rational to get values that exceeds 1? isnt it suppose to be a distribution function?

this is the C# implementation :

        double up = Math.Exp(-Math.Pow(x , 2) / ( 2 * s * s ));
        double down = ( s * Math.Sqrt(2 * Math.PI) );
        return up / down;

i double checked it several times and it seems fine to me so whats wrong? my implementation or understanding?

for example if we define x=0 and s=0.1 this impl would return 3.989...

Was it helpful?

Solution

A distribution function, a pdf, has the property that its values are >= 0 and the integral of the pdf over -inf to +inf must be 1. But the integrand, that is the pdf, can take any value >= 0, including values greater than 1.

In other words, there is no reason, a priori, to believe that a pdf value > 1 indicates a problem.

You can think about this for the normal curve by considering what reducing the variance means. Smaller variance values concentrate the probability mass in the centre. Given that the total mass is always one, as the mass concentrates in the centre, the peak value must increase. You can see that trend in the graph the you link to.

What you should do is compare the output of your code with known good implementations. For instance, Wolfram Alpha gives the same value as you quote: http://www.wolframalpha.com/input/?i=normal+distribution+pdf+mean%3D0+standard+deviation%3D0.1+x%3D0&x=6&y=7

Do a little more testing of this nature, captured in a unit test, and you will be able to rely on your code with confidence.

OTHER TIPS

Wouldn't you want something more like this?

public static double NormalDistribution(double value)
{
    return (1 / Math.Sqrt(2 * Math.PI)) * Math.Exp(-Math.Pow(value, 2) / 2);
}

Yes, it's totally OK; The distribution itself (PDF) can be anything from 0 to +infinity; the thing should be in the range [0..1] is the corresponding integral(s) (e.g. CDF).

You can convince yourself if look at the case of non-random value: if the value is not a random at all and can have only one constant value the distribution degenerates (standard error is zero, mean is the value) into Dirac Delta Function: a peak of infinite hight but of zero width; integral however (CDF) from -infinity to +infinity is 1.

   // If you have special functions implemented (i.e. Erf) 

   // outcoume is in [0..inf) range
   public static Double NormalPDF(Double value, Double mean, Double sigma) {
     Double v = (value - mean) / sigma;

     return Math.Exp(-v * v / 2.0) / (sigma * Math.Sqrt(Math.PI * 2));
   }

   // outcome is in [0..1] range
   public static Double NormalCDF(Double value, Double mean, Double sigma, Boolean isTwoTail) {
     if (isTwoTail)
       value = 1.0 - (1.0 - value) / 2.0;

     //TODO: You should have Erf implemented
     return 0.5 + Erf((value - mean) / (Math.Sqrt(2) * sigma)) / 2.0;
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top