Forecasting Microsoft Stock with LSTM

Apr 8, 2025

Lecture Notes: Forecasting Microsoft Stock using LSTM Neural Networks

Introduction

  • Presenter: Greg Hogg
  • Focus: Forecasting Microsoft stock using LSTM (Long Short Term Memory) neural networks.
  • Importance: Useful project for resumes, recommended to watch the entire video for clarity.

Data Acquisition

  • Source: Yahoo Finance, Microsoft Corporation stock page.
  • Steps to Download:
    • Change time period from 1 year to max.
    • Download CSV file.
    • Import CSV into Google Colab using pandas.

Data Preparation

  • Initial Setup:
    • Import necessary libraries: pandas, datetime.
    • Load CSV data with pandas.read_csv().
  • Data Details:
    • 9082 rows of stock data from 1986 to March 23, 2022.
    • Columns: date, open, high, low, close, adjusted close, volume.
  • Data Cleaning:
    • Focus on 'date' and 'close' columns.
    • Convert date strings to datetime objects using a custom function.
    • Set 'date' as the index of the dataframe.

Data Visualization

  • Plotting:
    • Use matplotlib to plot stock data from 1986 to 2022.
    • Notable trend: significant stock increase post-2016.

Problem Conversion to Supervised Learning

  • Method:
    • Create a function to convert dataframe to windowed dataframe for model input.
    • Generate a target date and values for three previous days for each date.

Preparing Data for TensorFlow

  • Conversion:
    • Convert data to NumPy arrays suitable for TensorFlow.
    • Separate data into dates, input matrix (X), and output vector (Y).
  • Data Splitting:
    • Split data into training (80%), validation (10%), and test (10%) datasets.

Building the LSTM Model

  • Tools: TensorFlow and Keras.
  • Model Specification:
    • Sequential model with input layer, LSTM layer (64 units), and dense layers (32 units each).
    • Compile using mean squared error as loss function, Adam optimizer.
  • Training:
    • Train model on training data, validate on validation data.
    • Monitor mean absolute error for validation.

Model Evaluation

  • Performance:
    • Model performs well on training data.
    • Validation and test predictions show poor extrapolation capabilities.

Improvement Strategies

  • Re-training with Different Data:
    • Train model on more recent data (post-2021), improve validation/test performance.
    • Adjust data split accordingly.

Recursive Forecasting

  • Methodology:
    • Use last known observations to predict future values recursively.
    • Evaluate long-term prediction capabilities.
  • Conclusion:
    • LSTM models struggle with extrapolating beyond the trained range.
    • Stock prediction is inherently difficult, especially for specific future dates.

Final Thoughts and Recommendations

  • Limitations: LSTM models may not predict well outside trained data range.
  • Stock Prediction Advice: Model suitable for long-term trend analysis rather than daily predictions.
  • Conclusion: Understanding stock trends is complex, and predictions should be made cautiously.