Skip to main content

Stock price predictions with machine learning

This article unveils the role of neural networks and machine learning in forecasting stock market trends and movements.
Created on February 15|Last edited on May 1



Table of Content



Introduction

In today’s financial markets, the demand for accurate stock predictions is higher than ever. As a result, many are turning to the latest advancements in machine learning to find an edge in predicting how stock prices will move. Neural networks, which mimic the human brain's structure, are at the forefront of these technological advancements, offering new possibilities for understanding and forecasting the complex patterns of stock market movements.
The article guides readers through a comprehensive exploration of stock prediction using neural networks. We will start by dissecting the multifaceted nature of the stock market, highlighting the key factors that influence stock prices, from macroeconomic indicators to company-specific performances. Following this, we introduce the concept of neural networks, their structure, function, and the revolutionary way they learn from data to make predictions.
Through this step-by-step walkthrough, we will demonstrate the implementation of a neural network model tailored for stock price forecasting, covering everything from data preparation and model building to training and evaluation. Moreover, we will address the challenges encountered in the process and propose practical solutions to enhance machine learning model accuracy and reliability.
This journey aims to demystify the application of neural networks in stock prediction and equip readers with the knowledge to harness their power in navigating the complexities of financial markets.

Stock market dynamics

The stock market is a complex and multifaceted entity, influenced by a myriad of factors ranging from macroeconomic indicators to individual company performance and overall market sentiment. Such factors do include:
1. Economic Indicators
  • GDP Growth
  • Interest Rates
  • Inflation
2. Company Performance
  • Earnings Reports
  • Revenue Growth
  • Debt Levels
3. Market Sentiment
  • Investor Confidence
  • Speculation
4. Political and Regulatory Environment
  • Tax Policies
  • Trade Policies

However, even with the complexity and the potential for unexpected changes in the stock market, our objective remains to uncover hidden patterns. By analyzing historical data and understanding the interplay of various influencing factors, we strive to learn from the past. This learning enables us to make more informed and better predictions about future stock market movements, aiming to navigate the uncertainties of the financial markets with greater insight and confidence.

Introduction to neural networks

Neural networks are a foundational element of artificial intelligence (AI) and machine learning, drawing inspiration from the structure and function of the human brain. They are designed to recognize patterns and solve complex problems by learning from data.
We will briefly explain how Neural Networks works, as understanding the basics of neural networks, including their architecture, activation functions, and learning mechanisms, is crucial for leveraging their capabilities in various applications, such as stock prediction.
A neural network consists of layers of interconnected nodes or neurons, each resembling a simplified version of a human neuron. The typical structure comprises three main types of layers: input, hidden, and output.

  • Input Layer: This layer receives the raw data. Each neuron in the input layer represents a feature of the dataset. For example, in stock market predictions, features could include previous stock prices, trading volume, or economic indicators.
  • Hidden Layers: Between the input and output layers are one or more hidden layers, which perform computations through their neurons. These layers extract and process features from the input data, capturing complex relationships within the data. The number and size of hidden layers can significantly impact the network's ability to model complex problems.
  • Output Layer: The final layer produces the neural network's output, which could be a continuous value (for regression problems) or a category (for classification problems). In stock prediction, the output might be the predicted price of a stock.

Activation functions

Activation functions are mathematical equations applied to a neuron's input, determining whether it should be activated ("fired") or not. These functions introduce non-linear properties to the network, enabling it to learn complex patterns:
Sigmoid: A function that outputs values between 0 and 1, useful for binary classification.
ReLU (Rectified Linear Unit): Outputs the input directly if it is positive, otherwise, it outputs zero. It's widely used for its computational efficiency and simplicity.
Tanh (Hyperbolic Tangent): Outputs values between -1 and 1, making it useful in hidden layers of a neural network.

The stock price dataset we'll use


The Google Stock Prediction dataset on Kaggle serves as an invaluable resource for practitioners and learners in the field of Deep Learning (DL), especially those interested in exploring the complexities of time series forecasting with models like Recurrent Neural Networks (RNNs) and Long Short-Term Memory networks (LSTMs). Containing 1,257 rows and 14 columns, this dataset is tailored to provide a detailed snapshot of Google's stock performance over a specified period.

