
p7]c        	   @@  sg  d  d l  m Z d  d l m Z m Z m Z d  d l m Z d  d l Z d  d l	 m
 Z
 d  d l m Z m Z d  d l m Z d  d l m Z m Z m Z m Z d  d	 l m Z d  d
 l m Z d  d l j j j Z d  d l j j Z d  d l  m! Z! m" Z" d  d l# m$ Z$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ d  d l, m- Z- d  d l. m/ Z/ d  d l0 m1 Z1 d  d l2 m3 Z3 m4 Z4 d  d l5 m6 Z6 d Z7 d Z8 d Z9 d Z: d Z; d Z< d Z= d Z> d Z? e> i d d 6d d 6d d  6e? d! 6e< d" 6Z@ e> i d d 6d d 6d d  6e? d! 6e= d" 6ZA d# ZB e> i d$ d 6d d 6eB d  6e? d! 6e< d" 6ZC e> i d$ d 6d d 6eB d  6e? d! 6e= d" 6ZD d% ZE d& ZF d' d( jG e> jH d(  d)  i d d 6eF d  6d* d! 6d( eE d( e= d" 6ZI d' d( jG e> jH d(  d)  i d d 6eF d  6d* d! 6d( eE d( d( jG e= jH d(  d+   d, d( jG e= jH d(  d+  d" 6ZJ d-   ZK d.   ZL eM d/  ZN eM d  d0 d1  ZO d2   ZP eQ d3  ZR d4   ZS d5   ZT d6   ZU d7   ZV d e jW f d8     YZX d$ eX f d9     YZY d: e jZ f d;     YZ[ d< e j\ f d=     YZ] e j^ e] e[  d> e[ f d?     YZ_ d@ e] f dA     YZ` e j^ e` e_  ea dB k rcd  d lb jc Zd d  dC l0 me Ze ee dD dE g dD dF g dG dH Zf eX ef  Zg eg jh dI dJ dK dL dL f  Zi e jj jk dM  ee dD dN dO g dL dF dP g dG dH Zl eX el  Zm em jh dI dJ dK d) d) f  Zn eX el  Zo eo jh dI dJ dK d) d) f dQ dR  Zp ed jq jr js dS eQ  Zt eX et ju  Zv ev jh dI dJ dK dT d  f  Zw ee dL dE dU dF g dV g dG dH Zx eX ex  Zy ey jh dK d+ dL f dQ dR dI dJ dW ez  Z{ ee dD dE g dL dF dX dY g dG dH Z| eX e|  Z} e} jh dK dL d+ f dQ dR dI dJ  Z~ ee dD dE dO dF dZ g dL dU g dG dH Z eX e  Z e jh dK d[ dL f dI dJ dQ dR  Z ee dL d\ g dD dE dO dF dZ g dG dH Z eX e  Z e jh dK d[ dL f dI dJ dQ dR  Z d  d] l m Z e d^  Z e d_ Z eY e dL dL dL f  jh   Z n  d S(`   i    (   t   absolute_import(   t   string_typest   ranget   long(   t   datetimeN(   t   optimize(   t   tt   norm(   t   lfilter(   t   dott   logt   zerost   pi(   t   inv(   t   cache_readonly(   t   yule_walkert   OLS(   t   lagmatt	   add_trendt   _ar_transparamst   _ar_invtransparamst   _ma_transparamst   _ma_invtransparamst   unintegratet   unintegrate_levels(   t   util(   t   AR(   t   arma2ma(   t   approx_hess_cst   approx_fprime_cs(   t   KalmanFilters  
    Notes
    -----
    If exogenous variables are given, then the model that is fit is

    .. math::

       \phi(L)(y_t - X_t\beta) = \theta(L)\epsilon_t

    where :math:`\phi` and :math:`\theta` are polynomials in the lag
    operator, :math:`L`. This is the regression model with ARMA errors,
    or ARMAX model. This specification is used, whether or not the model
    is fit using conditional sum of square or maximum-likelihood, using
    the `method` argument in
    :meth:`statsmodels.tsa.arima_model.%(Model)s.fit`. Therefore, for
    now, `css` and `mle` refer to estimation methods only. This may
    change for the case of the `css` model in future versions.
sp  endog : array-like
        The endogenous variable.
    order : iterable
        The (p,q) order of the model for the number of AR parameters,
        differences, and MA parameters to use.
    exog : array-like, optional
        An optional array of exogenous variables. This should *not* include a
        constant or trend. You can specify this in the `fit` method.s-   Autoregressive Moving Average ARMA(p,q) Models;   Autoregressive Integrated Moving Average ARIMA(p,d,q) Modelsr  endog : array-like
        The endogenous variable.
    order : iterable
        The (p,d,q) order of the model for the number of AR parameters,
        differences, and MA parameters to use.
    exog : array-like, optional
        An optional array of exogenous variables. This should *not* include a
        constant or trend. You can specify this in the `fit` method.sM   
        Notes
        -----
        Use the results predict method instead.
s_  
        Notes
        -----
        It is recommended to use dates with the time-series models, as the
        below will probably make clear. However, if ARIMA is used without
        dates and/or `start` and `end` are given as indices, then these
        indices are in terms of the *original*, undifferenced series. Ie.,
        given some undifferenced observations::

         1970Q1, 1
         1970Q2, 1.5
         1970Q3, 1.25
         1970Q4, 2.25
         1971Q1, 1.2
         1971Q2, 4.1

        1970Q1 is observation 0 in the original series. However, if we fit an
        ARIMA(p,1,q) model then we lose this first observation through
        differencing. Therefore, the first observation we can forecast (if
        using exact MLE) is index 1. In the differenced series this is index
        0, but we refer to it as 1 from the original series.
s  
        %(Model)s model in-sample and out-of-sample prediction

        Parameters
        ----------
        %(params)s
        start : int, str, or datetime
            Zero-indexed observation number at which to start forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type.
        end : int, str, or datetime
            Zero-indexed observation number at which to end forecasting, ie.,
            the first forecast is start. Can also be a date string to
            parse or a datetime type. However, if the dates index does not
            have a fixed frequency, end must be an integer index if you
            want out of sample prediction.
        exog : array-like, optional
            If the model is an ARMAX and out-of-sample forecasting is
            requested, exog must be given. Note that you'll need to pass
            `k_ar` additional lags for any exogenous variables. E.g., if you
            fit an ARMAX(2, q) model and want to predict 5 steps, you need 7
            observations to do this.
        dynamic : bool, optional
            The `dynamic` keyword affects in-sample prediction. If dynamic
            is False, then the in-sample lagged values are used for
            prediction. If `dynamic` is True, then in-sample forecasts are
            used in place of lagged dependent variables. The first forecasted
            value is `start`.
        %(extra_params)s

        Returns
        -------
        %(returns)s
        %(extra_section)s
s3   predict : array
            The predicted values.

t   ARMAt   ModelsC   params : array-like
            The fitted parameters of the model.t   paramst    t   extra_paramst   returnst   extra_sections   typ : str {'linear', 'levels'}

            - 'linear' : Linear prediction in terms of the differenced
              endogenous variables.
            - 'levels' : Predict the levels of the original endogenous
              variables.
t   ARIMAs          Examples
        --------
        >>> import statsmodels.api as sm
        >>> import matplotlib.pyplot as plt
        >>> import pandas as pd
        >>>
        >>> dta = sm.datasets.sunspots.load_pandas().data[['SUNACTIVITY']]
        >>> dta.index = pd.date_range(start='1700', end='2009', freq='A')
        >>> res = sm.tsa.ARMA(dta, (3, 0)).fit()
        >>> fig, ax = plt.subplots()
        >>> ax = dta.loc['1950':].plot(ax=ax)
        >>> fig = res.plot_predict('1990', '2012', dynamic=True, ax=ax,
        ...                        plot_insample=False)
        >>> plt.show()

        .. plot:: plots/arma_predict_plot.py
s  alpha : float, optional
            The confidence intervals for the forecasts are (1 - alpha)%
        plot_insample : bool, optional
            Whether to plot the in-sample series. Default is True.
        ax : matplotlib.Axes, optional
            Existing axes to plot with.s.   
        Plot forecasts
                      s   
i   s?   fig : matplotlib.Figure
            The plotted Figure instancei   sO   
        This is hard-coded to only allow plotting of the forecasts in levels.
c         C@  s*   x# t  |  D] } t j |   }  q W|  S(   N(   R   t   npt   cumsum(   t   xt   nt   _(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   cumsum_n   s    c         C@  sT   |  d k  r t  d |    n1 | s1 d | k rP |  | k  rP t  d |    n  d  S(   Ni    sC   The start index %d of the original series has been differenced awayt   mlesE   Start must be >= k_ar for conditional MLE or dynamic forecast. Got %d(   t
   ValueError(   t   startt   k_art   k_difft   methodt   dynamic(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _check_arima_start   s    c         C@  s  | r t  j |  } | r' d | k s@ | | k rX | d k rX | | | | !| | *q | r~ | | | | | | !| | *q | | | | *n d } |  } | d k r| d k rt  j |  d k r | d k r | d d  d f } nO t  j |  d k r=t |  | k r$t d   n  | d d d  f } n  t t  j | |  | d d d d	 } | d |	 j   } | t  j	 d |	 d d d
  f | j d  d d  d f } qs| d |	 j   } t  j
 | g |  } n | d k rdt  j | |  } t | | d d d d	 } t  j	 d |	 d d d
  f | j d  d d  d f } n t  j |  } t  j | | d  }  | r| r| | | | !|  | *n | r| | |  | *n  |  | | f S(   sZ   
    Returns endog, resid, mu of appropriate length for out of sample
    prediction.
    R-   i    i   Ns%   1d exog given and len(exog) != k_exogt   originalt   int   trimt   bothi(   R'   R   t   Nonet   ndimt   lenR.   R   R	   t   sumt   r_t   array(   t   endogt   pt   qt   k_trendt   k_exogR/   t   errorst
   trendparamt   exparamst   arparamst   maparamst   stepsR2   t   exogt   residt   yt   Xt   mu(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _get_predict_out_of_sample   sB    + !'EAR-   c         C@  s  t  |  | | f | | d t \ } } } } t | | | | | |	 | | | | | | |
 |  \ } } } t j |  } | d k r | r | d t j | | |   t j | | |   S| d t j | | |   Sn  | r d } n d } x t t | | d   D]f } | | t j | | | | | ! t j | | |  | | | | ! } | | | <| | | | <qWx\ t | d | d  D]C } | | t j | | | | | ! } | | | <| | | | <qW| | d t j | | | d  | | d <| S(   Nt   reversei   i    i(   t   _unpack_paramst   TrueRO   R'   R   R	   R   t   min(   R!   RI   RD   R@   RA   RB   RC   R?   RJ   R/   R2   RE   RF   RG   RH   RK   RN   t   forecastt   it   fcast(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _arma_predict_out_of_sample1  s6    		$	 "&
%
.c   	      C@  sj   d | k r | | } n | | | } |  } d | k rF | | 8} n  t  t |  | d  } | | | !S(   s.   
    Pre- and in-sample fitting for ARMA.
    R-   i   (   RS   R;   (	   R/   t   endR?   RK   R0   R2   t   fittedvaluest   fv_startt   fv_end(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _arma_predict_in_sampleY  s    c         C@  s   | \ } } | | } |  | | } |  | | | !}	 |  |  }
 |  | | !} | r| |
 | |	 d  d  d  | d  d  d  f S|
 | |	 | f S(   Ni(    (   R!   t   orderRB   RC   RP   R@   RA   t   kRH   RG   t   trendRF   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRQ   i  s    

*c         C@  s2   |  \ } } } t  | | d  } | | |  | f S(   Ni   (   t   max(   R]   R0   t   k_maR^   t   k_lags(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _unpack_orderu  s    c   
      C@  s   | \ } } | p g  } t  j |  j g | d  } g  | D] } d j d | f  ^ q: } t  j |  j g | d  } g  | D] } d j d | f  ^ q} } t  j d d |  }	 |	 | | | } | S(   Ni    R"   s   ar.s   ma.(   R   t   make_lag_namest   ynamest   join(
   t   dataRB   R]   t
   exog_namesR0   Ra   t   ar_lag_namesRU   t   ma_lag_namest
   trend_name(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _make_arma_names{  s    ((c         C@  s   d } | d  k r< | d k r< t j t |   d f  } nu | d  k	 ru | d k ru t | d d d t d d } n< | d  k	 r | d k r | j   d k r d  } n  d } n  | d k r d } n  | | f S(	   Ni   t   cR_   t   prependt   has_constantt   raiset   nci    (   R9   R'   t   onesR;   R   RR   t   var(   R?   RJ   R_   RB   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _make_arma_exog  s    !			c         C@  s   |  | k r t  d   n  d  S(   Ns+   Insufficient degrees of freedom to estimate(   R.   (   t   nobst   n_params(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _check_estimable  s    c           B@  s   e  Z e j i e d  6e d 6d d 6e i d d 6d 6Z d d d d d  Z	 d d	  Z
 d d
  Z d   Z d   Z d   Z d   Z d d  Z d   Z d d d e d  Z e e _ e d  Z e d  Z e d  Z d d d e d d d d d d d 
 Z e d d d   Z RS(   t   modelR!   R"   R#   R   R    t   extra_sectionst   nonec         C@  sL  t  t |   j | | | | d | t | d  r: | n t j |  } | j d k rk | j d d k sz | j d k r t d   n  |  j	 j
 } t t |  j  t |   | d |  _ } | d |  _ }	 t | |	 d  |  _ | d  k	 r-| j d k r| d  d   d  f } n  | j d }
 n d }
 |
 |  _ |  j |  _ d  S(   Nt   missingR:   i   i   s&   endog must be 1-d or 2-d with 1 columni    (   t   superR   t   __init__t   hasattrR'   t   asarrayR:   t   shapeR.   Rg   RJ   Rw   R;   R?   R<   R0   Ra   R`   Rb   R9   RC   Rh   t   _orig_exog_names(   t   selfR?   R]   RJ   t   datest   freqR{   t   _endogR0   Ra   RC   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR}     s     %$1	c      	   C@  s  | \ } } } t  | | |  } t j |  j t j  } |  j } | d k r t | |  j   j }	 |	 | | *| t j	 | |	  j
   8} n  | d k r| d k rwt |  }
 | d k r#t t d |
 d d d   } | |
 k r |
 d } n  t |  j d d d d	 d
 |  } n7 | |
 k r<|
 d } n  t |  j d d	 d
 |  } | j } | j } | | t |  k rt d   n  | | t j	 t | | d d |  } | | | k  r| | | } d } n d } | | | } t | | d  | } t | | d  | } t j | | f  } t | t | | |  |  j   j } | | | | | | +qt | d | d | | | | | | +n  | d k r| d k rt | d | d } | | | | | +n  | r;t j t j t j t j d | | | | !f   d k   r;t d   nR | rt j t j t j t j d | | | f   d k   rt d   n  | S(   s  
        Get starting parameters for fit.

        Parameters
        ----------
        order : iterable
            (p,q,k) - AR lags, MA lags, and number of exogenous variables
            including the constant.
        start_ar_lags : int, optional
            If start_ar_lags is not None, rather than fitting an AR process
            according to best BIC, fits an AR process with a lag length equal
            to start_ar_lags.

        Returns
        -------
        start_params : array
            A first guess at the starting parameters.

        Notes
        -----
        If necessary, fits an AR process with the laglength start_ar_lags, or
        selected according to best BIC if start_ar_lags is None.  Obtain the
        residuals.  Then fit an ARMA(p,q) model via OLS using these residuals
        for a first approximation.  Uses a separate OLS regression to find the
        coefficients of exogenous variables.

        References
        ----------
        Hannan, E.J. and Rissanen, J.  1982.  "Recursive estimation of mixed
            autoregressive-moving average order."  `Biometrika`.  69.1.

        Durbin, J. 1960. "The Fitting of Time-Series Models."
        `Review of the International Statistical Institute`. Vol. 28, No. 3
        i    i   g      Y@i   g      @t   ict   bicR_   Rq   t   maxlags   Proper starting parameters cannot be found for this order with this number of observations. Use the start_params argument, or set start_ar_lags to an integer less than len(endog) - q.R7   R8   R]   s   The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.s   The computed initial MA coefficients are not invertible
