Daniel Varga commited on
Commit
969cffd
1 Parent(s): 8ec3059
Files changed (1) hide show
  1. demo_prophet.py +15 -15
demo_prophet.py CHANGED
@@ -22,6 +22,11 @@ def prophet_backend(train_data, forecast_horizon):
22
  yearly_seasonality=False, weekly_seasonality=True, daily_seasonality=True,
23
  holidays=HOLIDAY_DF)
24
 
 
 
 
 
 
25
  model.fit(train_data)
26
 
27
  # Create a DataFrame with future timestamps for the evaluation period
@@ -34,17 +39,7 @@ def prophet_backend(train_data, forecast_horizon):
34
  for key in ('yhat', 'yhat_lower', 'yhat_upper'):
35
  forecast[key] = np.maximum(forecast[key], PREDICTION_LOWER_BOUND)
36
 
37
- return forecast
38
-
39
-
40
- def sklearn_backend(train_data, forecast_horizon):
41
- dc = train_data[['y']]
42
- # inserting new column with yesterday's consumption values
43
- for i in range(1, 4 * 24 + 1):
44
- dc.loc[:, 'd%02d' % i] = dc.loc[:,'y'].shift(4 * 24 + i) # t-2days to t-1day
45
- dc.loc[:, 'w%02d' % i] = dc.loc[:,'y'].shift(7 * 4 * 24 + i) # t-7days to t-8days
46
- dc.info()
47
- exit()
48
 
49
 
50
  def prediction_task(backend, df, split_date, forecast_horizon):
@@ -53,12 +48,15 @@ def prediction_task(backend, df, split_date, forecast_horizon):
53
  eval_data = df[df['ds'] > split_date]
54
  eval_data = eval_data.head(forecast_horizon)
55
 
56
- forecast = backend(train_data, forecast_horizon)
57
 
58
  mae = mean_absolute_error(eval_data['y'], forecast['yhat'])
59
 
60
  do_vis = False
61
  if do_vis:
 
 
 
62
  plt.figure(figsize=(12, 6))
63
  plt.plot(eval_data['ds'], eval_data['y'], label='Actual', color='blue')
64
  plt.plot(forecast['ds'], forecast['yhat'], label='Predicted', color='red')
@@ -69,14 +67,14 @@ def prediction_task(backend, df, split_date, forecast_horizon):
69
  plt.legend()
70
  plt.grid(True)
71
  plt.show()
72
- '''
73
  fig1 = model.plot(forecast)
74
  plt.plot(eval_data['ds'], eval_data['y'], c='r')
75
  plt.show()
76
- '''
77
 
78
  fig2 = model.plot_components(forecast)
79
  plt.show()
 
80
 
81
  return mae, eval_data['y'].mean()
82
 
@@ -100,7 +98,8 @@ df['y'] = df['Consumption']
100
 
101
 
102
  # TODO 15 minutes timestep hardwired!
103
- forecast_horizon = 7 * 24 * 4
 
104
 
105
 
106
  start_date = '2021-06-01'
@@ -112,6 +111,7 @@ weekly_date_range = pd.date_range(start=start_date, end=end_date, freq='8d')
112
  maes = []
113
  mean_values = []
114
  for split_date in weekly_date_range:
 
115
  mae, mean_value = prediction_task(prophet_backend, df, split_date, forecast_horizon)
116
  maes.append(mae)
117
  mean_values.append(mean_value)
 
22
  yearly_seasonality=False, weekly_seasonality=True, daily_seasonality=True,
23
  holidays=HOLIDAY_DF)
24
 
25
+ # we can also play with setting daily_seasonality=False above, and then manually adding
26
+ # model.add_seasonality("daily", 1, fourier_order=10, prior_scale=100, mode="multiplicative")
27
+ # ...it didn't really work though. bumping the fourier_order helps, but makes the model slow.
28
+ # the rest didn't have much effect.
29
+
30
  model.fit(train_data)
31
 
32
  # Create a DataFrame with future timestamps for the evaluation period
 
39
  for key in ('yhat', 'yhat_lower', 'yhat_upper'):
40
  forecast[key] = np.maximum(forecast[key], PREDICTION_LOWER_BOUND)
41
 
42
+ return forecast, model
 
 
 
 
 
 
 
 
 
 
43
 
44
 
45
  def prediction_task(backend, df, split_date, forecast_horizon):
 
48
  eval_data = df[df['ds'] > split_date]
49
  eval_data = eval_data.head(forecast_horizon)
50
 
51
+ forecast, model = backend(train_data, forecast_horizon)
52
 
53
  mae = mean_absolute_error(eval_data['y'], forecast['yhat'])
54
 
55
  do_vis = False
56
  if do_vis:
57
+ future = model.make_future_dataframe(periods=forecast_horizon, freq='15T', include_history=True)
58
+ forecast = model.predict(future)
59
+
60
  plt.figure(figsize=(12, 6))
61
  plt.plot(eval_data['ds'], eval_data['y'], label='Actual', color='blue')
62
  plt.plot(forecast['ds'], forecast['yhat'], label='Predicted', color='red')
 
67
  plt.legend()
68
  plt.grid(True)
69
  plt.show()
70
+
71
  fig1 = model.plot(forecast)
72
  plt.plot(eval_data['ds'], eval_data['y'], c='r')
73
  plt.show()
 
74
 
75
  fig2 = model.plot_components(forecast)
76
  plt.show()
77
+ exit()
78
 
79
  return mae, eval_data['y'].mean()
80
 
 
98
 
99
 
100
  # TODO 15 minutes timestep hardwired!
101
+ forecast_horizon = 24 * 4
102
+ print("forecast horizon", forecast_horizon // 4, "hours")
103
 
104
 
105
  start_date = '2021-06-01'
 
111
  maes = []
112
  mean_values = []
113
  for split_date in weekly_date_range:
114
+ # prophet_backend is the only backend currently
115
  mae, mean_value = prediction_task(prophet_backend, df, split_date, forecast_horizon)
116
  maes.append(mae)
117
  mean_values.append(mean_value)