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
There are several alternatives for the linear algebra with trade-offs for speed and numerical stability.
expanding window, i.e. recursive OLS uses now the kalman filter.
Compared to recursive estimation, we have the problem of downdating the observation that we drop as the window moves.
Another alternative (just an idea right now while looking the the recursive estimation formulas):
We can store the updating change in the inverse moment matrix and remove it again when we move the window, i.e. instead of calculating a downdating step, we just keep the updating step around for reuse.
(In the SUR (e.g. VAR or contemporaneous correlation) case we can also recursively update the cov_resid matrix. formula in PcGive documentation 2 chapter 11 p.142 for system estimation. )
and another look at the code:
It looks like pandas is doing three loops through the period/nobs to get the basic results
params: based on cumulative x'x
rolling_rank to get the df
_var_beta_raw to get the cov_params, using optionally HAC/Newey-West
some of the other results can be computed in a vectorized way from above, but there are additional results that use again a loop
_resid_stats uses a loop to compute sum of squares, ssr, sst used for example for rsquared
Going 3, 4 or more times through the nobs loop might not save much compared to doing the full OLS loop, assuming we want to get more results than the minimal estimate.
One possibility might then be to do a hybrid
To a plain OLS loop to store some basic statistics, then use the existing methods from pandas to compute the results that can be computed in the vectorized form based on those statistics.
(With cov_type nonrobust we should be able to compute more from the (cumulative) cross-products x'x for each observation observation, but with robust cov_type, including newey-west, we need the extra computation based on each window, and we need to get the residuals for each window.
I just hit this when porting some code, and it's going to be more of an issue down the road as codebases gradually start migrating up to modern pandas. Is there anything project outsiders can do to help?
(I still haven't used pandas moving ols, but have a better idea now after browsing the code.)
The relatively cheap way would be to take the entire module or class and replace extra dependencies (like pandas.math with more generic numpy or statsmodels functions but keep to looping logic without essential changes, plus adjust some API conventions/namings to statsmodels.
The larger solution would be to refactor and reimplement some things the avoids repeated loops, possibly op for the user (e.g. the user needs to decide in advance if robust standard errors like HAC/Newey-West are needed). This would most likely require checking the timing to see what is really worth it. In this case the overall windowing logic could be kept from the pandas version, but with fewer loops for extra results.
An alternative would be to write a single pass version where we compute an OLS for each window, but the user has to decide in advance which results should be kept. I think this would look more like the recipes/discussions on stackoverflow to reuse statsmodels OLS.
The first solution should work as a relatively quick replacement for what pandas had.
@josef-pkt I just did the "relatively cheap way" while working on #3866, and would be happy to submit a PR while I get started on actually refactoring it, if you think it's worth the trouble. What sort of API/naming changes would I have to make for this to be more statsmodels-like?
@jengelman Based on what I remember from looking at it in summer, I would think that the initial port could leave all the structure as is. The API would be better if it were consistent with our naming conventions, e.g. in OLS and RegressionResults.
Also unit tests against an explicit loop over OLS could verify that we have the same definitions and defaults in the moving version as in the simple OLS version. (I remember seeing some pandas issues for HAC/newey_west but never looked at those.) Unit tests to cross-check the two models would be relatively easy to write.
The overall pattern and usecase is quite different from our main models which only produce a single estimation result, so the structure will not be close to them, but it would be useful for consistency and lower learning cost for users if it would "feel" like a vectorized version of statsmodels.OLS.
An issue for future development, extensions and refactorings is to see whether it is possible to isolate the API a bit from the internal structure and computation, or to make the model/windowing structure general or modular enough to make code reuse for extensions easier. (As example, in our current models it was and is relatively easy to add new fit methods like fit_regularized or fit_constrained with relatively little extra effort and code.)
That's mostly pretty vague, but I would have to look at specific details to know more.
Activity
kshedden commentedon Mar 6, 2015
Link to code:
https://github.com/pydata/pandas/blob/1d94dd2ada7d0af57c245b083d26d60b8649a04e/pandas/stats/ols.py#L601
See also #2203
josef-pkt commentedon Aug 25, 2016
https://github.com/pydata/pandas/blob/master/pandas/stats/ols.py#L819
pandas moving window is using cumulative moment matrices, as discussed many years ago.
There are several alternatives for the linear algebra with trade-offs for speed and numerical stability.
expanding window, i.e. recursive OLS uses now the kalman filter.
Compared to recursive estimation, we have the problem of downdating the observation that we drop as the window moves.
Another alternative (just an idea right now while looking the the recursive estimation formulas):
We can store the updating change in the inverse moment matrix and remove it again when we move the window, i.e. instead of calculating a downdating step, we just keep the updating step around for reuse.
(In the SUR (e.g. VAR or contemporaneous correlation) case we can also recursively update the cov_resid matrix. formula in PcGive documentation 2 chapter 11 p.142 for system estimation. )
josef-pkt commentedon Jun 6, 2017
another stackoverflow question and link to code in old version https://stackoverflow.com/questions/44380068/pandas-rolling-regression-alternatives-to-looping
and another look at the code:
It looks like pandas is doing three loops through the period/nobs to get the basic results
df_var_beta_rawto get the cov_params, using optionally HAC/Newey-Westsome of the other results can be computed in a vectorized way from above, but there are additional results that use again a loop
Going 3, 4 or more times through the nobs loop might not save much compared to doing the full OLS loop, assuming we want to get more results than the minimal estimate.
One possibility might then be to do a hybrid
To a plain OLS loop to store some basic statistics, then use the existing methods from pandas to compute the results that can be computed in the vectorized form based on those statistics.
(With cov_type nonrobust we should be able to compute more from the (cumulative) cross-products x'x for each observation observation, but with robust cov_type, including newey-west, we need the extra computation based on each window, and we need to get the residuals for each window.
dsm054 commentedon Jun 9, 2017
I just hit this when porting some code, and it's going to be more of an issue down the road as codebases gradually start migrating up to modern pandas. Is there anything project outsiders can do to help?
josef-pkt commentedon Jun 9, 2017
(I still haven't used pandas moving ols, but have a better idea now after browsing the code.)
The relatively cheap way would be to take the entire module or class and replace extra dependencies (like pandas.math with more generic numpy or statsmodels functions but keep to looping logic without essential changes, plus adjust some API conventions/namings to statsmodels.
The larger solution would be to refactor and reimplement some things the avoids repeated loops, possibly op for the user (e.g. the user needs to decide in advance if robust standard errors like HAC/Newey-West are needed). This would most likely require checking the timing to see what is really worth it. In this case the overall windowing logic could be kept from the pandas version, but with fewer loops for extra results.
An alternative would be to write a single pass version where we compute an OLS for each window, but the user has to decide in advance which results should be kept. I think this would look more like the recipes/discussions on stackoverflow to reuse statsmodels OLS.
The first solution should work as a relatively quick replacement for what pandas had.
jengelman commentedon Aug 15, 2017
@josef-pkt I just did the "relatively cheap way" while working on #3866, and would be happy to submit a PR while I get started on actually refactoring it, if you think it's worth the trouble. What sort of API/naming changes would I have to make for this to be more statsmodels-like?
josef-pkt commentedon Aug 15, 2017
@jengelman Based on what I remember from looking at it in summer, I would think that the initial port could leave all the structure as is. The API would be better if it were consistent with our naming conventions, e.g. in OLS and RegressionResults.
Also unit tests against an explicit loop over OLS could verify that we have the same definitions and defaults in the moving version as in the simple OLS version. (I remember seeing some pandas issues for HAC/newey_west but never looked at those.) Unit tests to cross-check the two models would be relatively easy to write.
The overall pattern and usecase is quite different from our main models which only produce a single estimation result, so the structure will not be close to them, but it would be useful for consistency and lower learning cost for users if it would "feel" like a vectorized version of statsmodels.OLS.
An issue for future development, extensions and refactorings is to see whether it is possible to isolate the API a bit from the internal structure and computation, or to make the model/windowing structure general or modular enough to make code reuse for extensions easier. (As example, in our current models it was and is relatively easy to add new fit methods like fit_regularized or fit_constrained with relatively little extra effort and code.)
That's mostly pretty vague, but I would have to look at specific details to know more.
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS
ENH: Add rolling WLS and OLS