A comprehensive quantitative finance project that implements Capital Asset Pricing Model (CAPM) regression analysis and Modern Portfolio Theory (MPT) optimization to construct optimal investment portfolios. The system analyzes stock returns, estimates betas, calculates expected returns, and optimizes portfolio allocations to maximize risk-adjusted returns.
- CAPM Regression Analysis: Estimates beta coefficients and alpha for each stock using linear regression against market returns
- Portfolio Optimization: Maximizes Sharpe ratio using scipy optimization to find optimal asset allocations
- Efficient Frontier Visualization: Plots the efficient frontier with Monte Carlo simulation cloud
- Capital Market Line (CML): Displays the risk-return trade-off line combining risk-free assets with optimal risky portfolios
- Comprehensive Reporting: Generates detailed numerical reports comparing optimized portfolios against equal-weight benchmarks
- Regression Visualization: Individual scatter plots with regression lines for each stock showing CAPM relationships
- Python 3.11 or higher
- pip package manager
-
Clone the repository
git clone https://github.com/Dharshan2004/capm-portfolio-optimizer.git cd capm-portfolio-optimizer -
Create a virtual environment (recommended)
python3 -m venv env source env/bin/activate # On Windows: env\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
Run the main script to perform complete portfolio analysis:
python main.pyEdit the tickers list in main.py:
tickers = ['NVDA', 'LULU', 'JPM', 'SLV'] # Add your desired stocksNote: The current stock universe (NVDA, LULU, JPM, SLV) was selected because these are stocks I personally invest in my portfolio. This project serves as both a learning exercise and a practical tool to analyze and optimize my own investment allocations.
- Data Collection: Downloads historical stock and market data from Yahoo Finance
- Regression Analysis: Calculates CAPM betas and alphas for each stock
- Portfolio Optimization: Optimizes asset weights to maximize Sharpe ratio
- Report Generation: Displays comprehensive performance metrics
- Visualization: Generates regression plots and efficient frontier charts
capm-portfolio-optimizer/
βββ main.py # Main execution script
βββ regression.py # CAPM regression functions
βββ optimizer.py # Portfolio optimization functions
βββ helpers.py # Data fetching and utility functions
βββ visualisation.py # Plotting and visualization functions
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ img/ # Generated visualization outputs
βββ CAPM Regression Figure.png
βββ Efficient Frontier Figure.png
βββ Portfolio Optimization Report.png
The CAPM describes the relationship between systematic risk and expected return:
Formula: E(R_i) = R_f + Ξ²_i Γ (E(R_m) - R_f)
Where:
E(R_i)= Expected return of asset iR_f= Risk-free rateΞ²_i= Beta coefficient (sensitivity to market movements)E(R_m)= Expected market return
MPT optimizes portfolio allocation by:
- Minimizing risk for a given level of return, or
- Maximizing return for a given level of risk
The optimization uses the Sharpe Ratio as the objective function:
Sharpe Ratio = (Portfolio Return - Risk-Free Rate) / Portfolio Volatility
The efficient frontier represents the set of optimal portfolios that offer:
- The highest expected return for a given level of risk, or
- The lowest risk for a given level of expected return
The CML shows the risk-return trade-off for portfolios combining:
- Risk-free assets (Treasury bills)
- The optimal risky portfolio (tangent point on efficient frontier)
Formula: E(R_p) = R_f + [(E(R_m) - R_f) / Ο_m] Γ Ο_p
The program generates three main outputs:
-
Regression Models Visualization: Scatter plots showing each stock's relationship with the market, including regression lines, beta coefficients, and R-squared values
-
Portfolio Optimization Report:
- Optimized portfolio weights (trade list)
- Performance metrics (return, volatility, Sharpe ratio)
- Benchmark comparison (optimized vs equal-weight)
-
Efficient Frontier Chart:
- Efficient frontier curve
- Capital Market Line (CML)
- Monte Carlo simulation cloud (color-coded by distance to frontier)
- Optimal portfolio point
- Risk-free rate point
The regression analysis shows how each stock's returns correlate with market movements, providing beta coefficients that measure systematic risk. Each subplot displays the scatter of daily returns with the fitted regression line, showing the CAPM relationship.
The efficient frontier chart displays the optimal risk-return trade-offs available from the portfolio. It includes the efficient frontier curve, Capital Market Line (CML), Monte Carlo simulation cloud (color-coded by distance to optimality), and the optimal portfolio point.
The optimization report provides detailed performance metrics comparing the optimized portfolio against an equal-weight benchmark strategy, including allocation weights, risk metrics, and Sharpe ratio comparisons.
- Python 3.11: Core programming language
- NumPy: Numerical computations and array operations
- Pandas: Data manipulation and analysis
- SciPy: Optimization algorithms (minimize function)
- scikit-learn: Linear regression models
- Matplotlib: Data visualization and plotting
- yfinance: Financial data retrieval from Yahoo Finance
The optimization report includes:
- Portfolio Allocation: Exact percentage weights for each stock
- Performance Metrics: Annualized return, volatility, and Sharpe ratio
- Benchmark Comparison: Side-by-side comparison with equal-weight strategy
- Optimization Validation: Confirms whether optimization improved risk-adjusted returns
Project for: QuantNova
Type: Experiential Learning Project
-
Data Alignment Issues:
- Initially faced challenges aligning stock returns with market returns due to different trading calendars and missing data points
- Solution: Implemented robust data cleaning that aligns datasets by date and handles NaN values appropriately
-
Broadcasting Errors in NumPy:
- Encountered shape mismatches when performing vectorized operations between pandas Series and numpy arrays
- Solution: Explicitly converted pandas Series to numpy arrays using
.valuesand ensured proper type casting
-
Optimization Convergence:
- Portfolio optimization sometimes failed to converge or produced unrealistic corner solutions (100% allocation to one stock)
- Solution: Implemented proper constraint handling, added maximum weight limits, and improved initial guess strategies
-
CAPM Formula Implementation:
- Initially used individual stock mean returns instead of market returns in the CAPM expected return calculation
- Solution: Corrected to use market return in the formula:
E(R_i) = R_f + Ξ²_i Γ (E(R_m) - R_f)
-
Efficient Frontier Calculation:
- Calculating the efficient frontier required solving multiple optimization problems, which was computationally intensive
- Solution: Optimized the number of frontier points and used efficient optimization methods (SLSQP)
-
Visualization Complexity:
- Creating clear, informative visualizations that show multiple concepts (efficient frontier, CML, Monte Carlo cloud) without clutter
- Solution: Used color coding, proper legends, and strategic z-ordering to create layered, interpretable charts
-
Financial Theory Application:
- Gained deep understanding of how CAPM and Modern Portfolio Theory translate from theory to practical implementation
- Learned the importance of proper risk-free rate selection and market proxy choice
-
Numerical Optimization:
- Developed skills in using scipy's optimization functions with constraints and bounds
- Understood the importance of initial guesses and constraint formulation for convergence
-
Data Handling in Finance:
- Learned to handle financial time series data, including log returns, annualization, and covariance matrix calculations
- Gained experience with real-world data issues like missing values and alignment problems
-
Code Organization:
- Practiced modular programming by separating concerns (data fetching, regression, optimization, visualization)
- Learned to avoid duplicate calculations by passing pre-computed values between functions
-
Visualization Best Practices:
- Understood how to create informative financial visualizations that communicate complex concepts clearly
- Learned to use color coding and legends effectively to show multiple data dimensions
-
Portfolio Theory Insights:
- Gained practical understanding of how diversification works through covariance matrices
- Learned why certain stocks get zero weight in optimized portfolios (high correlation or poor risk-adjusted returns)
-
Professional Reporting:
- Developed skills in creating business-focused reports that translate mathematical results into actionable insights
- Learned to present quantitative results in a format accessible to portfolio managers
This project provided valuable hands-on experience in quantitative finance, combining theoretical knowledge with practical implementation skills.


