Last active
January 4, 2022 01:44
-
-
Save marcosan93/22b45cb517bf077a87e9c9e060543221 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_period, training_days): | |
| """ | |
| Runs Facebook Prophet to get predictions over a set period | |
| of time. Uses FBP to train and predict every N days and gets the | |
| price forecasts. | |
| """ | |
| # DF for the predicted values | |
| pred_df = pd.DataFrame() | |
| # Running the model each day | |
| for i in tqdm(range(training_days, len(prices))): | |
| # Training and Predicting the last day on the forecast | |
| forecast = fbpTrainPredict( | |
| prices[i-training_days:i], | |
| forecast_period | |
| ).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(lambda x: str(x)[:10]) | |
| prices['ds'] = prices['ds'].apply(lambda x: str(x)[:10]) | |
| # 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_period) | |
| # 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