ARIMA(1,1,1)拟合股价数据出现SARIMAX结果及绘图异常问题咨询
问题1:ARIMA模型输出SARIMAX摘要的解释
在statsmodels库中,ARIMA类底层是基于SARIMAX实现的——ARIMA本质是SARIMAX的特例(无季节性参数,即季节性order为(0,0,0))。所以输出的摘要标题显示"SARIMAX"是正常现象,不影响模型本身是ARIMA(1,1,1)的属性。你可以查看摘要中的**Model:**字段,会标注ARIMA(1,1,1),同时参数部分只会显示AR(1)和MA(1)的系数,没有季节性相关参数,这就说明你拟合的确实是ARIMA模型。
问题2:plot_predict只显示水平线的解决方法
出现水平线的核心原因是你将DataFrame的索引转换为了Period类型(to_period('B')),而plot_predict对Period索引的处理存在兼容性问题,导致x轴刻度异常,视觉上呈现水平线。可以通过以下两种方式解决:
方法1:拟合模型前不转换为Period索引
如果你的原始索引已经是datetime类型,直接去掉转Period的步骤即可:
from statsmodels.tsa.arima.model import ARIMA from statsmodels.graphics.tsaplots import plot_predict import matplotlib.pyplot as plt # 保留原datetime索引,不转Period model = ARIMA(dataset_for_prediction['Adj Close'], order=(1,1,1)) model_fit = model.fit() print(model_fit.summary()) plot_predict(model_fit) plt.xticks(rotation=45) plt.show()
方法2:保留Period索引,绘图时转换为datetime
如果必须使用Period索引(比如业务需求),可以在绘图时手动将x轴的Period刻度转换为datetime:
from statsmodels.tsa.arima.model import ARIMA from statsmodels.graphics.tsaplots import plot_predict import matplotlib.pyplot as plt dataset_for_prediction.index = dataset_for_prediction.index.to_period('B') model = ARIMA(dataset_for_prediction['Adj Close'], order=(1,1,1)) model_fit = model.fit() print(model_fit.summary()) # 绘图并转换x轴索引 fig, ax = plt.subplots() plot_predict(model_fit, ax=ax) # 将Period类型的刻度转为datetime ax.set_xticklabels([tick.to_timestamp() for tick in ax.get_xticks()]) plt.xticks(rotation=45) plt.show()
或者你也可以手动提取实际值和预测值,转换索引后绘图,更灵活:
# 提取数据并转换索引 actual = dataset_for_prediction['Adj Close'] pred = model_fit.predict(start=0, end=len(actual)-1) actual.index = actual.index.to_timestamp() pred.index = pred.index.to_timestamp() # 绘制对比图 plt.plot(actual, label='实际股价') plt.plot(pred, label='预测股价') plt.legend() plt.xticks(rotation=45) plt.show()
内容的提问来源于stack exchange,提问作者learningmathematics
相关产品推荐
相关产品推荐