Dataset content

Each of the 14 columns represents a unique attribute of the stock's performance, with the rows capturing the daily values for these attributes:
  1. Symbol: The ticker symbol of the company, which is Google in this case.
  2. Date: The date of the trading session, providing a temporal dimension for analysis.
  3. Close: The stock's closing price for the trading day.
  4. High: The highest price at which the stock traded during the day.
  5. Low: The lowest price at which the stock traded during the day.
  6. Open: The price at which the stock opened the trading day.
  7. Volume: The total number of shares traded during the day.
  8. AdjClose: The stock's closing price after adjustments for stock splits and dividend distributions.
  9. AdjHigh: The adjusted highest price for the day.
  10. AdjLow: The adjusted lowest price for the day.
  11. AdjOpen: The adjusted opening price.
  12. AdjVolume: The adjusted volume of stock traded.
  13. DivCash: The dividend cash amount declared by the company for the stock.
  14. SplitFactor: The factor by which the stock's existing shares are divided to form a larger number of proportionally less valuable shares.

What can we use the Google Stock Prediction dataset for?

This Google Stock Prediction dataset is specifically designed to act as a stepping stone for those venturing into stock predictions using advanced machine learning techniques. With its comprehensive range of features, including both raw and adjusted stock prices along with trading volume, it offers a rich base for:
  1. Developing predictive models: Using RNNs and LSTMs to predict future stock prices, particularly focusing on predicting the 'close' and 'open' values for the next 30 days.
  2. Understanding stock market dynamics: Analyzing the dataset can help understand how different factors like trading volume and stock splits affect stock prices.
  3. Technical analysis for trading strategies: Employing the dataset to apply technical analysis and develop trading strategies based on historical price movements and volumes.

Implementing a stock prediction model using a neural network

Step 1: Importing the necessary libraries

This step involves importing all the necessary libraries for our task such as:
  • pandas and numpy for data manipulation.
  • train_test_split from sklearn to divide the data into training and testing sets.
  • MinMaxScaler from sklearn for feature scaling, normalizing the input features to a range between 0 and 1.
  • tensorflow and keras for building and training the neural network model.
  • matplotlib for plotting the results.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt

Step 2: Import and initialize a new Weights & Biases run

import wandb
from wandb.keras import WandbCallback

# Initialize a new W&B run
wandb.init(project='google_stock_prediction', entity='Insert your W&B username here')

Step 3: Saving the model configuration into Weights & Biases

config = wandb.config
config.learning_rate = 0.001
config.epochs = 100
config.batch_size = 32
config.hidden_layers = [50, 50] # Example configuration

Step 4: Preparing the dataset

Here, the stock price data is loaded from a CSV file. The Date column is converted into a datetime format and then transformed into its ordinal representation (a single integer representing the date) to be used as a feature for model training.
Load the dataset
file_path = '/kaggle/input/google-stock-prediction/GOOG.csv' # Update this path
data = pd.read_csv(file_path)
Convert 'Date' to datetime and extract ordinal as a feature
data['date'] = pd.to_datetime(data['date'])
data['date'] = data['date'].map(pd.Timestamp.toordinal)

Step 5: Data preprocessing for model training

This section prepares the data for the neural network:
  • The date column is used as the feature (X), and the close price as the target variable (y).
  • The features are scaled using MinMaxScaler to improve the neural network's convergence during training.
  • The dataset is split into training and testing sets, with 80% used for training and 20% for testing.
Define features and target variable
X = data[['date']].values # Convert to NumPy array for TensorFlow compatibility
y = data['close'].values # Convert to NumPy array
Normalize the features
scaler = MinMaxScaler(feature_range=(0, 1))
X_scaled = scaler.fit_transform(X)
Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=0)

Step 6: Building and training the neural network for stock price prediction

In this step, a neural network is constructed using TensorFlow's Keras API:
  • The model is sequential, meaning layers are added in sequence.
  • Two hidden layers with 50 neurons each and ReLU activation functions are added to introduce non-linearity.
  • The output layer has a single neuron (since it's a regression problem) without an activation function.
  • The model is compiled with the Adam optimizer and mean squared error loss function.
  • It's then trained on the training data for 100 epochs in batches of 32.
Initialize the neural network
model = Sequential()
model.add(Dense(units=50, activation='relu', input_dim=1))
model.add(Dense(units=50, activation='relu'))
model.add(Dense(units=1))
Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
Train the model
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=0)

Step 7 Evaluating and plotting the stock price prediction model

Finally, the trained model is used to make predictions on the test set. The actual vs. predicted stock prices are plotted to visualize the model's performance. The inverse_transform method is used to convert the scaled date values back to their original format for plotting.
Make predictions
predictions = model.predict(X_test)
Plot the results
plt.figure(figsize=(10, 6))
plt.scatter(scaler.inverse_transform(X_test), y_test, color='red', label='Actual Close Price')
plt.scatter(scaler.inverse_transform(X_test), predictions, color='blue', label='Predicted Close Price', alpha=0.5)
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.title('Google Stock Price Prediction with Neural Network')
plt.legend()

# Save plot to file
plot_filename = "prediction_plot.png"
plt.savefig(plot_filename)
plt.close()
Log the plot into W&B
wandb.log({"Prediction vs Actual": wandb.Image(plot_filename)})
wandb.finish()

Explaining the final results



In the above graph that we have logged into Weights & Biases, the red markers signify the actual stock prices, while the blue markers represent the model's predictions. The visual comparison illustrates that the model's forecasts closely align with the true stock prices, indicating a high level of accuracy in its predictions.
However, a closer examination reveals a trend: the model's predictions are more precise for earlier dates compared to those further out in time. This observation suggests that the model's predictive accuracy tends to diminish over longer forecast horizons. This phenomenon could be attributed to several factors, including the increasing uncertainty associated with predicting future market movements and the potential non-stationarity of financial time series data.
Over time, new information becomes available, market conditions change, and the underlying dynamics that drive stock prices may evolve, making it more challenging for the model, which is trained on historical data, to accurately forecast future prices. This trend underscores the importance of continuously updating the model with recent data and possibly revisiting the model's architecture or training approach to adapt to changing market dynamics for maintaining its predictive performance over time.

Challenges to predicting stock prices with this dataset

At the forefront of the challenges is the relatively small size of the dataset utilized in this article. The Google Stock Prediction dataset comprises only 1,257 entries. Although this amount is not negligible, it falls short of the ideal dataset size required for training a robust model.
One of the challenges is that the current dataset primarily focuses on historical stock prices and volumes. This limitation overlooks the multifaceted nature of stock market movements, which are influenced by a wide array of factors beyond past prices.
Another limitation is that the basic features derived from stock prices (e.g., open, close, high, low, volume) might not capture complex patterns or the predictive signals required for accurate forecasting.
Lastly, overfitting the training data is a common challenge, leading to poor generalization on unseen data. A solution does such an issue would be implementing regularization techniques (e.g., L1, L2, dropout) and systematically tuning hyperparameters (e.g., learning rate, number of layers, number of neurons) using approaches like grid search or random search can help find the optimal model configuration that balances bias and variance.

Conclusion

In this article, we've shown how machine learning can be a game-changer for predicting stock market movements. By breaking down the complex factors that drive stock prices and leveraging the advanced capabilities of neural networks, we've highlighted a method that could significantly improve the accuracy of stock predictions. This approach not only offers a deeper understanding of market dynamics but also presents a practical tool for investors and analysts seeking to make more informed decisions.
As we move forward, the role of machine learning in financial analysis is set to grow even more important. The continuous advancements in technology promise to enhance these models further, making stock market predictions more reliable and precise. This progress marks an exciting time for the finance industry, promising a future where investment strategies are increasingly driven by the insights provided by neural network models, transforming the landscape of financial decision-making.
Iterate on AI agents and models faster. Try Weights & Biases today.