예제 #1
0
def mcarma22(niter=10, nsample=1000, ar=None, ma=None, sig=0.5):
    '''run Monte Carlo for ARMA(2,2)

    DGP parameters currently hard coded
    also sample size `nsample`

    was not a self contained function, used instances from outer scope
      now corrected

    '''
    #nsample = 1000
    #ar = [1.0, 0, 0]
    if ar is None:
        ar = [1.0, -0.55, -0.1]
    #ma = [1.0, 0, 0]
    if ma is None:
        ma = [1.0, 0.3, 0.2]
    results = []
    results_bse = []
    for _ in range(niter):
        y2 = arma_generate_sample(ar, ma, nsample + 1000, sig)[-nsample:]
        y2 -= y2.mean()
        arest2 = Arma(y2)
        rhohat2a, cov_x2a, infodict, mesg, ier = arest2.fit((2, 2))
        results.append(rhohat2a)
        err2a = arest2.geterrors(rhohat2a)
        sige2a = np.sqrt(np.dot(err2a, err2a) / nsample)
        #print 'sige2a', sige2a,
        #print 'cov_x2a.shape', cov_x2a.shape
        #results_bse.append(sige2a * np.sqrt(np.diag(cov_x2a)))
        if not cov_x2a is None:
            results_bse.append(sige2a * np.sqrt(np.diag(cov_x2a)))
        else:
            results_bse.append(np.nan + np.zeros_like(rhohat2a))
    return np.r_[ar[1:], ma[1:]], np.array(results), np.array(results_bse)
예제 #2
0
    def setup_class(cls):

        nobs = 500
        ar = [1, -0.5, 0.1]
        ma = [1, 0.7]
        dist = lambda n: np.random.standard_t(3, size=n)
        np.random.seed(8659567)
        x = arma_generate_sample(ar, ma, nobs, sigma=1, distrvs=dist,
                                 burnin=500)

        mod = Arma(x)
        order = (2, 1)
        cls.res_ls = mod.fit(order=order)
        cls.res = mod.fit_mle(order=order,
                              start_params=np.r_[cls.res_ls[0], 1],
                              method='nm', disp=False)

        cls.res1_table = np.array(
          [[  0.4339072 ,  -0.08402653,   0.73292344,   1.61661128],
           [  0.05854268,   0.05562941,   0.04034178,   0.0511207 ],
           [  7.4118102 ,  -1.51046975,  18.16785075,  31.62341666],
           [  0.        ,   0.1309236 ,   0.        ,   0.        ],
           [  0.06713617,   0.05469138,   0.03785006,   0.1071093 ],
           [  0.05504093,   0.0574849 ,   0.04350945,   0.02510928]])

        cls.res1_conf_int = np.array([[ 0.31916567,  0.54864874],
                               [-0.19305817,  0.0250051 ],
                               [ 0.65385501,  0.81199188],
                               [ 1.51641655,  1.71680602]])

        cls.ls_params = np.array([ 0.43393123, -0.08402678,  0.73293058])
        cls.ls_bse = np.array([ 0.0377741 ,  0.03567847,  0.02744488])
예제 #3
0
    def setup_class(cls):

        nobs = 500
        ar = [1, -0.5, 0.1]
        ma = [1, 0.7]
        dist = lambda n: np.random.standard_t(3, size=n)
        np.random.seed(8659567)
        x = arma_generate_sample(ar, ma, nobs, sigma=1, distrvs=dist,
                                 burnin=500)

        mod = Arma(x)
        order = (2, 1)
        cls.res_ls = mod.fit(order=order)
        cls.res = mod.fit_mle(order=order,
                              start_params=np.r_[cls.res_ls[0], 1],
                              method='nm', disp=False)

        cls.res1_table = np.array(
          [[  0.4339072 ,  -0.08402653,   0.73292344,   1.61661128],
           [  0.05854268,   0.05562941,   0.04034178,   0.0511207 ],
           [  7.4118102 ,  -1.51046975,  18.16785075,  31.62341666],
           [  0.        ,   0.1309236 ,   0.        ,   0.        ],
           [  0.06713617,   0.05469138,   0.03785006,   0.1071093 ],
           [  0.05504093,   0.0574849 ,   0.04350945,   0.02510928]])

        cls.res1_conf_int = np.array([[ 0.31916567,  0.54864874],
                               [-0.19305817,  0.0250051 ],
                               [ 0.65385501,  0.81199188],
                               [ 1.51641655,  1.71680602]])

        cls.ls_params = np.array([ 0.43393123, -0.08402678,  0.73293058])
        cls.ls_bse = np.array([ 0.0377741 ,  0.03567847,  0.02744488])
