Exploring Time Series Forecasting: Uncovering Trends and Patterns
What is Time Series Forecasting?
Time series forecasting is the process of making future value predictions using data from the past. Weather, sales, and stock price predictions are a few examples from various businesses. It has been used more recently to forecast price movements for cryptocurrencies like Bitcoin and Ethereum.
In this article, I would like to show you how to use Time Series Forecasting to implement Reading and Displaying Bitcoin(BTC) Time Series Data using the Autoregressive Integrated Moving Average (ARIMA) model.
Implementation
I will start by reading the historical prices for BTC using the Pandas data reader and yfinance libraries. But first Let’s install it using a pip command in the terminal.
pip install pandas-datareader
pip install yfinance
import Packages
Let’s import the all library itself and relax the display limits on columns and rows:
import pandas_datareader.data as pdr
from statsmodels.tsa.arima.model import ARIMA
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import yfinance as yfin
import numpy as np
from sklearn.metrics import mean_squared_error
yfin.pdr_override()
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
sns.set()yfin.pdr_override()
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
sns.set()start_date = '2021-01-01'
end_date = datetime.datetime.now()
btc_price= pdr.get_data_yahoo(['BTC-USD'],start_date,end_date)
btc_price.head()# let select the column Close: The last price at which BTC was purchased on that day
btc_price = btc_price[['Close']]
# save it as csv file
btc_price.to_csv("btc.csv")btc_df = pd.read_csv("btc.csv")
btc_df.rename(columns = {'Close':'BTC-USD'}, inplace = True)
btc_df.index = pd.to_datetime(btc_df['Date'], format='%Y-%m-%d')
btc_df.head()
Plotting
plt.ylabel('BTC Price')
plt.xlabel('Date')
plt.xticks(rotation=45)
plt.plot(btc_df.index, btc_df['BTC-USD'])
Train/test split
train = btc_df[btc_df.index < pd.to_datetime("2023-1-1", format='%Y-%m-%d')]
test = btc_df[btc_df.index > pd.to_datetime("2023-1-1", format='%Y-%m-%d')]
plt.plot(train, color = "black")
plt.plot(test, color = "red")
plt.ylabel('BTC Price')
plt.xlabel('Date')
plt.xticks(rotation=45)
plt.title("Train/Test split for BTC Data")
plt.show()
Model
# output y
y = train['BTC-USD']
model = ARIMA(y, order = (1,0,1))
model = model.fit()y_pred = model.get_forecast(len(test.index))
y_pred_df = y_pred.conf_int(alpha = 0.05)
y_pred_df["Predictions"] = model.predict(start = y_pred_df.index[0], end = y_pred_df.index[-1])
y_pred_df.index = test.index
y_pred_df.head()y_pred_out = y_pred_df["Predictions"]
plt.plot(train, color = "black")
plt.plot(y_pred_out, color='green', label = 'ARIMA Predictions')
plt.plot(test, color = "red")
plt.ylabel('BTC Price')
plt.xlabel('Date')
plt.xticks(rotation=45)
plt.title("Train/Test split for BTC Data")
plt.legend()
# means squared error
arma_rmse = np.sqrt(mean_squared_error(test["BTC-USD"].values, y_pred_df["Predictions"]))
print("RMSE: ",arma_rmse)
output:
RMSE: 2573.424351673843