Stock price forecasting using Monte Carlo simulation and Linear Regression ML model with 95% R² accuracy on AAPL data
A comprehensive stock price forecasting project implementing both statistical simulation and supervised machine learning approaches with train/test validation.
This project explores two different methodologies for stock price prediction:
- Monte Carlo Simulation - Statistical approach using random sampling based on historical volatility
- Linear Regression Model - Supervised machine learning with sliding window feature engineering
Both models are validated using train/test split methodology and evaluated on Apple Inc. (AAPL) historical stock data.
- Simulates 10,000 possible future price paths
- Generates confidence intervals (5th, 50th, 95th percentiles)
- Visualizes uncertainty in predictions
- Based on historical mean returns and volatility
- Sliding window feature engineering (5-day lookback)
- Train/test split validation (80/20)
- Achieved R² score of 0.95 on test data
- Comprehensive performance metrics (RMSE, MAE, R²)
- Provides probabilistic forecasts with confidence intervals
- Captures market uncertainty through 10,000 simulated scenarios
- Useful for risk assessment and scenario analysis
- Test R² Score: 0.9509
- Test RMSE: $3.30
- Test MAE: $2.49
- Successfully predicts next-day prices with 95% variance explained
- Python 3.9+
- Libraries:
yfinance- Financial data acquisitionnumpy- Numerical computingpandas- Data manipulationmatplotlib- Data visualizationscikit-learn- Machine learning algorithms
├── monte_carlo_simulation.py # Monte Carlo implementation
├── linear_regression_model.py # ML model implementation
├── requirements.txt # Dependencies
└── README.md # Project documentation
- Clone the repository:
git clone https://github.com/yourusername/stock-price-prediction.git
cd stock-price-prediction- Install dependencies:
pip install -r requirements.txtpython monte_carlo_simulation.pyGenerates 10,000 price simulations and visualizes confidence intervals.
python linear_regression_model.pyTrains ML model and displays prediction accuracy metrics.
- Calculate historical daily returns and volatility
- Use normal distribution to generate random future returns
- Simulate 10,000 possible price paths
- Aggregate results to show confidence intervals
- Feature engineering with 5-day sliding window
- Train/test split (80/20) for validation
- Linear regression model training
- Performance evaluation on unseen test data
- Time series forecasting techniques
- Statistical modeling vs machine learning approaches
- Train/test validation methodology
- Feature engineering for financial data
- Model evaluation metrics (RMSE, MAE, R²)
The project generates multiple visualizations:
- Monte Carlo simulation paths with confidence bands
- Train/test split timeline
- Scatter plots for prediction accuracy
- Add more technical indicators as features
- Implement LSTM neural network for comparison
- Expand to multiple stocks
- Real-time prediction dashboard
- Incorporate sentiment analysis from news data
MIT License - feel free to use this project for learning purposes.
Binoy Saha
- GitHub: https://github.com/binoysaha025
- LinkedIn: https://www.linkedin.com/in/binoysaha025/
- Data sourced from Yahoo Finance via yfinance API
- Inspired by quantitative finance methodologies