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.143320 0.749968 10.803790 6.143320 6.143320 -0.035883 -0.035883 -0.035883 -0.035883 -0.035883 -0.035883 0.0 0.0 0.0 6.107438
2018-12-23 6.140754 0.862639 11.400232 6.140754 6.140754 -0.032846 -0.032846 -0.032846 -0.032846 -0.032846 -0.032846 0.0 0.0 0.0 6.107908
2018-12-24 6.138188 0.828253 11.574750 6.138188 6.138188 0.025082 0.025082 0.025082 0.025082 0.025082 0.025082 0.0 0.0 0.0 6.163270
2018-12-25 6.135621 0.540491 11.316455 6.135621 6.135621 -0.020201 -0.020201 -0.020201 -0.020201 -0.020201 -0.020201 0.0 0.0 0.0 6.115420
2018-12-26 6.133055 0.751823 11.142846 6.133055 6.133055 0.018654 0.018654 0.018654 0.018654 0.018654 0.018654 0.0 0.0 0.0 6.151709
2018-12-27 6.130489 0.923553 11.641867 6.130489 6.130489 0.030489 0.030489 0.030489 0.030489 0.030489 0.030489 0.0 0.0 0.0 6.160978
2018-12-28 6.127923 1.076292 11.500066 6.127916 6.127946 0.014705 0.014705 0.014705 0.014705 0.014705 0.014705 0.0 0.0 0.0 6.142628
2018-12-29 6.125356 0.927312 11.153359 6.125302 6.125440 -0.035883 -0.035883 -0.035883 -0.035883 -0.035883 -0.035883 0.0 0.0 0.0 6.089474
2018-12-30 6.122790 0.873291 11.383074 6.122696 6.122977 -0.032846 -0.032846 -0.032846 -0.032846 -0.032846 -0.032846 0.0 0.0 0.0 6.089944
2018-12-31 6.120224 0.832878 11.464349 6.120045 6.120502 0.025082 0.025082 0.025082 0.025082 0.025082 0.025082 0.0 0.0 0.0 6.145306