예제 #4
0
def mcarma22(niter=10, nsample=1000, ar=None, ma=None, sig=0.5):
    '''run Monte Carlo for ARMA(2,2)

    DGP parameters currently hard coded
    also sample size `nsample`

    was not a self contained function, used instances from outer scope
      now corrected

    '''
    #nsample = 1000
    #ar = [1.0, 0, 0]
    if ar is None:
        ar = [1.0, -0.55, -0.1]
    #ma = [1.0, 0, 0]
    if ma is None:
        ma = [1.0,  0.3,  0.2]
    results = []
    results_bse = []
    for _ in range(niter):
        y2 = arma_generate_sample(ar,ma,nsample+1000, sig)[-nsample:]
        y2 -= y2.mean()
        arest2 = Arma(y2)
        rhohat2a, cov_x2a, infodict, mesg, ier = arest2.fit((2,2))
        results.append(rhohat2a)
        err2a = arest2.geterrors(rhohat2a)
        sige2a = np.sqrt(np.dot(err2a,err2a)/nsample)
        #print('sige2a', sige2a,
        #print('cov_x2a.shape', cov_x2a.shape
        #results_bse.append(sige2a * np.sqrt(np.diag(cov_x2a)))
        if cov_x2a is not None:
            results_bse.append(sige2a * np.sqrt(np.diag(cov_x2a)))
        else:
            results_bse.append(np.nan + np.zeros_like(rhohat2a))
    return np.r_[ar[1:], ma[1:]], np.array(results), np.array(results_bse)
예제 #5
0
def test_compare_arma():
    #this is a preliminary test to compare arma_kf, arma_cond_ls and arma_cond_mle
    #the results returned by the fit methods are incomplete
    #for now without random.seed

    #np.random.seed(9876565)
    x = fa.ArmaFft([1, -0.5], [1., 0.4], 40).generate_sample(size=200,
            burnin=1000)

# this used kalman filter through descriptive
#    d = ARMA(x)
#    d.fit((1,1), trend='nc')
#    dres = d.res

    modkf = ARMA(x)
    ##rkf = mkf.fit((1,1))
    ##rkf.params
    reskf = modkf.fit((1,1), trend='nc', disp=-1)
    dres = reskf

    modc = Arma(x)
    resls = modc.fit(order=(1,1))
    rescm = modc.fit_mle(order=(1,1), start_params=[0.4,0.4, 1.], disp=0)

    #decimal 1 corresponds to threshold of 5% difference
    #still different sign  corrcted
    #assert_almost_equal(np.abs(resls[0] / d.params), np.ones(d.params.shape), decimal=1)
    assert_almost_equal(resls[0] / dres.params, np.ones(dres.params.shape),
        decimal=1)
    #rescm also contains variance estimate as last element of params

    #assert_almost_equal(np.abs(rescm.params[:-1] / d.params), np.ones(d.params.shape), decimal=1)
    assert_almost_equal(rescm.params[:-1] / dres.params, np.ones(dres.params.shape), decimal=1)
