Stock Prediction Using Linear Regression
By: Aidan Wilson
Tags: Stocks, Linear Regression, Market Analysis, Data Science, Predictions

Does it work?

Simple Linear Regression

I will briefly touch on simple linear regression in this post, but I do have an article specifically about simple linear regression using Python that can be found here and it may be a bit more detailed and helpful.

Linear regression can be used to find a relationship between two or more variables of interest and allows us to make predictions once these relationships are found. In simple linear regression, there are only two variables: one dependent variable and one independent variable.

Simple linear regression will provide a line of best fit, or the regression line. This regression line can be written as the following formula:

y — The independent variable

— The independent variable ⍺ — The constant/y-intercept

— The constant/y-intercept β — The beta coefficient (slope)

— The beta coefficient (slope) ε — The error term/residuals

Stock Prediction

Stock price data is notoriously difficult, or impossible, to predict. With that being said, lets try.

We will be using SPY for this example. SPY stands for the SPDR S&P 500 Trust ETF and is designed to track the performance of the S&P 500 market index.

First, we will start with importing the packages that we will need for this analysis.

import pandas as pd

import yfinance as yf

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegressionv

Then we can use yfinance to get our data and quickly visualize the closing price data.

data = yf.download('SPY')

plt.figure(figsize=(10, 5))

data['Close'].plot()

The index of our data contains the date for each closing price. To make things easier, lets just move the dates into their own column using the .reset_index() function provided by Pandas. We can then make input data a NumPy array and reshape it to the required input shape for the linear regression. Our output data will be the closing price and we will store that in the y variable.

data = data.reset_index()

x = np.array(data.index).reshape(-1, 1)

y = data['Close']

Now we can use Scikit-learn’s LinearRegression model and fit it to our input and output data. To get the regression line, the .predict() will be used to get the model’s predictions for each x value.

linreg = LinearRegression().fit(x, y)

linreg.score(x, y)

predictions = linreg.predict(x)

We can plot the actual closing price data and then plot the regression line on top of it.

plt.figure(figsize=(15,5))

plt.plot(data['Close'])

plt.plot(data.index, predictions)

In this specific example, using SPY, we can see that while the regression line is not exactly accurate in exact closing price values, it does capture the general trend of the data.

We can extend the regression line to view where the model predicts the SPY closing price will end up in the future.

We can also check the R² score by using the .score() function. This R² value of about .76 is pretty good and means our model did pretty well explaining the variance in the data.

print('R^2:', linreg.score(x, y)) R^2: 0.7562986239574692

Limitations

Simple linear regression will not make you millions of dollars. Relying on this strategy will most likely make you lose significantly more than you would win. This linear regression model will fail if any random and unpredictable event occurs.

Not every stock has a steady linear increase. To illustrate this point, let's look at Nikola Corp. and their closing price along with the regression line.

NKLA Closing Price

This does not look to promising. Furthermore, we can look at Apple Inc, which has had great success. Apple’s stock is growing exponentially and simple linear regression will not be able to capture this.