def lowess2(x, y, xest, f=2./3., iter=3): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations.""" x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) nest = len(xest) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(iter): # fit xest for i in range(nest): weights = delta * west[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowess2: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2
def newgeo(self,geo,forces): err = -ravel(array(forces)) maxerr = max(abs(err)) print "Maxerror = ",maxerr if maxerr < self.errcut and not self.started: self.started = 1 print "GDIIS started" newgeo = [] nat = len(geo) nit = len(self.errors) if self.started: self.errors.append(err) self.geos.append(geo) if self.started and nit > 1: #print "Nit = ",nit a = zeros((nit+1,nit+1),Float) b = zeros(nit+1,Float) for i in range(nit): for j in range(nit): a[i,j] = dot(self.errors[i], self.errors[j]) for i in range(nit): a[nit,i] = a[i,nit] = -1. b[nit] = -1. c = solve_linear_equations(a,b) newf = zeros(len(self.errors[0]),Float) for i in range(nit): newf += c[i]*self.errors[i] newf = reshape(newf,(nat,3)) for i in range(nat): atno = 0 x = y = z = 0 fx = fy = fz = 0 for j in range(nit): atno,(xj,yj,zj) = self.geos[j][i] x += c[j]*xj y += c[j]*yj z += c[j]*zj newgeo.append((atno,(x,y,z))) for i in range(nat): atno,(xi,yi,zi) = newgeo[i] fxi,fyi,fzi = newf[i] newgeo[i] = atno,(xi-fxi,yi-fyi,zi-fzi) else: for i in range(nat): atno,(x,y,z) = geo[i] fx,fy,fz = forces[i] x += self.sdstep*fx y += self.sdstep*fy z += self.sdstep*fz newgeo.append((atno,(x,y,z))) return newgeo
def _getBasisCoef(self, x, T): """returns coefficients of basis polynomials given T, i.e. their values at x: (poly0, xi) where polynomials in rows, coefficients [a0,a1,...ak] where k=len(x)-1 similar goes for T """ numPoints = T.shape[1] # number of points that polynomials are calculated == len(x) assert len(x) == numPoints, "len(x)=" + str(x) + ", T.shape[1]=" + str(numPoints) + " do not match" numLinSys = T.shape[0] # number of polynomials lin_sys_b = Numeric.transpose(T) lin_sys_a = Numeric.ones((numPoints, numPoints), Numeric.Float) lin_sys_a[:,1] = x for idx in range(2,numPoints): lin_sys_a[:,idx] = lin_sys_a[:,idx-1] * x return Numeric.transpose(LinearAlgebra.solve_linear_equations(lin_sys_a, lin_sys_b))
def quadFit(X, Y): """quadFit(X, Y) --> [a1, a2, ... , b1, b2, ... , c] Fits (X,Y) data to a generalized quadratic: X, Y -- arrays of the (x,y) data, where x is either a scalar or a tuple (x1, x2, x3, ...) Returns [a1, a2, ..., b1, b2, ... , c] such that y = a1*x1^2 + a2*x2^2 + .... b1*x1 + b2*x2 + ... + c""" from numpy import asarray, reshape, ones, concatenate X = asarray(X) if len(X.shape) == 1: X = reshape(X, (X.shape[0], 1)) A = concatenate((X ** 2, X, ones((X.shape[0], 1))), 1) alpha = dot(transpose(A), A) beta = dot(transpose(A), Y) return solve_linear_equations(alpha, beta)
def _getBasisCoef(self, x, T): """returns coefficients of basis polynomials given T, i.e. their values at x: (poly0, xi) where polynomials in rows, coefficients [a0,a1,...ak] where k=len(x)-1 similar goes for T """ numPoints = T.shape[ 1] # number of points that polynomials are calculated == len(x) assert len(x) == numPoints, "len(x)=" + str(x) + ", T.shape[1]=" + str( numPoints) + " do not match" numLinSys = T.shape[0] # number of polynomials lin_sys_b = Numeric.transpose(T) lin_sys_a = Numeric.ones((numPoints, numPoints), Numeric.Float) lin_sys_a[:, 1] = x for idx in range(2, numPoints): lin_sys_a[:, idx] = lin_sys_a[:, idx - 1] * x return Numeric.transpose( LinearAlgebra.solve_linear_equations(lin_sys_a, lin_sys_b))
def lowessW(x, y, xest, f=2./3., iter=3, dWeights=None, callback=None): """Returns estimated values of y in data points xest (or None if estimation fails). Lowess smoother: Robust locally weighted regression. The lowess function fits a nonparametric regression curve to a scatterplot. The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines a data point in the scatterplot. The function returns the estimated (smooth) values of y. The smoothing span is given by f. A larger value for f will result in a smoother curve. The number of robustifying iterations is given by iter. The function will run faster with a smaller number of iterations. Data points may be assigned weights; if None, all weights equal 1. """ x = Numeric.asarray(x, 'd') y = Numeric.asarray(y, 'd') xest = Numeric.asarray(xest, 'd') n = len(x) if n <> len(y): raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(x)=%i not equal to len(y)=%i" % (len(x), len(y)) nest = len(xest) # weights of data points (optional) if dWeights <> None: dWeights = Numeric.asarray(dWeights, 'd') if len(dWeights) <> n: raise AttributeError, "Error: lowessW(x,y,xest,f,iter,dWeights): len(dWeights)=%i not equal to len(x)=%i" % (len(dWeights), len(x)) ## dWeights = dWeights.reshape((n,1)) else: ## dWeights = Numeric.ones((n,1)) dWeights = Numeric.ones((n,)) r = min(int(Numeric.ceil(f*n)),n-1) # radius: num. of points to take into LR h = [Numeric.sort(abs(x-x[i]))[r] for i in range(n)] # distance of the r-th point from x[i] w = Numeric.clip(abs(([x]-Numeric.transpose([x]))/h),0.0,1.0) w = 1-w*w*w w = w*w*w hest = [Numeric.sort(abs(x-xest[i]))[r] for i in range(nest)] # r-th min. distance from xest[i] to x west = Numeric.clip(abs(([xest]-Numeric.transpose([x]))/hest),0.0,1.0) # shape: (len(x), len(xest)) west = 1-west*west*west west = west*west*west yest = Numeric.zeros(n,'d') yest2 = Numeric.zeros(nest,'d') delta = Numeric.ones(n,'d') try: for iteration in range(int(iter)): # fit xest for i in range(nest): ## print delta.shape, west[:,i].shape, dWeights.shape weights = delta * west[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest2[i] = beta[0] + beta[1]*xest[i] # fit x (to calculate residuals and delta) for i in range(n): weights = delta * w[:,i] * dWeights b = Numeric.array([sum(weights*y), sum(weights*y*x)]) A = Numeric.array([[sum(weights), sum(weights*x)], [sum(weights*x), sum(weights*x*x)]]) beta = LinearAlgebra.solve_linear_equations(A,b) yest[i] = beta[0] + beta[1]*x[i] residuals = y-yest s = MLab.median(abs(residuals)) delta = Numeric.clip(residuals/(6*s),-1,1) delta = 1-delta*delta delta = delta*delta if callback: callback() except LinearAlgebra.LinAlgError: print "Warning: NumExtn.lowessW: LinearAlgebra.solve_linear_equations: Singular matrix" yest2 = None return yest2