Repository navigation
Closed
Description
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}

Activity
josef-pkt commentedon Jun 22, 2016
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
bashtage commentedon Jul 17, 2019
This is still broken.
BUG/ENH: Check exog in ARMA and ARIMA predict