mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-17 08:40:56 +00:00
Generalize seasonality representation (Python)
This commit is contained in:
parent
825108b226
commit
b3017c025f
2 changed files with 88 additions and 71 deletions
|
|
@ -78,7 +78,6 @@ class Prophet(object):
|
||||||
parameters, which will include uncertainty in seasonality.
|
parameters, which will include uncertainty in seasonality.
|
||||||
uncertainty_samples: Number of simulated draws used to estimate
|
uncertainty_samples: Number of simulated draws used to estimate
|
||||||
uncertainty intervals.
|
uncertainty intervals.
|
||||||
daily_seasonality: Boolean, fit daily seasonality
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -96,7 +95,6 @@ class Prophet(object):
|
||||||
mcmc_samples=0,
|
mcmc_samples=0,
|
||||||
interval_width=0.80,
|
interval_width=0.80,
|
||||||
uncertainty_samples=1000,
|
uncertainty_samples=1000,
|
||||||
daily_seasonality=False,
|
|
||||||
):
|
):
|
||||||
self.growth = growth
|
self.growth = growth
|
||||||
|
|
||||||
|
|
@ -134,6 +132,7 @@ class Prophet(object):
|
||||||
self.y_scale = None
|
self.y_scale = None
|
||||||
self.t_scale = None
|
self.t_scale = None
|
||||||
self.changepoints_t = None
|
self.changepoints_t = None
|
||||||
|
self.seasonalities = {}
|
||||||
self.stan_fit = None
|
self.stan_fit = None
|
||||||
self.params = {}
|
self.params = {}
|
||||||
self.history = None
|
self.history = None
|
||||||
|
|
@ -358,81 +357,91 @@ class Prophet(object):
|
||||||
# Add a column of zeros in case no seasonality is used.
|
# Add a column of zeros in case no seasonality is used.
|
||||||
pd.DataFrame({'zeros': np.zeros(df.shape[0])})
|
pd.DataFrame({'zeros': np.zeros(df.shape[0])})
|
||||||
]
|
]
|
||||||
|
for name, (period, series_order) in self.seasonalities.items():
|
||||||
# Seasonality features
|
|
||||||
if self.yearly_seasonality > 0:
|
|
||||||
seasonal_features.append(self.make_seasonality_features(
|
seasonal_features.append(self.make_seasonality_features(
|
||||||
df['ds'],
|
df['ds'],
|
||||||
365.25,
|
period,
|
||||||
self.yearly_seasonality,
|
series_order,
|
||||||
'yearly',
|
name,
|
||||||
))
|
|
||||||
|
|
||||||
if self.weekly_seasonality > 0:
|
|
||||||
seasonal_features.append(self.make_seasonality_features(
|
|
||||||
df['ds'],
|
|
||||||
7,
|
|
||||||
self.weekly_seasonality,
|
|
||||||
'weekly',
|
|
||||||
))
|
|
||||||
|
|
||||||
if self.daily_seasonality > 0:
|
|
||||||
seasonal_features.append(self.make_seasonality_features(
|
|
||||||
df['ds'],
|
|
||||||
1,
|
|
||||||
self.daily_seasonality,
|
|
||||||
'daily',
|
|
||||||
))
|
))
|
||||||
|
|
||||||
if self.holidays is not None:
|
if self.holidays is not None:
|
||||||
seasonal_features.append(self.make_holiday_features(df['ds']))
|
seasonal_features.append(self.make_holiday_features(df['ds']))
|
||||||
return pd.concat(seasonal_features, axis=1)
|
return pd.concat(seasonal_features, axis=1)
|
||||||
|
|
||||||
|
def parse_seasonality_args(self, name, arg, auto_disable, default_order):
|
||||||
|
"""Get number of fourier components for built-in seasonalities.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
name: string name of the seasonality component.
|
||||||
|
arg: 'auto', True, False, or number of fourier components as provided.
|
||||||
|
auto_disable: bool if seasonality should be disabled when 'auto'.
|
||||||
|
default_order: int default fourier order
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Number of fourier components, or 0 for disabled.
|
||||||
|
"""
|
||||||
|
if arg == 'auto':
|
||||||
|
fourier_order = 0
|
||||||
|
if name in self.seasonalities:
|
||||||
|
logger.info(
|
||||||
|
'Found custom seasonality named "{name}", '
|
||||||
|
'disabling built-in {name} seasonality.'.format(name=name)
|
||||||
|
)
|
||||||
|
elif auto_disable:
|
||||||
|
logger.info(
|
||||||
|
'Disabling {name} seasonality. Run prophet with '
|
||||||
|
'{name}_seasonality=True to override this.'.format(
|
||||||
|
name=name)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
fourier_order = default_order
|
||||||
|
elif arg is True:
|
||||||
|
fourier_order = default_order
|
||||||
|
elif arg is False:
|
||||||
|
fourier_order = 0
|
||||||
|
else:
|
||||||
|
fourier_order = int(arg)
|
||||||
|
return fourier_order
|
||||||
|
|
||||||
def set_auto_seasonalities(self):
|
def set_auto_seasonalities(self):
|
||||||
"""Set seasonalities that were left on auto.
|
"""Set seasonalities that were left on auto.
|
||||||
|
|
||||||
Turns on yearly seasonality if there is >=2 years of history.
|
Turns on yearly seasonality if there is >=2 years of history.
|
||||||
Turns on weekly seasonality if there is >=2 weeks of history, and the
|
Turns on weekly seasonality if there is >=2 weeks of history, and the
|
||||||
spacing between dates in the history is <7 days.
|
spacing between dates in the history is <7 days.
|
||||||
|
Turns on daily seasonality if there is >=2 days of history, and the
|
||||||
|
spacing between dates in the history is <1 day.
|
||||||
"""
|
"""
|
||||||
first = self.history['ds'].min()
|
first = self.history['ds'].min()
|
||||||
last = self.history['ds'].max()
|
last = self.history['ds'].max()
|
||||||
if self.yearly_seasonality == 'auto':
|
dt = self.history['ds'].diff()
|
||||||
if last - first < pd.Timedelta(days=730):
|
min_dt = dt.iloc[dt.nonzero()[0]].min()
|
||||||
self.yearly_seasonality = 0
|
|
||||||
logger.info('Disabling yearly seasonality. Run prophet with '
|
# Yearly seasonality
|
||||||
'yearly_seasonality=True to override this.')
|
yearly_disable = last - first < pd.Timedelta(days=730)
|
||||||
else:
|
fourier_order = self.parse_seasonality_args(
|
||||||
self.yearly_seasonality = 10
|
'yearly', self.yearly_seasonality, yearly_disable, 10)
|
||||||
elif self.yearly_seasonality is True:
|
if fourier_order > 0:
|
||||||
self.yearly_seasonality = 10
|
self.seasonalities['yearly'] = (365.25, fourier_order)
|
||||||
|
|
||||||
if self.weekly_seasonality == 'auto':
|
# Weekly seasonality
|
||||||
dt = self.history['ds'].diff()
|
weekly_disable = ((last - first < pd.Timedelta(weeks=2)) or
|
||||||
min_dt = dt.iloc[dt.nonzero()[0]].min()
|
(min_dt >= pd.Timedelta(weeks=1)))
|
||||||
if ((last - first < pd.Timedelta(weeks=2)) or
|
fourier_order = self.parse_seasonality_args(
|
||||||
(min_dt >= pd.Timedelta(weeks=1))):
|
'weekly', self.weekly_seasonality, weekly_disable, 3)
|
||||||
self.weekly_seasonality = 0
|
if fourier_order > 0:
|
||||||
logger.info('Disabling weekly seasonality. Run prophet with '
|
self.seasonalities['weekly'] = (7, fourier_order)
|
||||||
'weekly_seasonality=True to override this.')
|
|
||||||
else:
|
# Daily seasonality
|
||||||
self.weekly_seasonality = 3
|
daily_disable = ((last - first < pd.Timedelta(days=2)) or
|
||||||
elif self.weekly_seasonality is True:
|
(min_dt >= pd.Timedelta(days=1)))
|
||||||
self.weekly_seasonality = 3
|
fourier_order = self.parse_seasonality_args(
|
||||||
|
'daily', self.daily_seasonality, daily_disable, 4)
|
||||||
if self.daily_seasonality == 'auto':
|
if fourier_order > 0:
|
||||||
# disabled by default but if the average difference is <1 day
|
self.seasonalities['daily'] = (1, fourier_order)
|
||||||
# then we assume there is intra-day modeling
|
|
||||||
dt = self.history['ds'].diff()
|
|
||||||
min_dt = dt.iloc[dt.nonzero()[0]].min()
|
|
||||||
if (min_dt< pd.Timedelta(days=1)):
|
|
||||||
self.daily_seasonality = 4
|
|
||||||
logger.info('Enabling daily seasonality. Run prophet with '
|
|
||||||
'daily_seasonality=False to override this.')
|
|
||||||
else:
|
|
||||||
self.daily_seasonality = 0
|
|
||||||
elif self.daily_seasonality is True:
|
|
||||||
self.daily_seasonality = 4
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def linear_growth_init(df):
|
def linear_growth_init(df):
|
||||||
|
|
|
||||||
|
|
@ -252,43 +252,51 @@ class TestProphet(TestCase):
|
||||||
self.assertEqual(future.iloc[i]['ds'], correct[i])
|
self.assertEqual(future.iloc[i]['ds'], correct[i])
|
||||||
|
|
||||||
def test_auto_weekly_seasonality(self):
|
def test_auto_weekly_seasonality(self):
|
||||||
# Should be True
|
# Should be enabled
|
||||||
N = 15
|
N = 15
|
||||||
train = DATA.head(N)
|
train = DATA.head(N)
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
self.assertEqual(m.weekly_seasonality, 'auto')
|
self.assertEqual(m.weekly_seasonality, 'auto')
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.weekly_seasonality, True)
|
self.assertIn('weekly', m.seasonalities)
|
||||||
# Should be False due to too short history
|
self.assertEqual(m.seasonalities['weekly'], (7, 3))
|
||||||
|
# Should be disabled due to too short history
|
||||||
N = 9
|
N = 9
|
||||||
train = DATA.head(N)
|
train = DATA.head(N)
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.weekly_seasonality, False)
|
self.assertNotIn('weekly', m.seasonalities)
|
||||||
m = Prophet(weekly_seasonality=True)
|
m = Prophet(weekly_seasonality=True)
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.weekly_seasonality, True)
|
self.assertIn('weekly', m.seasonalities)
|
||||||
# Should be False due to weekly spacing
|
# Should be False due to weekly spacing
|
||||||
train = DATA.iloc[::7, :]
|
train = DATA.iloc[::7, :]
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.weekly_seasonality, False)
|
self.assertNotIn('weekly', m.seasonalities)
|
||||||
|
m = Prophet(weekly_seasonality=2)
|
||||||
|
m.fit(DATA)
|
||||||
|
self.assertEqual(m.seasonalities['weekly'], (7, 2))
|
||||||
|
|
||||||
def test_auto_yearly_seasonality(self):
|
def test_auto_yearly_seasonality(self):
|
||||||
# Should be True
|
# Should be enabled
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
self.assertEqual(m.yearly_seasonality, 'auto')
|
self.assertEqual(m.yearly_seasonality, 'auto')
|
||||||
m.fit(DATA)
|
m.fit(DATA)
|
||||||
self.assertEqual(m.yearly_seasonality, True)
|
self.assertIn('yearly', m.seasonalities)
|
||||||
# Should be False due to too short history
|
self.assertEqual(m.seasonalities['yearly'], (365.25, 10))
|
||||||
|
# Should be disabled due to too short history
|
||||||
N = 240
|
N = 240
|
||||||
train = DATA.head(N)
|
train = DATA.head(N)
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.yearly_seasonality, False)
|
self.assertNotIn('yearly', m.seasonalities)
|
||||||
m = Prophet(yearly_seasonality=True)
|
m = Prophet(yearly_seasonality=True)
|
||||||
m.fit(train)
|
m.fit(train)
|
||||||
self.assertEqual(m.yearly_seasonality, True)
|
self.assertIn('yearly', m.seasonalities)
|
||||||
|
m = Prophet(yearly_seasonality=7)
|
||||||
|
m.fit(DATA)
|
||||||
|
self.assertEqual(m.seasonalities['yearly'], (365.25, 7))
|
||||||
|
|
||||||
|
|
||||||
DATA = pd.read_csv(StringIO("""
|
DATA = pd.read_csv(StringIO("""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue