大趨勢 Trend
FBP識別趨勢和sparse prior息息相關(guān)(但是沒有查到相關(guān)資料)驻襟,sparse prior的強(qiáng)度changepoint_prior_scale決定了趨勢的靈活性,默認(rèn)值為0.05遮斥,增加這個值可以另模型趨勢更加擬合歷史數(shù)據(jù)层亿,通常在0.001~0.5之間
m = Prophet(changepoint_prior_scale=0.001)
FBP在識別突變點changepoint時,默認(rèn)情況下,會先指定25個可能點均勻地放在時間序列的前80%绊谭,然后只選取少數(shù)速率變化大的作為突變點,這個設(shè)置在大多數(shù)情況下都適用汪拥。如果需要达传,可以調(diào)節(jié)可能點的個數(shù)n_changepoints和范圍changepoint_range,或者手動添加突變點
m = Prophet(n_changepoints=30, changepoint_range=0.9)?
m = Prophet(changepoints=['2014-01-01'])
當(dāng)數(shù)據(jù)的周期性規(guī)律比大趨勢更突出時迫筑,可以強(qiáng)制設(shè)置趨勢平緩:
m = Prophet(growth='flat')
周期性?Seasonalities?
FBP使用傅里葉級數(shù)估算模型的季節(jié)性規(guī)律宪赶,傅里葉級數(shù)決定了季節(jié)性變化有多快。年度季節(jié)性的級數(shù)yearly_seasonality默認(rèn)值是10脯燃,適用于大部分情況搂妻,當(dāng)趨勢的季節(jié)波動頻率更高時可以調(diào)高這個值,但過高可能會導(dǎo)致過擬合
m = Prophet(yearly_seasonality=20).fit(df)
周期性的影響規(guī)模prior_scale辕棚,可以在定義時或添加自定義時調(diào)節(jié)欲主,通常在0.1~10之間:
m = Prophet()
m.add_seasonality(name='weekly', period=7, fourier_order=3, prior_scale=0.1)
周期的模式有加性和乘性可以選擇邓厕,加性周期的波動是固定的,乘性周期的波動會隨著時間增長而放大扁瓢,可以在定義時或添加自定義時調(diào)節(jié):
m = Prophet(seasonality_mode='multiplicative')
m.add_seasonality('quarterly', period=91.25, fourier_order=8, mode='additive')
m.add_regressor('regressor', mode='additive')
Specifying Custom Seasonalities 定制周期
FBP能自動為模型計算每日详恼、周度和年度的季節(jié)性規(guī)律(當(dāng)識別到模型需要時),如果需要計算每小時引几、月度和季度的季節(jié)性規(guī)律昧互,需要通過函數(shù)add_seasonality()添加
m = Prophet(weekly_seasonality=False)?
m.add_seasonality(name='monthly', period=30.5, fourier_order=5)?
forecast = m.fit(df).predict(future)
Conditional Seasonalities 條件周期
當(dāng)季節(jié)性規(guī)律需要滿足一定的觸發(fā)條件時,F(xiàn)BP提供了解決辦法伟桅。比如敞掘,某數(shù)據(jù)只有1~2月和8~12月內(nèi)具有周度季節(jié)性波動。先設(shè)置在季節(jié)內(nèi)和非季節(jié)內(nèi)的條件:
def is_nfl_season(ds):? ??
????date = pd.to_datetime(ds)? ??
????return (date.month > 8 or date.month < 2)
df['on_season'] = df['ds'].apply(is_nfl_season)
df['off_season'] = ~df['ds'].apply(is_nfl_season)
然后關(guān)閉內(nèi)置的周度季節(jié)性贿讹,隨后分別添加渐逃、預(yù)測條件內(nèi)外的周度季節(jié)性
m = Prophet(weekly_seasonality=False)
m.add_seasonality(name='weekly_on_season', period=7, fourier_order=3, condition_name='on_season')
m.add_seasonality(name='weekly_off_season', period=7, fourier_order=3, condition_name='off_season')
future['on_season'] = future['ds'].apply(is_nfl_season)
future['off_season'] = ~future['ds'].apply(is_nfl_season)
forecast = m.fit(df).predict(future)
節(jié)日 Holidays
FBP可以為模型添加節(jié)日節(jié)點,包含節(jié)日名稱holiday和日期ds民褂,還可以設(shè)定影響天數(shù)的范圍lower_window和upper_window
playoffs = pd.DataFrame({ 'holiday': 'playoff', 'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16', '2010-01-24', '2010-02-07', '2011-01-08', '2013-01-12', '2014-01-12', '2014-01-19', '2014-02-02', '2015-01-11', '2016-01-17', '2016-01-24', '2016-02-07']), 'lower_window': 0, 'upper_window': 1, })?
superbowls = pd.DataFrame({ 'holiday': 'superbowl', 'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']), 'lower_window': 0, 'upper_window': 1, })?
holidays = pd.concat((playoffs, superbowls))?
查看節(jié)日的影響:
forecast[(forecast['playoff']+forecast['superbowl']).abs()>0[['ds','playoff','superbowl']]
當(dāng)節(jié)日過擬合時茄菊,可以調(diào)節(jié)節(jié)日的影響規(guī)模holidays_prior_scale,或者在節(jié)日數(shù)據(jù)框里設(shè)置prior_scale赊堪,其默認(rèn)值為10面殖,通常在0.01~10之間:
m = Prophet(holidays=holidays, holidays_prior_scale=0.05).fit(df)
其他回歸器Additional regressors
FBP還支持為時間序列添加其他的特征,可以將其他特征合并到數(shù)據(jù)集中哭廉,然后添加函數(shù)add_regressor()實現(xiàn)脊僚,其預(yù)測趨勢圖也可以如其他規(guī)律一般被查看
m=Prophet(mcmc_samples=300,holidays=holidays_df,holidays_prior_scale=0.25,changepoint_prior_scale=0.01,seasonality_mode='multiplicative',\yearly_seasonality=10,\weekly_seasonality=True,\daily_seasonality=False)
m.add_regressor('temp',prior_scale=0.5,mode='multiplicative')
m.add_regressor('rain',prior_scale=0.5,mode='multiplicative')
m.add_regressor('sun',prior_scale=0.5,mode='multiplicative')
m.add_regressor('wind',prior_scale=0.5,mode='multiplicative')
注意!添加的回歸器必須是一個過去到未來都知曉/已預(yù)測出來的數(shù)據(jù)遵绰,因此辽幌,回歸器也可以是另一條時間序列,前提是這一條時間序列更加容易預(yù)測
限制增長趨勢Forecasting Growth
FBP支持對預(yù)測上下限的界定椿访。默認(rèn)情況下乌企,F(xiàn)BP使用線性增長模型,當(dāng)使用邏輯增長模型時成玫,可以設(shè)定上限cap和下限floor加酵,設(shè)定下限時必須同時設(shè)定上限
m = Prophet(growth = 'logistic')
m.fit(df)
future = m.make_future_dataframe(periods = 1826)
future['cap'] = 8.5
fcst = m.predict(future)
fig = m.plot(fcst)
評估結(jié)果 Diagnostics
FBP提供滑窗交叉驗證方法評估效果,輸入?yún)?shù)需要起始預(yù)測點initial哭当,意為從時間序列的第initial個時間起開始預(yù)測猪腕、測試效果;間隔階段period钦勘,意為每+period時間后進(jìn)行一次預(yù)測陋葡;和預(yù)測范圍horizon,意為每次預(yù)測時長為horizon長度
from fbprophet.diagnostics importcross_validation
df_cv=cross_validation(m,initial='730 days',period='180 days',horizon='365 days')
結(jié)果表查看和可視化:
df_p=performance_metrics(df_cv)
plot_cross_validation_metric(df.cv, metric='mape')
調(diào)參心得
最重要的四個參數(shù):
changepoint_prior_scale:突變點影響規(guī)模彻采,影響趨勢的靈活性脖岛。默認(rèn)值為0.05朵栖,一般建議[0.001, 0.5]。該值越大柴梆,突變點帶來的波動越大,越容易導(dǎo)致過擬合终惑;
seasonality_prior_scale:周期性影響規(guī)模绍在,決定周期的靈活性。默認(rèn)值為10雹有,一般建議?[0.01, 10]偿渡。該值越大,周期性波動越大
holidays_prior_scale:假日影響規(guī)模霸奕。默認(rèn)值為10溜宽,一般建議?[0.01, 10]。該值越大质帅,假日帶來的波動越大
seasonality_mode:周期性模式适揉。['additive','multiplicative']煤惩,默認(rèn)加性嫉嘀。當(dāng)數(shù)據(jù)取log時加性即原數(shù)據(jù)的乘性
其他:
傅里葉級數(shù):決定函數(shù)的周期性,該值越大則周期越長(存疑)
changepoint_range:突變點范圍魄揉,默認(rèn)為0.8剪侮,代表在前80%的數(shù)據(jù)里選取突變點
n_changepoints:突變點個數(shù),默認(rèn)為25洛退,代表均勻地選取25個突變點
參考自:
Seasonality, holiday effects and regressors.ipynb
Multiplicative seasonality.ipynb
Auckland cycling and weather.ipynb