Yes, that code is wrong. There are 3 issues. When you say sum, you really mean integral, which is the area under the curve of the normal distribution, and is indeed 1. Correct code, which outputs 1 (almost) is below:
m=1; // mean
s=1; // std
sum = 0;
// Gaussian distribution
function f(x){
return (1/(Math.sqrt(2Math.PI (s2)))) * Math.exp(-(1/2)*((x-m)2)/(s**2))
}
// Run
for (x=-10; x<10; x+=0.01){
sum += f(x)*0.01;
//console.log(sum)
}
console.log(sum)
The changes:
- The code is performing numerical integration, which graphically is represented by adding a number of small rectangles under the curve. The area of these rectangles is equal to length*height. In this case, height is the function value at a specific x-value, and width is the step in x-values we take. When we add up all of the rectangles across every x-value in a certain range (from here -10 to 10), we get an approximation to the integral between those bounds. I have altered the line
sum = f(x)
to read sum += f(x)*0.01
, to represent both the fact that we are adding up each rectangle (=
to +=
), and that we care about the area (adding *0.01
).
- Your gaussian distribution function is incorrect. The gaussian or normal distribution has a factor of -1/2 in the exponent, which I have added.
- Your bounds were wrong. Strictly speaking, 1 is the area under the gaussian curve from negative infinity to infinity, across all x values. However, by looking at a graph of the gaussian (here), you can see that the area is negligible at most x-values, except from roughly -2 to 4. By choosing bounds of 0 to 3, you cut off a large section of the area. My bounds of -10 to 10 were arbitrarily chosen, and really it does not matter as long as the bounds are at least -2 to 4, which contains 99.7% of the area.
My code outputs 1 (almost) as expected.
2 small things:
You can have a normal distribution with any mean and standard deviation you want, but the standard way is mean=0 and standard deviation=1, as this results in a curve nicely centered. You can see this by adjusting mean and standard deviation values here.
My code outputs not 1, but 1.000000000000003. This small error has 3 factors contributing to it:
Floating point arithmetic, which this program uses, is inexact, and subject to rounding errors
Numerical integration is also not exact, and only an approximation of the true integral
We have not actually integrated across all x-values, merely from -10 to 10, which would also introduce negligible error.