📌 Portfolio Optimization Based on CAPM and Mean-Variance Optimization (MVO) Chinese
This project applies the Capital Asset Pricing Model (CAPM) and Mean-Variance Optimization (MVO) to compute the optimal investment portfolio using Quadratic Programming (QP).
- Calculate CAPM parameters (β value, expected return, idiosyncratic risk)
- Optimize portfolio weights to minimize risk
- Plot the Efficient Frontier to analyze risk-return trade-offs
📂 FinancialMachineLearning
│── 📂 doc # Project Documentation
│ │── 📜 CF969.pdf # Report Example
│ │── 📜 CF969 - SP ZU - Assignment.pdf # Assignment Requirements
│── 📂 report # Research Reports
│ │── 📜 report_cn.md # Chinese Report
│ │── 📜 report_en.md # English Report
│ │── 📜 capm_regression_*.png # CAPM Regression Analysis Charts
│ │── 📜 efficient_frontier.png # Efficient Frontier Chart
│ │── 📜 idiosyncratic_risk.png # Idiosyncratic Risk Bar Chart
│ │── 📜 optimal_portfolio_weights.png # Optimal Portfolio Weights Chart
│── 📂 result # Computation Results
│ │── 📜 adj_close_data.csv # Adjusted Closing Price Data
│ │── 📜 capm_results.csv # CAPM Calculation Results
│ │── 📜 efficient_frontier.png # Efficient Frontier Chart
│ │── 📜 optimal_portfolio_weights.csv # Optimal Portfolio Weights
│ │── 📜 returns_data.csv # Daily Returns Data
│── 📜 .gitignore # Git Ignore File
│── 📜 all_code.png # Code Screenshot
│── 📜 financial.ipynb # **Jupyter Notebook Containing All Code**
│── 📜 README.md # README (English)
│── 📜 README_cn.md # README (Chinese)
This project runs on Python 3.12.7, and it is recommended to use Conda for environment setup:
conda create -n financial python=3.12.7
conda activate financialpip install -r requirements.txtOr manually install:
pip install numpy pandas matplotlib statsmodels cvxopt yfinanceAll code is stored in financial.ipynb, which includes data retrieval, CAPM calculation, portfolio optimization, and visualization.
Follow the steps below to run the project:
jupyter labOr:
jupyter notebookThen open financial.ipynb and execute the cells sequentially.
1️⃣ Data Retrieval
- Fetch historical adjusted closing prices from Yahoo Finance
- Compute daily returns
- Save results to
adj_close_data.csvandreturns_data.csv
2️⃣ CAPM Computation
- Compute β values (Beta), expected returns, and idiosyncratic risk
- Store results in
capm_results.csv
3️⃣ Optimal Portfolio Computation
- Use Quadratic Programming (QP) to compute optimal weights
- Target returns: 0.08, 0.12, 0.15
- Save results in
optimal_portfolio_weights.csv
4️⃣ Plot Efficient Frontier
- Compute portfolio risk
- Generate Efficient Frontier chart (
efficient_frontier.png)
During the computation process, several key issues were encountered. Below are their descriptions and solutions:
(Complete issue description and solution remain unchanged)
✅ Expand asset classes (e.g., bonds, ETFs) for better portfolio optimization
✅ Incorporate market volatility modeling using GARCH or Fama-French factor models
✅ Optimize portfolio weights using dynamic asset allocation (DFA) strategies
🔹 Email: generalfly666@outlook.com
🔹 GitHub: GitHub Repository
🔹 Contribution: Fork this repository and submit a Pull Request.
🚀 This project optimizes CAPM calculations and portfolio optimization methods. Contributions and discussions are welcome! 😊
