This project performs portfolio optimization using historical data of S&P 500 companies. The goal is to create an optimized portfolio that maximizes the Sharpe ratio while minimizing volatility. The optimization process involves:
- Fetching historical price data for S&P 500 companies.
- Calculating sector-wise average closing prices.
- Generating random portfolios and calculating their metrics.
- Running Monte Carlo simulations to evaluate potential portfolios.
- Applying Random Forest Regression for further portfolio optimization.
Ensure you have the following libraries installed:
openpyxlpandasyfinancematplotlibscipyscikit-learnPyPortfolioOptcvxpy
You can install these libraries using pip:
bash pip install openpyxl pandas yfinance matplotlib scipy scikit-learn PyPortfolioOpt cvxpy
-
Setup and Data Fetching:
- Fetch the list of S&P 500 companies and their sectors from Wikipedia.
- Replace ticker symbols for consistency and handle missing data.
- Fetch historical adjusted closing prices for each sector.
-
Data Processing:
- Calculate the average closing prices for each sector.
- Compute the logarithmic returns and remove negative returns.
- Generate random portfolio weights and normalize them.
-
Optimization Metrics:
- Calculate expected returns, volatility, and Sharpe ratio for the portfolios.
- Save the results to Excel files for further analysis.
-
Monte Carlo Simulation:
- Run simulations to generate various portfolios and their metrics.
- Analyze the results to identify the portfolio with the maximum Sharpe ratio and minimum volatility.
-
Machine Learning Enhancement:
- Use Random Forest Regression to predict portfolio weights based on portfolio metrics.
- Evaluate the model performance using Mean Squared Error.
The code is organized into sections as follows:
- Library Installation - Install necessary libraries.
- Data Fetching - Retrieve and preprocess data from the web.
- Data Processing - Calculate averages and log returns.
- Optimization Metrics - Compute portfolio metrics.
- Monte Carlo Simulation - Simulate and analyze portfolios.
- Machine Learning - Train and evaluate a Random Forest model for portfolio weight prediction.
The final output includes:
- Portfolio weights with the highest Sharpe ratio.
- Portfolio weights with the lowest volatility.
- Predicted weights from the Random Forest model.