You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: add a function to compute residuals for RollingOLS #7521
I'm wondering if it is possible to get residuals from the fit object of a statsmodels.regression.rolling.RollingOLS model? Something akin to statsmodels.regression.linear_model.RegressionResultsWrapper.resid, statsmodels.regression.recursive_ls.RecursiveLSResultsWrapper.resid or statsmodels.regression.recursive_ls.RecursiveLSResultsWrapper.resid_recursive?
(As an aside, @ChadFulton is there a DOI associated with Harvey 1989 that you mention here? As someone unfamiliar with the literature googling Harvey 1989 seems to return a reference to The condition of postmodernity by David Harvey which I don't think is the relevant text?)
My impression is that calculating the residuals from the rolling regression would be fairly straightforward? I.e. some generalized version of the following
Harvey, A. (1990). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge: Cambridge University Press. doi:10.1017/CBO9781107049994
As an aside, it seems like most texts, e.g. Durbin & Koopman, reference what I believe is the original edition from 1989 but the only reference I could find online was to the reprint in 1990.
I have not used rolling OLS extensively for anything myself, but I suppose that the appropriate residuals depend on what you plan to do with them.
For example, in your code you are computing residuals e_i as e_i = y_i - E[y_i | y_{i-1}, y_{i-2}, ..., y-{i-w}], where the window size is w. This is computing the residual for each datapoint i using the regression based on observation i and the previous w-1 observations.
But there is no particular reason you couldn't compute them as, for example, the datapoint i and the subsequentw-1 observations, e_i = y_i - E[y_i | y_{i+1}, y_{i+2}, ..., y-{i+w}].
Re DOI, apologies. some more searching turned up the relevant text
Harvey, A. (1990). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge: Cambridge University Press. doi:10.1017/CBO9781107049994
As an aside, it seems like most texts, e.g. Durbin & Koopman, reference what I believe is the original edition from 1989 but the only reference I could find online was to the reprint in 1990.
Thanks, this is a good point, and in any case we should provide the reference in the file where we cite it.
I implemented RollingOLS. I wasn't sure what residuals should look like since there are at least 2 different definitions. 1. A set of T-window+1 set of residuals for each regression run, or two a single set of rolling residuals that would only include the last observation in the residual, e.g., if using observations [12, 13, 14, 15, 16], then only y[16] - x[16]@beta[16].
I think if you use pandas the second type are very easy to get
Thanks @bashtage for clarifying that the ambiguity around what .resid would refer to in a rolling model is why there is no implementation. I will close this as a result, with the understanding that if a user wants residuals based on the second approach you describe they can extract them as you demonstrate above.
Has there been any further implementation for the second case (set of residuals for each regression run) in rollingOLS? I would need them to compute the standard deviations of the residuals in each run.
Rolling residual variance estimators should be easy to compute along side other rolling quantities. Since these would be a 1d array and well-defined, this doesn't create the issue that rolling residuals do.
Yes, storing a 1d-array of residual standard deviations should be easy enough, but I was wondering if there was an easy way to access them via the attributes without having to fork the function.
Would be great if I could work on this, given your 2022 comment on "adding a function that would handle the obvious cases".
Here is my proposal of adding get_resid(out_of_sample=False) method on rolling results that calculates residuals on request, addressing your concerns in #6986 about storing.
It takes into account two cases. The default is to calculate y[t] - x[t] @ params[t] using coefficients from the window ending with t (and including t itself), following your second definition from 2021. When out_of_sample=True, one can calculate one-step ahead error: y[t] - x[t] @ params[t-1], again using coefficients from the previous window.
Since it utilizes the design matrix of the model, it works well with from_formula and categoricals (workaround y - (params * x).sum(1) doesn't work with from_formula according to 2022 report). Moreover, it provides unweighted residuals for RollingWLS (as WLS.resid) while preserving pandas index in case of pandas inputs. It produces np.nan for values before the window has reached sufficient number of observations (min_nobs) and for observations with np.nan value. In order not to allocate much memory, the method doesn't provide residuals matrix for all windows.
The feature is already implemented and tested on my fork, which has passed comparison with WLS fitted for each window and R's per-window lm().
Is the API fine or would you rather prefer another name/resid property instead?
Activity
matthewgilbert commentedon Jun 27, 2021
Re DOI, apologies. some more searching turned up the relevant text
Harvey, A. (1990). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge: Cambridge University Press. doi:10.1017/CBO9781107049994
As an aside, it seems like most texts, e.g. Durbin & Koopman, reference what I believe is the original edition from 1989 but the only reference I could find online was to the reprint in 1990.
ChadFulton commentedon Jun 28, 2021
I have not used rolling OLS extensively for anything myself, but I suppose that the appropriate residuals depend on what you plan to do with them.
For example, in your code you are computing residuals
e_iase_i = y_i - E[y_i | y_{i-1}, y_{i-2}, ..., y-{i-w}], where the window size isw. This is computing the residual for each datapointiusing the regression based on observationiand the previousw-1observations.But there is no particular reason you couldn't compute them as, for example, the datapoint
iand the subsequentw-1observations,e_i = y_i - E[y_i | y_{i+1}, y_{i+2}, ..., y-{i+w}].ChadFulton commentedon Jun 28, 2021
Thanks, this is a good point, and in any case we should provide the reference in the file where we cite it.
bashtage commentedon Jun 28, 2021
I implemented
RollingOLS. I wasn't sure whatresidualsshould look like since there are at least 2 different definitions. 1. A set of T-window+1 set of residuals for each regression run, or two a single set of rolling residuals that would only include the last observation in the residual, e.g., if using observations [12, 13, 14, 15, 16], then only y[16] - x[16]@beta[16].I think if you use pandas the second type are very easy to get
matthewgilbert commentedon Jun 28, 2021
Thanks @bashtage for clarifying that the ambiguity around what
.residwould refer to in a rolling model is why there is no implementation. I will close this as a result, with the understanding that if a user wants residuals based on the second approach you describe they can extract them as you demonstrate above.bashtage commentedon Jun 28, 2021
Code is pretty easy, and now this is documented.
robertmartin8 commentedon Jun 23, 2022
For people using
RollingOLS.from_formula, this won't work because ofInterceptand categorical encodings.The workaround is to first transform your data using
patsy.Given some
mod_str = "y ~ x1 + C(x2)"and your dataframedata:bashtage commentedon Jun 28, 2022
I think that we should probably add a function that would handle the obvious cases.
[-]Residuals for RollingOLS[/-][+]ENH: add a function to compute residuals for RollingOLS[/+]AnthonyTremblayy commentedon Apr 9, 2024
Has there been any further implementation for the second case (set of residuals for each regression run) in rollingOLS? I would need them to compute the standard deviations of the residuals in each run.
bashtage commentedon Apr 9, 2024
Rolling residual variance estimators should be easy to compute along side other rolling quantities. Since these would be a 1d array and well-defined, this doesn't create the issue that rolling residuals do.
AnthonyTremblayy commentedon Apr 10, 2024
Yes, storing a 1d-array of residual standard deviations should be easy enough, but I was wondering if there was an easy way to access them via the attributes without having to fork the function.
shan3520 commentedon Oct 5, 2026
Would be great if I could work on this, given your 2022 comment on "adding a function that would handle the obvious cases".
Here is my proposal of adding get_resid(out_of_sample=False) method on rolling results that calculates residuals on request, addressing your concerns in #6986 about storing.
It takes into account two cases. The default is to calculate y[t] - x[t] @ params[t] using coefficients from the window ending with t (and including t itself), following your second definition from 2021. When out_of_sample=True, one can calculate one-step ahead error: y[t] - x[t] @ params[t-1], again using coefficients from the previous window.
Since it utilizes the design matrix of the model, it works well with from_formula and categoricals (workaround y - (params * x).sum(1) doesn't work with from_formula according to 2022 report). Moreover, it provides unweighted residuals for RollingWLS (as WLS.resid) while preserving pandas index in case of pandas inputs. It produces np.nan for values before the window has reached sufficient number of observations (min_nobs) and for observations with np.nan value. In order not to allocate much memory, the method doesn't provide residuals matrix for all windows.
The feature is already implemented and tested on my fork, which has passed comparison with WLS fitted for each window and R's per-window lm().
Is the API fine or would you rather prefer another name/resid property instead?
ENH: Add on-demand residuals to rolling regression results