Created
January 14, 2022 16:39
-
-
Save marcosan93/52042820a81f978e5a913cbf9aefa855 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def runningFBP(prices, forecast_hours, training_hours): | |
| """ | |
| Runs Facebook Prophet to get predictions over a set period | |
| of time. Uses FBP to train and predict every hour and gets the | |
| price forecasts in the next N hours. | |
| """ | |
| # DF for the predicted values | |
| pred_df = pd.DataFrame() | |
| # Running the model each day | |
| for i in tqdm(range(training_hours, len(prices))): | |
| # Training and Predicting the last hour on the forecast | |
| forecast = fbpTrainPredict( | |
| prices[i-training_hours:i], | |
| forecast_hours | |
| ).tail(1)[[ | |
| 'ds', | |
| 'yhat', | |
| 'yhat_lower', | |
| 'yhat_upper' | |
| ]] | |
| # Adding the forecast predicted | |
| pred_df = pred_df.append(forecast, ignore_index=True) | |
| # Prepping for merge by converting date values to be the same type | |
| pred_df['ds'] = pred_df['ds'].apply(str) | |
| prices['ds'] = prices['ds'].apply(str) | |
| # Shifting the forecasts back in order to compare it to the 'current' open values | |
| pred_df[['yhat', 'yhat_lower', 'yhat_upper']] = pred_df[['yhat', 'yhat_lower', 'yhat_upper']].shift(-forecast_hours) | |
| # Merging with the prices DF in order to compare values for positions later | |
| merge_df = prices[['ds', 'open']].merge( | |
| pred_df, | |
| on='ds', | |
| how='outer' | |
| ).dropna().set_index('ds') | |
| return merge_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment