Coverage for pybreakpoints/recresid_.py : 90%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
Citations:
- Brown, RL, J Durbin, and JM Evans. 1975. Techniques for Testing the Consistency of Regression Relationships over Time. Journal of the Royal Statistical Society. Series B (Methodological) 37 (2): 149-192.
- Judge George G., William E. Griffiths, R. Carter Hill, Helmut Lütkepohl, and Tsoung-Chao Lee. 1985. The theory and practice of econometrics. New York: Wiley. ISBN: 978-0-471-89530-5
"""
def _recresid(X, y, span):
# Initial fit
np.dot(XTX_j, X[span - 1, :]))
# Prediction with previous beta
# Update
""" Return standardized recursive residuals for y ~ X
Parameters ---------- X : array like 2D (n_obs x n_features) design matrix y : array like 1D independent variable span : int, optional Minimum number of observations for initial regression. If ``span`` is None, use the number of features in ``X``
Returns ------- array like np.ndarray, pd.Series, or xr.DataArray containing recursive residuals standardized by prediction error variance
Notes ----- For a matrix :math:`X_t` of :math:`T` total observations of :math:`n` variables, the :math:`t` th recursive residual is the forecast prediction error for :math:`y_t` using a regression fit on the first :math:`t - 1` observations. Recursive residuals are scaled and standardized so they are :math:`N(0, 1)` distributed.
Using notation from Brown, Durbin, and Evans (1975) and Judge, et al (1985):
.. math:: w_r = \\frac{y_r - \\boldsymbol{x}_r^{\prime}\\boldsymbol{b}_{r-1}} {\sqrt{(1 + \\boldsymbol{x}_r^{\prime} S_{r-1}\\boldsymbol{x}_r)}} = \\frac {y_r - \\boldsymbol{x}_r^{\prime}\\boldsymbol{b}_r} {\sqrt{1 - \\boldsymbol{x}_r^{\prime}S_r\\boldsymbol{x}_r}}
r = k + 1, \ldots, T,
where :math:`S_{r}` is the residual sum of squares after fitting the model on :math:`r` observations.
A quick way of calculating :math:`\\boldsymbol{b}_r` and :math:`S_r` is using an update formula (Equations 4 and 5 in Brown, Durbin, and Evans; Equation 5.5.14 and 5.5.15 in Judge et al):
.. math:: \\boldsymbol{b}_r = b_{r-1} + \\frac {S_{r-1}\\boldsymbol{x}_j (y_r - \\boldsymbol{x}_r^{\prime}\\boldsymbol{b}_{r-1})} {1 + \\boldsymbol{x}_r^{\prime}S_{r-1}x_r}
.. math:: S_r = S_{j-1} - \\frac{S_{j-1}\\boldsymbol{x}_r\\boldsymbol{x}_r^{\prime}S_{j-1}} {1 + \\boldsymbol{x}_r^{\prime}S_{j-1}\\boldsymbol{x}_r}
See Also -------- statsmodels.stats.diagnostic.recursive_olsresiduals """
if isinstance(y, (pd.Series, pd.DataFrame)): rresid = pd.Series(data=rresid, index=y.index[span:], name='recresid') elif isinstance(y, xr.DataArray): rresid = xr.DataArray(rresid, coords={'time': y.get_index('time')[span:]}, dims=('time', ), name='recresid')
|