What is the difference between random.normalvariate() and random.gauss() in python?

This is an interesting question. In general, the best way to know the difference between two python implementations is to inspect the code yourself: import inspect, random str_gauss = inspect.getsource(random.gauss) str_nv=inspect.getsource(random.normalvariate) and then you print each of the strings to see how the sources differ. A quick look at the codes show that not only … Read more

Gaussian fit for Python

Here is corrected code: import pylab as plb import matplotlib.pyplot as plt from scipy.optimize import curve_fit from scipy import asarray as ar,exp x = ar(range(10)) y = ar([0,1,2,3,4,5,4,3,2,1]) n = len(x) #the number of data mean = sum(x*y)/n #note this correction sigma = sum(y*(x-mean)**2)/n #note this correction def gaus(x,a,x0,sigma): return a*exp(-(x-x0)**2/(2*sigma**2)) popt,pcov = curve_fit(gaus,x,y,p0=[1,mean,sigma]) plt.plot(x,y,’b+:’,label=”data”) … Read more

Sample from multivariate normal/Gaussian distribution in C++

Since this question has garnered a lot of views, I thought I’d post code for the final answer that I found, in part, by posting to the Eigen forums. The code uses Boost for the univariate normal and Eigen for matrix handling. It feels rather unorthodox, since it involves using the “internal” namespace, but it … Read more

How to specify upper and lower limits when using numpy.random.normal

It sounds like you want a truncated normal distribution. Using scipy, you could use scipy.stats.truncnorm to generate random variates from such a distribution: import matplotlib.pyplot as plt import scipy.stats as stats lower, upper = 3.5, 6 mu, sigma = 5, 0.7 X = stats.truncnorm( (lower – mu) / sigma, (upper – mu) / sigma, loc=mu, … Read more