From: D2Hitman on 20 Jul 2010 22:13 I want to fit an n-dimensional distribution with an n-dimensional gaussian. So far i have managed to do this in 2d (see below). I am not sure how to convert this to work in n-dimensions. Using "ravel" on the arrays is not ideal, but optimize does not appear to work on multidimensional arrays. It seems "meshgrid" also does not translate to nd. import numpy as np from scipy import optimize #2D #random points N = 1000 nd = 2 a = np.random.normal(loc=[1.,2.], scale=[1.,2.], size=[N,nd]) #histogram points bins = [10, 20] H, e = np.histogramdd(a, bins=bins) #find center rather than left edge edges = [] for i in range(len(e)): edges.append( e[i][:-1] + np.diff(e[i])/2. ) #not n-dimensional x, y = np.meshgrid(edges, edges) x, y = np.ravel(x), np.ravel(y) H = np.ravel(H) #2D gaussian gauss2d = lambda p, x, y: p * np.exp( (-0.5*(x-p)**2/p**2) - (0.5*(y-p)**2/p**2) ) + p residuals = lambda p, x, y, H: H - gauss2d(p, x, y) #Fitting p0 = [1., 0., 1., 0., 1., 0] plsq, cov_x, infodict, mesg, ier = optimize.leastsq(residuals, p0, args=(x, y, H), full_output=True) #Reading off _x, _y = 0.091, 1.293 print '%.3f %.3f %.4f' % (_x, _y, gauss2d(plsq, _x,_y) / plsq) -- View this message in context: http://old.nabble.com/Multidimensional-Fitting-tp29221343p29221343.html Sent from the Python - python-list mailing list archive at Nabble.com. From: Robert Kern on 20 Jul 2010 22:22 On 7/20/10 10:13 PM, D2Hitman wrote:> > I want to fit an n-dimensional distribution with an n-dimensional gaussian. > So far i have managed to do this in 2d (see below). I am not sure how to > convert this to work in n-dimensions. Using "ravel" on the arrays is not > ideal, but optimize does not appear to work on multidimensional arrays. It > seems "meshgrid" also does not translate to nd. You will want to ask scipy questions on the scipy mailing list: http://www.scipy.org/Mailing_Lists Don't try to fit a Gaussian to a histogram using least-squares. It's an awful way to estimate the parameters. Just use np.mean() and np.cov() to estimate the mean and covariance matrix directly. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From: D2Hitman on 20 Jul 2010 23:56 Robert Kern-2 wrote:> > Don't try to fit a Gaussian to a histogram using least-squares. It's an > awful > way to estimate the parameters. Just use np.mean() and np.cov() to > estimate the > mean and covariance matrix directly. > Ok, what about distributions other than gaussian? Would you use leastsq in that case? If yes, i will post that to the scipy mailing list. -- View this message in context: http://old.nabble.com/Multidimensional-Fitting-tp29221343p29221776.html Sent from the Python - python-list mailing list archive at Nabble.com. From: Robert Kern on 21 Jul 2010 00:31 On 7/20/10 11:56 PM, D2Hitman wrote:> > > Robert Kern-2 wrote: >> >> Don't try to fit a Gaussian to a histogram using least-squares. It's an >> awful >> way to estimate the parameters. Just use np.mean() and np.cov() to >> estimate the >> mean and covariance matrix directly. >> > > Ok, what about distributions other than gaussian? Would you use leastsq in > that case? If yes, i will post that to the scipy mailing list. No, you would use a maximum likelihood method. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco  |