Answers.Body: <p>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:</p>
  <pre><code>m=1; // mean
  s=1; // std
  sum = 0;
  
  // Gaussian distribution
  function f(x){
      return (1/(Math.sqrt(2*Math.PI * (s**2)))) * Math.exp(-(1/2)*((x-m)**2)/(s**2))
  }
  
  // Run
  for (x=-10; x&lt;10; x+=0.01){
      sum += f(x)*0.01;
      //console.log(sum)
  }
  
  console.log(sum)
  </code></pre>
  <p>The changes:</p>
  <ol>
  <li>The code is performing <a href="https://en.wikipedia.org/wiki/Numerical_integration" rel="nofollow noreferrer">numerical integration</a>, 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 <code>sum = f(x)</code> to read <code>sum += f(x)*0.01</code>, to represent both the fact that we are adding up each rectangle (<code>=</code> to <code>+=</code>), and that we care about the area (adding <code>*0.01</code>).</li>
  <li>Your gaussian distribution function is incorrect. The gaussian or <a href="https://en.wikipedia.org/wiki/Normal_distribution" rel="nofollow noreferrer">normal distribution</a> has a factor of -1/2 in the exponent, which I have added.</li>
  <li>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 (<a href="https://www.desmos.com/calculator/7ynqs2o0nh" rel="nofollow noreferrer">here</a>), 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 <a href="https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule" rel="nofollow noreferrer">99.7%</a> of the area.</li>
  </ol>
  <p>My code outputs 1 (almost) as expected.</p>
  <p>2 small things:</p>
  <ol>
  <li><p>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 <a href="https://www.desmos.com/calculator/7ynqs2o0nh" rel="nofollow noreferrer">here</a>.</p>
  </li>
  <li><p>My code outputs not 1, but 1.000000000000003. This small error has 3 factors contributing to it:</p>
  <ol>
  <li><p>Floating point arithmetic, which this program uses, is inexact, and subject to rounding errors</p>
  </li>
  <li><p>Numerical integration is also not exact, and only an approximation of the true integral</p>
  </li>
  <li><p>We have not actually integrated across all x-values, merely from -10 to 10, which would also introduce negligible error.</p>
  </li>
  </ol>
  </li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-06T04:54:03.800Z
Answers.LastActivityDate: 2023-08-06T04:55:14.150Z
Answers.LastEditDate: 2023-08-06T04:55:14.150Z
Answers.UserDisplayName: Iron filings
Body: <p>I assume the sum of the Gaussian distribution is 1. However, the actual calculation in JavaScript yields a value greater than 1 in JavaScript.
  Is this code wrong?</p>
  <p>gaussian_distribution.html]</p>
  <pre><code>&lt;script&gt;
  m=1; // mean
  s=1; // std
  sum = 0;
  
  // Gaussian distribution
  function f(x){
      return (1/(Math.sqrt(2*Math.PI * (s**2)))) * Math.exp(((x-m)**2)/(s**2))
  }
  
  // Run
  for (x=0; x&lt;3; x+=0.01){
      sum = f(x);
      //console.log(sum)
  }
  
  console.log(sum)
  &lt;/script&gt;
  </code></pre>
  
Tags: <math>
Title: Sum of gaussian distribution
UserDisplayName: diffusion stable
ThreadText: # Sum of gaussian distribution
  
  (Asked by: username_0 on 2023-08-06)
  
  I assume the sum of the Gaussian distribution is 1. However, the actual calculation in JavaScript yields a value greater than 1 in JavaScript.
  Is this code wrong?
  gaussian_distribution.html]
  
  **Tags:** math
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-08-06. Score: 2
  
  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(2*Math.PI * (s**2)))) * 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.
  
  
  ---
  
  