from statsmodels.sandbox import tsa from statsmodels.tsa.arma_mle import Arma # local import from statsmodels.tsa.arima_process import arma_generate_sample examples = ['arma'] if 'arma' in examples: print("\nExample 1") print('----------') ar = [1.0, -0.8] ma = [1.0, 0.5] y1 = arma_generate_sample(ar, ma, 1000, 0.1) y1 -= y1.mean() #no mean correction/constant in estimation so far arma1 = Arma(y1) arma1.nar = 1 arma1.nma = 1 arma1res = arma1.fit_mle(order=(1, 1), method='fmin') print(arma1res.params) #Warning need new instance otherwise results carry over arma2 = Arma(y1) arma2.nar = 1 arma2.nma = 1 res2 = arma2.fit(method='bfgs') print(res2.params) print(res2.model.hessian(res2.params)) print(ndt.Hessian(arma1.loglike, stepMax=1e-2)(res2.params)) arest = tsa.arima.ARIMA(y1) resls = arest.fit((1, 0, 1)) print(resls[0])
print 'truelhs', np.r_[ar[1:], ma[1:]] ###bug in current version, fixed in Skipper and 1 more ###arr[1:q,:] = params[p+k:p+k+q] # p to p+q short params are MA coeffs ###ValueError: array dimensions are not compatible for copy ##arma22 = ARMA_kf(y22, constant=False, order=(2,2)) ##res = arma22.fit(start_params=start_params) ##print res.params print '\nARIMA new' arest2 = Arma(y22) naryw = 4 #= 30 resyw = sm.regression.yule_walker(y22, order=naryw, inv=True) arest2.nar = naryw arest2.nma = 0 e = arest2.geterrors(np.r_[1, -resyw[0]]) x = sm.tsa.tsatools.lagmat2ds(np.column_stack((y22, e)), 3, dropex=1, trim='both') yt = x[:, 0] xt = x[:, 1:] res_ols = sm.OLS(yt, xt).fit() print 'hannan_rissannen' print res_ols.params start_params = res_ols.params start_params_mle = np.r_[-res_ols.params[:2], res_ols.params[2:], #res_ols.scale] #areste.var()]
###bug in current version, fixed in Skipper and 1 more ###arr[1:q,:] = params[p+k:p+k+q] # p to p+q short params are MA coeffs ###ValueError: array dimensions are not compatible for copy ##from statsmodels.tsa.arima import ARMA as ARMA_kf ##arma22 = ARMA_kf(y22, constant=False, order=(2,2)) ##res = arma22.fit(start_params=start_params) ##print res.params print('\nARIMA new') arest2 = Arma(y22) naryw = 4 #= 30 resyw = sm.regression.yule_walker(y22, order=naryw, inv=True) arest2.nar = naryw arest2.nma = 0 e = arest2.geterrors(np.r_[1, -resyw[0]]) x=sm.tsa.tsatools.lagmat2ds(np.column_stack((y22,e)),3,dropex=1, trim='both') yt = x[:,0] xt = x[:,1:] res_ols = sm.OLS(yt, xt).fit() print('hannan_rissannen') print(res_ols.params) start_params = res_ols.params start_params_mle = np.r_[-res_ols.params[:2], res_ols.params[2:], #res_ols.scale] #areste.var()] np.sqrt(res_ols.scale)]
results += ["exact mle kalmanf", arma2.params, arma2.sigma2**.5, arma2.llf] print('time used:', t1-t0) t1=time() print("CSS MLE - ARMA Class") arma2.fit(order=(2,2), trend='nc', method="css") t2=time() arma2.llf = arma2.loglike_css(arma2._invtransparams(arma2.params)) print("params: ", arma2.params) print("sigma: ", arma2.sigma2**.5) results += ["css kalmanf", arma2.params, arma2.sigma2**.5, arma2.llf] print('time used:', t2-t1) print("Arma.fit_mle results") # have to set nar and nma manually arma1.nar = 2 arma1.nma = 2 t2=time() ret = arma1.fit_mle() t3=time() print("params, first 4, sigma, last 1 ", ret.params) results += ["Arma.fit_mle ", ret.params[:4], ret.params[-1], ret.llf] print('time used:', t3-t2) print("Arma.fit method = \"ls\"") t3=time() ret2 = arma1.fit(order=(2,0,2), method="ls") t4=time() print(ret2[0]) results += ["Arma.fit ls", ret2[0]] print('time used:', t4-t3)
results += ["exact mle kalmanf", arma2.params, arma2.sigma2**.5, arma2.llf] print('time used:', t1 - t0) t1 = time() print("CSS MLE - ARMA Class") arma2.fit(order=(2, 2), trend='nc', method="css") t2 = time() arma2.llf = arma2.loglike_css(arma2._invtransparams(arma2.params)) print("params: ", arma2.params) print("sigma: ", arma2.sigma2**.5) results += ["css kalmanf", arma2.params, arma2.sigma2**.5, arma2.llf] print('time used:', t2 - t1) print("Arma.fit_mle results") # have to set nar and nma manually arma1.nar = 2 arma1.nma = 2 t2 = time() ret = arma1.fit_mle() t3 = time() print("params, first 4, sigma, last 1 ", ret.params) results += ["Arma.fit_mle ", ret.params[:4], ret.params[-1], ret.llf] print('time used:', t3 - t2) print("Arma.fit method = \"ls\"") t3 = time() ret2 = arma1.fit(order=(2, 0, 2), method="ls") t4 = time() print(ret2[0]) results += ["Arma.fit ls", ret2[0]] print('time used:', t4 - t3)
from statsmodels.sandbox import tsa from statsmodels.tsa.arma_mle import Arma # local import from statsmodels.tsa.arima_process import arma_generate_sample examples = ['arma'] if 'arma' in examples: print "\nExample 1" print '----------' ar = [1.0, -0.8] ma = [1.0, 0.5] y1 = arma_generate_sample(ar,ma,1000,0.1) y1 -= y1.mean() #no mean correction/constant in estimation so far arma1 = Arma(y1) arma1.nar = 1 arma1.nma = 1 arma1res = arma1.fit_mle(order=(1,1), method='fmin') print arma1res.params #Warning need new instance otherwise results carry over arma2 = Arma(y1) arma2.nar = 1 arma2.nma = 1 res2 = arma2.fit(method='bfgs') print res2.params print res2.model.hessian(res2.params) print ndt.Hessian(arma1.loglike, stepMax=1e-2)(res2.params) arest = tsa.arima.ARIMA(y1) resls = arest.fit((1,0,1)) print resls[0]