integers following Normal distribution
how can I produce positive integers, say in (1,n), which follow the normal distribution with parameters (m,sigma).
You could use the binomial distribution to approximate a normal distribution with only integer entries (because of the Central Limit Theorem). You will have to relate the mean of the binomial to the mean of the normal distribution. However, you can not get both the mean to be m and variance to be sigma exactly for any m and sigma, since for the binomial distribution they are closely related.
I think it is pretty self explanatory:
# Sample size (not counting negatives!) sample_size = 10000 # Parameters of the distribution mean = 10 sigma = 10 dist = RealDistribution('gaussian', sigma) # Getting the elements (notice the 'mean' in 'event'!) sample =  while len(sample) < sample_size : event = round(mean + dist.get_random_element()) if event >= 0 : sample.append(event) # Getting the frequencies and plotting sample_range = range(min(sample),1+max(sample)) frequencies = [sample.count(i) for i in sample_range] list_plot(zip(sample_range,frequencies),gridlines=[[mean,mean-sigma,mean+sigma],])
Asked: 2012-12-21 00:55:36 -0500
Seen: 504 times
Last updated: Dec 22 '12