B
    ¤ŠãZNf  ã            	   @   s  d Z ddlmZmZmZ ddlmZ ddlmZ ddl	Z
ddlZddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZ ddlm  mZ e
 dddddgdddddgdddddgg¡ZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"e #e"e!¡ dS ) zL
Recursive least squares model

Author: Chad Fulton
License: Simplified-BSD
é    )ÚdivisionÚabsolute_importÚprint_function)Úwarn)ÚOrderedDictN)ÚOLS)Ú_is_using_pandas)ÚMLEModelÚ
MLEResultsÚMLEResultsWrapper)ÚBunch)Úcache_readonlyÚresettable_cachegŸâ8ð*ñ?gö7Fü”ó?gp`r£Èºõ?g¤‰£aGø?g!*C¿
ú?gü´@q¶oå¿gÚsO²på¿gÜÅêE£qå¿g“›ò2Ôrå¿gRƒÄÑ°så¿g\b§×œâ¿gÿRP›‚†ç¿gZÞ°È
Yì¿gXŽ<[ñ¿gÏ£Éó¿c                   sv   e Zd ZdZ‡ fdd„Zed‡ fdd„	ƒZdd„ Zd‡ fd
d„	Zd‡ fdd„	Z	e
dd„ ƒZe
dd„ ƒZdd„ Z‡  ZS )ÚRecursiveLSa…  
    Recursive least squares

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`
    exog : array_like
        Array of exogenous regressors, shaped nobs x k.

    Notes
    -----
    Recursive least squares (RLS) corresponds to expanding window ordinary
    least squares (OLS).

    This model applies the Kalman filter to compute recursive estimates of the
    coefficients and recursive residuals.

    References
    ----------
    .. [*] Durbin, James, and Siem Jan Koopman. 2012.
       Time Series Analysis by State Space Methods: Second Edition.
       Oxford University Press.

    c                sâ   t |d ƒst |¡}t |d ƒ}|s,t |¡}|jdkrV|sL|d d …d f }n
t |¡}|jd | _| 	d| j¡ | 	dd¡ | 	dd¡ t
t| ƒj|f| j|dœ|—Ž | jd d …d d …d f j| d< t | j¡| d	< d
| d< d S )Né   Úloglikelihood_burnZinitializationZapproximate_diffuseZinitial_varianceg    eÍÍA)Úk_statesÚexogZdesignZ
transitiong      ð?)Úobs_covr   r   )r   ÚnpZ
asanyarrayZasarrayÚndimÚpdZ	DataFrameÚshapeÚk_exogÚ
setdefaultÚsuperr   Ú__init__r   ÚTZeyer   )ÚselfZendogr   ÚkwargsZexog_is_using_pandas)Ú	__class__© úBlib/python3.7/site-packages/statsmodels/regression/recursive_ls.pyr   8   s$    






zRecursiveLS.__init__Nc                s   t t| ƒ |||¡S )z8
        Not implemented for state space models
        )r   r	   Úfrom_formula)ÚclsZformulaÚdataZsubset)r    r!   r"   r#   ^   s    zRecursiveLS.from_formulac             C   s>   | j dd}|jd }t ||¡| j| j  }|| d< |   ¡ S )z€
        Fits the model by application of the Kalman filter

        Returns
        -------
        RecursiveLSResults
        T)Ú
return_ssmr   )r   r   r   )ÚsmoothÚstandardized_forecasts_errorr   ÚinnerÚnobsr   )r   Zsmoother_resultsZresidZsigma2r!   r!   r"   Úfite   s    	

zRecursiveLS.fitFc                sp   t t| ƒjg fddddœ|—Ž}|sl|jd d …df }d|jd d …d d …df ddœ}tt| ||d|d	ƒ}|S )
NTÚnone)ÚtransformedÚcov_typer&   éÿÿÿÿÚ	nonrobustz^Parameters and covariance matrix estimates are RLS estimates conditional on the entire sample.)Úcustom_cov_typeÚcustom_cov_paramsÚcustom_descriptionÚcustom)r.   Úcov_kwds)r   r   ÚfilterÚfiltered_stateÚfiltered_state_covÚRecursiveLSResultsWrapperÚRecursiveLSResults)r   r&   r   ÚresultÚparamsr5   )r    r!   r"   r6   y   s    

zRecursiveLS.filterc                sp   t t| ƒjg fddddœ|—Ž}|sl|jd d …df }d|jd d …d d …df ddœ}tt| ||d|d	ƒ}|S )
NTr,   )r-   r.   r&   r/   r0   z^Parameters and covariance matrix estimates are RLS estimates conditional on the entire sample.)r1   r2   r3   r4   )r.   r5   )r   r   r'   r7   r8   r9   r:   )r   r&   r   r;   r<   r5   )r    r!   r"   r'      s    

zRecursiveLS.smoothc             C   s   | j S )N)Ú
exog_names)r   r!   r!   r"   Úparam_names§   s    zRecursiveLS.param_namesc             C   s
   t  d¡S )Nr   )r   Zzeros)r   r!   r!   r"   Ústart_params«   s    zRecursiveLS.start_paramsc             K   s   dS )a  
        Update the parameters of the model

        Updates the representation matrices to fill in the new parameter
        values.

        Parameters
        ----------
        params : array_like
            Array of new parameters.
        transformed : boolean, optional
            Whether or not `params` is already transformed. If set to False,
            `transform_params` is called. Default is True..

        Returns
        -------
        params : array_like
            Array of parameters.
        Nr!   )r   r<   r   r!   r!   r"   Úupdate°   s    zRecursiveLS.update)N)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr#   r+   r6   r'   Úpropertyr>   r?   r@   Ú__classcell__r!   r!   )r    r"   r      s   &r   c                   s„   e Zd ZdZd‡ fdd„	Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
ddd„Zddd„Zddd„Zddd„Zd dd„Z‡  ZS )!r:   aÛ  
    Class to hold results from fitting a recursive least squares model.

    Parameters
    ----------
    model : RecursiveLS instance
        The fitted model instance

    Attributes
    ----------
    specification : dictionary
        Dictionary including all attributes from the recursive least squares
        model instance.

    See Also
    --------
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.mlemodel.MLEResults
    Úopgc                sF   t t| ƒj||||f|Ž tj| _| j ¡ | _t	f d| jj
iŽ| _d S )Nr   )r   r:   r   r   ÚinfZdf_residÚmodelZ_get_init_kwdsZ
_init_kwdsr   r   Úspecification)r   rJ   r<   Úfilter_resultsr.   r   )r    r!   r"   r   Ü   s    
zRecursiveLSResults.__init__c             C   sŠ   d}| j }d }}||j }t| j||… | j||…||…f dd|d}| jdk	rd| j||… |_| jdk	r†| j||…||…f |_|S )a7  
        Estimates of regression coefficients, recursively estimated

        Returns
        -------
        out: Bunch
            Has the following attributes:

            - `filtered`: a time series array with the filtered estimate of
                          the component
            - `filtered_cov`: a time series array with the filtered estimate of
                          the variance/covariance of the component
            - `smoothed`: a time series array with the smoothed estimate of
                          the component
            - `smoothed_cov`: a time series array with the smoothed estimate of
                          the variance/covariance of the component
            - `offset`: an integer giving the offset in the state vector where
                        this component begins
        Nr   )ÚfilteredÚfiltered_covÚsmoothedÚsmoothed_covÚoffset)	rK   r   r   r7   r8   Zsmoothed_staterO   Zsmoothed_state_covrP   )r   ÚoutÚspecÚstartrQ   Úendr!   r!   r"   Úrecursive_coefficientsê   s    


z)RecursiveLSResults.recursive_coefficientsc             C   s   | j j ¡ | j jd d  S )a9  
        Recursive residuals

        Returns
        -------
        resid_recursive : array_like
            An array of length `nobs` holding the recursive
            residuals.

        Notes
        -----
        The first `k_exog` residuals are typically unreliable due to
        initialization.

        )r   r   g      à?)rL   r(   Zsqueezer   )r   r!   r!   r"   Úresid_recursive  s    
