Prophet文档中文翻译--diagnostics

%matplotlib inline
from fbprophet import Prophet
import pandas as pd
from matplotlib import pyplot as plt
import logging
logging.getLogger('fbprophet').setLevel(logging.ERROR)
import warnings
warnings.filterwarnings("ignore")
df = pd.read_csv('examples/example_wp_log_peyton_manning.csv')
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=366)

Prophet包括时间序列交叉验证功能,以使用历史数据测量预测误差。这是通过选择历史中的截止点来完成的,并且对于每个截止点,使用截止点之前的数据拟合模型。然后我们可以将预测值与实际值进行比较。该图说明了Peyton Manning数据集的模拟历史预测,其中该模型用5年历史数据训练模型,并且在接下来的一年的时间范围内进行预测。

from fbprophet.diagnostics import cross_validation
df_cv = cross_validation(
    m, '365 days', initial='1825 days', period='365 days')
cutoff = df_cv['cutoff'].unique()[0]
df_cv = df_cv[df_cv['cutoff'].values == cutoff]

fig = plt.figure(facecolor='w', figsize=(10, 6))
ax = fig.add_subplot(111)
ax.plot(m.history['ds'].values, m.history['y'], 'k.')
ax.plot(df_cv['ds'].values, df_cv['yhat'], ls='-', c='#0072B2')
ax.fill_between(df_cv['ds'].values, df_cv['yhat_lower'],
                df_cv['yhat_upper'], color='#0072B2',
                alpha=0.2)
ax.axvline(x=pd.to_datetime(cutoff), c='gray', lw=4, alpha=0.5)
ax.set_ylabel('y')
ax.set_xlabel('ds')
ax.text(x=pd.to_datetime('2010-01-01'),y=12, s='Initial', color='black',
       fontsize=16, fontweight='bold', alpha=0.8)
ax.text(x=pd.to_datetime('2012-08-01'),y=12, s='Cutoff', color='black',
       fontsize=16, fontweight='bold', alpha=0.8)
ax.axvline(x=pd.to_datetime(cutoff) + pd.Timedelta('365 days'), c='gray', lw=4,
           alpha=0.5, ls='--')
ax.text(x=pd.to_datetime('2013-01-01'),y=6, s='Horizon', color='black',
       fontsize=16, fontweight='bold', alpha=0.8);

Prophet文档中文翻译--diagnostics

Prophet论文进一步描述了模拟历史预测。

可以使用cross_validation函数自动为一系列历史截止值完成此交叉验证过程。我们指定预测范围(horizon),然后可选地指定初始训练期(initial)的大小和截止日期(period)之间的间隔。默认情况下,初始训练周期设置为预测范围的三倍,并且每半个预测周期进行一次截止。

cross_validation的输出是一个数据框,其中包含真实值y和在每一个截止点(cutoff)对应的每一个预测值yhat及其置信区间(原句:The output of cross_validation is a dataframe with the true values y and the out-of-sample forecast values yhat, at each simulated forecast date and for each cutoff date)。特别是,对截止点(cutoff)和截止点+预测范围之间的每个观察点进行预测。然后可以使用该数据帧来计算yhaty的误差测量值。

在这里,我们进行交叉验证,以评估365天的预测性能,用730天的训练数据然后设置第一个截止点,然后每180天进行一次预测。在这8年的时间序列中,这相当于11个总预测。

from fbprophet.diagnostics import cross_validation
df_cv = cross_validation(m, initial='730 days', period='180 days', horizon = '365 days')
df_cv.head()
ds yhat yhat_lower yhat_upper y cutoff
0 2010-02-16 8.954694 8.449136 9.449550 8.242493 2010-02-15
1 2010-02-17 8.721064 8.199664 9.233732 8.008033 2010-02-15
2 2010-02-18 8.604730 8.131016 9.128100 8.045268 2010-02-15
3 2010-02-19 8.526543 8.039653 9.028587 7.928766 2010-02-15
4 2010-02-20 8.268414 7.820673 8.787285 7.745003 2010-02-15

在Python中,initialperiodhorizon的字符串应采用Pandas Timedelta使用的格式,该格式接受天或更短的单位。

performance_metrics方法可用于计算预测性能的一些有用统计数据(yhatyhat_loweryhat_uppery作比较),这个函数计算从截止点开始包含整个预测范围的的统计量(原句:The performance_metrics utility can be used to compute some useful statistics of the prediction performance (yhat, yhat_lower, and yhat_upper compared to y), as a function of the distance from the cutoff (how far into the future the prediction was).)。计算的统计量是均方误差(MSE),均方根误差(RMSE),平均绝对误差(MAE),平均绝对误差(MAPE)以及yhat_loweryhat_upper估计的覆盖范围。这些是按范围排序(ds减去cutoff),然后在df_cv数据框中的滚动时间序列窗口计算统计量。默认情况下,每个窗口中都会包含10%的预测,但可以使用rolling_window参数进行更改。

from fbprophet.diagnostics import performance_metrics
df_p = performance_metrics(df_cv)
df_p.head()
horizon mse rmse mae mape coverage
3297 37 days 0.480620 0.693267 0.501430 0.058179 0.683417
35 37 days 0.479641 0.692561 0.500503 0.058069 0.685930
2207 37 days 0.479581 0.692518 0.500415 0.058063 0.685930
2934 37 days 0.480096 0.692889 0.501474 0.058197 0.685930
393 37 days 0.482679 0.694751 0.501900 0.058299 0.685930

可以使用plot_cross_validation_metric方法显示交叉验证性能指标,此处显示为MAPE。点显示df_cv中每个预测的绝对百分比误差。蓝线显示MAPE,其中平均值取自点的滚动窗口。我们看到这个预测,对于未来一个月的预测,大约5%的典型的误差,并且对于一年的预测,误差增加到大约11%。

from fbprophet.plot import plot_cross_validation_metric
fig = plot_cross_validation_metric(df_cv, metric='mape')

Prophet文档中文翻译--diagnostics

可以使用可选参数rolling_window更改图中滚动窗口的大小,该参数指定在每个滚动窗口中使用的预测比例。默认值为0.1,对应于每个窗口中包含的df_cv的10%行数据;增加这将导致图中平均曲线更平滑。

initial阶段应该足够长,以捕获模型的所有组成部分,特别是季节性和额外的回归量:年季节性至少一年数据,周季节性至少一周数据。