def linefit(x,y,weights=None): """ Parameters ---------- y: 1D numpy array The data to be fitted x: 1D numpy array The x values of the y array. x and y must have the same shape. weights: 1D numpy array, must have the same shape as x and y weight values Examples -------- >>> x=N.array([-5, -4 ,-3 ,-2 ,-1, 0, 1, 2, 3, 4, 5]) >>> y=N.array([1, 5, 4, 7, 10, 8, 9, 13, 14, 13, 18]) >>> around(linefit(x,y), decimals=5) array([ 9.27273, 1.43636]) >>> x=N.array([1.3,1.3,2.0,2.0,2.7,3.3,3.3,3.7,3.7,4.,4.,4.,4.7,4.7,5.,5.3,5.3,5.3,5.7,6.,6.,6.3,6.7]) >>> y = N.array([2.3,1.8,2.8,1.5,2.2,3.8,1.8,3.7,1.7,2.8,2.8,2.2,3.2,1.9,1.8,3.5,2.8,2.1,3.4,3.2,3.,3.,5.9]) >>> around(linefit(x,y), decimals=5) array([ 1.42564, 0.31579]) """ if numerixenv.check_input(x) or numerixenv.check_input(y): raise ValueError, "Input is a NumArray array. This version of %s requires a Numpy array\n" % __name__ if len(x) != len(y): print "Error: X and Y must have equal size\n" return n = len(x) w = N.zeros((n,n)).astype(N.float) if weights == None: for i in N.arange(n): w[i,i] = 1 else: if len(weights) != n: print "Error: Weights must have the same size as X and Y.\n" return for i in N.arange(n): w[i,i] = weights[i] x = x.astype(N.float) y = y.astype(N.float) # take the weighted avg for calculatiing the covarince Xavg = N.sum(N.dot(w,x)) / N.sum(w.diagonal()) Yavg = N.sum(N.dot(w,y)) / N.sum(w.diagonal()) xm = x - Xavg xmt = N.transpose(xm) ym = y - Yavg b1 = N.dot(xmt,N.dot(w,ym)) / N.dot(xmt ,N.dot(w,xm)) b0 = Yavg - b1 * Xavg return b0, b1
def gfit1d(y, x=None, err = None, weights=None, par=None, parinfo=None, maxiter=200, quiet=0): """ Return the gaussian fit as an object. Parameters ---------- y: 1D Numarray array The data to be fitted x: 1D Numarray array (optional) The x values of the y array. x and y must have the same shape. err: 1D Numarray array (optional) 1D array with measurement errors, must be the same shape as y weights: 1D Numarray array (optiional) 1D array with weights, must be the same shape as y par: List (optional) Starting values for the parameters to be fitted parinfo: Dictionary of lists (optional) provides additional information for the parameters. For a detailed description see nmpfit.py. Parinfo can be used to limit parameters or keep some of them fixed. maxiter: number Maximum number of iterations to perform Default: 200 quiet: number if set to 1, nmpfit does not print to the screen Default: 0 Examples -------- >>> x=N.arange(10,20, 0.1) >>> y= 10*N.e**(-(x-15)**2/4) >>> print gfit1d(y,x=x, maxiter=20,quiet=1).params [ 10. 15. 1.41421356] """ if numerixenv.check_input(x) or numerixenv.check_input(y): raise ValueError, "Input is a NumArray array. This version of %s requires a Numpy array\n" % __name__ y = y.astype(N.float) if weights != None: weights = weights.astype(N.float) if err != None: err = err.astype(N.float) if x == None and len(y.shape)==1 : x = N.arange(len(y)).astype(N.float) if x.shape != y.shape: print "input arrays X and Y must be of equal shape.\n" return fa = {'x':x, 'y':y, 'err':err, 'weights':weights} if par != None: p = par else: ysigma = y.std() ind = N.nonzero(y > ysigma)[0] if len(ind) != 0: xind = int(ind.mean()) p2 = x[xind] p1 = y[xind] p3 = 1.0 else: ymax = y.max() ymin = y.min() ymean= y.mean() if (ymax - ymean) > (abs(ymin - ymean)): p1 = ymax else: p1 = ymin ind = (N.nonzero(y == p1))[0] p2 = x.mean() p3 = 1. p = [p1, p2, p3] m=nmpfit.mpfit(_gauss_funct, p,parinfo = parinfo, functkw=fa, maxiter=maxiter, quiet=quiet) if (m.status <=0): print 'error message = ', m.errmsg return m