Esempio n. 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)
Esempio n. 2
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)
Esempio n. 3
0
import numpy as np
from gwstatsmodels.tsa.arima_process import arma_generate_sample
from gwstatsmodels.iolib import savetxt

np.random.seed(12345)

# no constant
y_arma11 = arma_generate_sample([1., -.75], [1., .35], nsample=250)
y_arma14 = arma_generate_sample([1., -.75], [1., .35, -.75, .1, .35],
                                nsample=250)
y_arma41 = arma_generate_sample([1., -.75, .25, .25, -.75], [1., .35],
                                nsample=250)
y_arma22 = arma_generate_sample([1., -.75, .45], [1., .35, -.9], nsample=250)

y_arma50 = arma_generate_sample([1., -.75, .35, -.3, -.2, .1], [1.],
                                nsample=250)

y_arma02 = arma_generate_sample([1.], [1., .35, -.75], nsample=250)

# constant
constant = 4.5
y_arma11c = arma_generate_sample([1., -.75], [1., .35], nsample=250) + constant
y_arma14c = arma_generate_sample([1., -.75], [1., .35, -.75, .1, .35],
                                 nsample=250) + constant
y_arma41c = arma_generate_sample([1., -.75, .25, .25, -.75], [1., .35],
                                 nsample=250) + constant
y_arma22c = arma_generate_sample([1., -.75, .45],[1., .35, -.9], nsample=250) + \
                    constant

y_arma50c = arma_generate_sample([1., -.75, .35, -.3, -.2, .1], [1.],
                                 nsample=250) + constant
Esempio n. 4
0
    import scikits.talkbox as stb
    import scikits.talkbox.spectral.basic as stbs
except:
    hastalkbox = False

ar = [1., -0.7]#[1,0,0,0,0,0,0,-0.7]
ma = [1., 0.3]

ar = np.convolve([1.]+[0]*50 +[-0.6], ar)
ar = np.convolve([1., -0.5]+[0]*49 +[-0.3], ar)

n_startup = 1000
nobs = 1000
# throwing away samples at beginning makes sample more "stationary"

xo = arma_generate_sample(ar,ma,n_startup+nobs)
x = xo[n_startup:]

#moved to tsa.arima_process
#def arma_periodogram(ar, ma, **kwds):
#    '''periodogram for ARMA process given by lag-polynomials ar and ma
#
#    Parameters
#    ----------
#    ar : array_like
#        autoregressive lag-polynomial with leading 1 and lhs sign
#    ma : array_like
#        moving average lag-polynomial with leading 1
#    kwds : options
#        options for scipy.signal.freqz
#        default: worN=None, whole=0
Esempio n. 5
0
'''

import numpy as np
from gwstatsmodels.sandbox import tsa
from gwstatsmodels.tsa.arima_process import arma_generate_sample
from maketests_mlabwrap import HoldIt

if __name__ == '__main__':
    filen = 'savedrvs_tmp.py'
    np.set_printoptions(precision=14, linewidth=100)


    # check arma to return same as random.normal
    np.random.seed(10000)
    xo = arma_generate_sample([1], [1], nsample=100)
    xo2 = np.round(xo*1000).astype(int)
    np.random.seed(10000)
    rvs = np.random.normal(size=100)
    rvs2 = np.round(xo*1000).astype(int)
    assert (xo2==rvs2).all()

    nsample = 1000
    data =  HoldIt('rvsdata')

    np.random.seed(10000)
    xo = arma_generate_sample([1, -0.8, 0.5], [1], nsample=nsample)
    data.xar2 = np.round(xo*1000).astype(int)
    np.random.seed(10000)
    xo = np.random.normal(size=nsample)
    data.xnormal = np.round(xo*1000).astype(int)
Esempio n. 6
0
import matplotlib.mlab as mlab

from gwstatsmodels.tsa.arima_process import arma_generate_sample, arma_impulse_response
from gwstatsmodels.tsa.arima_process import arma_acovf, arma_acf, ARIMA
#from movstat import acf, acovf
#from gwstatsmodels.sandbox.tsa import acf, acovf, pacf
from gwstatsmodels.tsa.stattools import acf, acovf, pacf

ar = [1., -0.6]
#ar = [1., 0.]
ma = [1., 0.4]
#ma = [1., 0.4, 0.6]
#ma = [1., 0.]
mod = ''#'ma2'
x = arma_generate_sample(ar, ma, 5000)
x_acf = acf(x)[:10]
x_ir = arma_impulse_response(ar, ma)

#print x_acf[:10]
#print x_ir[:10]
#irc2 = np.correlate(x_ir,x_ir,'full')[len(x_ir)-1:]
#print irc2[:10]
#print irc2[:10]/irc2[0]
#print irc2[:10-1] / irc2[1:10]
#print x_acf[:10-1] / x_acf[1:10]

# detrend helper from matplotlib.mlab
def detrend(x, key=None):
    if key is None or key=='constant':
        return detrend_mean(x)
Esempio n. 7
0
    import scikits.talkbox as stb
    import scikits.talkbox.spectral.basic as stbs
except:
    hastalkbox = False

ar = [1., -0.7]  #[1,0,0,0,0,0,0,-0.7]
ma = [1., 0.3]

ar = np.convolve([1.] + [0] * 50 + [-0.6], ar)
ar = np.convolve([1., -0.5] + [0] * 49 + [-0.3], ar)

n_startup = 1000
nobs = 1000
# throwing away samples at beginning makes sample more "stationary"

xo = arma_generate_sample(ar, ma, n_startup + nobs)
x = xo[n_startup:]

#moved to tsa.arima_process
#def arma_periodogram(ar, ma, **kwds):
#    '''periodogram for ARMA process given by lag-polynomials ar and ma
#
#    Parameters
#    ----------
#    ar : array_like
#        autoregressive lag-polynomial with leading 1 and lhs sign
#    ma : array_like
#        moving average lag-polynomial with leading 1
#    kwds : options
#        options for scipy.signal.freqz
#        default: worN=None, whole=0
Esempio n. 8
0
    _wrap_attrs = wrap.union_dicts(tsbase.TimeSeriesResultsWrapper._wrap_attrs,
                                   _attrs)
    _methods = {}
    _wrap_methods = wrap.union_dicts(
        tsbase.TimeSeriesResultsWrapper._wrap_methods, _methods)


wrap.populate_wrapper(ARMAResultsWrapper, ARMAResults)

if __name__ == "__main__":
    import numpy as np
    import gwstatsmodels.api as sm

    # simulate arma process
    from gwstatsmodels.tsa.arima_process import arma_generate_sample
    y = arma_generate_sample([1., -.75], [1., .25], nsample=1000)
    arma = ARMA(y)
    res = arma.fit(trend='nc', order=(1, 1))

    np.random.seed(12345)
    y_arma22 = arma_generate_sample([1., -.85, .35], [1, .25, -.9],
                                    nsample=1000)
    arma22 = ARMA(y_arma22)
    res22 = arma22.fit(trend='nc', order=(2, 2))

    # test CSS
    arma22_css = ARMA(y_arma22)
    res22css = arma22_css.fit(trend='nc', order=(2, 2), method='css')

    data = sm.datasets.sunspots.load()
    ar = ARMA(data.endog)
Esempio n. 9
0
        if not wasinvertible:
            start_params = res.params.copy()
            start_params[self.nar: self.nar+self.nma] = mainv[1:]
            #need to add args kwds
            res = self.fit(start_params=start_params)
        return res



if __name__ == '__main__':
    nobs = 50
    ar = [1.0, -0.8, 0.1]
    ma = [1.0,  0.1,  0.2]
    #ma = [1]
    np.random.seed(9875789)
    y = arma_generate_sample(ar,ma,nobs,2)
    y -= y.mean() #I haven't checked treatment of mean yet, so remove
    mod = MLEGLS(y)
    mod.nar, mod.nma = 2, 2   #needs to be added, no init method
    mod.nobs = len(y)
    res = mod.fit(start_params=[0.1, -0.8, 0.2, 0.1, 1.])
    print 'DGP', ar, ma
    print res.params
    from gwstatsmodels.regression import yule_walker
    print yule_walker(y, 2)
    #resi = mod.fit_invertible(start_params=[0.1,0,0.2,0, 0.5])
    #print resi.params

    arpoly, mapoly = getpoly(mod, res.params[:-1])

    data = sm.datasets.sunspots.load()
Esempio n. 10
0
    _attrs = {}
    _wrap_attrs = wrap.union_dicts(tsbase.TimeSeriesResultsWrapper._wrap_attrs,
                                    _attrs)
    _methods = {}
    _wrap_methods = wrap.union_dicts(
                        tsbase.TimeSeriesResultsWrapper._wrap_methods,
                        _methods)
wrap.populate_wrapper(ARMAResultsWrapper, ARMAResults)

if __name__ == "__main__":
    import numpy as np
    import gwstatsmodels.api as sm

    # simulate arma process
    from gwstatsmodels.tsa.arima_process import arma_generate_sample
    y = arma_generate_sample([1., -.75],[1.,.25], nsample=1000)
    arma = ARMA(y)
    res = arma.fit(trend='nc', order=(1,1))

    np.random.seed(12345)
    y_arma22 = arma_generate_sample([1.,-.85,.35],[1,.25,-.9], nsample=1000)
    arma22 = ARMA(y_arma22)
    res22 = arma22.fit(trend = 'nc', order=(2,2))

    # test CSS
    arma22_css = ARMA(y_arma22)
    res22css = arma22_css.fit(trend='nc', order=(2,2), method='css')


    data = sm.datasets.sunspots.load()
    ar = ARMA(data.endog)
Esempio n. 11
0
import numpy as np
from gwstatsmodels.tsa.arima_process import arma_generate_sample
from gwstatsmodels.iolib import savetxt

np.random.seed(12345)

# no constant
y_arma11 = arma_generate_sample([1., -.75],[1., .35], nsample=250)
y_arma14 = arma_generate_sample([1., -.75],[1., .35, -.75, .1, .35],
                nsample=250)
y_arma41 = arma_generate_sample([1., -.75, .25, .25, -.75], [1., .35],
                nsample=250)
y_arma22 = arma_generate_sample([1., -.75, .45],[1., .35, -.9], nsample=250)

y_arma50 = arma_generate_sample([1., -.75, .35, -.3, -.2, .1], [1.],
                nsample=250)

y_arma02 = arma_generate_sample([1.], [1., .35, -.75], nsample=250)


# constant
constant = 4.5
y_arma11c = arma_generate_sample([1., -.75],[1., .35], nsample=250) + constant
y_arma14c = arma_generate_sample([1., -.75],[1., .35, -.75, .1, .35],
                nsample=250) + constant
y_arma41c = arma_generate_sample([1., -.75, .25, .25, -.75], [1., .35],
                nsample=250) + constant
y_arma22c = arma_generate_sample([1., -.75, .45],[1., .35, -.9], nsample=250) + \
                    constant

y_arma50c = arma_generate_sample([1., -.75, .35, -.3, -.2, .1], [1.],