Driss DS Idrissi
Driss DS Idrissi

Reputation: 77

Python : Generate normal distribution in the order of the bell

I want to generate normal distribution in the order of the bell. I used this code to generate the numbers:

import numpy as np

mu,sigma,n = 0.,1.,1000

def normal(x,mu,sigma):
    return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )

x = np.random.normal(mu,sigma,n) #generate random list of points from normal distribution
y = normal(x,mu,sigma) #evaluate the probability density at each point
x,y = x[np.argsort(y)],np.sort(y) #sort according to the probability density

which is a code proposed in : Generating normal distribution in order python, numpy

but the numbers are not following the bell form. Any ideas? Thank you very much

Upvotes: 5

Views: 13844

Answers (2)

TBhavnani
TBhavnani

Reputation: 771

Your code works just fine.

import numpy as np
import matplotlib.pyplot as plt

mu,sigma,n = 0.,1.,1000

def normal(x,mu,sigma):
    return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )

x = np.random.normal(mu,sigma,n) 
y = normal(x,mu,sigma) 


plt.plot(x,y)

enter image description here

Upvotes: 2

kabanus
kabanus

Reputation: 25980

A couple of things you are confusing.

random.normal draws n numbers randomly from a bell curve

So you have a 1000 numbers, each distinct, all drawn from the curve. To recreate the curve, you need to apply some binning. The amount of points in each bin will recreate the curve (just a single point by itself can hardly represent a probability). Using some extensive binning on your x vector of only a 1000 points:

h,hx=np.histogram(x,bins=50)

and plotting h as a function of hx (so I group your thousand numbers into 50 bins, the y axis will show the amount of points in the bins: enter image description here

Now we can see x was drawn from a bell distribution - the chance to fall in the center bin is determined by the Gaussian. This is a sampling, so each point may vary a bit of course - the more points you use and the finer binning and the better it will be (smoother).

y = normal(x,mu,sigma)

This just evaluates the Gaussian at any given x, so really, supply normal with any list of numbers around your mean (mu) and it will calculate the bell curve exactly (the exact probability). Plotting your y against x (Doesn't matter that your x is Gaussian itself, but it's a 1000 points around the mean, so it can recreate the functions): enter image description here

See how smooth that is? That's because it's not a sampling, it's an exact calculation of the function. You could have used just any 1000 points around 0 and it would have looked just as good.

Upvotes: 3

Related Questions