def generatePredictions(df, N=7): '''Generate forecast using Generalized Additive Models (GAM) which generalizes FBProphet. Input: ====== df: dataframe columns: ds: date y: observed data points (typically log of values in order to model a multiplicative process) N: int number of days of prediction into the future (default value 7) Output: ======= ddf: dataframe columns: ds: date y_hat: predicted value y_low: predicted low y_high: predicted high ''' # GAM m = PMProphet(df, growth=True, intercept=True, n_changepoints=25, changepoints_prior_scale=.01, name='model') # Fit the model (using NUTS) m.fit(method=Sampler.NUTS, draws=2500) # make N predictions into the future ddf = m.predict(N, alpha=0.2, include_history=True, plot=False) return ddf
def test_automatic_changepoints_manning(): df = pd.read_csv("examples/example_wp_log_peyton_manning.csv") m = PMProphet(df, auto_changepoints=True, growth=True, intercept=True, name="model") m.add_seasonality(seasonality=365, fourier_order=3) m.fit(method=Sampler.METROPOLIS) m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def test_manning(): df = pd.read_csv("examples/example_wp_log_peyton_manning.csv") df = df.head(180) m = PMProphet(df, growth=True, intercept=True, name="model") m.add_seasonality(seasonality=30, fourier_order=3) m.fit(method=Sampler.METROPOLIS, draws=2000) m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def test_seasonality_shape(): """ Verify that that the size of the fourier timeseries is correct """ df = pd.read_csv("examples/example_wp_log_peyton_manning.csv") m = PMProphet(df, auto_changepoints=True, growth=True, intercept=True, name="model") m.add_seasonality(seasonality=3, fourier_order=3, mode=Seasonality.ADDITIVE) m.add_seasonality(seasonality=30, fourier_order=3, mode=Seasonality.ADDITIVE) m.fit(method=Sampler.METROPOLIS, draws=2000) m.predict(60, alpha=0.2, include_history=True, plot=False) assert m.trace['seasonality_model'].shape[1] == 12 # (3 + 3) * 2 (sin and cos)
def test_multiplicative_seasonality(): df = pd.read_csv("examples/example_wp_log_peyton_manning.csv") m = PMProphet(df, auto_changepoints=True, growth=True, intercept=True, name="model") m.add_seasonality(seasonality=365, fourier_order=3, mode=Seasonality.MULTIPLICATIVE) m.fit(method=Sampler.METROPOLIS, draws=2000) m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def test_automatic_changepoints(): z = np.arange(200) + np.concatenate([np.zeros(100), np.arange(100) * -2]) df = pd.DataFrame() df["ds"] = pd.date_range(start="2018-01-01", periods=200) df["y"] = z m = PMProphet(df, auto_changepoints=True, growth=True, intercept=True, name="model") m.fit(method=Sampler.METROPOLIS, draws=2000) m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def test_automatic_changepoints_3_funneling_predictions(): deltas = np.random.normal(scale=.1, size=200) y = np.cumsum(deltas) df = pd.DataFrame() df["ds"] = pd.date_range(start="2018-01-01", periods=200) df["y"] = y m = PMProphet(df, auto_changepoints=True, growth=True, name="model") m.fit(method=Sampler.METROPOLIS, chains=1, draws=2000) m.predict(200, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def pm_prophet(train,test, column): # Fit both growth and intercept train = train.reset_index() train['date'] = pd.to_datetime(train['date']) train.columns = ['ds', 'y'] start = time.time() m = PMProphet(train, growth=True, intercept=True, n_change_points=2, name='model') # Add monthly seasonality (order: 3) m.add_seasonality(seasonality=30, order=3) # Add weekly seasonality (order: 3) m.add_seasonality(seasonality=7, order=3) # Fit the model (using NUTS, 1e+4 samples and no MAP init) m.fit( draws=10**4, method='NUTS', map_initialization=False, ) ddf = m.predict(7, alpha=0.4, include_history=True, plot=True) m.plot_components( intercept=False, ) pred = inverse_transform(ddf[-7:], 'y_hat') rmse_pm = sqrt(mean_squared_error(test, pred)) end = time.time() time_fb = end - start print('\n The total RMSE using FB prophet : %.3f' % rmse_pm) result.loc[len(result)] = [column, 'pm_prophet', rmse_pm, time_pm] #test_plt((20,10), test_df, predictions, column, 'pmprophet') result_plots['pm_prophet'] = pred result.loc[len(result)] = [' ', ' ', ' ', ' ']
def test_manning_reduced_six_months(): df = pd.read_csv("examples/example_wp_log_peyton_manning.csv") df = df.head(180) m = PMProphet(df, auto_changepoints=True, growth=True, intercept=True, name="model") m.add_seasonality(seasonality=7, fourier_order=3) m.add_seasonality(seasonality=30, fourier_order=3) m.fit() m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)
def test_multiplicative_seasonality(): z = np.sin(np.linspace(0, 200, 200)) * np.linspace(0, 200, 200) df = pd.DataFrame() df["ds"] = pd.date_range(start="2018-01-01", periods=200) df["y"] = z m = PMProphet(df, auto_changepoints=False, growth=True, intercept=False, name="model") with m.model: m.priors['growth'] = pm.Constant('growth_model', 1) m.add_seasonality(seasonality=3.14 * 2, fourier_order=3, mode=Seasonality.MULTIPLICATIVE) m.fit() m.predict(60, alpha=0.2, include_history=True, plot=True) m.plot_components(intercept=False)