예제 #6
0
    def setup_class(cls):
        nobs = 500
        ar = [1, -0.5, 0.1]
        ma = [1, 0.7]
        dist = partial(np.random.standard_t, 3)
        np.random.seed(8659567)
        x = arma_generate_sample(ar,
                                 ma,
                                 nobs,
                                 scale=1,
                                 distrvs=dist,
                                 burnin=500)

        with pytest.warns(FutureWarning):
            mod = Arma(x)
        order = (2, 1)
        cls.res_ls = mod.fit(order=order)
        cls.res = mod.fit_mle(
            order=order,
            start_params=np.r_[cls.res_ls[0], 1],
            method="nm",
            disp=False,
        )

        cls.res1_table = np.array([
            [0.43390720, -0.08402653, 0.73292344, 1.61661128],
            [0.05854268, 0.055629410, 0.04034178, 0.05112070],
            [7.41181020, -1.51046975, 18.16785075, 31.62341666],
            [0.00000000, 0.130923600, 0.00000000, 0.00000000],
            [0.06713617, 0.054691380, 0.03785006, 0.10710930],
            [0.05504093, 0.057484900, 0.04350945, 0.02510928],
        ])

        cls.res1_conf_int = np.array([
            [0.31916567, 0.54864874],
            [-0.19305817, 0.02500510],
            [0.65385501, 0.81199188],
            [1.51641655, 1.71680602],
        ])

        cls.ls_params = np.array([0.43393123, -0.08402678, 0.73293058])
        cls.ls_bse = np.array([0.0377741, 0.03567847, 0.02744488])
예제 #7
0
def test_compare_arma():
    #this is a preliminary test to compare arma_kf, arma_cond_ls and arma_cond_mle
    #the results returned by the fit methods are incomplete
    #for now without random.seed

    #np.random.seed(9876565)
    x = fa.ArmaFft([1, -0.5], [1., 0.4], 40).generate_sample(size=200,
                                                             burnin=1000)

    # this used kalman filter through descriptive
    #    d = ARMA(x)
    #    d.fit((1,1), trend='nc')
    #    dres = d.res

    modkf = ARMA(x)
    ##rkf = mkf.fit((1,1))
    ##rkf.params
    reskf = modkf.fit((1, 1), trend='nc', disp=-1)
    dres = reskf

    modc = Arma(x)
    resls = modc.fit(order=(1, 1))
    rescm = modc.fit_mle(order=(1, 1), start_params=[0.4, 0.4, 1.], disp=0)

    #decimal 1 corresponds to threshold of 5% difference
    #still different sign  corrcted
    #assert_almost_equal(np.abs(resls[0] / d.params), np.ones(d.params.shape), decimal=1)
    assert_almost_equal(resls[0] / dres.params,
                        np.ones(dres.params.shape),
                        decimal=1)
    #rescm also contains variance estimate as last element of params

    #assert_almost_equal(np.abs(rescm.params[:-1] / d.params), np.ones(d.params.shape), decimal=1)
    assert_almost_equal(rescm.params[:-1] / dres.params,
                        np.ones(dres.params.shape),
                        decimal=1)
예제 #8
0
import statsmodels.api as sm
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))
예제 #9
0
y22 = arma_generate_sample(ar, ma, nobs + 1000, 0.5)[-nobs:]
y22 -= y22.mean()
start_params = [0.1, 0.1, 0.1, 0.1]
start_params_lhs = [-0.1, -0.1, 0.1, 0.1]

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
예제 #10
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
##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],
예제 #11
0
from __future__ import print_function
from time import time
from statsmodels.tsa.arma_mle import Arma
from statsmodels.tsa.api import ARMA
import numpy as np

print("Battle of the dueling ARMAs")

y_arma22 = np.loadtxt(r'C:\Josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\statsmodels\tsa\y_arma22.txt')

arma1 = Arma(y_arma22)
arma2 = ARMA(y_arma22)

print("The actual results from gretl exact mle are")
params_mle = np.array([.826990, -.333986, .0362419, -.792825])
sigma_mle = 1.094011
llf_mle = -1510.233
print("params: ", params_mle)
print("sigma: ", sigma_mle)
print("llf: ", llf_mle)
print("The actual results from gretl css are")
params_css = np.array([.824810, -.337077, .0407222, -.789792])
sigma_css = 1.095688
llf_css = -1507.301

results = []
results += ["gretl exact mle", params_mle, sigma_mle, llf_mle]
results += ["gretl css", params_css, sigma_css, llf_css]

t0 = time()
print("Exact MLE - Kalman filter version using l_bfgs_b")
예제 #12
0
import numpy as np
from numpy.testing import assert_almost_equal
import matplotlib.pyplot as plt
import statsmodels.sandbox.tsa.fftarma as fa
from statsmodels.tsa.descriptivestats import TsaDescriptive
from statsmodels.tsa.arma_mle import Arma

x = fa.ArmaFft([1, -0.5], [1., 0.4], 40).generate_sample(size=200, burnin=1000)
d = TsaDescriptive(x)
d.plot4()

#d.fit(order=(1,1))
d.fit((1,1), trend='nc')
print(d.res.params)

modc = Arma(x)
resls = modc.fit(order=(1,1))
print(resls[0])
rescm = modc.fit_mle(order=(1,1), start_params=[-0.4,0.4, 1.])
print(rescm.params)

#decimal 1 corresponds to threshold of 5% difference
assert_almost_equal(resls[0] / d.res.params, 1, decimal=1)
assert_almost_equal(rescm.params[:-1] / d.res.params, 1, decimal=1)
#copied to tsa.tests

plt.figure()
plt.plot(x, 'b-o')
plt.plot(modc.predicted(), 'r-')
plt.figure()
plt.plot(modc.error_estimate)
예제 #13
0
order = (2, 1)
res = mod.fit(order=order)
res2 = mod.fit_mle(order=order, start_params=np.r_[res[0], 5, 1], method='nm')

print(res[0])
proc = ArmaProcess.from_coeffs(res[0][:order[0]], res[0][:order[1]])

print(ar, ma)
proc.nobs = nobs
# TODO: bug nobs is None, not needed ?, used in ArmaProcess.__repr__
print(proc.ar, proc.ma)

print(proc.ar_roots(), proc.ma_roots())

from statsmodels.tsa.arma_mle import Arma
modn = Arma(x)
resn = modn.fit_mle(order=order)

moda = ARMA(x, order=order)
resa = moda.fit( trend='nc')

print('\nparameter estimates')
print('ls  ', res[0])
print('norm', resn.params)
print('t   ', res2.params)
print('A   ', resa.params)

print('\nstandard deviation of parameter estimates')
#print 'ls  ', res[0]  #TODO: not available yet
print('norm', resn.bse)
print('t   ', res2.bse)
예제 #14
0
from time import time
from statsmodels.tsa.arma_mle import Arma
from statsmodels.tsa.api import ARMA
import numpy as np

print("Battle of the dueling ARMAs")

y_arma22 = np.loadtxt(
    r'C:\Josef\eclipsegworkspace\statsmodels-josef-experimental-gsoc\scikits\statsmodels\tsa\y_arma22.txt'
)

arma1 = Arma(y_arma22)
arma2 = ARMA(y_arma22)

print("The actual results from gretl exact mle are")
params_mle = np.array([.826990, -.333986, .0362419, -.792825])
sigma_mle = 1.094011
llf_mle = -1510.233
print("params: ", params_mle)
print("sigma: ", sigma_mle)
print("llf: ", llf_mle)
print("The actual results from gretl css are")
params_css = np.array([.824810, -.337077, .0407222, -.789792])
sigma_css = 1.095688
llf_css = -1507.301

results = []
results += ["gretl exact mle", params_mle, sigma_mle, llf_mle]
results += ["gretl css", params_css, sigma_css, llf_css]

t0 = time()
예제 #15
0
import statsmodels.api as sm
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))