Skip to content

Exogenous variable restarts from index 0 when crossing last in-sample date #2323

Closed

Description

@cstefansen

When predicting over a period that includes in-sample dates followed by out-of-sample dates, predict goes back to the first index in the exogenous variable when crossing the last in-sample date.

Expected behavior: respect the time/dates in the exogenous variable; don't start from 0 in the middle of the prediction.

Reproducer (iPython Notebook):

%matplotlib inline
import matplotlib.pyplot as plt
import numpy
import numpy as np
import pandas
import statsmodels.api as sm
numpy.set_printoptions(linewidth=120)

fit_start_date = '2012-01-01'
fit_end_date = '2014-06-29' 
predict_end_date = '2014-12-28'

# Define an exogenous variables.
date_index = pandas.date_range(fit_start_date, predict_end_date, freq='W-SUN')
season_indicator = [abs(x.timetuple().tm_yday - 182) / 182.0 for x in date_index]
exog = pandas.TimeSeries(season_indicator, index=date_index)

# Define the endogenous variable as a scaled and noisy version of the same thing, up to fit_end_date.
np.random.seed(0)  # Keep noise the same between runs.
noise = pandas.Series(np.abs(np.random.randn(len(date_index))), index=date_index).div(3.0)
endog = pandas.Series(season_indicator, index=date_index).mul(2.0).pow(2.0).add(noise)[:fit_end_date]

arma = sm.tsa.ARMA(endog=endog, exog=exog[:fit_end_date],
                    order=(2, 0), freq='W')
model = arma.fit(disp=False)

# Predictions crossing over fit_end_date
ar_predictions = model.predict(
        start=fit_start_date,
        end=predict_end_date,
        exog=exog)

# A workaround to get expected values.
ar_predictions_workaround = model.predict(
        start=fit_end_date,
        end=predict_end_date,
        exog=exog[fit_end_date:predict_end_date])

# Print exog, endog, actual predictions, and workaround (expected) predictions.
plt.figure(figsize=(16,5))
plt.plot(endog.index, endog, 'g', label='Endogenous')
plt.plot(exog.index, exog, 'm', label='Exogenous')
plt.plot(ar_predictions.index, ar_predictions, 'rx-', label='Actual prediction')
plt.plot(ar_predictions_workaround.index, ar_predictions_workaround, 'kx-', label='Expected prediction')
plt.legend(loc='upper center')

print 'StatsModels version:', sm.version.full_version
print model.params
print model.mle_retvals

Output:

StatsModels version: 0.6.1
const     -0.405181
x1         4.142397
ar.L1.y    0.584564
ar.L2.y    0.198743
dtype: float64
{'gopt': array([  4.30211422e-08,   2.22044605e-08,  -4.30211422e-08,   0.00000000e+00]), 'fcalls': 78, 'converged': True, 'fopt': 0.08108206616357157, 'warnflag': 0}

Also see attached graph.
screenshot from 2015-03-16 14 52 26

Activity

  1. added this to the 0.8 milestone on Jun 22, 2016
  2. josef-pkt commented on Jun 22, 2016

    @josef-pkt
    Member

    I don't know why this ticket got lost, without label and milestone.

    It's likely that this was the ARMA predict with exog bug fixed for 0.7.
    TODO: check if this is still broken

  3. bashtage commented on Jul 17, 2019

    @bashtage
    Member

    This is still broken.

  4. self-assigned this
    on Jul 23, 2019
  5. added a commit that references this issue on Jul 24, 2019
    d9f4b2b
  6. modified the milestones: 0.8, 0.11 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