B
    ¥ŠãZf‡  ã               @   s¤  d dl mZ d dlmZmZmZmZmZ d dlZ	d dlm
Z
mZ d dlmZmZ d dlmZ d dlmZ d dlmZmZmZmZ d dlm  m  mZ d dlm  mZ d d	lm Z m!Z!m"Z" d d
l#m$Z$m%Z% d dl&m'Z' d dl(m  m)Z* d dl+m,Z, dgZ-d4dd„Z.dd„ Z/d5dd„Z0G dd„ dej1ƒZ2G dd„ dej3ƒZ4G dd„ de*j5ƒZ6e* 7e6e4¡ e8dkr d dl9m:Z; e;j<j= >¡ Z=e2e=j?ƒZ@e@jAddZBe2e=j?ƒZCeCjAdddddd ZDd dlEZEd dlFmGZH eHjId!d"d#ZJeHjKeJeLe=j?ƒd$ZMeEjNeJjOeLe=j?ƒd%d&ZPe	 Qd!d!eLe=j?ƒ ¡ZReHjKeRd"d'ZRd dlOZOe	 SeeOjOjTeM U¡  VeW¡ƒ¡ZXeEjYe=j?eXd(Z=e2e=d"d'ZZeZjAddd)Z[e	 Sd*d+d,d-d.d-d/d0d1d2g
¡Z\e	 ]e\¡Z^d3Z_dS )6é    )Údivision)Ú	iteritemsÚrangeÚstring_typesÚlmapÚlongN)ÚdotÚidentity)ÚinvÚslogdet)Únorm)ÚOLS)ÚlagmatÚ	add_trendÚ_ar_transparamsÚ_ar_invtransparams)Úresettable_cacheÚcache_readonlyÚcache_writable)Úapprox_fprimeÚapprox_hess)ÚKalmanFilter)ÚutilÚARc             C   s   t j| d |dS )z<Helper function to calculate sum of squares along first axisé   )Úaxis)ÚnpÚsum)Úxr   © r   ú7lib/python3.7/site-packages/statsmodels/tsa/ar_model.pyÚsumofsq   s    r!   c             C   s$   |dks|r | |k r t d|  ƒ‚d S )NÚcmlezEStart must be >= k_ar for conditional MLE or dynamic forecast. Got %d)Ú
ValueError)ÚstartÚk_arÚmethodÚdynamicr   r   r    Ú_check_ar_start   s    r(   c          	   C   s¸   |d |… pd}||d … d d d… }t  || ¡}|rR| || |… |d |…< n| | d … |d |…< t  |¡}	x@t|ƒD ]4}
|t  |||
|
| … ¡ }||	|
< |||
| < q|W |	S )Nr   éÿÿÿÿ)r   Úzerosr   r   )ÚyÚparamsÚpÚk_trendZstepsr$   ÚmuZarparamsÚendogZforecastÚiZfcastr   r   r    Ú_ar_predict_out_of_sample$   s    
r2   c                   sÄ   e Zd Zejddejddœ Zd1‡ fdd„	Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zd2‡ fdd„	Zd3dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd4d(d)„Zd5‡ fd/d0„	Z‡  ZS )6r   zAutoregressive AR(p) modelzVendog : array-like
        1-d endogenous response variable. The independent variable.Ú )Úmodelr,   Zextra_paramsZextra_sectionsNÚnonec                sf   t t| ƒj|d |||d | j}|jdkrB|d d …d f }|| _n |jdkrb|jd dkrbtdƒ‚d S )N)Úmissingé   z'Only the univariate case is implemented)Úsuperr   Ú__init__r0   ÚndimÚshaper#   )Úselfr0   ÚdatesÚfreqr6   )Ú	__class__r   r    r9   ?   s    
zAR.__init__c             C   s   d S )Nr   )r<   r   r   r    Ú
initializeH   s    zAR.initializec             C   s<   | j }| j}| ¡ }t|||| …  ¡ ƒ|||| …< |S )z‚
        Transforms params to induce stationarity/invertability.

        Reference
        ---------
        Jones(1980)
        )r%   r.   Úcopyr   )r<   r,   r-   ÚkÚ	newparamsr   r   r    Ú_transparamsK   s
    $zAR._transparamsc             C   s<   | j }| j}| ¡ }t|||| …  ¡ ƒ|||| …< |S )z9
        Inverse of the Jones reparameterization
        )r%   r.   rA   r   )r<   Ústart_paramsr-   rB   rC   r   r   r    Ú_invtransparamsY   s
    $zAR._invtransparamsc             C   s@  | j }t ||||¡}t |||d|¡}	t |df¡}
ttt|d ƒt 	||¡ ƒt|	|	jƒ 
d¡ƒ}|j||dd}|}t |¡}x¶t|ƒD ]ª}|| t||
ƒ }tt||ƒ|jƒ}d| }ttt||ƒ|jƒ|ƒ}t||
ƒt||ƒ }
|t||ƒ }tt||ƒ|jƒt|	|	jƒ }||d krŽt||
ƒ||d | < qŽW dS )z¤
        Return the pre-sample predicted values using the Kalman Filter

        Notes
        -----
        See predict method for how to use start and p.
        r   r7   r   ÚF)Úorderg      ð?N)r.   r   ÚTÚRr   r*   r   r
   r	   ZkronZravelZreshapeÚZr   )r<   r,   r$   r-   Úendr+   ÚpredictedvaluesrB   ZT_matZR_matZalphaZQ_0ÚPZZ_matr1   Zv_matZF_matZFinvÚKÚLr   r   r    Ú_presample_fitc   s&    
zAR._presample_fitc       	         s¤   t | ddƒ}t | ddƒ}|d kr@|dkr2|s2d}n|}| j| }|d krR| jd }tt| ƒ |||¡\}}}}d|krŠ||k rŠtd| ƒ‚t||||ƒ ||||fS )Nr&   Úmler%   r   r)   zEStart must be >= k_ar for conditional MLE or dynamic forecast. Got %s)ÚgetattrZ_indexr8   r   Ú_get_prediction_indexr#   r(   )	r<   r$   rL   r'   Úindexr&   r%   Úout_of_sampleZprediction_index)r?   r   r    rT   †   s    

zAR._get_prediction_indexFc          	   C   s€  |   |||¡\}}}}|| dkr*tdƒ‚| j}| j}| j}	| j ¡ }
|rl||| d 7 }t|
|||||ƒS t 	|d | ¡}|	dkrø|rª|d dt 
||d… ¡  }nd}||k rø|  |||t|d |ƒ|
d|… | |¡ |d|| …  |7  < ||k r|S t| j|ƒ}t|| dƒ}t|| dƒ}tt|ƒ|| d ƒ}|||… ||d…< |r|t|
||||ƒ}tj||f }|S )aò  
        Returns in-sample and out-of-sample prediction.

        Parameters
        ----------
        params : array
            The fitted model parameters.
        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.
        dynamic : bool
            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`.

        Returns
        -------
        predicted values : array

        Notes
        -----
        The linear Gaussian Kalman filter is used to return pre-sample fitted
        values. The exact initial Kalman Filter is used. See Durbin and Koopman
        in the references for more information.
        r7   zend is before startrR   r   N)rT   r#   r%   r.   r&   r0   Úsqueezer2   r   r*   r   rQ   Úminr   ÚXÚmaxÚlenÚr_)r<   r,   r$   rL   r'   rV   Ú_r%   r.   r&   r0   rM   r/   ÚfittedvaluesZpv_startZfv_startZfv_endZforecastvaluesr   r   r    Úpredictž   sB    #

 
z
AR.predictc          	   C   sØ   | j }| j}|d }tjd||d… f }tj||f|jd}x|td|ƒD ]n}t ||d|… ¡dd… ||d |d d…f< ||d |d d…f  t || d… |¡dd… 8  < qJW ||j t 	| 
¡ ¡ }|S )z„
        Returns the inverse of the presample variance-covariance.

        Notes
        -----
        See Hamilton p. 125
        r7   r)   N)Údtype)r.   r%   r   r\   r*   r`   r   Z	correlaterI   ÚdiagZdiagonal)r<   r,   rB   r-   Zp1Zparams0ÚVpinvr1   r   r   r    Ú_presample_varcovò   s    0>zAR._presample_varcovc             C   sb   | j }| j}| j}t| ¡ t ||¡ ƒ}|| }| d t dtj ¡t |¡  |d|   S )zQ
        Loglikelihood of AR(p) process using conditional sum of squares
        r   )	ÚnobsÚYrY   r!   rW   r   r   ÚlogÚpi)r<   r,   rd   re   rY   ÚssrÚsigma2r   r   r    Ú_loglike_css	  s    "zAR._loglike_cssc          	   C   s,  | j }| j}| j}| j}| j}| jr.|  |¡}|d|…  ¡ }|rR|d g| }ndg}t 	|dt 
