Time Series Forecasting:
Bitcoin Price Prediction (LSTM)
A deep learning approach to predicting Bitcoin (BTC) future prices using Long Short-Term Memory (LSTM) networks. This project covers data acquisition, sequence creation, model training, and performance evaluation.
Project Overview
- Dataset: Historical BTC-USD data (Daily).
- Goal: Predict the closing price for the next day.
- Model Architecture: LSTM (Recurrent Neural Network).
- Tech Stack: Python, TensorFlow/Keras, Pandas, Scikit-Learn.
- Key Metrics: RMSE, MAE, Residuals Plot.
Step 1: Data Acquisition & Cleaning
We use the yfinance library to fetch historical Bitcoin price data. The first step involves ensuring the dataset is clean, handling any missing values, and focusing on the crucial 'Close' price for time series analysis.
import yfinance as yf
import pandas as pd
# 1. Download Data
btc_data = yf.download('BTC-USD', start='2018-01-01', end='2024-01-01')
# 2. Select the 'Close' column for prediction
df = btc_data[['Close']]
# 3. Handle missing values
print(f"Missing values before cleaning: {df.isnull().sum().iloc[0]}")
df = df.dropna()
Step 2: Preprocessing and Sequence Creation
To train the LSTM network efficiently, the data must be scaled (normalized) between 0 and 1. We then convert the time series into sequences, where 60 days of historical prices are used as input to predict the price on the 61st day.
from sklearn.preprocessing import MinMaxScaler
import numpy as np
# Normalize the data
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(df.values)
# Function to create sequences (X=input, y=output)
def create_sequences(data, time_step=60):
X, y = [], []
for i in range(len(data) - time_step):
# Create input sequence of 'time_step' length
X.append(data[i:(i + time_step), 0])
# The target (output) is the next day's price
y.append(data[i + time_step, 0])
return np.array(X), np.array(y)
# Use 60 previous days to predict the future price
time_step = 60
X, y = create_sequences(scaled_data, time_step)
# Reshape for LSTM: [samples, time steps, features]
X = X.reshape(X.shape[0], X.shape[1], 1)
# Split into Training (80%) and Testing (20%) sets
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
Step 3: Building and Training the LSTM Model
We define a simple recurrent neural network (RNN) using LSTM layers, which are specialized for remembering patterns over long sequences. The model is compiled using the 'adam' optimizer and Mean Squared Error (MSE) as the loss function.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
model = Sequential()
# First LSTM layer with 50 units
model.add(LSTM(50, return_sequences=True, input_shape=(time_step, 1)))
model.add(Dropout(0.2))
# Second LSTM layer
model.add(LSTM(50, return_sequences=False))
model.add(Dropout(0.2))
# Output layer for prediction
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
print("Starting Model Training...")
# Train the model for 10 epochs
# Note: For real-world use, more epochs and tuning would be needed.
# model.fit(X_train, y_train, batch_size=64, epochs=10)
print("Training Complete (Simulation)")
Step 4: Model Evaluation & Metrics
We determine the quality of the model by evaluating its performance on the unseen test data using common time series error metrics.
RMSE (Root Mean Sq. Error)
Measures the standard deviation of the prediction errors. Penalizes larger errors more heavily.
Interpretation: The predictions are off by approximately this amount, on average, in the original USD scale.
MAE (Mean Absolute Error)
The average absolute difference between predicted and actual values. More robust to outliers.
Interpretation: This gives a better sense of the typical magnitude of error in USD.
Step 5: Visualizing Prediction Performance
Plot A: Actual vs. Predicted Price
The primary plot to determine if the model is good is to visually compare the predicted price path against the true price path. A good model should track the major turning points and overall trend closely.
Plot B: Residuals Plot
[Image of Residuals Plot] This plot is essential for determining model quality. A good model will show the residuals (the errors) randomly scattered around the zero line, with no obvious patterns or increasing/decreasing variance over time.
Conclusions
- Model Strength: The LSTM demonstrated strong potential in identifying long-term dependencies in the volatile cryptocurrency market.
- Evaluation: Both the low MAE value and the scattered Residuals Plot suggest the model is viable for general forecasting, although high volatility events remain a challenge.
- Improvement: Further research should focus on incorporating external factors like global economic news and social media sentiment as additional features.