예제 #1
0
 def test_custom_seasonality(self):
     holidays = pd.DataFrame({
         'ds': pd.to_datetime(['2017-01-02']),
         'holiday': ['special_day'],
         'prior_scale': [4.],
     })
     m = Prophet(holidays=holidays)
     m.add_seasonality(name='monthly',
                       period=30,
                       fourier_order=5,
                       prior_scale=2.)
     self.assertEqual(
         m.seasonalities['monthly'],
         {
             'period': 30,
             'fourier_order': 5,
             'prior_scale': 2.,
             'mode': 'additive',
             'condition_name': None
         },
     )
     with self.assertRaises(ValueError):
         m.add_seasonality(name='special_day', period=30, fourier_order=5)
     with self.assertRaises(ValueError):
         m.add_seasonality(name='trend', period=30, fourier_order=5)
     m.add_seasonality(name='weekly', period=30, fourier_order=5)
     # Test fourier order <= 0
     m = Prophet()
     with self.assertRaises(ValueError):
         m.add_seasonality(name='weekly', period=7, fourier_order=0)
     with self.assertRaises(ValueError):
         m.add_seasonality(name='weekly', period=7, fourier_order=-1)
     # Test priors
     m = Prophet(holidays=holidays,
                 yearly_seasonality=False,
                 seasonality_mode='multiplicative')
     m.add_seasonality(name='monthly',
                       period=30,
                       fourier_order=5,
                       prior_scale=2.,
                       mode='additive')
     m.fit(DATA.copy())
     self.assertEqual(m.seasonalities['monthly']['mode'], 'additive')
     self.assertEqual(m.seasonalities['weekly']['mode'], 'multiplicative')
     seasonal_features, prior_scales, component_cols, modes = (
         m.make_all_seasonality_features(m.history))
     self.assertEqual(sum(component_cols['monthly']), 10)
     self.assertEqual(sum(component_cols['special_day']), 1)
     self.assertEqual(sum(component_cols['weekly']), 6)
     self.assertEqual(sum(component_cols['additive_terms']), 10)
     self.assertEqual(sum(component_cols['multiplicative_terms']), 7)
     if seasonal_features.columns[0] == 'monthly_delim_1':
         true = [2.] * 10 + [10.] * 6 + [4.]
         self.assertEqual(sum(component_cols['monthly'][:10]), 10)
         self.assertEqual(sum(component_cols['weekly'][10:16]), 6)
     else:
         true = [10.] * 6 + [2.] * 10 + [4.]
         self.assertEqual(sum(component_cols['weekly'][:6]), 6)
         self.assertEqual(sum(component_cols['monthly'][6:16]), 10)
     self.assertEqual(prior_scales, true)
예제 #2
0
 def test_seasonality_modes(self):
     # Model with holidays, seasonalities, and extra regressors
     holidays = pd.DataFrame({
         'ds': pd.to_datetime(['2016-12-25']),
         'holiday': ['xmas'],
         'lower_window': [-1],
         'upper_window': [0],
     })
     m = Prophet(seasonality_mode='multiplicative', holidays=holidays)
     m.add_seasonality('monthly',
                       period=30,
                       mode='additive',
                       fourier_order=3)
     m.add_regressor('binary_feature', mode='additive')
     m.add_regressor('numeric_feature')
     # Construct seasonal features
     df = DATA.copy()
     df['binary_feature'] = [0] * 255 + [1] * 255
     df['numeric_feature'] = range(510)
     df = m.setup_dataframe(df, initialize_scales=True)
     m.history = df.copy()
     m.set_auto_seasonalities()
     seasonal_features, prior_scales, component_cols, modes = (
         m.make_all_seasonality_features(df))
     self.assertEqual(sum(component_cols['additive_terms']), 7)
     self.assertEqual(sum(component_cols['multiplicative_terms']), 29)
     self.assertEqual(
         set(modes['additive']),
         {
             'monthly', 'binary_feature', 'additive_terms',
             'extra_regressors_additive'
         },
     )
     self.assertEqual(
         set(modes['multiplicative']),
         {
             'weekly',
             'yearly',
             'xmas',
             'numeric_feature',
             'multiplicative_terms',
             'extra_regressors_multiplicative',
             'holidays',
         },
     )
예제 #3
0
 def test_conditional_custom_seasonality(self):
     m = Prophet(weekly_seasonality=False, yearly_seasonality=False)
     m.add_seasonality(name='conditional_weekly',
                       period=7,
                       fourier_order=3,
                       prior_scale=2.,
                       condition_name='is_conditional_week')
     m.add_seasonality(name='normal_monthly',
                       period=30.5,
                       fourier_order=5,
                       prior_scale=2.)
     df = DATA.copy()
     with self.assertRaises(ValueError):
         # Require all conditions names in df
         m.fit(df)
     df['is_conditional_week'] = [0] * 255 + [2] * 255
     with self.assertRaises(ValueError):
         # Require boolean compatible values
         m.fit(df)
     df['is_conditional_week'] = [0] * 255 + [1] * 255
     m.fit(df)
     self.assertEqual(
         m.seasonalities['conditional_weekly'],
         {
             'period': 7,
             'fourier_order': 3,
             'prior_scale': 2.,
             'mode': 'additive',
             'condition_name': 'is_conditional_week'
         },
     )
     self.assertIsNone(m.seasonalities['normal_monthly']['condition_name'])
     seasonal_features, prior_scales, component_cols, modes = (
         m.make_all_seasonality_features(m.history))
     # Confirm that only values without is_conditional_week has non zero entries
     conditional_weekly_columns = seasonal_features.columns[
         seasonal_features.columns.str.startswith('conditional_weekly')]
     self.assertTrue(
         np.array_equal(
             (seasonal_features[conditional_weekly_columns] != 0).any(
                 axis=1).values, df['is_conditional_week'].values))
