Skip to content

ENH: Migrate pandas MovingOLS #2302

Closed

Description

@jseabold

Part of moving the pandas stats stuff to statsmodels to support.

see also #2745

Activity

  1. josef-pkt commented on Aug 25, 2016

    @josef-pkt
    Member

    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. )

  2. josef-pkt commented on Jun 6, 2017

    @josef-pkt
    Member

    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

    • 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.

  3. dsm054 commented on Jun 9, 2017

    @dsm054

    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?

  4. josef-pkt commented on Jun 9, 2017

    @josef-pkt
    Member

    (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.

  5. jengelman commented on Aug 15, 2017

    @jengelman

    @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?

  6. josef-pkt commented on Aug 15, 2017

    @josef-pkt
    Member

    @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.

  7. self-assigned this
    on Jul 26, 2019
  8. added 14 commits that reference this issue on Jul 28, 2019
    9b0daf9
    42eb005
    fda12b6
    07cab65
    2393f2f
    437f610
    3164b29
    dcad05b
    ee662e6
    79958cd
    3b32961
    592e64b
    fa0c54c
    82377ac
  9. added this to the 0.11 milestone on Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions