Predicting Tech Stock Prices with LSTM Neural Networks
Written on
Chapter 1: Introduction
The stock market operates in a complex and ever-changing environment, influenced by numerous variables, making accurate price forecasting quite difficult. However, recent advancements in machine learning methodologies, particularly Long Short-Term Memory (LSTM) neural networks, have demonstrated significant potential for accurately predicting financial time series data.
In this article, we conduct an in-depth investigation into applying LSTM models to forecast the prices of well-known technology stocks. By utilizing historical stock data, we will evaluate the models' effectiveness in identifying trends and patterns, thereby offering insights that can aid in making informed investment decisions.
Throughout this discussion, we will cover various stages of the analysis, including data preparation, model development, training, assessment, and visualization. Join us as we navigate the intriguing domain of predictive analysis concerning technology stock valuations.
Understanding Time Series Data
Time series data refers to data points indexed in chronological order. This type of data is ubiquitous, making it essential for any data analyst or scientist to understand how to manipulate it. This analysis will focus on stock data from major tech companies like Apple, Amazon, Google, and Microsoft. We'll leverage libraries such as Seaborn and Matplotlib to extract stock information using the yfinance package. Based on historical performance, we will explore various methods to assess risk and predict future stock prices using the LSTM approach.
Using Yahoo Finance for Data Retrieval
To predict stock behavior, our first step is to retrieve historical data, which we can accomplish through Yahoo Finance. The yfinance library provides an efficient and Pythonic way to download market data:
pip install -q yfinance
This command installs the yfinance package, which allows us to fetch financial data seamlessly.
Data Extraction and Preparation
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
from pandas_datareader.data import DataReader
import yfinance as yf
from pandas_datareader import data as pdr
yf.pdr_override()
from datetime import datetime
# Define the tech stocks to analyze
tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN']
end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)
for stock in tech_list:
globals()[stock] = yf.download(stock, start, end)
company_list = [AAPL, GOOG, MSFT, AMZN]
company_name = ["APPLE", "GOOGLE", "MICROSOFT", "AMAZON"]
for company, com_name in zip(company_list, company_name):
company["company_name"] = com_name
df = pd.concat(company_list, axis=0)
df.tail(10)
Several libraries are utilized, including pandas, numpy, matplotlib, and seaborn. These libraries facilitate data handling, numerical computations, and visualization, allowing us to present insights into stock performance effectively.
When examining our data, we observe that it contains numerical values indexed by date. It's important to note that weekends are omitted from this dataset.
Descriptive Statistics of Data
# Summary Stats
AAPL.describe()
This code generates descriptive statistics for Apple Inc.'s stock data, summarizing key metrics like count, mean, and standard deviation.
# General info
AAPL.info()
The info() method provides details about the DataFrame, including the index, columns, and memory usage.
Visualizing Closing Prices
We will visualize the closing prices of these technology stocks to understand their historical performance better:
plt.figure(figsize=(15, 10))
plt.subplots_adjust(top=1.25, bottom=1.2)
for i, company in enumerate(company_list, 1):
plt.subplot(2, 2, i)
company['Adj Close'].plot()
plt.ylabel('Adj Close')
plt.xlabel(None)
plt.title(f"Closing Price of {tech_list[i - 1]}")
plt.tight_layout()
Volume of Trades
Next, we analyze the daily trading volume of the stocks:
plt.figure(figsize=(15, 10))
plt.subplots_adjust(top=1.25, bottom=1.2)
for i, company in enumerate(company_list, 1):
plt.subplot(2, 2, i)
company['Volume'].plot()
plt.ylabel('Volume')
plt.xlabel(None)
plt.title(f"Sales Volume for {tech_list[i - 1]}")
plt.tight_layout()
Calculating Moving Averages
Moving averages help smooth out price data. Here’s how we calculate and visualize them:
ma_day = [10, 20, 50]
for ma in ma_day:
for company in company_list:
column_name = f"MA for {ma} days"
company[column_name] = company['Adj Close'].rolling(ma).mean()
fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(10)
fig.set_figwidth(15)
AAPL[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[0,0])
axes[0,0].set_title('APPLE')
GOOG[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[0,1])
axes[0,1].set_title('GOOGLE')
MSFT[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[1,0])
axes[1,0].set_title('MICROSOFT')
AMZN[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[1,1])
axes[1,1].set_title('AMAZON')
fig.tight_layout()
Daily Returns Analysis
Next, we will evaluate the daily returns:
for company in company_list:
company['Daily Return'] = company['Adj Close'].pct_change()
fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(10)
fig.set_figwidth(15)
AAPL['Daily Return'].plot(ax=axes[0,0], legend=True, linestyle='--', marker='o')
axes[0,0].set_title('APPLE')
GOOG['Daily Return'].plot(ax=axes[0,1], legend=True, linestyle='--', marker='o')
axes[0,1].set_title('GOOGLE')
MSFT['Daily Return'].plot(ax=axes[1,0], legend=True, linestyle='--', marker='o')
axes[1,0].set_title('MICROSOFT')
AMZN['Daily Return'].plot(ax=axes[1,1], legend=True, linestyle='--', marker='o')
axes[1,1].set_title('AMAZON')
fig.tight_layout()
Correlation Between Stocks
To understand how stocks move relative to each other, we can compute and visualize their correlations:
closing_df = pdr.get_data_yahoo(tech_list, start=start, end=end)['Adj Close']
tech_rets = closing_df.pct_change()
plt.figure(figsize=(12, 10))
plt.subplot(2, 2, 1)
sns.heatmap(tech_rets.corr(), annot=True, cmap='summer')
plt.title('Correlation of stock return')
plt.subplot(2, 2, 2)
sns.heatmap(closing_df.corr(), annot=True, cmap='summer')
plt.title('Correlation of stock closing price')
Building the LSTM Model for Prediction
To predict future stock prices, we will build an LSTM model:
from keras.models import Sequential
from keras.layers import Dense, LSTM
# Build the model
model = Sequential()
model.add(LSTM(128, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(LSTM(64, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train the model
model.fit(x_train, y_train, batch_size=1, epochs=1)
Making Predictions and Evaluating the Model
After training the model, we will make predictions and evaluate their accuracy:
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
rmse = np.sqrt(np.mean(((predictions - y_test) ** 2)))
Visualizing Predictions Against Actual Prices
Finally, we will visualize the actual prices against the predicted values:
train = data[:training_data_len]
valid = data[training_data_len:]
valid['Predictions'] = predictions
plt.figure(figsize=(16, 6))
plt.title('Model')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD ($)', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])
plt.legend(['Train', 'Val', 'Predictions'], loc='lower right')
plt.show()
Conclusion
In this notebook, we explored stock market data and learned how to load, visualize, and analyze it using various tools. From understanding correlation to assessing risk and predicting future stock prices using LSTM networks, we covered a comprehensive range of techniques. For any questions or further discussion, feel free to leave comments below.
Video: Stock Price Prediction and Forecasting Using Stacked LSTM - Deep Learning - YouTube
This video provides a practical guide on implementing stacked LSTM models for stock price prediction, showcasing techniques and methodologies.
Video: Stock Price Prediction & Forecasting with LSTM Neural Networks in Python - YouTube
In this video, viewers learn how to utilize LSTM neural networks in Python for forecasting stock prices, with a focus on practical implementation.