I would like to draw npts random variables distributed as a gaussian with mean mu and dispersion sigma. I know how to do this in Numpy:
x = np.random.normal(loc=mu, scale=sigma, size=npts)
print(np.std(x), np.mean(x))
0.1998, 0.3997
This should also be possible to do using scipy.special.erfinv via inverse transforms, beginning from a uniform distribution:
u = np.random.uniform(0, 1, npts)
However, I can't figure out how to get the scaling correct. Has anyone done this before?
Try this:
mean = 100
sigma = 7
x = mean + 2**0.5 * sigma * erfinv(np.random.uniform(size=10**5) * 2 - 1)
x.mean(), x.std()
Out: (99.965915366042381, 7.0062395839075107)
The conversion from erf to normal distribution is from John D. Cook's blog.