예제 #4
0
 def test_added_regressors(self):
     m = Prophet()
     m.add_regressor('binary_feature', prior_scale=0.2)
     m.add_regressor('numeric_feature', prior_scale=0.5)
     m.add_regressor('numeric_feature2',
                     prior_scale=0.5,
                     mode='multiplicative')
     m.add_regressor('binary_feature2', standardize=True)
     df = DATA.copy()
     df['binary_feature'] = ['0'] * 255 + ['1'] * 255
     df['numeric_feature'] = range(510)
     df['numeric_feature2'] = range(510)
     with self.assertRaises(ValueError):
         # Require all regressors in df
         m.fit(df)
     df['binary_feature2'] = [1] * 100 + [0] * 410
     m.fit(df)
     # Check that standardizations are correctly set
     self.assertEqual(
         m.extra_regressors['binary_feature'],
         {
             'prior_scale': 0.2,
             'mu': 0,
             'std': 1,
             'standardize': 'auto',
             'mode': 'additive',
         },
     )
     self.assertEqual(m.extra_regressors['numeric_feature']['prior_scale'],
                      0.5)
     self.assertEqual(m.extra_regressors['numeric_feature']['mu'], 254.5)
     self.assertAlmostEqual(m.extra_regressors['numeric_feature']['std'],
                            147.368585,
                            places=5)
     self.assertEqual(m.extra_regressors['numeric_feature2']['mode'],
                      'multiplicative')
     self.assertEqual(m.extra_regressors['binary_feature2']['prior_scale'],
                      10.)
     self.assertAlmostEqual(m.extra_regressors['binary_feature2']['mu'],
                            0.1960784,
                            places=5)
     self.assertAlmostEqual(m.extra_regressors['binary_feature2']['std'],
                            0.3974183,
                            places=5)
     # Check that standardization is done correctly
     df2 = m.setup_dataframe(df.copy())
     self.assertEqual(df2['binary_feature'][0], 0)
     self.assertAlmostEqual(df2['numeric_feature'][0], -1.726962, places=4)
     self.assertAlmostEqual(df2['binary_feature2'][0], 2.022859, places=4)
     # Check that feature matrix and prior scales are correctly constructed
     seasonal_features, prior_scales, component_cols, modes = (
         m.make_all_seasonality_features(df2))
     self.assertEqual(seasonal_features.shape[1], 30)
     names = ['binary_feature', 'numeric_feature', 'binary_feature2']
     true_priors = [0.2, 0.5, 10.]
     for i, name in enumerate(names):
         self.assertIn(name, seasonal_features)
         self.assertEqual(sum(component_cols[name]), 1)
         self.assertEqual(
             sum(np.array(prior_scales) * component_cols[name]),
             true_priors[i],
         )
     # Check that forecast components are reasonable
     future = pd.DataFrame({
         'ds': ['2014-06-01'],
         'binary_feature': [0],
         'numeric_feature': [10],
         'numeric_feature2': [10],
     })
     with self.assertRaises(ValueError):
         m.predict(future)
     future['binary_feature2'] = 0
     fcst = m.predict(future)
     self.assertEqual(fcst.shape[1], 37)
     self.assertEqual(fcst['binary_feature'][0], 0)
     self.assertAlmostEqual(
         fcst['extra_regressors_additive'][0],
         fcst['numeric_feature'][0] + fcst['binary_feature2'][0],
     )
     self.assertAlmostEqual(
         fcst['extra_regressors_multiplicative'][0],
         fcst['numeric_feature2'][0],
     )
     self.assertAlmostEqual(
         fcst['additive_terms'][0],
         fcst['yearly'][0] + fcst['weekly'][0] +
         fcst['extra_regressors_additive'][0],
     )
     self.assertAlmostEqual(
         fcst['multiplicative_terms'][0],
         fcst['extra_regressors_multiplicative'][0],
     )
     self.assertAlmostEqual(
         fcst['yhat'][0],
         fcst['trend'][0] * (1 + fcst['multiplicative_terms'][0]) +
         fcst['additive_terms'][0],
     )
     # Check works if constant extra regressor at 0
     df['constant_feature'] = 0
     m = Prophet()
     m.add_regressor('constant_feature')
     m.fit(df)
     self.assertEqual(m.extra_regressors['constant_feature']['std'], 1)