You should induce invertibility, choose a different model order, or you can
pass your own start_params.N(   R   R'   R>   R?   t   float64RJ   R   t   fitR!   R	   t   squeezeR;   R9   t   intt   roundR   R0   R.   R   t   column_stackR`   R   t   allt   abst   rootsR=   (   R   R]   t   start_ar_lagsR@   RA   R^   t   start_paramsR?   RJ   t
   ols_paramsRu   R   t   armodt   arcoefs_tmpt   p_tmpRK   t   endog_startt   resid_startt	   lag_endogt	   lag_residRM   t   coefst   arcoefs(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _fit_start_params_hr  s^    #	
"'		
	),:6c         @  s   | d k r!   j  | |  } n   f d   }   j  | |  }   j r]   j |  } n  d g t |  } t j | | d t d d d d d	 d
 d | d d } | d }   j r   j |  } n  | S(   Ns   css-mlec         @  s     j  |   S(   N(   t   loglike_css(   R!   (   R   (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   <lambda>(  R"   i   t   approx_gradt   mi   t   pgtolgHz>t   factrg     @@t   boundst   iprintii    (   N(   NN(	   R   t   transparamst   _invtransparamsR9   R<   R   t   fmin_l_bfgs_bRR   t   _transparams(   R   R]   R2   R   R   t   funcR   t   mlefit(    (   R   s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _fit_start_params$  s    	
	c         C@  s   t  | |  j d t f S(   s   
        Compute the score function at params.

        Notes
        -----
        This is a numerical approximation.
        t   args(   R   t   logliket   False(   R   R!   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   score7  s    c         C@  s   t  | |  j d t f S(   sx   
        Compute the Hessian at params,

        Notes
        -----
        This is a numerical approximation.
        R   (   R   R   R   (   R   R!   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   hessianA  s    c         C@  s   |  j  |  j } } |  j |  j } t j |  } | d k rO | |  | | *n  | d k r t | | | | !j    | | | | +n  | d k r t | | | j    | | | )n  | S(   s   
        Transforms params to induce stationarity/invertability.

        Reference
        ---------
        Jones(1980)
        i    (	   R0   Ra   RC   RB   R'   t
   zeros_likeR   t   copyR   (   R   R!   R0   Ra   R^   t	   newparams(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   K  s    +%c         C@  s   |  j  |  j } } |  j |  j } | j   } | | | | !} | | | } | d k rt t |  | | | | +n  | d k r t |  | | | | | | +n  | S(   s9   
        Inverse of the Jones reparameterization
        i    (   R0   Ra   RC   RB   R   R   R   (   R   R   R0   Ra   R^   R   R   t   macoefs(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   d  s    "c   
      C@  s   t  |  d d  } t  |  d d  } t  |  d d  } | d  k rt d | k r^ | r^ d } n | } |  j | } n  t t |   j | | |  \ } } } }	 d | k r | | | k  r t d |   n  t | | | | |  | | | |	 f S(   NR2   R-   R0   i    R1   sE   Start must be >= k_ar for conditional MLE or dynamic forecast. Got %s(   t   getattrR9   t   _indexR|   R   t   _get_prediction_indexR.   R4   (
   R   R/   RX   R3   t   indexR2   R0   R1   t   out_of_samplet   prediction_index(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   v  s    	*c         C@  s  t  j |  } |  j |  j } } |  j |  j } t |  d d  } d | k r t j | |   \ } } } } } } }	 }
 } } } } t j	 | | | | | | |
 | | | |  } t
 | t  r| d } qn9|  j j   } |  j |  j } | d k r| t |  j | |   8} n  |  j } |  j } t | | | f |  j |  j d t \ } } } } t  j d | f t  j d | f } } t t | |   } xF t |  D]8 } t | | d  d d d  | | d   | | <qWt | | | d | } | d | } | j   S(	   s,  
        Get the errors of the ARMA process.

        Parameters
        ----------
        params : array-like
            The fitted ARMA parameters
        order : array-like
            3 item iterable, with the number of AR, MA, and exogenous
            parameters, including the trend
        R2   R-   i    RP   i   Nit   zi(   R'   R   R0   Ra   RC   RB   R   R   t   _init_kalman_statet	   geterrorst
   isinstancet   tupleR?   R   R	   RJ   RQ   R   R=   R   R`   R   R<   R   R   (   R   R!   R0   Ra   R^   R2   RL   Ru   Rb   R   t   Z_matR   t   R_matt   T_matt   paramsdtypeRD   t   trendparamsRF   RG   RH   t   bt   aR   RU   t   e(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s8    	-			(6c         C@  sg  t  |  d d  } |  j | | |  \ } } } } | rc | d  k rc |  j d k rc t d   n  |  j }	 |  j |  }
 |  j } | d  k	 r t j	 |  } |  j d k r | j
 d k r | d  d   d  f } q n  | d k rN|  j d k rN|  j d k rN| d k rN| rNt j |  j | d   |  j d   f | f  } qNn  | r|  j d k r|  j | | d   |  j d   f } | d  k	 rt j | | f  } q| } n  | | | d 7} t | | |
 | |  j |  j |  j |	 | | |  St | | |	 |
 | |  } | rct | | |
 | |  j |  j |  j |	 | d | 	} t j | | f } n  | S(   NR2   R-   i    s   You must provide exog for ARMAXi   (   R   R   R9   RC   R.   R?   R   R0   R'   R   R:   t   vstackRJ   RB   RW   Ra   R\   R=   (   R   R!   R/   RX   RJ   R3   R2   R   R+   R?   RK   R0   t   exog_insamplet   predictedvaluest   forecastvalues(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   predict  sH    !!		"8&	
		c         C@  sU   |  j  } | d k r% |  j | |  S| d k rA |  j | |  St d |   d S(   s   
        Compute the log-likelihood for ARMA(p,q) model

        Notes
        -----
        Likelihood used depends on the method set in fit
        R-   s   css-mlet   csss   Method %s not understoodN(   R-   s   css-mle(   R2   t   loglike_kalmanR   R.   (   R   R!   t
   set_sigma2R2   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    	c         C@  s   t  j | |  |  S(   sW   
        Compute exact loglikelihood for ARMA(p,q) model by the Kalman Filter.
        (   R   R   (   R   R!   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   
  s    c         C@  s  |  j  } |  j } |  j |  j } |  j j   j | j  } |  j } |  j	 ra |  j
 |  } n | } | d k r | t |  j | |   8} n  t j d | | | | !f t j d | | | f }	 }
 t j t | |  d | j } xF t |  D]8 } t |	 | d  d d d  | | d   | | <q Wt |	 |
 | d | d | } t j | |  } | | } | r| |  _ n  | d t d t  t |  | d | } | S(	   sA   
        Conditional Sum of Squares likelihood function.
        i    i   t   dtypeNiR   g       @i   (   R0   Ra   RC   RB   R?   R   t   astypeR   Ru   R   R   R	   RJ   R'   R=   R   R`   R   R<   R   t   sigma2R
   R   (   R   R!   R   R0   Ra   R^   RL   Ru   R   R   R   R   RU   RD   t   ssrR   t   llf(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s*    				;!6 
/Rm   s   css-mlet   lbfgsi  i   i   c         K@  s  |  j  } |  j } | |  _ |  j |  j } } |  j } t |  |  _ t | |  j |  \ } } | d k r | d k r | d k r | d k r t	 d   n  t
 t |  | | | |  | |  _ | |  _ t |  j | | | f |  j  |  _ | | } | d k r | d k r d } n  | j   |  _ } | d k rXt |  j  | |  _ n  | d k	 rvt j |  } n |  j | | | f | |
  } | r|  j |  } n  | d k r| j d d  | j d d  | j d	 d
  | j d t  n  t t |   j | d | d | d | d | d |	 | } | j } | rU|  j |  } n  t |  _ d } t |  | |  } | j  | _  | j! | _! t" |  S(   s  
        Fits ARMA(p,q) model using exact maximum likelihood via Kalman filter.

        Parameters
        ----------
        start_params : array-like, optional
            Starting parameters for ARMA(p,q). If None, the default is given
            by ARMA._fit_start_params.  See there for more information.
        transparams : bool, optional
            Whether or not to transform the parameters to ensure stationarity.
            Uses the transformation suggested in Jones (1980).  If False,
            no checking for stationarity or invertibility is done.
        method : str {'css-mle','mle','css'}
            This is the loglikelihood to maximize.  If "css-mle", the
            conditional sum of squares likelihood is maximized and its values
            are used as starting values for the computation of the exact
            likelihood via the Kalman filter.  If "mle", the exact likelihood
            is maximized via the Kalman Filter.  If "css" the conditional sum
            of squares likelihood is maximized.  All three methods use
            `start_params` as starting parameters.  See above for more
            information.
        trend : str {'c','nc'}
            Whether to include a constant or not.  'c' includes constant,
            'nc' no constant.
        solver : str or None, optional
            Solver to be used.  The default is 'lbfgs' (limited memory
            Broyden-Fletcher-Goldfarb-Shanno).  Other choices are 'bfgs',
            'newton' (Newton-Raphson), 'nm' (Nelder-Mead), 'cg' -
            (conjugate gradient), 'ncg' (non-conjugate gradient), and
            'powell'. By default, the limited memory BFGS uses m=12 to
            approximate the Hessian, projected gradient tolerance of 1e-8 and
            factr = 1e2. You can change these by using kwargs.
        maxiter : int, optional
            The maximum number of function evaluations. Default is 500.
        tol : float
            The convergence tolerance.  Default is 1e-08.
        full_output : bool, optional
            If True, all output from solver will be available in
            the Results object's mle_retvals attribute.  Output is dependent
            on the solver.  See Notes for more information.
        disp : int, optional
            If True, convergence information is printed.  For the default
            l_bfgs_b solver, disp controls the frequency of the output during
            the iterations. disp < 0 means no output in this case.
        callback : function, optional
            Called after each iteration as callback(xk) where xk is the current
            parameter vector.
        start_ar_lags : int, optional
            Parameter for fitting start_params. When fitting start_params,
            residuals are obtained from an AR fit, then an ARMA(p,q) model is
            fit via OLS using these residuals. If start_ar_lags is None, fit
            an AR process according to best BIC. If start_ar_lags is not None,
            fits an AR process with a lag length equal to start_ar_lags.
            See ARMA._fit_start_params_hr for more information.
        kwargs
            See Notes for keyword arguments that can be passed to fit.

        Returns
        -------
        statsmodels.tsa.arima_model.ARMAResults class

        See Also
        --------
        statsmodels.base.model.LikelihoodModel.fit : for more information
            on using the solvers.
        ARMAResults : results class returned by fit

        Notes
        -----
        If fit by 'mle', it is assumed for the Kalman Filter that the initial
        unknown state is zero, and that the initial variance is
        P = dot(inv(identity(m**2)-kron(T,T)),dot(R,R.T).ravel('F')).reshape(r,
        r, order = 'F')

        i    se   Estimation requires the inclusion of least one AR term, MA term, a constant or an exogenous variable.R   R   R   g:0yE>R   g      Y@R   i   R   R2   t   maxitert   full_outputt   dispt   callbackN(#   R0   Ra   R   R?   RJ   RC   R;   Ru   Rt   R.   Rw   RB   Rl   Rg   R   Rh   t   lowerR2   R9   R'   R   R   R   t
   setdefaultRR   R|   R   R   R!   R   R   t   ARMAResultst   mle_retvalst   mle_settingst   ARMAResultsWrapper(   R   R   R_   R2   R   t   solverR   R   R   R   R   t   kwargsR0   Ra   R?   RJ   RC   RB   R^   R   R!   t   normalized_cov_paramst   armafit(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   .  sX    N				0		
				c         O@  s   t  d   d  S(   Ns.   from_formula is not supported for ARMA models.(   t   NotImplementedError(   t   clst   formulaRg   t   subsett	   drop_colsR   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   from_formula  s    N(   t   __name__t
   __module__t   tsbaset   _tsa_doct   _arma_modelt   _arma_paramst   _armax_notest   __doc__R9   R}   R   R   R   R   R   R   R   R   R   R   t   _arma_predictRR   R   R   R   R   t   classmethodR   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s0   	k	
	
			37		c           B@  s   e  Z e j i e d  6e d 6d d 6e i d d 6d 6Z d d d d d  Z	 d	   Z
 d d d d d
  Z d d  Z d d d e d d d d d d d 
 Z d d d d e d  Z e e _ RS(   Rx   R!   R"   R#   R&   R    Ry   Rz   c         C@  sv   | \ } } }	 | d k r: t  | | |	 f | | | |  St t |   j |   }
 |
 j | | | | | |  |
 Sd  S(   Ni    (   R   R|   R&   t   __new__R}   (   R   R?   R]   RJ   R   R   R{   R@   t   dRA   t   mod(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    c         C@  sd   t  |  d d   } t  |  d d   } t  |  d d  } |  j |  j |  j |  j f |  j | | | f S(   NR   R   R{   Rz   (   R   R9   R?   Rb   R1   Ra   RJ   (   R   R   R   R{   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   __getnewargs__  s    c         C@  s  | \ } } }	 | d k r* t  d   n  t t |   j | | |	 f | | | |  | |  _ t |  j |  |  |  _ t j	 |  j d | |  _ t
 t |  j  | |	  | d  k	 r |  j | |  _ n  | d k r d |  j |  j _ n d j |  |  j |  j _ t |  j  }
 |  j j d } |  j j d  k	 r[|  j j |
 | |  j j d <n  |  j d  k	 r|  j r|  j |
 |  |  _ q|  j |
 | |  _ n  d  S(	   Ni   s   d > 2 is not supportedR*   i   s   D.s   D{0:d}.i    t
   row_labels(   R.   R|   R&   R}   R1   R   R?   t   _first_unintegrateR'   t   diffRw   R;   R9   RJ   t   endog_namesRg   Re   t   formatR   R   R   t   _cachet   _index_generated(   R   R?   R]   RJ   R   R   R{   R@   R   RA   t   orig_lengtht
   new_length(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR}     s*    +	!	c   
      C@  s  t  |  d d  } t  |  d d  } t  |  d d  } | d  k rt d | k r^ | r^ d } n | } |  j | } nG t | t t t j f  r | | 8} | d k  r t d |   q n  t | t t t j f  r | | 8} n  t	 t
 |   j | | |  \ } } } }	 d |  j k r0| r0| | 8} n  d | k r_| | | k  r_t d |   n  t | | | | |  | | | |	 f S(   NR2   R-   R0   i    R1   sD   The start index %d of the original series  has been differenced awaysE   Start must be >= k_ar for conditional MLE or dynamic forecast. Got %s(   R   R9   R   R   R   R   R'   t   integerR.   R|   R&   R   R2   R4   (
   R   R/   RX   R3   R   R2   R0   R1   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   
  s.    	
*Rm   s   css-mleR   i  i   i   c         K@  s   t  t |   j | | | | | | | | |	 |
 | 
 } d } t |  | j j |  } |  j | _ | j | _ | j	 | _	 t
 |  S(   s  
        Fits ARIMA(p,d,q) model by exact maximum likelihood via Kalman filter.

        Parameters
        ----------
        start_params : array-like, optional
            Starting parameters for ARMA(p,q).  If None, the default is given
            by ARMA._fit_start_params.  See there for more information.
        transparams : bool, optional
            Whehter or not to transform the parameters to ensure stationarity.
            Uses the transformation suggested in Jones (1980).  If False,
            no checking for stationarity or invertibility is done.
        method : str {'css-mle','mle','css'}
            This is the loglikelihood to maximize.  If "css-mle", the
            conditional sum of squares likelihood is maximized and its values
            are used as starting values for the computation of the exact
            likelihood via the Kalman filter.  If "mle", the exact likelihood
            is maximized via the Kalman Filter.  If "css" the conditional sum
            of squares likelihood is maximized.  All three methods use
            `start_params` as starting parameters.  See above for more
            information.
        trend : str {'c','nc'}
            Whether to include a constant or not.  'c' includes constant,
            'nc' no constant.
        solver : str or None, optional
            Solver to be used.  The default is 'lbfgs' (limited memory
            Broyden-Fletcher-Goldfarb-Shanno).  Other choices are 'bfgs',
            'newton' (Newton-Raphson), 'nm' (Nelder-Mead), 'cg' -
            (conjugate gradient), 'ncg' (non-conjugate gradient), and
            'powell'. By default, the limited memory BFGS uses m=12 to
            approximate the Hessian, projected gradient tolerance of 1e-8 and
            factr = 1e2. You can change these by using kwargs.
        maxiter : int, optional
            The maximum number of function evaluations. Default is 500.
        tol : float
            The convergence tolerance.  Default is 1e-08.
        full_output : bool, optional
            If True, all output from solver will be available in
            the Results object's mle_retvals attribute.  Output is dependent
            on the solver.  See Notes for more information.
        disp : int, optional
            If True, convergence information is printed.  For the default
            l_bfgs_b solver, disp controls the frequency of the output during
            the iterations. disp < 0 means no output in this case.
        callback : function, optional
            Called after each iteration as callback(xk) where xk is the current
            parameter vector.
        start_ar_lags : int, optional
            Parameter for fitting start_params. When fitting start_params,
            residuals are obtained from an AR fit, then an ARMA(p,q) model is
            fit via OLS using these residuals. If start_ar_lags is None, fit
            an AR process according to best BIC. If start_ar_lags is not None,
            fits an AR process with a lag length equal to start_ar_lags.
            See ARMA._fit_start_params_hr for more information.
        kwargs
            See Notes for keyword arguments that can be passed to fit.

        Returns
        -------
        `statsmodels.tsa.arima.ARIMAResults` class

        See Also
        --------
        statsmodels.base.model.LikelihoodModel.fit : for more information
            on using the solvers.
        ARIMAResults : results class returned by fit

        Notes
        -----
        If fit by 'mle', it is assumed for the Kalman Filter that the initial
        unknown state is zero, and that the initial variance is
        P = dot(inv(identity(m**2)-kron(T,T)),dot(R,R.T).ravel('F')).reshape(r,
        r, order = 'F')

        N(   R|   R&   R   R9   t   ARIMAResultst   _resultsR!   R1   R   R   t   ARIMAResultsWrapper(   R   R   R_   R2   R   R   R   R   R   R   R   R   R   R   t	   arima_fit(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR   ,  s    N			t   linearc         C@  s  t  | t t f  rX |  j |  \ } } } t  | t  rH | j } n  | |  j 7} n  | d k r | s | |  j |  j k r | d  k	 r t	 t
 |   j | | | | |  S|  j } d |  _ t	 t
 |   j | | | | |  }	 | |  _ |	 Sn| d k r|  j j }
 | sbt	 t
 |   j | | | | |  } |  j | | |  \ } } } } |  j } d |  j k rb| | d 7} | | d 7} | r| |  |
 | | d !} | d k r| t j |
 | d | d ! 7} n  t |
 | |  } t j | t | | |  | f } q_| |
 | | d !} | d k r_| t j |
 | d | d ! 7} q_q|  j } | r| |  |
 t | |  j d  | | d !} | d k r| t j |
 | d | d ! 7} n  t |
 | |  } t j | t | | |  | f } q| |
 t | |  | | d !} | d k r| t j |
 | d | d ! 7} qn<| |  j |  j k s| d  k ra|  j } |  j } |  j } |  j } |  j } t | | | f | | d t \ } } } } d |  _ t	 t
 |   j | | | | |  } | s?|  j | | |  \ } } } } | | 7} n  | |  _ |
 | d t j |  St	 t
 |   j | | | | |  } |
 | d t j |  S| St d |   d  S(	   NR  i    t   levelsR-   i   i   RP   s   typ %s not understood(   R   R   R   t   _get_index_label_loct   sliceR/   R1   R0   R9   R|   R&   R   Ra   Rg   R?   R   R2   R'   R   R   R=   R   R`   RC   RB   RQ   RR   R(   R.   (   R   R!   R/   RX   RJ   t   typR3   R+   RA   R   R?   R   R   R   t   fvR	  R0   R@   RC   RB   R1   RE   RF   RG   RH   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    					!	%	(	&%	"("							N(   R   R   R   R   t   _arima_modelt   _arima_paramsR   R   R9   R   R   R}   R   RR   R   R   R   t   _arima_predict(    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR&     s    					!"	ZeR   c           B@  s  e  Z d  Z i  Z d d d  Z e d    Z e d    Z e d    Z	 e d    Z
 e d    Z e d    Z e d	    Z e d
    Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z d d d e d  Z e e _ d   Z d   Z d d d d  Z d d  Z d d d d  Z d d d e d e d d  Z e  e _ RS(   s;  
    Class to hold results from fitting an ARMA model.

    Parameters
    ----------
    model : ARMA instance
        The fitted model instance
    params : array
        Fitted parameters
    normalized_cov_params : array, optional
        The normalized variance covariance matrix
    scale : float, optional
        Optional argument to scale the variance covariance matrix.

    Attributes
    ----------
    aic : float
        Akaike Information Criterion
        :math:`-2*llf+2* df_model`
        where `df_model` includes all AR parameters, MA parameters, constant
        terms parameters on constant terms and the variance.
    arparams : array
        The parameters associated with the AR coefficients in the model.
    arroots : array
        The roots of the AR coefficients are the solution to
        (1 - arparams[0]*z - arparams[1]*z**2 -...- arparams[p-1]*z**k_ar) = 0
        Stability requires that the roots in modulus lie outside the unit
        circle.
    bic : float
        Bayes Information Criterion
        -2*llf + log(nobs)*df_model
        Where if the model is fit using conditional sum of squares, the
        number of observations `nobs` does not include the `p` pre-sample
        observations.
    bse : array
        The standard errors of the parameters. These are computed using the
        numerical Hessian.
    df_model : array
        The model degrees of freedom = `k_exog` + `k_trend` + `k_ar` + `k_ma`
    df_resid : array
        The residual degrees of freedom = `nobs` - `df_model`
    fittedvalues : array
        The predicted values of the model.
    hqic : float
        Hannan-Quinn Information Criterion
        -2*llf + 2*(`df_model`)*log(log(nobs))
        Like `bic` if the model is fit using conditional sum of squares then
        the `k_ar` pre-sample observations are not counted in `nobs`.
    k_ar : int
        The number of AR coefficients in the model.
    k_exog : int
        The number of exogenous variables included in the model. Does not
        include the constant.
    k_ma : int
        The number of MA coefficients.
    k_trend : int
        This is 0 for no constant or 1 if a constant is included.
    llf : float
        The value of the log-likelihood function evaluated at `params`.
    maparams : array
        The value of the moving average coefficients.
    maroots : array
        The roots of the MA coefficients are the solution to
        (1 + maparams[0]*z + maparams[1]*z**2 + ... + maparams[q-1]*z**q) = 0
        Stability requires that the roots in modules lie outside the unit
        circle.
    model : ARMA instance
        A reference to the model that was fit.
    nobs : float
        The number of observations used to fit the model. If the model is fit
        using exact maximum likelihood this is equal to the total number of
        observations, `n_totobs`. If the model is fit using conditional
        maximum likelihood this is equal to `n_totobs` - `k_ar`.
    n_totobs : float
        The total number of observations for `endog`. This includes all
        observations, even pre-sample values if the model is fit using `css`.
    params : array
        The parameters of the model. The order of variables is the trend
        coefficients and the `k_exog` exognous coefficients, then the
        `k_ar` AR coefficients, and finally the `k_ma` MA coefficients.
    pvalues : array
        The p-values associated with the t-values of the coefficients. Note
        that the coefficients are assumed to have a Student's T distribution.
    resid : array
        The model residuals. If the model is fit using 'mle' then the
        residuals are created via the Kalman Filter. If the model is fit
        using 'css' then the residuals are obtained via `scipy.signal.lfilter`
        adjusted such that the first `k_ma` residuals are zero. These zero
        residuals are not returned.
    scale : float
        This is currently set to 1.0 and not used by the model or its results.
    sigma2 : float
        The variance of the residuals. If the model is fit by 'css',
        sigma2 = ssr/nobs, where ssr is the sum of squared residuals. If
        the model is fit by 'mle', then sigma2 = 1/nobs * sum(v**2 / F)
        where v is the one-step forecast error and F is the forecast error
        variance. See `nobs` for the difference in definitions depending on the
        fit.
    g      ?c         C@  s   t  t |   j | | | |  | j |  _ | j } | |  _ | j } | |  _ | j } | |  _ | j } | |  _ t | j	  |  _
 | j }	 |	 |  _ | | | |	 }
 |
 d |  _ |
 |  _ |  j |
 |  _ i  |  _ d  S(   Ni   (   R|   R   R}   R   Ru   RC   RB   R0   R;   R?   t   n_totobsRa   t   _ic_df_modelt   df_modelt   df_residR   (   R   Rx   R!   R   t   scaleRu   RC   RB   R0   Ra   R  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR}   Y  s&    											c         C@  s"   t  j t  j d |  j f  d S(   Ni   i(   R'   R   R=   RG   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   arrootsn  s    c         C@  s!   t  j t  j d |  j f  d S(   Ni   i(   R'   R   R=   RH   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   marootsr  s    c         C@  s'   |  j  } t j | j | j  d t S(   s   
        Returns the frequency of the AR roots.

        This is the solution, x, to z = abs(z)*exp(2j*np.pi*x) where z are the
        roots.
        i   (   R  R'   t   arctan2t   imagt   realR   (   R   t   z(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   arfreqv  s    	c         C@  s'   |  j  } t j | j | j  d t S(   s   
        Returns the frequency of the MA roots.

        This is the solution, x, to z = abs(z)*exp(2j*np.pi*x) where z are the
        roots.
        i   (   R  R'   R  R  R  R   (   R   R  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   mafreq  s    	c         C@  s%   |  j  |  j } |  j | | |  j !S(   N(   RC   RB   R!   R0   (   R   R^   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRG     s    c         C@  s(   |  j  |  j } |  j } |  j | | S(   N(   RC   RB   R0   R!   (   R   R^   R0   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRH     s    	c         C@  s   |  j  j |  j  S(   N(   Rx   R   R!   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    c         C@  s_   |  j  } |  j j |  } t |  d k rB t j d | d  St j t j t |    S(   Ni   g      i    (   R!   Rx   R   R;   R'   t   sqrtt   diagR   (   R   R!   t   hess(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   bse  s
    	c         C@  s&   |  j  } |  j j |  } t |  S(   N(   R!   Rx   R   R   (   R   R!   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt
   cov_params  s    	c         C@  s   d |  j  d |  j S(   Nii   (   R   R  (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   aic  s    c         C@  s(   |  j  } d |  j t j |  |  j S(   Ni(   Ru   R   R'   R
   R  (   R   Ru   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    	c         C@  s5   |  j  } d |  j d t j t j |   |  j S(   Nii   (   Ru   R   R'   R
   R  (   R   Ru   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   hqic  s    	c         C@  s   |  j  } | j j   } |  j } | j } | d  k	 ra | j d k ra | d k ra | | } qa n  | j d k r | d k r | | } n  | |  j } | S(   NR   i    (   Rx   R?   R   R0   RJ   R9   R2   RK   (   R   Rx   R?   R0   RJ   R  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRY     s    			c         C@  s   |  j  j |  j  S(   N(   Rx   R   R!   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRK     s    c         C@  s)   |  j  } t j t j |  j  |  d S(   Ni   (   R  R   t   sfR'   R   t   tvalues(   R   R  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   pvalues  s    	c         C@  s   |  j  j |  j | | | |  S(   N(   Rx   R   R!   (   R   R/   RX   RJ   R3   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    c         C@  sc   |  j  } t t j d |  j f t j d |  j f d | } t j | t j | d   } | S(   Ni   t   lagsi   (   R   R   R'   R=   RG   RH   R  R(   (   R   RI   R   t   ma_rept   fcasterr(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _forecast_error  s
    	 c         C@  s>   t  j d | d  } t j | | | | | | f } | S(   Ni   g       @(   R   t   ppfR'   t   c_(   R   RT   R*  t   alphat   constt   conf_int(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   _forecast_conf_int  s    i   g?c         C@  s  | d k	 r2t j |  } |  j d k rR | j d k rR | d d  d f } nL | j d k r t |  |  j k r t d   n  | d d d  f } n  | j d | k r t d   n  |  j | j d k r t d   n  |  j d k rJt j	 |  j
 j |  j d  |  j d  f | f  } qJn |  j rJt d   n  t |  j | |  j |  j |  j |  j |  j |  j
 j | d |  j
 j 	} |  j |  } |  j | | |  } | | | f S(	   s  
        Out-of-sample forecasts

        Parameters
        ----------
        steps : int
            The number of out of sample forecasts from the end of the
            sample.
        exog : array
            If the model is an ARMAX, you must provide out of sample
            values for the exogenous variables. This should not include
            the constant.
        alpha : float
            The confidence intervals for the forecasts are (1 - alpha) %

        Returns
        -------
        forecast : array
            Array of out of sample forecasts
        stderr : array
            Array of the standard error of the forecasts.
        conf_int : array
            2d array of the confidence interval for the forecast
        i   Ns%   1d exog given and len(exog) != k_exogi    s   new exog needed for each stepsI   exog must contain the same number of variables as in the estimated model.sS   Forecast values for exog are required when the model contains exogenous regressors.R2   (   R9   R'   R   RC   R:   R;   R.   R   R0   R   Rx   RJ   RB   RW   R!   RK   Ra   R?   R2   R+  R1  (   R   RI   RJ   R.  RT   R*  R0  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRT     s2    ,		c      	   C@  sD  d d l  m } |  j } | j j d } | j } t |  d d  } d | k rY | } n | |  j } |  j j	 d- k	 r |  j j	 } | | j d  g }	 |	 d | d j d  g 7}	 n& t |  d	 t t |  j j   }	 |  j |  j }
 } | st |
 | f  } n t |
 | | f  } d. d | j j | g f d | g f d/ d0 d |	 d g f d |	 d g f g } d t t |  j j   g f d d |  j g f d d |  j d g f d d |  j g f d d |  j g f d d |  j g f g } |   } | j |  d | d | d | | j |  d | d t d d l m } | r|
 rg  t d |
 d  D] } d  | ^ qm} g  t d | d  D] } d! | ^ q} | | } t j |  j |  j f } t j |  j  |  j! f } n | r7g  t d | d  D] } d! | ^ q} | } |  j } |  j! } nQ |
 rg  t d |
 d  D] } d  | ^ qQ} | } |  j } |  j  } n g  } t |  r@t j" |  } t j# | j$ | j% | | f  } | g  | D]8 } d" | d d# | d d" | d$ d" | d% f ^ qd& d' d( d) d* g d d+ d, | } | j& j' |  n  | S(1   s  Summarize the Model

        Parameters
        ----------
        alpha : float, optional
            Significance level for the confidence intervals.

        Returns
        -------
        smry : Summary instance
            This holds the summary table and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary
        i    (   t   Summarys    Model ResultsR1   R-   s   %m-%d-%Ys   - is    - s   Dep. Variable:s   Model:s   Method:s   Date:s   Time:s   Sample:R"   i   s   No. Observations:s   Log Likelihoods   %#5.3fs   S.D. of innovationsg      ?t   AICt   BICt   HQICt   gleftt   grightt   titleR.  t   use_t(   t   SimpleTables   AR.%ds   MA.%ds   %17.4fs   %+17.4fji   i   t   headerss               Reals            Imaginarys            Moduluss           Frequencyt   Rootst   stubsN(   s   Dep. Variable:N(   s   Date:N(   s   Time:N((   t   statsmodels.iolib.summaryR2  Rx   t	   __class__R   R2   R   R0   Rg   R   R9   t   strftimet   strR;   t
   orig_endogRa   R?   R   R   R#  R   R$  t   add_table_2colst   add_table_paramsR   t   statsmodels.iolib.tableR:  R   R'   R=   R  R  R  R  R   R   R  R  t   tablest   append(   R   R.  R2  Rx   R8  R2   R1   R/   R   t   sampleR0   Ra   R]   t   top_leftt	   top_rightt   smryR:  RU   t   arstubst   mastubsR=  R   R   t   modulusRg   t   rowt   roots_table(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   summary%  s    			!&	**
*	*	!B		s   %.4fc         C@  s|  d d l  m } t |  d d  } d |  j j k r= | } n | |  j } |  j j d k	 r |  j j } | | j	 d  g } | | d j	 d  g 7} n& t
 |  d t
 t |  j j   } |  j |  j }	 }
 |
 rs|	 rsg  t d |	 d  D] } d	 | ^ q } g  t d |
 d  D] } d
 | ^ q} | | } t j |  j |  j f } t j |  j |  j f } n |
 rg  t d |
 d  D] } d
 | ^ q} | } |  j } |  j } nQ |	 r	g  t d |	 d  D] } d	 | ^ q} | } |  j } |  j } n g  } t |  rxt j |  } t j | j | j | | f  } | |  } d d d d g | _ | | _ n  d d l m } | j   } | j |   } |  j j | d <| d | d <| d | d <d |  j d | d <d |  j  | d <t
 t |  j j!   | d <| j" |   } | j# |  | j$ | d | t |  rb| j$ | d d n  | j% d |  d |  | S(   s  Experimental summary function for ARIMA Results

        Parameters
        ----------
        title : string, optional
            Title for the top table. If not None, then this replaces the
            default title
        alpha : float
            significance level for the confidence intervals
        float_format: string
            print format for floats in parameters summary

        Returns
        -------
        smry : Summary instance
            This holds the summary table and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary2.Summary : class to hold summary
            results

        i    (   t	   DataFrameR1   R-   s   %m-%d-%Yis    - i   s   AR.%ds   MA.%dt   Realt	   Imaginaryt   Modulust	   Frequency(   t   summary2s   Method:s   Sample:s      s   %#5.3fg      ?s   S.D. of innovations:s   HQIC:s   No. Observations:t   float_formats   %17.4ft   resultsR8  N(&   t   pandasRR  R   Rx   R2   R0   Rg   R   R9   R@  RA  R;   RB  Ra   R   R'   R=   R  R  R  R  R   R   R  R  t   columnsR   t   statsmodels.iolibRW  R2  t   summary_modelR   R$  R?   t   summary_paramst   add_dictt   add_dft	   add_title(   R   R8  R.  RX  RR  R1   R/   R   RH  R0   Ra   RU   RL  RM  R=  R   R   RN  Rg   RW  RK  t
   model_infoR!   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRW    sd    	&**
*	*	!c      
   C@  s  d d l  m } m }	 |   }
 |	 |  \ } } |  j | | | |  } |  j j | | d t \ } } } }
 | r | } |  j |  } |  j | | | |  } n  t	 |  j
 d  r d d l m } | | d |  j
 j } | j d | d d	  } n | j |  | j   d
 j   } | rd j d |  } | j | | | d  d   d f | d  d   d f d d d d d | n  | r| j | | d |  |  j j | | d !d |  j j n  | j d d  | S(   Ni    (   t   _import_mplt   create_mpl_axR3   t   predict_dates(   t   SeriesR   t   axt   labelRT   is   {0:.0%} confidence intervali   t   colort   grayR.  g      ?t   loct   best(   t   statsmodels.graphics.utilsRc  Rd  R   Rx   R   R   R+  R1  R~   Rg   RZ  Rf  Re  t   plott	   get_linest	   get_xdataR   t   fill_betweenR?   R   t   legend(   R   R/   RX   RJ   R3   R.  t   plot_insampleRg  Rc  Rd  R+   t   figRT   R   RI   t   fc_errorR0  Rf  R)   Rh  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   plot_predict  s2    	'7,N(!   R   R   R   R   R9   R}   R   R  R  R  R  RG   RH   R   R!  R"  R#  R   R$  RY   RK   R'  R   R   t   _arma_results_predictR+  R1  RT   RQ  RW  RR   Rv  t   _plot_predict(    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s8   c				?d[%R   c           B@  sD   e  Z i  Z e j e j j e  Z i  Z e j e j j	 e  Z	 RS(    (
   R   R   t   _attrst   wrapt   union_dictsR   t   TimeSeriesResultsWrappert   _wrap_attrst   _methodst   _wrap_methods(    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s   	R  c           B@  st   e  Z d d d d  e d  Z e e _ d   Z d   Z d d d d  Z	 d d d e d e
 d d  Z e e _ RS(	   R  c         C@  s"   |  j  j |  j | | | | |  S(   N(   Rx   R   R!   (   R   R/   RX   RJ   R  R3   (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR     s    c         C@  so   |  j  } t t j d |  j f t j d |  j f d | } t j t j t | |  j	  d  |  } | S(   Ni   R(  i   (
   R   R   R'   R=   RG   RH   R  R(   R,   R1   (   R   RI   R   R)  t   fcerr(    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR+    s
    	,c         C@  s>   t  j d | d  } t j | | | | | | f } | S(   Ni   g       @(   R   R,  R'   R-  (   R   RT   R  R.  R/  R0  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR1  %  s    #i   g?c   	      C@  s  | d k	 r |  j d k rC | j d k rC | d d  d f } n  | j d | k re t d   n  |  j | j d k r t d   n  |  j d k r t j |  j j	 |  j d  |  j
 d  f | f  } q n |  j r t d   n  t |  j | |  j |  j |  j |  j
 |  j |  j j | d |  j j 	} |  j } |  j j j | } t | t | |   | } |  j |  } |  j | | |  } | | | f S(   s  
        Out-of-sample forecasts

        Parameters
        ----------
        steps : int
            The number of out of sample forecasts from the end of the
            sample.
        exog : array
            If the model is an ARIMAX, you must provide out of sample
            values for the exogenous variables. This should not include
            the constant.
        alpha : float
            The confidence intervals for the forecasts are (1 - alpha) %

        Returns
        -------
        forecast : array
            Array of out of sample forecasts
        stderr : array
            Array of the standard error of the forecasts.
        conf_int : array
            2d array of the confidence interval for the forecast

        Notes
        -----
        Prediction is done in the levels of the original endogenous variable.
        If you would like prediction of differences in levels use `predict`.
        i   Ni    s   new exog needed for each stepsI   exog must contain the same number of variables as in the estimated model.sS   Forecast values for exog are required when the model contains exogenous regressors.R2   (   R9   RC   R:   R   R.   R0   R'   R   Rx   RJ   RB   RW   R!   RK   Ra   R?   R2   R1   Rg   R   R   R+  R1  (	   R   RI   RJ   R.  RT   R   R?   R  R0  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRT   *  s.    ,			c      
   C@  s#  d d l  m } m }	 |   }
 |	 |  \ } } |  j | | | d |  } |  j j | | |  \ } } } }
 | r | } |  j |  } |  j | | | |  } n  t |  j	 d  r d d l
 m } | | d |  j	 j } | j d | d d	  } n | j |  | j   d
 j   } | rd j d |  } | j | | | d  d   d f | d  d   d f d d d d d | n  | rd d  l } |  j } | j d d |  j j  } t |  j j |  j j  } | j | | d |  | | | | | d !d | n  | j d d  | S(   Ni    (   Rc  Rd  R	  Re  (   Rf  R   Rg  Rh  RT   is   {0:.0%} confidence intervali   Ri  Rj  R.  g      ?s   D\d*\.R"   Rk  Rl  (   Rm  Rc  Rd  R   Rx   R   R+  R1  R~   Rg   RZ  Rf  Re  Rn  Ro  Rp  R   Rq  t   reR1   t   subR   R   R?   R   Rr  (   R   R/   RX   RJ   R3   R.  Rs  Rg  Rc  Rd  R+   Rt  RT   R   RI   Ru  R0  Rf  R)   Rh  R  R1   R	  (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyRv  h  s<    	$7	#N(   R   R   R9   R   R   t   _arima_results_predictR   R+  R1  RT   RR   Rv  t   _arima_plot_predict(    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR    s   			>+R  c           B@  s   e  Z RS(    (   R   R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyR    s   t   __main__(   t   arma_generate_sampleg      ?g      g      ?t   nsamplei  R_   Rq   R]   i   i90  g333333gffffff?gR2   R   t	   as_pandasi	   gffffffֿg?R   g      g?g333333ӿi   g      п(   t   webuset   wpi1t   wpi(   t
   __future__R    t   statsmodels.compat.pythonR   R   R   R   t   numpyR'   t   scipyR   t   scipy.statsR   R   t   scipy.signalR   R	   R
   R   R   t   numpy.linalgR   t   statsmodels.tools.decoratorsR   t   statsmodels.tsa.base.tsa_modelt   tsat   baset	   tsa_modelR   t   statsmodels.base.wrappert   wrapperRz  t#   statsmodels.regression.linear_modelR   R   t   statsmodels.tsa.tsatoolsR   R   R   R   R   R   R   R   t   statsmodels.tsa.vector_arR   t   statsmodels.tsa.ar_modelR   t   statsmodels.tsa.arima_processR   t   statsmodels.tools.numdiffR   R   t   statsmodels.tsa.kalmanfR   R   R   R   R  R  t   _predict_notest   _results_notest   _predictt   _predict_returnsR   Rw  t   _arima_extrasR  R  t   _arima_plot_predict_examplet   _plot_extrasRf   t   splitRx  R  R,   R4   R9   RO   RW   R\   R   RQ   Rc   Rl   Rt   Rw   t   TimeSeriesModelR   R&   t   TimeSeriesModelResultsR   t   ResultsWrapperR   t   populate_wrapperR  R  R   t   statsmodels.apit   apit   smR  RL   t   armaR   t   rest   randomt   seedt   y_arma22t   arma22t   res22t
   arma22_csst   res22csst   datasetst   sunspotst   loadRg   R?   t   art   resart   y_arma31t	   arma31cssRR   t   res31csst   y_arma13t	   arma13csst   res13csst   y_arma41t	   arma41csst   res41csst   y_arma14t	   arma14csst   res14csst   statsmodels.datasetsR  t   dtaR  R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/tsa/arima_model.pyt   <module>   s   ":

$  )"		9'					  4 #  !	$	!		$!	$!	$
