← All posts

Applying Linear Regression on Bitcoin's historical data

Originally published on Medium →

Hi folks! In this article I am going to share with you another learning experience in my path towards Artificial Intelligence: how I used a linear regression model to try to predict Bitcoin’s price based on its historical data.

Do you think it is possible? Let’s see.

The general approach

First things first, there’s a process (a set of questions) that I’m using that is a kind of “general approach” for Machine Learning problems. I learned it in Codecademy’s course “Build a Machine Learning Model With Python.” Here is the deal:

  • What do we want to answer / accomplish? Predict Bitcoin’s tomorrow price based on its historical data.
  • What data is relevant to help us answer this question? BTC historical data provided by Yahoo Finance such as open price, close price, volume of negotiations, etc. The plan is to use this data to try to predict what the Bitcoin value will be the next day.
  • What data cleaning and feature engineering can be done? Removal of empty values, feature normalization, maybe adding quadratic features.
  • Which model best fits the problem? I’m going to use a Linear Regression model because it is my object of study.
  • What is our success metric? I’ll use the Mean Absolute Error to evaluate the model’s predictions — anything below 100 bucks on average would be considered a success.
  • Use the model and present the results. Ok, let’s code!

Project repository

You can check out the project at marciojmo/stock-price-predictor.

Getting started

The first thing I did was actually getting the data from Yahoo Finance. I used the pandas_datareader library to get the data straight from the internet (so cool); we may also change the ticker and grab any stock data we want. After doing that, I used the pandas DataFrame .head() method to visualize what we got.

The goal

Since my goal was to use just this data to predict Bitcoin’s tomorrow price, I added a new column named “Prediction” that is a copy of the “Close” column shifted one position up. This way, every line on the dataset has an array of features (including the close price) mapping to Bitcoin’s closing price of the next day.

Data visualization

With everything set, it was time to plot some values against the prediction price and see if they have some kind of linear relationship (visually). I used a for loop to iterate over all independent variables and plot them against the Prediction value (our dependent variable in this case).

Cleaning and normalizing features

After visualizing the data, I decided to take the Volume column out of the equation because it doesn’t seem to provide a good linear relationship with the prediction price. I also removed empty values using the DataFrame isin() function and normalized the independent variables using the MinMaxScaler from scikit-learn’s preprocessing module.

Training and testing

With our x’s and y’s set, it’s time to train and test our model against the data. I’m using the train_test_split() function from scikit-learn’s model_selection module to split the dataset into training (70%) and testing (30%). Then I created a LinearRegression model from scikit-learn’s linear_model module and trained the model on the training set using the fit() method.

After that, I tested the model against the test data and used the r2_score() and mean_absolute_error() functions from scikit-learn’s metrics module to evaluate the model’s performance:

  • Mean absolute error: $632.79
  • Mean absolute percentage error: 2.53%
  • Coefficient of determination: 1.00

Results

As shown above, the model misses tomorrow’s prediction price with a mean absolute error of $632.79 (2.53%). I really don’t know why the coefficient of determination is 1 in this case, and I’ll leave that to the statistics people to help me explain.

I also ran the model against the last data row to predict tomorrow’s price: the expected price for BTC-USD at 2021-09-11 was $45,074.05 — pretty close to the actual Yahoo Finance value.

Would you bet your money on this algorithm? I wouldn’t. Haha. Better keep studying. See you!

The original article includes code screenshots and plots — check the Medium version or the repository for the full code.