Advanced Prophet Usage¶
You can scan through fbprophet docs and find many options how to tweak your model. Some of that functionality is moved to initialization stage to be compatible with Sklearn API. We will showcase the parts that were moved to initialization, but you can also look for other model parameters that could help fine-tuning your model
[1]:
from hcrystalball.wrappers import ProphetWrapper
[2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = [12, 6]
[3]:
from hcrystalball.utils import generate_tsdata
X, y = generate_tsdata(n_dates=365*2)
[4]:
ProphetWrapper?
Advanced Holidays¶
For holidays, we are able to define instead of single boolean attribute distribution around given day. We define lower_window
, upper_window
and prior_scales
[5]:
extra_holidays = {
'Whit Monday':{'lower_window': 2, 'upper_window':2, 'prior_scale': 10},
'Good Friday':{'lower_window': 1, 'upper_window':1, 'prior_scale': 30}
}
Unusual Seasonalities¶
[6]:
extra_seasonalities = [
{
'name':'bi-weekly',
'period': 14.,
'fourier_order': 5,
'prior_scale': 15.0,
'mode': None
},
{
'name':'bi-yearly',
'period': 365*2.,
'fourier_order': 5,
'prior_scale': 5.0,
'mode': None
},
]
Exogenous Variables¶
[7]:
extra_regressors = ['trend_line']
X['trend_line'] = np.arange(len(X))
[8]:
prophet = ProphetWrapper(
extra_holidays=extra_holidays,
extra_seasonalities=extra_seasonalities,
extra_regressors=extra_regressors,
name='prophet_extra',
)
[9]:
(prophet.fit(X[:-10], y[:-10])
.predict(X[-10:])
.merge(y, left_index=True, right_index=True, how='outer')
.tail(50)
.plot()
);
Compared to non-tweaked model, this shows our fine-tuning helped to increase performance significantly
[10]:
(ProphetWrapper().fit(X[:-10], y[:-10])
.predict(X[-10:])
.merge(y, left_index=True, right_index=True, how='outer')
.tail(50)
.plot()
);
Full Prophet Output¶
If you need, you can also pass full_prophet_output
and get rich predict output
[11]:
(ProphetWrapper(full_prophet_output=True, conf_int=True)
.fit(X[:-10], y[:-10])
.predict(X[-10:])
)
[11]:
trend | prophet_lower | prophet_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | prophet | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2018-12-22 | 6.145099 | 1.181676 | 11.490063 | 6.145099 | 6.145099 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.0 | 0.0 | 0.0 | 6.152264 |
2018-12-23 | 6.142491 | 0.887537 | 11.073496 | 6.142491 | 6.142491 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.0 | 0.0 | 0.0 | 6.146207 |
2018-12-24 | 6.139883 | 0.718226 | 11.250557 | 6.139883 | 6.139883 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | 0.0 | 0.0 | 0.0 | 6.100455 |
2018-12-25 | 6.137275 | 1.022219 | 11.413502 | 6.137275 | 6.137275 | -0.017522 | -0.017522 | -0.017522 | -0.017522 | -0.017522 | -0.017522 | 0.0 | 0.0 | 0.0 | 6.119753 |
2018-12-26 | 6.134667 | 0.757220 | 11.358743 | 6.134667 | 6.134667 | -0.016530 | -0.016530 | -0.016530 | -0.016530 | -0.016530 | -0.016530 | 0.0 | 0.0 | 0.0 | 6.118137 |
2018-12-27 | 6.132059 | 1.146918 | 11.462771 | 6.132059 | 6.132059 | 0.041103 | 0.041103 | 0.041103 | 0.041103 | 0.041103 | 0.041103 | 0.0 | 0.0 | 0.0 | 6.173161 |
2018-12-28 | 6.129451 | 1.068698 | 11.784980 | 6.129450 | 6.129451 | 0.021497 | 0.021497 | 0.021497 | 0.021497 | 0.021497 | 0.021497 | 0.0 | 0.0 | 0.0 | 6.150947 |
2018-12-29 | 6.126842 | 0.672384 | 11.511821 | 6.126805 | 6.126871 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.007165 | 0.0 | 0.0 | 0.0 | 6.134007 |
2018-12-30 | 6.124234 | 1.086645 | 11.198027 | 6.124120 | 6.124308 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.003715 | 0.0 | 0.0 | 0.0 | 6.127950 |
2018-12-31 | 6.121626 | 1.259572 | 11.210170 | 6.121439 | 6.121766 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | -0.039428 | 0.0 | 0.0 | 0.0 | 6.082198 |