Skip to content

supersonicwisd1/apple_stock_price_prediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Stock Price Prediction using LSTM Neural Networks

This project implements a stock price prediction system using Long Short-Term Memory (LSTM) neural networks to forecast future stock prices based on historical data. The implementation focuses on Apple (AAPL) stock data but can be adapted for other stocks.

Overview

Stock price prediction is a challenging task due to the complex, non-linear nature of financial markets. This project uses LSTM networks, a specialized type of Recurrent Neural Network (RNN), to capture temporal dependencies and patterns in stock price movements.

Why LSTM?

LSTM networks were chosen for this project for several key reasons:

  1. Long-term Dependencies: Unlike traditional RNNs, LSTMs can effectively capture long-term dependencies in time series data through their unique memory cell architecture.

  2. Handling Vanishing Gradients: LSTMs solve the vanishing gradient problem common in standard RNNs, making them better suited for learning from long sequences of data.

  3. Sequential Data Processing: Stock prices are sequential data where past values influence future predictions. LSTMs are specifically designed to process sequential data.

  4. Non-linear Pattern Recognition: LSTMs can capture complex non-linear patterns in stock price movements, which is crucial for accurate predictions.

Project Structure

The project consists of the following main components:

  • apple_stock_prediction.py: Main script containing the LSTM model implementation
  • Data preprocessing and feature engineering
  • Model architecture and training
  • Prediction and visualization

Implementation Details

1. Data Preprocessing

  • Data Collection: Historical stock data is obtained using the yfinance library
  • Feature Scaling: MinMaxScaler is used to normalize data between 0 and 1
  • Sequence Creation: Data is transformed into sequences for LSTM processing
  • Train-Test Split: Data is split into training and testing sets

2. Model Architecture

The LSTM model architecture consists of:

  • Input Layer: Processes sequential data
  • LSTM Layers: Multiple stacked LSTM layers for deep learning
  • Dense Layer: Final layer for price prediction
  • Dropout: Used for regularization to prevent overfitting

3. Training Process

  • Optimizer: Adam optimizer for efficient training
  • Loss Function: Mean Squared Error (MSE) for regression
  • Batch Size: Mini-batch training for better generalization
  • Epochs: Multiple iterations over the training data

4. Prediction and Evaluation

  • Model makes predictions on test data
  • Results are inverse transformed to original scale
  • Visualization of actual vs predicted prices
  • Performance metrics calculation (RMSE, MAE)

Requirements

  • Python 3.x
  • TensorFlow
  • Keras
  • NumPy
  • Pandas
  • yfinance
  • scikit-learn
  • matplotlib

Usage

  1. Clone the repository
  2. Install required dependencies
  3. Run the script:
    python apple_stock_prediction.py

Model Performance and Evaluation Metrics

The model's performance is evaluated using several regression metrics:

  1. Root Mean Square Error (RMSE)

    • Measures the standard deviation of prediction errors
    • More sensitive to large errors due to squaring
    • Same unit as the target variable (stock price)
  2. Mean Absolute Error (MAE)

    • Average absolute difference between predicted and actual values
    • Less sensitive to outliers than RMSE
    • Provides a linear score of model accuracy
  3. Mean Absolute Percentage Error (MAPE)

    • Shows error in percentage terms
    • Useful for understanding relative prediction accuracy
    • Independent of stock price scale
  4. R-squared (R²) Score

    • Indicates how well the model fits the data
    • Ranges from 0 to 1 (1 being perfect prediction)
    • Shows the proportion of variance explained by the model

These metrics are more appropriate for regression problems like stock price prediction, as they measure the magnitude of prediction errors rather than classification accuracy.

Limitations and Considerations

  1. Market Volatility: Stock markets are influenced by many external factors that cannot be captured solely by historical data.
  2. Black Swan Events: Unexpected events can cause significant market movements that are impossible to predict.
  3. Technical Analysis Only: The model relies purely on technical analysis and doesn't consider fundamental factors.

Disclaimer

This project is for educational purposes only. Stock market investments carry risks, and predictions should not be the sole basis for making investment decisions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors