Skip to content

Attach dates to VAR forecasts #3531

Closed
Closed

Description

@jbrockmendel

VARProcess.forecast currently returns an np.ndarray without any labelling.

In many cases data.row_labels will be a pd.DatetimeIndex. If it has either a freq or inferred_freq that is non-None, then we can extend that index forward and return the forecast as a DataFrame.

I've thrown together a quick version of this but want to get input before making a full-fledged PR. See attached.
var_with_dates.txt

Activity

  1. josef-pkt commented on Mar 3, 2017

    @josef-pkt
    Member

    main problem: I have no clue about this. :(

    @ChadFulton refactored and modernized the date and related index handling for tsa, but VAR hasn't been touched yet.
    We have VECM as a major enhancement that is similar to VAR and needs to go in ASAP #3246
    (VECM PR is focused on the algorithms and estimators and might or does not handle any user friendly features like indices. Those need to get a generic tsa pattern for the entire VAR type category.)
    VAR also has a stalled refactoring PR #3028 (#815)

    specifically:
    I don't know whether the proposal could be merged without problems with other patterns and PRs or not.
    Your _get_forecast_index should have an equivalent in the current ARMA and statespace models, which are supposed to handle date and time index correctly if available.

    (I can also look into it, but it will be at least a week because spring break for kids is coming up)

    @ChadFulton and @yogabonito will be more up-to-date than I am.

  2. josef-pkt commented on Mar 3, 2017

    @josef-pkt
    Member

    again: I fully agree with the objective, and VAR needs better datetime handling, but I'm not familiar enough with the implementation details to evaluate this.

  3. ChadFulton commented on Mar 4, 2017

    @ChadFulton
    Member

    tl;dr

    I think if you override forecast in VARResults so that it doesn't require the user to pass in data and then add 'forecast': 'dates', to the _methods dictionary in VARResultsWrapper it should work (but probably there are some fiddly things that won't work right away even so).


    I also have to admit that I am not familiar with all the details here, although in my case I don't have much familiarity with what's going on in var_model.py, and Josef has mentioned that there are PRs which make improvements, but I haven't gone through them. Since I'm starting to go through the VECM PR, I'll be learning more about our VAR models at the same time.

    I think this question is better directed at VARResults, since I think VARProcess is meant to be data-agnostic, and also it doesn't have access to a model object from which to get e.g. model.data.row_labels or model.endog_names.

    The first thing I notice is that, as far as I can tell, VARResults really should override the forecast method (and forecast_interval?), because it looks like users would currently have to pass res.forecast(res.y, steps) to get forecasts at all (rather than e.g res.forecast(steps) as in most of our results classes).

    The second thing is that usually this kind of thing is meant to be handled by the wrapper classes (see e.g. statsmodels/base/data.py:PandasData.attach_rows). I think this should work with forecasts, even with k_endog > 1, because it's the way I do it in statespace.MLEModel and that can handle k_endog > 1. To do this, I think you add 'forecast': 'dates', to the _methods dictionary in VARResultsWrapper.

    The last thing is that the wrapper system is not currently very robust to multiple endogenous variables or to forecasting. For the first problem, you can see that in statsmodels/tsa/statespace/mlemodel.py:PredictionResults.conf_int I just create a DataFrame manually, because in that case I need to create lower_<endog_name> and upper_<endog_name> columns for each endogenous variable, and the wrapper classes can't handle that.

    The problem with forecasting in the wrapper classes is that they only work if the following attributes are set

    model.data.predict_start
    model.data.predict_end
    model.data.predict_dates

    these were originally an integral part of the date / time handling in TimeSeriesModel (which caused a number of issues) but with the recent date refactoring they are no longer required / used for anything in TimeSeriesModel except that they are set for backwards compatibility with the wrapper classes. But we prefer not to use global state in the model class, so this is something that I think should be refactored / fixed.

    Now that we have a lot of the complexity in the date / time handling offloaded to Pandas, it should be easier to refactor the wrappers to be better able to handle general time series models. It's on my list, eventually, but please (!) feel free to beat me to it :)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions