#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Sep 28 11:11:06 2021 @author: sadrachpierre """ import pandas as pd import pandas_datareader as web import datetime import matplotlib.pyplot as plt import seaborn as sns from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.tsa.arima.model import ARIMA pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) # btc = web.get_data_yahoo(['BTC-USD'], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2020, 12, 2)) # btc = btc['Close'] # btc.to_csv("btc.csv") btc = pd.read_csv("btc.csv") btc.index = pd.to_datetime(btc['Date'], format='%Y-%m-%d') del btc['Date'] print(btc.head()) sns.set() plt.ylabel('BTC Price') plt.xlabel('Date') plt.xticks(rotation=45) plt.plot(btc.index, btc['BTC-USD'], ) plt.show() train = btc[btc.index < pd.to_datetime("2020-11-01", format='%Y-%m-%d')] test = btc[btc.index >= pd.to_datetime("2020-11-01", format='%Y-%m-%d')] print(test) plt.plot(train, color = "black", label = 'Training') plt.plot(test, color = "red", label = 'Testing') plt.ylabel('BTC Price') plt.xlabel('Date') plt.xticks(rotation=45) plt.title("Train/Test split for BTC Data") y = train['BTC-USD'] ARMAmodel = SARIMAX(y, order = (1, 0, 1)) ARMAmodel = ARMAmodel.fit() y_pred = ARMAmodel.get_forecast(len(test.index)) y_pred_df = y_pred.conf_int(alpha = 0.05) y_pred_df["Predictions"] = ARMAmodel.predict(start = y_pred_df.index[0], end = y_pred_df.index[-1]) y_pred_df.index = test.index y_pred_out = y_pred_df["Predictions"] plt.plot(y_pred_out, color='green', label = 'ARMA Predictions') plt.legend() import numpy as np from sklearn.metrics import mean_squared_error arma_rmse = np.sqrt(mean_squared_error(test["BTC-USD"].values, y_pred_df["Predictions"])) print("ARMA RMSE: ",arma_rmse) ARIMAmodel = ARIMA(y, order = (5, 4, 2)) ARIMAmodel = ARIMAmodel.fit() y_pred = ARIMAmodel.get_forecast(len(test.index)) y_pred_df = y_pred.conf_int(alpha = 0.05) y_pred_df["Predictions"] = ARIMAmodel.predict(start = y_pred_df.index[0], end = y_pred_df.index[-1]) y_pred_df.index = test.index y_pred_out = y_pred_df["Predictions"] plt.plot(y_pred_out, color='Yellow', label = 'ARIMA Predictions') plt.legend() import numpy as np from sklearn.metrics import mean_squared_error arma_rmse = np.sqrt(mean_squared_error(test["BTC-USD"].values, y_pred_df["Predictions"])) print("ARIMA RMSE: ",arma_rmse) SARIMAXmodel = SARIMAX(y, order = (5, 4, 2), seasonal_order=(2,2,2,12)) SARIMAXmodel = SARIMAXmodel.fit() y_pred = SARIMAXmodel.get_forecast(len(test.index)) y_pred_df = y_pred.conf_int(alpha = 0.05) y_pred_df["Predictions"] = SARIMAXmodel.predict(start = y_pred_df.index[0], end = y_pred_df.index[-1]) y_pred_df.index = test.index y_pred_out = y_pred_df["Predictions"] plt.plot(y_pred_out, color='Blue', label = 'SARIMA Predictions') plt.legend() import numpy as np from sklearn.metrics import mean_squared_error arma_rmse = np.sqrt(mean_squared_error(test["BTC-USD"].values, y_pred_df["Predictions"])) print("SARIMA RMSE: ",arma_rmse)