||d… ¡  ¡}	||	dd…df  }
|  |¡}t t |
j|¡|
¡ ¡ }t||d…  ¡ t ||¡ ƒ}d| ||  }|| _t|ƒd }d|t dtj ¡t |¡  | ||  ||   }|S )zO
        Loglikelihood of AR(p) process using exact maximum likelihood
        Nr   r7   g      ð?g      à¿r   )rd   rY   r0   r%   r.   ÚtransparamsrD   rA   r   Úasarrayr   rc   r   rI   Úitemr!   rW   ri   r   rf   rg   )r<   r,   rd   rY   r0   r%   r.   ZypÚcZmupZdiffprb   Z
diffpVpinvrh   ri   ZlogdetÚlogliker   r   r    Ú_loglike_mle  s,    
 
 4zAR._loglike_mlec             C   s"   | j dkr|  |¡S |  |¡S dS )a¡  
        The loglikelihood of an AR(p) process

        Parameters
        ----------
        params : array
            The fitted parameters of the AR model

        Returns
        -------
        llf : float
            The loglikelihood evaluated at `params`

        Notes
        -----
        Contains constant term.  If the model is fit by OLS then this returns
        the conditonal maximum likelihood.

        .. math:: \frac{\left(n-p\right)}{2}\left(\log\left(2\pi\right)+\log\left(\sigma^{2}\right)\right)-\frac{1}{\sigma^{2}}\sum_{i}\epsilon_{i}^{2}

        If it is fit by MLE then the (exact) unconditional maximum likelihood
        is returned.

        .. math:: -\frac{n}{2}log\left(2\pi\right)-\frac{n}{2}\log\left(\sigma^{2}\right)+\frac{1}{2}\left|V_{p}^{-1}\right|-\frac{1}{2\sigma^{2}}\left(y_{p}-\mu_{p}\right)^{\prime}V_{p}^{-1}\left(y_{p}-\mu_{p}\right)-\frac{1}{2\sigma^{2}}\sum_{t=p+1}^{n}\epsilon_{i}^{2}

        where

        :math:`\mu_{p}` is a (`p` x 1) vector with each element equal to the
        mean of the AR process and :math:`\sigma^{2}V_{p}` is the (`p` x `p`)
        variance-covariance matrix of the first `p` observations.
        r"   N)r&   rj   rp   )r<   r,   r   r   r    ro   :  s    !

z
AR.loglikec             C   s   | j }t||ddS )a  
        Return the gradient of the loglikelihood at params.

        Parameters
        ----------
        params : array-like
            The parameter values at which to evaluate the score function.

        Notes
        -----
        Returns numerical gradient.
        g:Œ0âŽyE>)Úepsilon)ro   r   )r<   r,   ro   r   r   r    Úscorea  s    zAR.scorec             C   s   dS )z%
        Not Implemented Yet
        Nr   )r<   r,   r   r   r    Úinformationq  s    zAR.informationc             C   s   | j }t||ƒS )z4
        Returns numerical hessian for now.
        )ro   r   )r<   r,   ro   r   r   r    Úhessianw  s    z
