Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created September 23, 2021 19:23
Show Gist options
  • Select an option

  • Save marcosan93/4502f65f7900c29c9fc0ab9c0e2d02f9 to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/4502f65f7900c29c9fc0ab9c0e2d02f9 to your computer and use it in GitHub Desktop.
def runningFBP(ticker, window=730, ma_period=5, days_to_train=365, forecast_period=10):
"""
Runs the facebook prophet model over the provided ticker. Trains with last N days given
by days_to_train. Forecast N days into the future based on given forecast_period. Moving average
is applied to the dataset based on given ma_period. Returns the root mean squared error and a DF
of the actual values and the predicted values for the same day.
"""
# Getting and Formatting Data
df = getData(ticker, window=window, ma_period=ma_period)
# DF for the predicted values
pred_df = pd.DataFrame()
# Running the model on each day
for i in tqdm(range(days_to_train, window-forecast_period, forecast_period)):
# Training and Predicting the last day on the forecast
forecast = fbpTrainPredict(df[i-days_to_train:i],
forecast_period=forecast_period).tail(forecast_period)[['ds',
'yhat',
'yhat_lower',
'yhat_upper']]
# Adding the last day predicted
pred_df = pred_df.append(forecast, ignore_index=True)
# Combining the predicted df and original df
comb_df = df[['ds', 'Open']].merge(pred_df,
on='ds',
how='outer').sort_values(by='ds')
# Setting the index to the dates
comb_df.set_index('ds', inplace=True)
return comb_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment