2012-07-23

Random variables in Python


import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm


# generate 1000 instances of a normal random variable
mean, sigma = 0, 1
sample = norm.rvs(mean, sigma, size=1000)


# a 50-bin histogram of the sample
plt.hist(sample, 50, normed=1, facecolor='white')


# the population distribution
range = np.arange(-5, 5, 0.001)
plt.plot(range, norm.pdf(range, mean, sigma), color='red', linewidth=4)


plt.show()

Result:



Addendum: the code above can be improved

No comments:

Post a Comment