AR.hessianc             C   s:   | j }t||dd}t |¡}|r0t|d|d}|| _|S )zt
        Private method to build the RHS matrix for estimation.

        Columns are trend terms then lags.
        Zboth)ÚmaxlagZtrimT)ZprependÚtrend)r0   r   r   Zget_trendorderr   r.   )r<   r%   rv   r0   rY   r.   r   r   r    Ú_stackX~  s    
z
AR._stackXrn   rR   c          	   C   s  | j }||d… }|| _|  ||¡}|| _| j}td|ƒ}i }	|dkr´xPt||d ƒD ]>}
|||
 d… }t|ƒj|
|d|ddd}t	d| ƒ|	|
< qVW t
dd	„ t|	ƒD ƒƒ\}}nfd
}x`t||d dƒD ]L}
|||
 d… }t|ƒj|
|d|ddd}d}t |jd ¡|krÊ|
}P qÊW |S )a2  
        Select the lag order according to the information criterion.

        Parameters
        ----------
        maxlag : int
            The highest lag length tried. See `AR.fit`.
        ic : str {'aic','bic','hqic','t-stat'}
            Criterion used for selecting the optimal lag length.
            See `AR.fit`.
        trend : str {'c','nc'}
            Whether to include a constant or not. 'c' - include constant.
            'nc' - no constant.

        Returns
        -------
        bestlag : int
            Best lag according to IC.
        Nr7   zt-statr   éd   )ru   r&   Úfull_outputrv   ÚmaxiterÚdispzfit.c             s   s   | ]\}}||fV  qd S )Nr   )Ú.0rB   Úresr   r   r    ú	<genexpr>³  s    z"AR.select_order.<locals>.<genexpr>g»jg	RQú?r)   é#   )r0   re   rw   rY   r.   rZ   r   r   ÚfitÚevalrX   r   r   ÚabsÚtvalues)r<   ru   Úicrv   r&   r0   re   rY   rB   ZresultsZlagZ	endog_tmpr€   ZbesticZbestlagÚstopr   r   r    Úselect_orderŒ  s6    


zAR.select_orderr"   TÚlbfgsr   r7   c          	      sJ  |  ¡ }|dkrtd| ƒ‚|| _|| _|| _t| jƒ}| j}|dkr^ttd|d d  ƒƒ}|}|dk	r–|  ¡ }|dkr†td| ƒ‚|  	||||¡}|| _
||d…dd…f }|  ||¡}| j}t | j||¡| _|| _|| _|d	krt||ƒ ¡ }|j}|| | _|j|j | _nä|d
krü|  ¡ }|| _|dkrLt||ƒ ¡ j}n*t|ƒ|| krvtdt|ƒ|| f ƒ‚|  |¡}|dkrº| dd¡ | dd¡ | dd¡ | dd¡ tt| ƒjf ||||	|
|dœ|—Ž}|j}| jrü|  |¡}d| _tj  |¡}t !||j"¡}t#| ||ƒ}|d
krB|	rB|j$|_$|j%|_%t&|ƒS )a  
        Fit the unconditional maximum likelihood of an AR(p) process.

        Parameters
        ----------
        maxlag : int
            If `ic` is None, then maxlag is the lag length used in fit.  If
            `ic` is specified then maxlag is the highest lag order used to
            select the correct lag order.  If maxlag is None, the default is
            round(12*(nobs/100.)**(1/4.))
        method : str {'cmle', 'mle'}, optional
            cmle - Conditional maximum likelihood using OLS
            mle - Unconditional (exact) maximum likelihood.  See `solver`
            and the Notes.
        ic : str {'aic','bic','hic','t-stat'}
            Criterion used for selecting the optimal lag length.
            aic - Akaike Information Criterion
            bic - Bayes Information Criterion
            t-stat - Based on last lag
            hqic - Hannan-Quinn Information Criterion
            If any of the information criteria are selected, the lag length
            which results in the lowest value is selected.  If t-stat, the
            model starts with maxlag and drops a lag until the highest lag
            has a t-stat that is significant at the 95 % level.
        trend : str {'c','nc'}
            Whether to include a constant or not. 'c' - include constant.
            'nc' - no constant.

        The below can be specified if method is 'mle'

        transparams : bool, optional
            Whether or not to transform the parameters to ensure stationarity.
            Uses the transformation suggested in Jones (1980).
        start_params : array-like, optional
            A first guess on the parameters.  Default is cmle estimates.
        solver : str or None, optional
            Solver to be used if method is 'mle'.  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'.
        maxiter : int, optional
            The maximum number of function evaluations. Default is 35.
        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 : bool, optional
            If True, convergence information is output.
        callback : function, optional
            Called after each iteration as callback(xk) where xk is the current
            parameter vector.
        kwargs
            See Notes for keyword arguments that can be passed to fit.

        References
        ----------
        Jones, R.H. 1980 "Maximum likelihood fitting of ARMA models to time
            series with missing observations."  `Technometrics`.  22.3.
            389-95.

        See also
        --------
        statsmodels.base.model.LikelihoodModel.fit
        )r"   ZywrR   zMethod %s not recognizedNé   g      Y@g      Ð?)ÚaicÚbicÚhqiczt-statzic option %s not understoodr"   rR   z6Length of start params is %d. There are %d parameters.r‡   Zpgtolg:Œ0âŽyE>ZfactrÚmZapprox_gradT)rE   r&   rz   ry   r{   ÚcallbackF)'Úlowerr#   r&   rv   rk   r[   r0   ÚintÚroundr†   r%   rw   r.   r   Zmake_lag_namesZendog_namesZ
exog_namesre   rY   r   r€   r,   rd   rh   ri   rF   Ú
setdefaultr8   r   rD   r   ÚlinalgZpinvr   rI   Ú	ARResultsZmle_retvalsZmle_settingsÚARResultsWrapper)r<   ru   r&   r„   rv   rk   rE   Úsolverrz   ry   r{   r   Úkwargsrd   r0   r%   re   rY   r.   Zarfitr,   ZmlefitZ	pinv_exogÚnormalized_cov_params)r?   r   r    r€   Ä  sr    F








zAR.fit)NNr5   )N)NNF)rn   rR   )Nr"   Nrn   TNr‡   r   r7   r7   N)Ú__name__Ú
__module__Ú__qualname__ÚtsbaseZ_tsa_docÚbaseZ_missing_param_docÚ__doc__r9   r@   rD   rF   rQ   rT   r_   rc   rj   rp   ro   rr   rs   rt   rw   r†   r€   Ú__classcell__r   r   )r?   r    r   8   s.   
	
#
T%'
8  c                   s   e Zd ZdZi Zd$‡ fdd„	Zeƒ dd„ ƒZeƒ dd	„ ƒZe	d
d„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZd%dd„Zejj d¡Zd  d¡Zd edd!… ed"d#…  e ed#d…  ¡e_‡  ZS )&r“   u¹
  
    Class to hold results from fitting an AR model.

    Parameters
    ----------
    model : AR Model instance
        Reference to the model that is fit.
    params : array
        The fitted parameters from the AR Model.
    normalized_cov_params : array
        inv(dot(X.T,X)) where X is the lagged values.
    scale : float, optional
        An estimate of the scale of the model.

    Returns
    -------
    **Attributes**

    aic : float
        Akaike Information Criterion using Lutkephol's definition.
        :math:`log(sigma) + 2*(1 + k_ar + k_trend)/nobs`
    bic : float
        Bayes Information Criterion
        :math:`\log(\sigma) + (1 + k_ar + k_trend)*\log(nobs)/nobs`
    bse : array
        The standard errors of the estimated parameters. If `method` is 'cmle',
        then the standard errors that are returned are the OLS standard errors
        of the coefficients. If the `method` is 'mle' then they are computed
        using the numerical Hessian.
    fittedvalues : array
        The in-sample predicted values of the fitted AR model. The `k_ar`
        initial values are computed via the Kalman Filter if the model is
        fit by `mle`.
    fpe : float
        Final prediction error using LÃ¼tkepohl's definition
        ((n_totobs+k_trend)/(n_totobs-k_ar-k_trend))*sigma
    hqic : float
        Hannan-Quinn Information Criterion.
    k_ar : float
        Lag length. Sometimes used as `p` in the docs.
    k_trend : float
        The number of trend terms included. 'nc'=0, 'c'=1.
    llf : float
        The loglikelihood of the model evaluated at `params`. See `AR.loglike`
    model : AR model instance
        A reference to the fitted AR model.
    nobs : float
        The number of available observations `nobs` - `k_ar`
    n_totobs : float
        The number of total observations in `endog`. Sometimes `n` in the docs.
    params : array
        The fitted parameters of the model.
    pvalues : array
        The p values associated with the standard errors.
    resid : array
        The residuals of the model. If the model is fit by 'mle' then the
        pre-sample residuals are calculated using fittedvalues from the Kalman
        Filter.
    roots : array
        The roots of the AR process 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.
    scale : float
        Same as sigma2
    sigma2 : float
        The variance of the innovations (residuals).
    trendorder : int
        The polynomial order of the trend. 'nc' = None, 'c' or 't' = 0,
        'ct' = 1, etc.
    tvalues : array
        The t-values associated with `params`.
    Nç      ð?c       	         sš   t t| ƒ ||||¡ tƒ | _|j| _t|jƒ}|| _|j	| _	|j
| _
|j}|| _|j}|| _d }|dkrr|d }|| _|| | _|| j  | _| j_d S )Nr   r7   )r8   r“   r9   r   Ú_cacherd   r[   r0   Ún_totobsrY   re   r%   r.   Ú
trendorderÚdf_modelZdf_residr4   )	r<   r4   r,   r—   Úscaler¡   r%   r.   r¢   )r?   r   r    r9   ¨  s$    

zARResults.__init__c             C   s0   | j }|jdkr$d| j t| jƒ S | j jS d S )Nr"   g      ð?)r4   r&   rd   r!   Úresidri   )r<   r4   r   r   r    ri   ½  s    
zARResults.sigma2c             C   s   | j S )N)ri   )r<   r   r   r    r¤   Å  s    zARResults.scalec             C   sz   | j jdkrL| j}t ||¡}|| j| j | j  }t t 	| j
|d¡¡S t| j| j jƒ}t t 	tj |¡ ¡¡S d S )Nr"   )r¤   )r4   r&   r¥   r   r   rd   r%   r.   Zsqrtra   Z
cov_paramsr   r,   ro   r’   r
   )r<   r¥   rh   Z	ols_scaleZhessr   r   r    ÚbseÉ  s    zARResults.bsec             C   s   t  t | j¡¡d S )Nr   )r   Zsfr   r‚   rƒ   )r<   r   r   r    ÚpvaluesÔ  s    zARResults.pvaluesc             C   s    t  | j¡dd| j  | j  S )Nr   r7   )r   rf   ri   r£   rd   )r<   r   r   r    r‰   Ø  s    zARResults.aicc             C   s4   | j }t | j¡dt t |¡¡ | d| j   S )Nr   r7   )rd   r   rf   ri   r£   )r<   rd   r   r   r    r‹   ã  s     zARResults.hqicc             C   s"   | j }| j}|| ||  | j S )N)rd   r£   ri   )r<   rd   r£   r   r   r    Úfpeð  s    zARResults.fpec             C   s*   | j }t | j¡d| j t |¡ |  S )Nr7   )rd   r   rf   ri   r£   )r<   rd   r   r   r    rŠ   ÷  s    zARResults.bicc             C   sB   | j }|j ¡ }|jdkr.|| jd … | j S |j ¡ | j S d S )Nr"   )r4   r0   rW   r&   r%   r^   )r<   r4   r0   r   r   r    r¥     s
    

zARResults.residc             C   s*   | j }t tjd| j|d …  f ¡d S )Nr7   r)   )r.   r   Úrootsr\   r,   )r<   rB   r   r   r    r©     s    zARResults.rootsc             C   s   | j  | j¡S )N)r4   r_   r,   )r<   r   r   r    r^     s    zARResults.fittedvaluesFc             C   s   | j }| j ||||¡}|S )N)r,   r4   r_   )r<   r$   rL   r'   r,   rM   r   r   r    r_     s    zARResults.predictÚ
a          confint : bool, float
            Whether to return confidence intervals.  If `confint` == True,
            95 % confidence intervals are returned.  Else if `confint` is a
            float, then it is assumed to be the alpha value of the confidence
            interval.  That is confint == .05 returns a 95% confidence
            interval, and .10 would return a 90% confidence interval.é   é   é   )NrŸ   )NNF)r˜   r™   rš   r   r    r9   r   ri   r¤   r   r¦   r§   r‰   r‹   r¨   rŠ   r¥   r©   r^   r_   r   ÚsplitZpreddocZ	extra_docÚjoinrž   r   r   )r?   r    r“   [  s(   I
r“   c               @   s4   e Zd Zi Ze ejje¡Zi Z	e ejj
e	¡Z
dS )r”   N)r˜   r™   rš   Z_attrsÚwrapZunion_dictsr›   ZTimeSeriesResultsWrapperZ_wrap_attrsZ_methodsZ_wrap_methodsr   r   r   r    r”   6  s   

r”   Ú__main__é	   )ru   rR   Zbfgsiô  g»½×Ùß|Û=)ru   r&   r•   rz   Zgtoli¤  ÚA)Zyearr>   )Z
start_dateÚlengthzA-DEC)r$   Zperiodsr>   )r>   )rU   )r&   ru   iÌ  iÉ  iÄ  iË  iÎ  iÏ  iß  ií  g      ~@g      à?)r   )r   )`Z
__future__r   Zstatsmodels.compat.pythonr   r   r   r   r   Znumpyr   r   r	   Znumpy.linalgr
   r   Zscipy.statsr   Z#statsmodels.regression.linear_modelr   Zstatsmodels.tsa.tsatoolsr   r   r   r   Zstatsmodels.tsa.base.tsa_modelZtsarœ   Z	tsa_modelr›   Zstatsmodels.base.modelr4   Zstatsmodels.tools.decoratorsr   r   r   Zstatsmodels.tools.numdiffr   r   Z$statsmodels.tsa.kalmanf.kalmanfilterr   Zstatsmodels.base.wrapperÚwrapperr°   Zstatsmodels.tsa.vector_arr   Ú__all__r!   r(   r2   ZTimeSeriesModelr   ZTimeSeriesModelResultsr“   ZResultsWrapperr”   Zpopulate_wrapperr˜   Zstatsmodels.apiZapiZsmZdatasetsZsunspotsÚloadr0   Zar_olsr€   Zres_olsZar_mleZres_mle_bfgsZpandasZscikits.timeseriesZ
timeseriesZtsZDateZd1Z
date_arrayr[   Zts_drZDatetimeIndexZdatetimeZ	pandas_drZaranger=   rl   ZfromordinalZ	toordinalZastyper   Zdt_datesZSeriesÚmodr}   ZIBMZdiffÚwZthetar   r   r   r    Ú<module>   sj   

    ' \




$
