默认情况下,Prophet 适合加法季节性,即可以将季节性的影响添加到趋势项中。
但有的情况下,时间序列有一个明显的年周期,但是预测的季节性波动较大,可能在时间序列开始时太大而结束时太小。 于是在这种情况下,季节性并不是 Prophet 中假设的恒定附加因素,而是随趋势增长。这就体现了乘法季节性。
导入数据集,完成初始化设置
import pystan
import fbprophet
import pandas as pd
from fbprophet import Prophet
df = pd.read_csv('D:\Machine learning\prophet-main\example_air_passengers.csv')
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(50, freq='MS')
forecast = m.predict(future)
fig = m.plot(forecast)
设置乘法季节性,训练模型
m = Prophet(seasonality_mode='multiplicative')
m.fit(df)
forecast = m.predict(future)
fig = m.plot(forecast)
并画图
fig.show()
训练结果与图像如下
INFO:numexpr.utils:Note: NumExpr detected 16 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
INFO:numexpr.utils:NumExpr defaulting to 8 threads.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -2.46502
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
99 401.677 0.0020169 115.615 1 1 130
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
144 401.793 1.05996e-005 59.3202 2.224e-007 0.001 238 LS failed, Hessian reset
199 401.842 8.23524e-007 78.4056 0.2832 1 311
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
253 401.843 3.04642e-008 64.9714 0.22 1 385
Optimization terminated normally:
Convergence detected: relative gradient magnitude is below tolerance
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -2.46502
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
99 501.604 0.000654695 77.4368 0.7922 0.7922 139
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
199 503.323 2.7516e-005 73.7442 1 1 260
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
224 503.365 0.000152713 82.0492 1.951e-006 0.001 320 LS failed, Hessian reset
271 503.376 5.07205e-006 94.1896 6.533e-008 0.001 418 LS failed, Hessian reset
299 503.377 7.57048e-008 54.4034 0.8718 0.8718 460
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
301 503.377 8.09861e-009 72.6659 0.1896 0.1896 463
Optimization terminated normally:
Convergence detected: absolute parameter change was below tolerance
可以看到震荡越来越明显,拟合结果与数据匹配度高,适合用乘法季节性。
使用seasonality_mode='multiplicative',假期效应也被建模为乘法。
默认情况下,季节性或回归量都与seasonality_mode匹配;但可以添加季节性或回归量时指定 mode='additive' 或 mode='multiplicative' 作为参数以覆盖原有默认的seasonality_mode。
m = Prophet(seasonality_mode='multiplicative')
m.add_seasonality('quarterly', period=91.25, fourier_order=8, mode='additive')
m.add_regressor('regressor', mode='additive')
默认情况下,Prophet 将返回预测 yhat 的不确定区间。
预测中不确定性的来源包括:趋势的不确定性、季节性估计中的不确定性、额外的观测噪声。
模型中假设未来的趋势变化到与历史的趋势变化相似;未来趋势变化的平均频率和幅度将与我们在历史的相同;
趋势的不确定性是最大的预测不确定性来源。可以通过向前预测趋势变化,通过计算它们趋势的分布获得不确定区间。
增加 changepoint_prior_scale ,即选用历史中更多的变化点进行建模,会使不确定性区间成为过拟合的有用指标,使趋势变化的平均速率灵活性更高,增加预测的不确定性。
可以使用参数 interval_width 设置不确定区间的宽度(默认为 80%)
forecast = Prophet(interval_width=0.95).fit(df).predict(future)
结果
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -2.46502
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
99 401.677 0.0020169 115.615 1 1 130
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
144 401.793 1.05996e-005 59.3202 2.224e-007 0.001 238 LS failed, Hessian reset
199 401.842 8.23524e-007 78.4056 0.2832 1 311
Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
253 401.843 3.04642e-008 64.9714 0.22 1 385
Optimization terminated normally:
Convergence detected: relative gradient magnitude is below tolerance
画图
fig=m.plot(forecast)
fig.show()
默认情况下,Prophet 只会返回趋势和观察噪声的不确定性。 获得季节性的不确定性,要进行完整的贝叶斯抽样。
m = Prophet(mcmc_samples=300)
forecast = m.fit(df).predict(future)