Search code examples
pythonnumpydistributionnormal-distribution

Distribution plot with wrong total value


To create enter image description here I have made a distribution plot with code below:

from numpy import *
import numpy as np
import matplotlib.pyplot as plt

sigma = 4.1

x = np.linspace(-6*sigma, 6*sigma, 200)

def distr(n):
    def g(x):
        return (1/(sigma*sqrt(2*pi)))*exp(-0.5*(x/sigma)**2)
    FxSum = 0
    a = list()
    for i in range(n):
        # divide into 200 parts and sum one by one
        numb = g(-6*sigma + (12*sigma*i)/n)
        FxSum += numb
        a.append(FxSum)
    return a

plt.plot(x, distr(len(x)))
plt.show()

enter image description here

This is, of course, a way of getting the result without using hist(), cdf() or any other options from Python libraries.

Why the total sum is not 1? It shouldn't depend from (for example) sigma.


Solution

  • Almost right, but in order to integrate you have to multiply the function value g(x) times your tiny interval dx (12*sigma/200). That's the area you sum up:

    from numpy import *
    import numpy as np
    import matplotlib.pyplot as plt
    
    sigma = 4.1
    
    x = np.linspace(-6*sigma, 6*sigma, 200)
    
    def distr(n):
        def g(x):
            return (1/(sigma*sqrt(2*pi)))*exp(-0.5*(x/sigma)**2)
        FxSum = 0
        a = list()
        for i in range(n):
            # divide into 200 parts and sum one by one
            numb = g(-6*sigma + (12*sigma*i)/n) * (12*sigma/200)
            FxSum += numb
            a.append(FxSum)
        return a
    
    plt.plot(x, distr(len(x)))
    plt.show()