我将 ETS 模型应用于该时间序列。我需要拟合值的标准差和置信区间。
import os
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
from statsmodels.tsa.exponential_smoothing.ets import ETSModel
df_air = pd.read_csv('https://raw.githubusercontent.com/AileenNielsen/TimeSeriesAnalysisWithPython/master/data/AirPassengers.csv',
parse_dates=['Month'],
date_parser=lambda x: pd.to_datetime(x, format='%Y-%m', errors = 'coerce'))
df_air = df_air.set_index('Month')
data = df_air['#Passengers']
model_mul = ETSModel(data, error="mul", trend="mul", seasonal="mul", damped_trend=True, seasonal_periods=12)
fit_mul = model_mul.fit(maxiter=10000)
print(fit_mul.fittedvalues)
如何获得拟合值的标准差和置信区间?
在文档中,您可以使用函数
get_prediction()
获取模型预测以及置信区间。
pred = fit_mul.get_prediction()
df = pred.summary_frame(alpha=0.05)
print(df.head())
# mean mean_numerical pi_lower pi_upper
#1949-01-01 113.241964 113.306568 105.861134 121.671326
#1949-02-01 111.889290 111.871512 104.055665 119.540671
#1949-03-01 134.951988 135.038488 125.197954 143.861504
#1949-04-01 130.773432 130.740944 121.976745 139.822335
#1949-05-01 131.279296 131.257138 121.549210 140.543643
print(fit_mul.sse)
# 0.19104403079657817
data.plot(label="data")
fit_mul.fittedvalues.plot(label="statsmodels fit")
pred.predicted_mean.plot(label="predicted mean")
df["mean"].plot(label="mean prediction")
df["pi_lower"].plot(linestyle="--", color="tab:blue", label="95% interval")
df["pi_upper"].plot(linestyle="--", color="tab:blue", label="_")
plt.legend()
plt.show()