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()
);
../../../_images/examples_tutorial_wrappers_06_advanced_prophet_12_0.png

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()
);
../../../_images/examples_tutorial_wrappers_06_advanced_prophet_14_0.png

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.117205 0.686035 11.333847 6.117205 6.117205 -0.002926 -0.002926 -0.002926 -0.002926 -0.002926 -0.002926 0.0 0.0 0.0 6.114279
2018-12-23 6.114680 0.969916 11.303319 6.114680 6.114680 -0.043927 -0.043927 -0.043927 -0.043927 -0.043927 -0.043927 0.0 0.0 0.0 6.070754
2018-12-24 6.112155 0.940186 11.282949 6.112155 6.112155 -0.031393 -0.031393 -0.031393 -0.031393 -0.031393 -0.031393 0.0 0.0 0.0 6.080763
2018-12-25 6.109630 0.962221 11.703887 6.109630 6.109630 0.047504 0.047504 0.047504 0.047504 0.047504 0.047504 0.0 0.0 0.0 6.157134
2018-12-26 6.107105 0.730774 11.363140 6.107105 6.107105 -0.004015 -0.004015 -0.004015 -0.004015 -0.004015 -0.004015 0.0 0.0 0.0 6.103089
2018-12-27 6.104580 0.919625 11.052703 6.104580 6.104580 0.007980 0.007980 0.007980 0.007980 0.007980 0.007980 0.0 0.0 0.0 6.112560
2018-12-28 6.102055 0.939445 11.299684 6.101999 6.102055 0.026777 0.026777 0.026777 0.026777 0.026777 0.026777 0.0 0.0 0.0 6.128832
2018-12-29 6.099529 0.919247 11.527920 6.099389 6.099541 -0.002926 -0.002926 -0.002926 -0.002926 -0.002926 -0.002926 0.0 0.0 0.0 6.096603
2018-12-30 6.097004 0.751200 11.620490 6.096674 6.097128 -0.043927 -0.043927 -0.043927 -0.043927 -0.043927 -0.043927 0.0 0.0 0.0 6.053078
2018-12-31 6.094479 0.564785 11.646958 6.093958 6.094736 -0.031393 -0.031393 -0.031393 -0.031393 -0.031393 -0.031393 0.0 0.0 0.0 6.063087