z"RecursiveLSResults.resid_recursivec             C   s4   | j }t | j| j d… ¡tj| j|d… dd S )aÜ  
        Cumulative sum of standardized recursive residuals statistics

        Returns
        -------
        cusum : array_like
            An array of length `nobs - k_exog` holding the
            CUSUM statistics.


        Notes
        -----
        The CUSUM statistic takes the form:

        .. math::

            W_t = \frac{1}{\hat \sigma} \sum_{j=k+1}^t w_j

        where :math:`w_j` is the recursive residual at time :math:`j` and
        :math:`\hat \sigma` is the estimate of the standard deviation
        from the full sample.

        Excludes the first `k_exog` datapoints.

        Due to differences in the way :math:`\hat \sigma` is calculated, the
        output of this function differs slightly from the output in the
        R package strucchange and the Stata contributed .ado file cusum6. The
        calculation in this package is consistent with the description of
        Brown et al. (1975)

        References
        ----------
        .. [*] Brown, R. L., J. Durbin, and J. M. Evans. 1975.
           "Techniques for Testing the Constancy of
           Regression Relationships over Time."
           Journal of the Royal Statistical Society.
           Series B (Methodological) 37 (2): 149-92.

        Nr   )Úddof)r   r   ÚcumsumrW   Zstd)r   Úllbr!   r!   r"   Úcusum)  s    )zRecursiveLSResults.cusumc             C   s*   t  | j| jd… d ¡}|d }|| S )a˜  
        Cumulative sum of squares of standardized recursive residuals
        statistics

        Returns
        -------
        cusum_squares : array_like
            An array of length `nobs - k_exog` holding the
            CUSUM of squares statistics.

        Notes
        -----
        The CUSUM of squares statistic takes the form:

        .. math::

            s_t = \left ( \sum_{j=k+1}^t w_j^2 \right ) \Bigg /
                  \left ( \sum_{j=k+1}^T w_j^2 \right )

        where :math:`w_j` is the recursive residual at time :math:`j`.

        Excludes the first `k_exog` datapoints.

        References
        ----------
        .. [*] Brown, R. L., J. Durbin, and J. M. Evans. 1975.
           "Techniques for Testing the Constancy of
           Regression Relationships over Time."
           Journal of the Royal Statistical Society.
           Series B (Methodological) 37 (2): 149-92.

        Né   r/   )r   rY   rW   r   )r   ZnumerZdenomr!   r!   r"   Úcusum_squaresV  s    "z RecursiveLSResults.cusum_squaresr   çš™™™™™©?ú
upper leftNc             C   s*  t |ttfƒr|g}t|ƒ}| jj}x0t|ƒD ]$}|| }	t |	tƒr.| |	¡||< q.W ddlm	}
 ddl
m}m} |ƒ }|||ƒ}x˜t|ƒD ]Š}|| }	| |d|d ¡}t| jdƒrÔ| jjdk	rÔ| jj ¡ }nt | j¡}| j}| j}|j||d… |j|	|d…f d||	  d | ¡ \}}|dk	rò|
 d|d	  ¡}t |j|	|	dd…f ¡}|j|	 ||  }|j|	 ||  }|j||d… ||d… ||d… d
d}dd| d  }|dkrò|jddd| ¡ d d}|  |¡ |  |¡ |j!|||d ||d k rŽ|j" #g ¡ qŽW | $¡  |S )aI  
        Plot the recursively estimated coefficients on a given variable

        Parameters
        ----------
        variables : int or str or iterable of int or string, optional
            Integer index or string name of the variable whose coefficient will
            be plotted. Can also be an iterable of integers or strings. Default
            is the first variable.
        alpha : float, optional
            The confidence intervals for the coefficient are (1 - alpha) %
        legend_loc : string, optional
            The location of the legend in the plot. Default is upper left.
        fig : Matplotlib Figure instance, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the grid will be created in the provided
            figure using `fig.add_subplot()`.
        figsize : tuple, optional
            If a figure is created, this argument allows specifying a size.
            The tuple is (width, height).

        Notes
        -----
        All plots contain (1 - `alpha`) %  confidence intervals.
        r   )Únorm)Ú_import_mplÚcreate_mpl_figr   ÚdatesNzRecursive estimates: %s)Úlabelg       @gš™™™™™É?)Úalphaz$%.3g \%%$ confidence intervaléd   )r   r   )Zfc)Úloc)%Ú
isinstanceÚintÚstrÚlenrJ   r=   ÚrangeÚindexZscipy.statsr`   Ústatsmodels.graphics.utilsra   rb   Úadd_subplotÚhasattrr%   rc   Ú	_mpl_reprr   Úaranger*   r   rV   ÚplotrM   Zget_legend_handles_labelsZppfZsqrtrN   Zfill_betweenZ	RectangleZget_facecolorÚappendÚlegendZxaxisZset_ticklabelsZtight_layout)r   Z	variablesre   Ú
legend_locÚfigÚfigsizeZk_variablesr=   ÚiZvariabler`   ra   rb   ÚpltÚaxrc   rZ   ZcoefZhandlesÚlabelsZcritical_valueZ
std_errorsZci_lowerZci_upperZci_polyZci_labelÚpr!   r!   r"   Úplot_recursive_coefficient|  sT    


&



z-RecursiveLSResults.plot_recursive_coefficientc                s„   |dkrd‰n$|dkrd‰n|dkr*d‰nt dƒ‚| j‰ | jˆ  | d ‰‡ ‡‡fd	d
„}|dkrrt ˆ | jg¡}||ƒ ||ƒfS )a—  
        Parameters
        ----------
        alpha : float, optional
            The significance bound is alpha %.
        ddof : int, optional
            The number of periods additional to `k_exog` to exclude in
            constructing the bounds. Default is zero. This is usually used
            only for testing purposes.
        points : iterable, optional
            The points at which to evaluate the significance bounds. Default is
            two points, beginning and end of the sample.

        Notes
        -----
        Comparing against the cusum6 package for Stata, this does not produce
        exactly the same confidence bands (which are produced in cusum6 by
        lw, uw) because they burn the first k_exog + 1 periods instead of the
        first k_exog. If this change is performed
        (so that `tmp = (self.nobs - llb - 1)**0.5`), then the output here
        matches cusum6.

        The cusum6 behavior does not seem to be consistent with
        Brown et al. (1975); it is likely they did that because they needed
        three initial observations to get the initial OLS estimates, whereas
        we do not need to do that.
        g{®Gáz„?g}?5^ºIò?gš™™™™™©?g¼t“Vî?gš™™™™™¹?gffffffî?zInvalid significance level.g      à?c                s   ˆˆ dˆ | ˆ   ˆ  S )Nr\   r!   )Úx)rZ   ÚscalarÚtmpr!   r"   Ú<lambda>
  s    z?RecursiveLSResults._cusum_significance_bounds.<locals>.<lambda>N)Ú
ValueErrorr   r*   r   Úarray)r   re   rX   ÚpointsÚ
upper_liner!   )rZ   r€   r   r"   Ú_cusum_significance_boundsá  s    z-RecursiveLSResults._cusum_significance_boundsc             C   sú   ddl m}m} |ƒ }|||ƒ}| ddd¡}t| jdƒrT| jjdk	rT| jj ¡ }	nt 	| j
¡}	| j}
|j|	|
d… | jdd |jd|	|
 |	d d	d
d |  |¡\}}|j|	|
 |	d g|dd|d  d | |	|
 |	d g|d¡ |j|d |S )a  
        Plot the CUSUM statistic and significance bounds.

        Parameters
        ----------
        alpha : float, optional
            The plotted significance bounds are alpha %.
        legend_loc : string, optional
            The location of the legend in the plot. Default is upper left.
        fig : Matplotlib Figure instance, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the grid will be created in the provided
            figure using `fig.add_subplot()`.
        figsize : tuple, optional
            If a figure is created, this argument allows specifying a size.
            The tuple is (width, height).

        Notes
        -----
        Evidence of parameter instability may be found if the CUSUM statistic
        moves out of the significance bounds.

        References
        ----------
        .. [*] Brown, R. L., J. Durbin, and J. M. Evans. 1975.
           "Techniques for Testing the Constancy of
           Regression Relationships over Time."
           Journal of the Royal Statistical Society.
           Series B (Methodological) 37 (2): 149-92.

        r   )ra   rb   r   rc   NZCUSUM)rd   r/   Úkg333333Ó?)Zcolorre   zk--z%d%% significancerf   )rg   )rn   ra   rb   ro   rp   r%   rc   rq   r   rr   r*   r   rs   r[   Zhlinesr‡   ru   )r   re   rv   rw   rx   ra   rb   rz   r{   rc   rZ   Ú
lower_liner†   r!   r!   r"   Ú
plot_cusum  s     "
zRecursiveLSResults.plot_cusumc       	      C   sÈ   | j }d| j|  d }ydddddg |d ¡}W n tk
rP   td	ƒ‚Y nX td
d
…|f }|d |d  |d |  |d |d   }|d
kr¦t || jg¡}|| | j|  }|| || fS )aÐ  
        Notes
        -----
        Comparing against the cusum6 package for Stata, this does not produce
        exactly the same confidence bands (which are produced in cusum6 by
        lww, uww) because they use a different method for computing the
        critical value; in particular, they use tabled values from
        Table C, pp. 364-365 of "The Econometric Analysis of Time Series"
        Harvey, (1990), and use the value given to 99 observations for any
        larger number of observations. In contrast, we use the approximating
        critical values suggested in Edgerton and Wells (1994) which allows
        computing relatively good approximations for any number of
        observations.
        g      à?r   gš™™™™™¹?gš™™™™™©?gš™™™™™™?g{®Gáz„?g{®Gázt?r\   zInvalid significance level.Nr   g      ø?)r   r*   rm   rƒ   Ú_cusum_squares_scalarsr   r„   )	r   re   r…   rZ   ÚnZixZscalarsZcritÚliner!   r!   r"   Ú"_cusum_squares_significance_boundsL  s    ,z5RecursiveLSResults._cusum_squares_significance_boundsc             C   s  ddl m}m} |ƒ }|||ƒ}| ddd¡}t| jdƒrT| jjdk	rT| jj ¡ }	nt 	| j
