[Numpy-discussion] Statistical distributions on samples

Andrea Gavana andrea.gavana@gmail....
Mon Aug 15 04:10:37 CDT 2011

Hi Chris & Brennan,

On 15 August 2011 00:59, Brennan Williams wrote:
> You can use scipy.stats.truncnorm, can't you? Unless I misread, you want to
> sample a normal distribution but with generated values only being within a
> specified range? However you also say you want to do this with triangular
> and log normal and for these I presume the easiest way is to sample and then
> accept/reject.
> Brennan
> On 13/08/2011 2:53 a.m., Christopher Jordan-Squire wrote:
> Hi Andrea--An easy way to get something like this would be
> import numpy as np
> import scipy.stats as stats
> sigma = #some reasonable standard deviation for your application
> x = stats.norm.rvs(size=1000, loc=125, scale=sigma)
> x = x[x>50]
> x = x[x<200]
> That will give a roughly normal distribution to your velocities, as long as,
> say, sigma<25. (I'm using the rule of thumb for the normal distribution that
> normal random samples lie 3 standard deviations away from the mean about 1
> out of 350 times.) Though you won't be able to get exactly normal errors
> about your mean since normal random samples can theoretically be of any
> size.
> You can use this same process for any other distribution, as long as you've
> chosen a scale variable so that the probability of samples being outside
> your desired interval is really small. Of course, once again your random
> errors won't be exactly from the distribution you get your original samples
> from.

Thank you for your answer. Indeed, it appears that a truncated
distribution implementation exists only for the normal distribution
(in the subset of distributions I need to use). I haven't checked yet
what the code for truncnorm does but maybe it might be possible to
apply the same approach for other distributions. In any case the
sampling/reject/accept approach is the best approach for me, due to my
ignorance about statistical things :-)

Thank you again.


"Imagination Is The Only Weapon In The War Against Reality."

>>> import PyQt4.QtGui
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: No module named PyQt4.QtGui
>>> import pygtk
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: No module named pygtk
>>> import wx

More information about the NumPy-Discussion mailing list