Python Calculate Area Under Gaussian Curve w/ Python Reimann Sum

  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Python Sum
Click For Summary
The discussion centers on a program designed to calculate the area under the curve of a Gaussian distribution using a Riemann sum approach. The integral from negative infinity to infinity for a Gaussian distribution is known to equal 1, but the program is calculating approximately 1.5π, which is equivalent to numpy.arccos(-1). The user outlines the parameters for the Gaussian function, including the standard deviation and mean, and describes the iterative process for calculating the area. A key point of feedback highlights an error in the area calculation formula, suggesting that the correct implementation should average the y-values before multiplying by the step size, improving accuracy in the area computation. The conversation emphasizes the importance of proper mathematical formulation in programming for statistical calculations.
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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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