¡}	| j}
|j|	|
d… | jdd t 	|
| j
¡|
 | j
|
  }|j|	|
d… |dd	d
 |  |¡\}}|j|	|
 |	d g|dd|d  d | |	|
 |	d g|d¡ |j|d |S )a  
        Plot the CUSUM of squares statistic and significance bounds.

        Parameters
        ----------
        alpha : float, optional
            The plotted significance bounds are alpha %.
        legend_loc : string, optional
            The location of the legend in the plot. Default is upper left.
        fig : Matplotlib Figure instance, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the grid will be created in the provided
            figure using `fig.add_subplot()`.
        figsize : tuple, optional
            If a figure is created, this argument allows specifying a size.
            The tuple is (width, height).

        Notes
        -----
        Evidence of parameter instability may be found if the CUSUM of squares
        statistic moves out of the significance bounds.

        Critical values used in creating the significance bounds are computed
        using the approximate formula of [1]_.

        References
        ----------
        .. [*] Brown, R. L., J. Durbin, and J. M. Evans. 1975.
           "Techniques for Testing the Constancy of
           Regression Relationships over Time."
           Journal of the Royal Statistical Society.
           Series B (Methodological) 37 (2): 149-92.
        .. [1] Edgerton, David, and Curt Wells. 1994.
           "Critical Values for the Cusumsq Statistic
           in Medium and Large Sized Samples."
           Oxford Bulletin of Economics and Statistics 56 (3): 355-65.

        r   )ra   rb   r   rc   NzCUSUM of squares)rd   rˆ   g333333Ó?)re   r/   zk--z%d%% significancerf   )rg   )rn   ra   rb   ro   rp   r%   rc   rq   r   rr   r*   r   rs   r]   rŽ   ru   )r   re   rv   rw   rx   ra   rb   rz   r{   rc   rZ   Zref_liner‰   r†   r!   r!   r"   Úplot_cusum_squaresm  s"    )
z%RecursiveLSResults.plot_cusum_squares)rH   )r   r^   r_   NN)r   N)r^   r_   NN)N)r^   r_   NN)rA   rB   rC   rD   r   rF   rV   r   rW   r[   r]   r~   r‡   rŠ   rŽ   r   rG   r!   r!   )r    r"   r:   Ç   s   &-&  
c
/ 
;
! r:   c               @   s0   e Zd Zi Ze eje¡Zi Ze ej	e¡Z	dS )r9   N)
rA   rB   rC   Z_attrsÚwrapZunion_dictsr   Z_wrap_attrsZ_methodsZ_wrap_methodsr!   r!   r!   r"   r9   ²  s   r9   )$rD   Z
__future__r   r   r   Úwarningsr   Zstatsmodels.compat.collectionsr   Znumpyr   Zpandasr   Z#statsmodels.regression.linear_modelr   Zstatsmodels.tools.datar   Z#statsmodels.tsa.statespace.mlemodelr	   r
   r   Zstatsmodels.tools.toolsr   Zstatsmodels.tools.decoratorsr   r   Zstatsmodels.base.wrapperÚbaseÚwrapperr   r„   r‹   r   r:   r9   Zpopulate_wrapperr!   r!   r!   r"   Ú<module>   s.    *   n