def _hermnorm(N): # return the negatively normalized hermite polynomials up to order N-1 # (inclusive) # using the recursive relationship # p_n+1 = p_n(x)' - x*p_n(x) # and p_0(x) = 1 plist = [None]*N plist[0] = poly1d(1) for n in range(1,N): plist[n] = plist[n-1].deriv() - poly1d([1,0])*plist[n-1] return plist
def pdf_moments(cnt): """Return the Gaussian expanded pdf function given the list of central moments (first one is mean). """ N = len(cnt) if N < 2: raise ValueError, "At least two moments must be given to" + \ "approximate the pdf." totp = poly1d(1) sig = sqrt(cnt[1]) mu = cnt[0] if N > 2: Dvals = _hermnorm(N+1) for k in range(3,N+1): # Find Ck Ck = 0.0 for n in range((k-3)/2): m = k-2*n if m % 2: # m is odd momdiff = cnt[m-1] else: momdiff = cnt[m-1] - sig*sig*scipy.factorial2(m-1) Ck += Dvals[k][m] / sig**m * momdiff # Add to totp totp = totp + Ck*Dvals[k] def thisfunc(x): xn = (x-mu)/sig return totp(xn)*exp(-xn*xn/2.0)/sqrt(2*pi)/sig return thisfunc
def pade(an, m): """Given Taylor series coefficients in an, return a Pade approximation to the function as the ratio of two polynomials p / q where the order of q is m. """ an = asarray(an) N = len(an) - 1 n = N-m if (n < 0): raise ValueError, \ "Order of q <m> must be smaller than len(an)-1." Akj = eye(N+1,n+1) Bkj = zeros((N+1,m),'d') for row in range(1,m+1): Bkj[row,:row] = -(an[:row])[::-1] for row in range(m+1,N+1): Bkj[row,:] = -(an[row-m:row])[::-1] C = hstack((Akj,Bkj)) pq = dot(linalg.inv(C),an) p = pq[:n+1] q = r_[1.0,pq[n+1:]] return poly1d(p[::-1]), poly1d(q[::-1])