def levinson_durbin_nitime(s, order=10, isacov=False): '''Levinson-Durbin recursion for autoregressive processes ''' #from nitime ## if sxx is not None and type(sxx) == np.ndarray: ## sxx_m = sxx[:order+1] ## else: ## sxx_m = ut.autocov(s)[:order+1] if isacov: sxx_m = s else: sxx_m = acovf(s)[:order + 1] #not tested phi = np.zeros((order + 1, order + 1), 'd') sig = np.zeros(order + 1) # initial points for the recursion phi[1, 1] = sxx_m[1] / sxx_m[0] sig[1] = sxx_m[0] - phi[1, 1] * sxx_m[1] for k in xrange(2, order + 1): phi[k, k] = (sxx_m[k] - np.dot(phi[1:k, k - 1], sxx_m[1:k][::-1])) / sig[k - 1] for j in xrange(1, k): phi[j, k] = phi[j, k - 1] - phi[k, k] * phi[k - j, k - 1] sig[k] = sig[k - 1] * (1 - phi[k, k]**2) sigma_v = sig[-1] arcoefs = phi[1:, -1] return sigma_v, arcoefs, pacf, phi #return everything
def levinson_durbin_nitime(s, order=10, isacov=False): '''Levinson-Durbin recursion for autoregressive processes ''' #from nitime ## if sxx is not None and type(sxx) == np.ndarray: ## sxx_m = sxx[:order+1] ## else: ## sxx_m = ut.autocov(s)[:order+1] if isacov: sxx_m = s else: sxx_m = acovf(s)[:order+1] #not tested phi = np.zeros((order+1, order+1), 'd') sig = np.zeros(order+1) # initial points for the recursion phi[1,1] = sxx_m[1]/sxx_m[0] sig[1] = sxx_m[0] - phi[1,1]*sxx_m[1] for k in xrange(2,order+1): phi[k,k] = (sxx_m[k]-np.dot(phi[1:k,k-1], sxx_m[1:k][::-1]))/sig[k-1] for j in xrange(1,k): phi[j,k] = phi[j,k-1] - phi[k,k]*phi[k-j,k-1] sig[k] = sig[k-1]*(1 - phi[k,k]**2) sigma_v = sig[-1]; arcoefs = phi[1:,-1] return sigma_v, arcoefs, pacf, phi #return everything
plt.plot(wm,sdm/sdm[0], '-', wm[maxind], sdm[maxind]/sdm[0], 'o') else: plt.plot(wm, sdm, '-', wm[maxind], sdm[maxind], 'o') plt.title('matplotlib') if hastalkbox: sdp, wp = stbs.periodogram(x) plt.subplot(2,3,3) if rescale: plt.plot(wp,sdp/sdp[0]) else: plt.plot(wp, sdp) plt.title('stbs.periodogram') xacov = acovf(x, unbiased=False) plt.subplot(2,3,4) plt.plot(xacov) plt.title('autocovariance') nr = len(x)#*2/3 #xacovfft = np.fft.fft(xacov[:nr], 2*nr-1) xacovfft = np.fft.fft(np.correlate(x,x,'full')) #abs(xacovfft)**2 or equivalently xacovfft = xacovfft * xacovfft.conj() plt.subplot(2,3,5) if rescale: plt.plot(xacovfft[:nr]/xacovfft[0]) else: plt.plot(xacovfft[:nr])
plt.plot(wm, sdm / sdm[0], '-', wm[maxind], sdm[maxind] / sdm[0], 'o') else: plt.plot(wm, sdm, '-', wm[maxind], sdm[maxind], 'o') plt.title('matplotlib') if hastalkbox: sdp, wp = stbs.periodogram(x) plt.subplot(2, 3, 3) if rescale: plt.plot(wp, sdp / sdp[0]) else: plt.plot(wp, sdp) plt.title('stbs.periodogram') xacov = acovf(x, unbiased=False) plt.subplot(2, 3, 4) plt.plot(xacov) plt.title('autocovariance') nr = len(x) #*2/3 #xacovfft = np.fft.fft(xacov[:nr], 2*nr-1) xacovfft = np.fft.fft(np.correlate(x, x, 'full')) #abs(xacovfft)**2 or equivalently xacovfft = xacovfft * xacovfft.conj() plt.subplot(2, 3, 5) if rescale: plt.plot(xacovfft[:nr] / xacovfft[0]) else: plt.plot(xacovfft[:nr])
kwargs.setdefault("linestyle", "None") a, = self.plot(lags, c, **kwargs) b = None return lags, c, a, b arrvs = ar_generator() ##arma = ARIMA() ##res = arma.fit(arrvs[0], 4, 0) arma = ARIMA(arrvs[0]) res = arma.fit((4, 0, 0)) print res[0] acf1 = acf(arrvs[0]) acovf1b = acovf(arrvs[0], unbiased=False) acf2 = autocorr(arrvs[0]) acf2m = autocorr(arrvs[0] - arrvs[0].mean()) print acf1[:10] print acovf1b[:10] print acf2[:10] print acf2m[:10] x = arma_generate_sample([1.0, -0.8], [1.0], 500) print acf(x)[:20] import scikits.statsmodels.api as sm print sm.regression.yule_walker(x, 10) import matplotlib.pyplot as plt
kwargs.setdefault('linestyle', 'None') a, = self.plot(lags, c, **kwargs) b = None return lags, c, a, b arrvs = ar_generator() ##arma = ARIMA() ##res = arma.fit(arrvs[0], 4, 0) arma = ARIMA(arrvs[0]) res = arma.fit((4, 0, 0)) print res[0] acf1 = acf(arrvs[0]) acovf1b = acovf(arrvs[0], unbiased=False) acf2 = autocorr(arrvs[0]) acf2m = autocorr(arrvs[0] - arrvs[0].mean()) print acf1[:10] print acovf1b[:10] print acf2[:10] print acf2m[:10] x = arma_generate_sample([1.0, -0.8], [1.0], 500) print acf(x)[:20] import scikits.statsmodels.api as sm print sm.regression.yule_walker(x, 10) import matplotlib.pyplot as plt #ax = plt.axes() plt.plot(x)
xhat5, err5 = VARMA(x,B,C) #print err5 #in differences #VARMA(np.diff(x,axis=0),B,C) #Note: # * signal correlate applies same filter to all columns if kernel.shape[1]<K # e.g. signal.correlate(x0,np.ones((3,1)),'valid') # * if kernel.shape[1]==K, then `valid` produces a single column # -> possible to run signal.correlate K times with different filters, # see the following example, which replicates VAR filter x0 = np.column_stack([np.arange(T), 2*np.arange(T)]) B[:,:,0] = np.ones((P,K)) B[:,:,1] = np.ones((P,K)) B[1,1,1] = 0 xhat0 = VAR(x0,B) xcorr00 = signal.correlate(x0,B[:,:,0])#[:,0] xcorr01 = signal.correlate(x0,B[:,:,1]) print np.all(signal.correlate(x0,B[:,:,0],'valid')[:-1,0]==xhat0[P:,0]) print np.all(signal.correlate(x0,B[:,:,1],'valid')[:-1,0]==xhat0[P:,1]) #import error #from movstat import acovf, acf from scikits.statsmodels.tsa.stattools import acovf, acf aav = acovf(x[:,0]) print aav[0] == np.var(x[:,0]) aac = acf(x[:,0])