Skip to content

Implement WalkForward cross-validator for time series data. #14376

Closed

Description

@saninstein

Description

Implement the walk forward cv for time series data with gap between the train set and the test set.

Expected Results

image

Expanding
image

Activity

  1. changed the title [-]Implement WalkForward cross-validator for time series with gap.[/-] [+]Implement WalkForward cross-validator for time series data.[/+] on Jul 15, 2019
  2. clstaudt commented on Jul 15, 2019

    @clstaudt

    @saninstein Why the gap?

  3. ksanderer commented on Jul 22, 2019

    @ksanderer

    @clstaudt gap is useful feature for models evaluation in stock trading. In price prediction it's very common case when model performs very well on the data right after training set and degrades over time. So, in some cases it can be useful to skip a little amount of "good" data that occurred after training set.

  4. adrinjalali commented on Jul 22, 2019

    @adrinjalali
    Member

    I think our usual position on time-series related features is that it's out of scope for sklearn (at least for now). And to me, it would make sense to revisit the matter once we have sample properties such as timestamp attached to the data.

    I'd like to have at least one other opinion from @scikit-learn/core-devs on this, but my vote is a "won't fix" resolution for now.

  5. GaelVaroquaux commented on Jul 22, 2019

    @GaelVaroquaux
    Member
  6. ksanderer commented on Jul 22, 2019

    @ksanderer

    @adrinjalali there is TimeSeriesSplit which already implemented in sklearn :)

    TimeSeriesSplit is specific case of proposed WalkForwardCV (can be achieved with gap=0, expanding=True args), actually TimeSeriesSplit can be easily replaced with WalkForwardCV (just set default args as I mentioned before).

    I'm part of the stock trading ML team and we successfully using various sklearn features and sklearn compatible libraries. There was lack of proper CV splitter and we decided to contribute it back to sklearn, but if this is not the case we will use it internally :)

  7. adrinjalali commented on Jul 22, 2019

    @adrinjalali
    Member

    TimeSeriesSplit is specific case of proposed WalkForwardCV (can be achieved with gap=0, expanding=True args), actually TimeSeriesSplit can be easily replaced with WalkForwardCV (just set default args as I mentioned before).

    Fair, but then I'd probably try to patch TimeSeriesSplit in a backward compatible way to add the feature you need. That also has a higher chance of being accepted by the community.

  8. ksanderer commented on Jul 22, 2019

    @ksanderer

    Timeseries prediction not the only case for WalkForwardCV. It useful when dataset observation is ordered in time and validation need to be done in the same way.

    For example one of our task is loosing trades filtering classification problem when we trying to improve existing trading strategy with ml model wich will "give permission" to trade.


    Fair, but then I'd probably try to patch TimeSeriesSplit in a backward compatible way to add the feature you need. That also has a higher chance of being accepted by the community.

    I believe it would be the best solution.

  9. clstaudt commented on Jul 22, 2019

    @clstaudt

    I am a data scientist working on time series forecasting models. I assumed time series-specific things are out of scope for scikit-learn, so I started to implement my own validation code, slightly different from the method described in this thread.

    Later I learned of the existence of TimeSeriesSplit and wondered whether I have started to reinvent the wheel. I would prefer to contribute the validation code to an existing, established project.

    Since there is clearly a demand for this kind of model evaluation, I still wonder where it fits.

  10. GaelVaroquaux commented on Jul 22, 2019

    @GaelVaroquaux
    Member
  11. clstaudt commented on Jul 22, 2019

    @clstaudt

    It would be useful to create a package that implements all these in a consistent way. It would probably pick up momentum.

    Yes. Sign me up.

  12. adrinjalali commented on Jul 22, 2019

    @adrinjalali
    Member
  13. amueller commented on Jul 23, 2019

    @amueller
    Member

    I think adding a better time-series cross-validation is in scope.

  14. amueller commented on Jul 23, 2019

    @amueller
    Member
  15. jnothman commented on Jul 23, 2019

    @jnothman
    Member

    Re scope, I agree with @amueller that we should be open to extending this to common use-cases. Basically, we generally assume in scikit-learn estimators (i.e. sklearn package) that the model should be more-or-less invariant to sample order and feature order. This excludes time series estimators. However, we do not have this constraint in cross validation splitters where we have long considered sample order something to pay attention to; ultimately, cross validation is where the core assumptions around ML lie.

    But as @amueller also points out, really the conversation should be continued in the existing pull requests, moving them towards an agreeable state.

  16. adrinjalali commented on Jul 23, 2019

    @adrinjalali
    Member

    Sure, but I'm wary of cases where the actual timestamp of the data should matter in the split (which IMO it should), and not the mere count of the rows. I don't think we're going to handle the timestamps anytime soon, are we?

  17. mjbommar commented on Nov 3, 2019

    @mjbommar
    Contributor

    In the 5 years since I first proposed this in #3202, this question has come up at least 50 times in conversations teaching or applying. @saninstein , did you make a decision about whether to push for inclusion here or -contrib? I would love to help if there is anything you need assistance with to get this over the line (somewhere).

  18. svenstehle commented on Dec 23, 2019

    @svenstehle
    Contributor

    I would also like to contribute. Wrote in another issue about this and would like to expand on TimeSeriesSplit or collaborate on creating another package for that. I feel this is something that is related to splits in the CV domain and should be in sklearn.
    To be honest though I am completely confused as to where to go and what to do now that I want to contribute. I am mindful of my time and I would like to use it in the right way for the community.

  19. amueller commented on Dec 23, 2019

    @amueller
    Member

    @mjbommar I think as I and @jnothman above say, we are quite open in moving forward and there are some exiting PRs, in particular #13761 and #13204 and feedback on the two APIs would be much appreciated.

    I think #13204 looks to be the most mature so maybe going from there makes the most sense? I'm not sure if @kykosic is still working on it, given the delay in our response?

  20. amueller commented on Dec 23, 2019

    @amueller
    Member

    Hm though #13204 doesn't implement WalkForward... Do we want to merge #13204 first and then implement WalkForward later?
    Should that be a separate CV object?

  21. kykosic commented on Dec 23, 2019

    @kykosic
    Contributor

    @amueller I had forgotten about #13204 until this post came up. I will address the reviews on it over the next week and see if it still fits in.

  22. amueller commented on Dec 23, 2019

    @amueller
    Member

    @kykosic awesome, thanks!

  23. ManuelZ commented on Jul 24, 2020

    @ManuelZ

    Curious about why was this issue closed because of #13204 being finished. I thought that #13204 was a pre-requisite for this one.

  24. thomasjpfan commented on Jul 24, 2020

    @thomasjpfan
    Member

    #13204 added gap to TimeSeriesSplit that was the feature requested by this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs DecisionNew Feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions