In the fast-paced world of financial markets, predicting equity share prices is a challenging yet valuable task. Time series forecasting enables analysts and investors to gain insights into future price movements based on historical patterns.

In this article, we explore how to build a Long Short-Term Memory (LSTM) model to forecast the share price of Toyota Motor Corporation (Ticker: 7203.T) using deep learning techniques. We’ll also explain the model’s design choices, the rationale behind the optimizer and loss function, and how to interpret the prediction accuracy metrics.

👉 Get the full code on GitHub

Why Use LSTM for Time Series Forecasting?

Traditional time series models, such as ARIMA and Exponential Smoothing, rely on assumptions of linearity and stationarity. However, stock market data is often non-linear and influenced by multiple complex factors. LSTM networks—a special type of Recurrent Neural Network (RNN)—are designed to learn from long-term dependencies and non-linear patterns in sequential data.

Key benefits of using LSTM for stock price prediction include:

  • Memory of long-term dependencies: LSTMs can retain information over longer time steps, effectively capturing trends and seasonality.
  • Handling noisy data: They are robust in recognizing patterns even in noisy and volatile financial datasets.
  • Flexibility: LSTM models can be tuned with various hyperparameters to improve prediction accuracy.

    Model Architecture

    Below is the LSTM model architecture used for forecasting Toyota’s stock price:

    model = Sequential([
      LSTM(50, return_sequences=True, input_shape=(X_train.shape[1], 1)),
      Dropout(0.2),
      LSTM(50),
      Dropout(0.2),
      Dense(1)
    ])
    
    model.compile(optimizer='Nadam', loss='mean_squared_error')
    model.fit(X_train, y_train, epochs=15, batch_size=30, verbose=2)
    

    Layer-by-Layer Breakdown

    • Sequential():Defines a linear stack of layers, suitable for models with one input and one output.
    • LSTM(50, return_sequences=True, input_shape=(X_train.shape[1], 1))

      Adds an LSTM layer with 50 memory units (neurons). More units help the network learn complex patterns.
    • return_sequences=True: Outputs a sequence to feed into the next LSTM layer—necessary when stacking multiple LSTM layers.
    • input_shape: Defines the shape of each input sample. (X_train.shape[1], 1) means each input has timesteps equal to the window size and one feature (e.g., closing price).
    • Dropout(0.2): Randomly disables 20% of neurons during training to prevent overfitting and encourage generalization.
    • LSTM(50): A second LSTM layer with 50 units. Since this is the final LSTM, return_sequences is not needed.
    • Dropout(0.2): Another dropout layer to reduce overfitting.
    • Dense(1): A fully connected output layer with a single neuron to predict the next share price value.

    Why Use optimizer='Nadam' and loss='mean_squared_error'?

    optimizer='Nadam':

    • Nadam (Nesterov-accelerated Adaptive Moment Estimation) combines the benefits of the Adam optimizer with Nesterov momentum.
    • It dynamically adapts the learning rate for each parameter.
    • Nesterov momentum helps the model converge faster and more smoothly by anticipating the future gradient direction.
    • This makes Nadam especially effective for noisy data such as stock prices.

      loss='mean_squared_error':

      • Mean Squared Error (MSE) is the standard loss function for regression tasks like price prediction.
      • It penalizes larger errors more severely, ensuring the model focuses on minimizing significant deviations.

      Training Parameters

      • epochs=15: The model will iterate over the entire training dataset 15 times. Too many epochs risk overfitting; too few might underfit.
      • batch_size=30: The number of samples processed before the model’s internal weights are updated. Smaller batch sizes help the model generalize better but increase training time.
      • verbose=2: Controls the level of logging output during training.

      Model Accuracy

      To evaluate performance, metrics were calculated on both the normalized series (scaled data for training) and the level series (actual prices after inverse transformation).

      Metric      Normalized Series     Level Series
      MAPE         5.09%        3.44%
      R²            96%      

      Interpretation:

      • MAPE: Shows the average percentage error. A MAPE of ~3.44% for the level series means the model’s predictions deviate by about 3.44% on average.
      • R² Score: The model explains about 96% of the variance in Toyota’s share price, indicating excellent predictive performance.

      Conclusion

      Using an LSTM network with the Nadam optimizer and MSE loss function provides a robust framework for forecasting Toyota’s share price. The stacked LSTM layers and dropout regularization help capture complex temporal dependencies while reducing overfitting. The final evaluation metrics demonstrate high accuracy and strong generalization, offering a reliable tool for financial time series prediction.

      You can find the full Python code and all resources in the GitHub repository.

      5 Comments

      The more you ask questions, that will enrich the answer, so whats your question?

      1. Good post keep going

        ReplyDelete
      2. Make more prediction

        ReplyDelete
      3. Were do you Reference data for this

        ReplyDelete
      4. The data utilized for this analysis was sourced from Yahoo Finance.

        ReplyDelete

      Post a Comment

      The more you ask questions, that will enrich the answer, so whats your question?

      Previous Post Next Post

      Translate

      AKSTATS

      Learn it 🧾 --> Do it 🖋 --> Get it 🏹📉📊