我试图绘制状态模型的AutoRegResults的剩余值,但results.resid
只在调用该方法时才返回NaN
。但是,当我调用plot_diagnostics()
时,它能够没有问题地绘制正则化的残差。我怎样才能得到实际的残差?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.ar_model import AutoReg
import warnings
df=pd.read_csv('Bank_of_England_Database.csv',
sep=',',
parse_dates=["Date"],
dayfirst=True,
index_col="Date")
df.rename({list(df.columns)[-1] : 'Spot Exchange Rate'},
axis='columns',
inplace=True)
df['RW 11'] = df.rolling(window=11, min_periods=11, center=True).mean()
xbar = df['Spot Exchange Rate'].mean()
df['demean'] = df['Spot Exchange Rate'] - xbar
fig = plt.figure()
fig.suptitle("AR(p) Residuals")
lags = [1] #, 2, 3]
for lag in lags:
warnings.filterwarnings("ignore") # Stops a FutureWarning and ValueWarning about dates
model = AutoReg(df['demean'], lags=lag)
results = model.fit()
resid = results.resid # Returns NaN
print(resid.head())
plt.plot(df.index[lag:], resid, label=f"lag={lag}")
results.plot_diagnostics()
plt.show()
Date
2015-05-01 NaN
2015-05-05 NaN
2015-05-06 NaN
2015-05-07 NaN
2015-05-08 NaN
dtype: float64
No handles with labels found to put in legend.
编辑
更新后的代码显示同一版本:
import pandas as pd
import numpy as np
from statsmodels.tsa.api import AutoReg
import statsmodels as sm
import matplotlib.pyplot as plt
print(f"statsmodel version: {sm.__version__}")
df=pd.read_csv('Bank_of_England_Database.csv',
sep=',',
parse_dates=["Date"],
dayfirst=True,
index_col="Date")
df.rename({list(df.columns)[-1] : 'Spot Exchange Rate'},
axis='columns',
inplace=True)
df['demean'] = df['Spot Exchange Rate'] - df['Spot Exchange Rate'].mean()
res = AutoReg(df['demean'], lags=2).fit()
results.plot_diagnostics()
print(f"All NaN: {np.isnan(res.resid).all()}")
plt.show()
结果:
statsmodel version: 0.12.2
/usr/local/lib/python3.9/site-packages/statsmodels/tsa/base/tsa_model.py:581: ValueWarning: A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting.
warnings.warn('A date index has been provided, but it has no'
/usr/local/lib/python3.9/site-packages/statsmodels/tsa/ar_model.py:248: FutureWarning: The parameter names will change after 0.12 is released. Set old_names to False to use the new names now. Set old_names to True to use the old names.
warnings.warn(
All NaN True
发布于 2021-03-11 08:35:50
使用状态模型0.12.2似乎不可能重现此问题。
from statsmodels.tsa.arima_process import ArmaProcess
from statsmodels.tsa.api import AutoReg
import numpy as np
y = ArmaProcess.from_coeffs([1.8,-0.9]).generate_sample(250)
res = AutoReg(y,lags=2,old_names=False).fit()
print(f"All finite (no nan/inf): {np.all(np.isfinite(res.resid))}")
# Try a Series
ys = pd.Series(y, index=pd.date_range("1900-1-1",periods=250,freq="M"))
res = AutoReg(ys,lags=2,old_names=False).fit()
print(f"All finite using Series (no nan/inf): {np.all(np.isfinite(res.resid))}")
# Try a Series with no freq
idx = pd.date_range("1900-1-1",periods=750,freq="M")
uneven_idx = sorted(np.random.default_rng().choice(idx, size=250, replace=False))
ys = pd.Series(y, index=uneven_idx)
res = AutoReg(y,lags=2,old_names=False).fit()
print(f"All finite using Series w/o freq (no nan/inf): {np.all(np.isfinite(res.resid))}")
这会产生
All finite (no nan/inf): True
All finite using Series (no nan/inf): True
All finite using Series w/o freq (no nan/inf): True
可能需要升级到最新版本。
https://stackoverflow.com/questions/66523923
复制相似问题