setar models

Upload: anthony-alarcon-moreno

Post on 06-Jul-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Setar Models

    1/3

    This continues with the Sunspots section of the ARMA Notebook example.

    Note: here we consider the raw Sunspot series to match the ARMA example, although many

    sources in the literature apply a transformation to the series before modeling.

    In [ ]:import numpy as np

    import  pandas as  pd 

    from  scipy import stats

    import  matplotlib.pyplot as  plt

    import statsmodels.api as sm 

    import statsmodels.tsa.setar_model as setar_model

    In [ ]:

    print sm.datasets.sunspots.NOTE

    In [ ]:

    dta = sm.datasets.sunspots.load_pandas().data

    In [ ]:

    dta.index = pd.Index(sm.tsa.datetools.dates_from_range('!""'# '$""%'))

    del dta[&E&]

    In [ ]:

    dta.plot(figsi*e=($#%))+

    First we'll fit an AR(3) process to the data as in the ARMA Notebook Example.

    In [ ]:

    arma_mod," = sm.tsa.-(dta# (,#")).fit()

    print arma_mod,".params

    print arma_mod,".ai# arma_mod,"./i# arma_mod,".01i

    To try and capture nonlinearities, we'll fit a SETAR(2) model to the data to allow for two

    regimes, and we let each regime be an AR(3) process. Here we're not specifying the delay or

    threshold values, so they'll be optimally selected from the model.

    Note: In the summary, the \gamma parameter(s) are the threshold value(s).

    In [ ]:

    setar_mod$, = setar_model.2ET(dta# $# ,).fit()

    print setar_mod$,.summar3()

    Note that the The AIC and BIC criteria prefer the SETAR model to the AR model.We can also directly test for the appropriate model, noting that an AR(3) is the same as a

    SETAR(1;1,3), so the specifications are nested.

    Note: this is a bootstrapped test, so it is rather slow until improvements can be made.

    In [ ]:

    setar_mod$, = setar_model.2ET(dta# $# ,).fit()

    http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/tsa_arma.htmlhttp://statsmodels.sourceforge.net/devel/examples/notebooks/generated/tsa_arma.htmlhttp://statsmodels.sourceforge.net/devel/examples/notebooks/generated/tsa_arma.html

  • 8/17/2019 Setar Models

    2/3

    f_stat# p4alue# /s_f_stats = setar_mod$,.order_test() # by default tests

    against SETAR(1)

    print p4alue

    The null hypothesis is a SETAR(1), so it looks like we can safely reject it in favor of the

    SETAR(2) alternative.

    One thing to note, though, is that the default assumptions oforder_test() is that there is

    homoskedasticity, which may be unreasonable here. So we can force the test to allow for

    heteroskedasticity of general form (in this case it doesn't look like it matters, however).

    In [ ]:

    f_stat_0# p4alue_0# /s_f_stats_0 = 

    setar_mod$,.order_test(0eteros5edastiit3='g')

    print p4alue

    In [ ]:

    setar_mod$,.resid.plot(figsi*e=("#6))+

    We have two new types of parameters estimated here compared to an ARMA model. The delayand the threshold(s). The delay parameter selects which lag of the process to use as the

    threshold variable, and the thresholds indicate which values of the threshold variable separate

    the datapoints into the (here two) regimes.

    The confidence interval for the threshold parameter is generated (as in Hansen (1997)) by

    inverting the likelihood ratio statistic created from considering the selected threshold value

    against ecah alternative threshold value, and comparing against critical values for various

    confidence interval levels. We can see that graphically by plotting the likelihood ratio sequence

    against each alternate threshold.

    Alternate thresholds that correspond to likelihood ratio statistics less than the critical value areincluded in a confidence set, and the lower and upper bounds of the confidence interval are the

    smallest and largest threshold, respectively, in the confidence set.

    In [ ]:

    setar_mod$,.plot_t0res0old_i("# fig7idt0="# fig0eig0t=6)+

    As in the ARMA Notebook Example, we can take a look at in-sample dynamic prediction and

    out-of-sample forecasting.

    In [ ]:

    predit_arma_mod," = arma_mod,".predit('88"'# '$"$'# d3nami=True)

    predit_setar_mod$, = setar_mod$,.predit('88"'# '$"$'# d3nami=True)

    In [ ]:

    ax = dta.ix['86"':].plot(figsi*e=($#%))

    ax = predit_arma_mod,".plot(ax=ax# st3le='r99'# line7idt0=$#

    la/el='(,) 3nami ;redition')+

    ax = predit_setar_mod$,.plot(ax=ax# st3le='599'# line7idt0=$#

    la/el='2ET($+,#,) 3nami ;redition')+

    ax.legend()+

  • 8/17/2019 Setar Models

    3/3

    ax.axis((9$"."# ,%."# 9N?TI@IT# predit_arma_mod,")

    print '2ET($+,#,): '# rmsfe(dta.2>N?TI@IT# predit_setar_mod$,)

    If we extend the forecast window, however, it is clear that the SETAR model is the only one that

    even begins to fit the shape of the data, because the data is cyclic.

    In [ ]:

    predit_arma_mod,"_long = arma_mod,".predit('8A"'# '$"$'#

    d3nami=True)

    predit_setar_mod$,_long = setar_mod$,.predit('8A"'# '$"$'#

    d3nami=True)

    ax = dta.ix['86"':].plot(figsi*e=($#%))

    ax = predit_arma_mod,"_long.plot(ax=ax# st3le='r99'# line7idt0=$#

    la/el='(,) 3nami ;redition')+

    ax = predit_setar_mod$,_long.plot(ax=ax# st3le='599'# line7idt0=$#

    la/el='2ET($+,#,) 3nami ;redition')+

    ax.legend()+

    ax.axis((9$"."# ,%."# 9N?TI@IT# predit_arma_mod,"_long)

    print '2ET($+,#,): '# rmsfe(dta.2>N?TI@IT# predit_setar_mod$,_long)