mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-16 08:38:30 +00:00
Adds holiday_mode independent of seasonality (#2477)
This commit is contained in:
parent
415bb6ef97
commit
265ac0550b
5 changed files with 30 additions and 3 deletions
|
|
@ -297,6 +297,7 @@ def prophet_copy(m, cutoff=None):
|
|||
weekly_seasonality=False,
|
||||
daily_seasonality=False,
|
||||
holidays=m.holidays,
|
||||
holidays_mode=m.holidays_mode,
|
||||
seasonality_mode=m.seasonality_mode,
|
||||
seasonality_prior_scale=m.seasonality_prior_scale,
|
||||
changepoint_prior_scale=m.changepoint_prior_scale,
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ class Prophet(object):
|
|||
uncertainty estimation and speed up the calculation.
|
||||
stan_backend: str as defined in StanBackendEnum default: None - will try to
|
||||
iterate over all available backends and find the working one
|
||||
holidays_mode: 'additive' or 'multiplicative'. Defaults to seasonality_mode.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
|
|
@ -96,6 +97,7 @@ class Prophet(object):
|
|||
uncertainty_samples=1000,
|
||||
stan_backend=None,
|
||||
scaling: str = 'absmax',
|
||||
holidays_mode=None,
|
||||
):
|
||||
self.growth = growth
|
||||
|
||||
|
|
@ -115,6 +117,10 @@ class Prophet(object):
|
|||
self.holidays = holidays
|
||||
|
||||
self.seasonality_mode = seasonality_mode
|
||||
self.holidays_mode = holidays_mode
|
||||
if holidays_mode is None:
|
||||
self.holidays_mode = self.seasonality_mode
|
||||
|
||||
self.seasonality_prior_scale = float(seasonality_prior_scale)
|
||||
self.changepoint_prior_scale = float(changepoint_prior_scale)
|
||||
self.holidays_prior_scale = float(holidays_prior_scale)
|
||||
|
|
@ -199,6 +205,10 @@ class Prophet(object):
|
|||
raise ValueError(
|
||||
'seasonality_mode must be "additive" or "multiplicative"'
|
||||
)
|
||||
if self.holidays_mode not in ['additive', 'multiplicative']:
|
||||
raise ValueError(
|
||||
'holidays_mode must be "additive" or "multiplicative"'
|
||||
)
|
||||
|
||||
def validate_column_name(self, name, check_holidays=True,
|
||||
check_seasonalities=True, check_regressors=True):
|
||||
|
|
@ -817,7 +827,7 @@ class Prophet(object):
|
|||
)
|
||||
seasonal_features.append(features)
|
||||
prior_scales.extend(holiday_priors)
|
||||
modes[self.seasonality_mode].extend(holiday_names)
|
||||
modes[self.holidays_mode].extend(holiday_names)
|
||||
|
||||
# Additional regressors
|
||||
for name, props in self.extra_regressors.items():
|
||||
|
|
@ -882,7 +892,7 @@ class Prophet(object):
|
|||
modes[mode].append(mode + '_terms')
|
||||
modes[mode].append('extra_regressors_' + mode)
|
||||
# After all of the additive/multiplicative groups have been added,
|
||||
modes[self.seasonality_mode].append('holidays')
|
||||
modes[self.holidays_mode].append('holidays')
|
||||
# Convert to a binary matrix
|
||||
component_cols = pd.crosstab(
|
||||
components['col'], components['component'],
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ SIMPLE_ATTRIBUTES = [
|
|||
'yearly_seasonality', 'weekly_seasonality', 'daily_seasonality',
|
||||
'seasonality_mode', 'seasonality_prior_scale', 'changepoint_prior_scale',
|
||||
'holidays_prior_scale', 'mcmc_samples', 'interval_width', 'uncertainty_samples',
|
||||
'y_scale', 'y_min', 'scaling', 'logistic_floor', 'country_holidays', 'component_modes'
|
||||
'y_scale', 'y_min', 'scaling', 'logistic_floor', 'country_holidays', 'component_modes',
|
||||
'holidays_mode'
|
||||
]
|
||||
|
||||
PD_SERIES = ['changepoints', 'history_dates', 'train_holiday_names']
|
||||
|
|
@ -135,6 +136,9 @@ def _handle_simple_attributes_backwards_compat(model_dict):
|
|||
if 'scaling' not in model_dict:
|
||||
model_dict['scaling'] = 'absmax'
|
||||
model_dict['y_min'] = 0.
|
||||
# : handle holidays_mode parameter introduced in #2477
|
||||
if 'holidays_mode' not in model_dict:
|
||||
model_dict['holidays_mode'] = model_dict['seasonality_mode']
|
||||
|
||||
def model_from_dict(model_dict):
|
||||
"""Recreate a Prophet model from a dictionary.
|
||||
|
|
|
|||
|
|
@ -339,6 +339,7 @@ class TestProphetCopy:
|
|||
else:
|
||||
assert (m1.holidays == m2.holidays).values.all()
|
||||
assert m1.country_holidays == m2.country_holidays
|
||||
assert m1.holidays_mode == m2.holidays_mode
|
||||
assert m1.seasonality_mode == m2.seasonality_mode
|
||||
assert m1.seasonality_prior_scale == m2.seasonality_prior_scale
|
||||
assert m1.changepoint_prior_scale == m2.changepoint_prior_scale
|
||||
|
|
|
|||
|
|
@ -549,6 +549,17 @@ class TestProphetSeasonalComponent:
|
|||
with pytest.raises(ValueError):
|
||||
Prophet(seasonality_mode="batman", stan_backend=backend)
|
||||
|
||||
def test_set_holidays_mode(self, backend):
|
||||
# Setting attribute
|
||||
m = Prophet(stan_backend=backend)
|
||||
assert m.holidays_mode == "additive"
|
||||
m = Prophet(seasonality_mode="multiplicative", stan_backend=backend)
|
||||
assert m.holidays_mode == "multiplicative"
|
||||
m = Prophet(holidays_mode="multiplicative", stan_backend=backend)
|
||||
assert m.holidays_mode == "multiplicative"
|
||||
with pytest.raises(ValueError):
|
||||
Prophet(holidays_mode="batman", stan_backend=backend)
|
||||
|
||||
def test_seasonality_modes(self, daily_univariate_ts, backend):
|
||||
# Model with holidays, seasonalities, and extra regressors
|
||||
holidays = pd.DataFrame(
|
||||
|
|
|
|||
Loading…
Reference in a new issue