Calculate Area Under Gaussian Curve w/ Python Reimann Sum

  • Context: Python 
  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Python Sum
Click For Summary
SUMMARY

This discussion focuses on calculating the area under a Gaussian curve using a Riemann sum in Python. The user implements a program that approximates the integral of a Gaussian distribution, which should equal 1, but encounters an error leading to an incorrect result of approximately 1.5π. Key variables include the standard deviation (StandDev), mean (MPCAverage), and the step size (0.01). The user identifies a mistake in the area calculation formula and suggests a correction for improved accuracy.

PREREQUISITES
  • Understanding of Gaussian distributions and their properties
  • Familiarity with Python programming and NumPy library
  • Knowledge of Riemann sums and numerical integration techniques
  • Basic concepts of statistical measures such as mean and standard deviation
NEXT STEPS
  • Explore Python's SciPy library for more efficient numerical integration methods
  • Learn about the Central Limit Theorem and its relation to Gaussian distributions
  • Investigate advanced techniques for error analysis in numerical methods
  • Study the implications of different step sizes on the accuracy of Riemann sums
USEFUL FOR

Data scientists, statisticians, and Python developers interested in numerical methods for integrating functions, particularly those working with Gaussian distributions.

dacruick
Messages
1,038
Reaction score
1
Hi I have a program that is to calculate the area under the curve of a Gaussian distribution.
The integral from -inf to inf is 1, and my Reimann sum program calculates close to 1.5pi = numpy.arccos(-1)
a = 1 / (StandDev * pow(2*pi,0.5))
b = MPCAverage #MPC is the x-axis of my gaussian curve
c = StandDev

step = 0.01
IntervalSize = 1.27 #Irrelevant
x2 = 0
s = 0
while s < IntervalSize/step:
x1 = x2
x2 += step
y1 = a*numpy.exp(-(x1-b)**2/(c*c*2))
y2 = a*numpy.exp(-(x2-b)**2/(c*c*2))
Area += (step)*(y2+y1/2)
s += 1
print Area

any tips would be greatly appreciated. Sorry for the spacing error, it won't let me put them in.
 
Technology news on Phys.org
Nevermind!

Area += (step)*(y2+y1/2) = DumbMove
Area += (step)*((y2+y1)/2) = BetterMove
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
3
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K