Predicting Bitcoin Price Using RNN: A Deep Dive into Time Series Forecasting
Bitcoin (BTC) is known for its volatility, which makes it an attractive asset for investors and traders looking to make quick profits. However, predicting its future price movements is no easy feat. In this blog post, I’ll walk you through how I used time series forecasting and Recurrent Neural Networks (RNNs) to predict Bitcoin’s price based on historical data.
What is Time Series Forecasting?
Time series forecasting involves predicting future values based on previously observed data points. It’s a vital tool for finance, weather prediction, and stock market analysis, where the future is often determined by the past. Time series data has a natural temporal ordering, and unlike standard regression tasks, it’s crucial to account for this temporal structure.
For BTC forecasting, I decided to predict the price one hour into the future, using data from the past 24 hours. The data I used came from historical BTC trading activity on the Coinbase and Bitstamp exchanges.
Data Preprocessing: Cleaning the Noise
The raw dataset contained several features including the open price, close price, high and low prices, and transaction volumes. Since not all data points were relevant for forecasting, I simplified the data using the following steps:
Here’s a snippet of the preprocessing code:
data['Timestamp'] = pd.to_datetime(data['Timestamp'], unit='s')
data = data.set_index('Timestamp').resample('H').agg({'Close': 'last'}).dropna()
data['Close'] = (data['Close'] - data['Close'].min()) / (data['Close'].max() - data['Close'].min())
Setting up the tf.data.Dataset for Model Inputs
TensorFlow’s tf.data.Dataset API makes it easy to load large datasets efficiently. After preprocessing the data, I split it into training and validation sets. I created sequences of 24 hours of BTC prices (used as inputs), and the next hour’s price (used as the target).
The tf.data.Dataset API allows you to easily batch, shuffle, and feed data into the model in an efficient manner. Here’s how I set up the datasets:
train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)).batch(32)
val_dataset = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(32)
The RNN Model Architecture
For this task, I opted for a simple RNN architecture. RNNs are particularly well-suited for time series data because they maintain a "memory" of previous data points. Here’s the structure I used:
Here’s the code for the model:
model = tf.keras.Sequential([
layers.SimpleRNN(50, activation='relu', return_sequences=False),
layers.Dense(1)
])
model.compile(optimizer='adam', loss='mse')
I chose Mean Squared Error (MSE) as the loss function because it is commonly used for regression tasks where the goal is to minimize the difference between predicted and actual values.
Model Performance and Results
The model was trained for 10 epochs, with the training and validation loss decreasing steadily. Below is a plot of the training and validation loss:
As you can see, the model’s performance improved with each epoch, and the validation loss reached a low value, suggesting that the model was able to generalize well to unseen data.
Here are some sample predictions from the model (denormalized to the original scale):
While the model performed reasonably well, BTC’s price volatility means that there’s always an inherent uncertainty in forecasting.
Conclusion: My Thoughts on Forecasting BTC
Forecasting BTC price movements is both an exciting and challenging task. Given its volatile nature, even the best models can only predict trends to a certain degree. Neural networks, particularly RNNs, are great for time series data because of their ability to capture temporal dependencies. However, the unpredictable factors that affect BTC (like market sentiment, regulations, and macroeconomic events) make precise predictions difficult.
That being said, my model provided valuable insights into short-term trends. With more data, fine-tuning, and advanced architectures like LSTMs or GRUs, the predictions could be improved further.
You can check out the full project and code on my GitHub.
Feel free to reach out if you have any questions or suggestions. If you’re looking to dive into time series forecasting, BTC price prediction is a great place to start!
This draft outlines a detailed and engaging explanation of your process. You can post this on Medium or LinkedIn with some additional polish and a relevant image for better reach. Make sure to add your GitHub link and any relevant data visualizations like loss curves for better clarity.
Let me know if you need further revisions!
Founder @ INJEN.IO - Machine Learning Studio
6 个月Davis you model is flawed, the train/test split is wrong ! You are testing on the train dataset ! Hence the ??good?? forecasts you obtain… Look at your plot, you essentially predict the past. Test/val must be performed on unseen data. Ps : bitcoin is a random walk which is basically unforecastable. Your forecast plot is a red flag !