This project demonstrates portfolio optimization and risk analysis using Python.
I've analyzed 5 major US stocks (AAPL, MSFT, TSLA, JNJ, JPM) to build an optimal portfolio using daily returns, risk metrics, and Monte Carlo simulation.
The project workflow includes:
- Data Collection: Downloading daily stock prices from Yahoo Finance.
- Exploratory Data Analysis (EDA): Visualizing price movements, computing daily returns, and checking correlations.
- Portfolio Construction: Building an equal-weighted portfolio and computing performance metrics (returns, volatility, Sharpe ratio, drawdown).
- Performance Attribution: Determining each stock’s contribution to portfolio returns.
- Portfolio Optimization: Using Monte Carlo simulation to find the portfolio with maximum Sharpe ratio.
- Visualization: Plotting cumulative returns, correlation heatmap, and optimal portfolio allocation.
Tools & Libraries:
- Python
- pandas, NumPy
- matplotlib, seaborn
- yfinance
Stocks Analyzed:
- Apple Inc. (AAPL)
- Microsoft Corp. (MSFT)
- Tesla Inc. (TSLA)
- Johnson & Johnson (JNJ)
- JPMorgan Chase & Co. (JPM)
Sample Data (Closing Prices):
| Date | AAPL | JNJ | JPM | MSFT | TSLA |
|---|---|---|---|---|---|
| 2025-01-02 | 242.53 | 139.00 | 233.97 | 414.57 | 379.28 |
| 2025-01-03 | 242.04 | 139.17 | 237.17 | 419.29 | 410.44 |
| 2025-01-06 | 243.67 | 138.66 | 236.01 | 423.75 | 411.05 |
| 2025-01-07 | 240.89 | 141.14 | 238.28 | 418.32 | 394.36 |
| 2025-01-08 | 241.38 | 137.31 | 238.24 | 420.49 | 394.94 |
Visualization:
Daily Returns (sample):
| Date | AAPL | JNJ | JPM | MSFT | TSLA |
|---|---|---|---|---|---|
| 2025-01-03 | -0.00201 | 0.00118 | 0.01367 | 0.01140 | 0.08216 |
| 2025-01-06 | 0.00674 | -0.00368 | -0.00488 | 0.01063 | 0.00149 |
| 2025-01-07 | -0.01139 | 0.01789 | 0.00963 | -0.01281 | -0.04060 |
| 2025-01-08 | 0.00202 | -0.02708 | -0.00016 | 0.00519 | 0.00147 |
| 2025-01-10 | -0.02410 | -0.00148 | -0.01341 | -0.01321 | -0.00051 |
Weights: 20% per stock (equal weight)
Cumulative Return Graph: 
- Initial $1 investment grows to ~$1.30 over the year.
Portfolio Metrics:
| Metric | Value |
|---|---|
| Annual Return | 0.3055 |
| Volatility | 0.2362 |
| Sharpe Ratio | 1.2594 |
| Maximum Drawdown | -0.2251 |
Interpretation:
- Sharpe Ratio 1.26: Portfolio provides good return per unit of risk.
- Max Drawdown -22.5%: Largest peak-to-trough loss.
- Annual Return 30.5%: Total growth over the year.
Contribution to Portfolio Return (Absolute & %):
| Ticker | Absolute | % Contribution |
|---|---|---|
| AAPL | 0.0329 | 11.19% |
| JNJ | 0.0824 | 28.03% |
| JPM | 0.0693 | 23.57% |
| MSFT | 0.0361 | 12.28% |
| TSLA | 0.0733 | 24.92% |
Observation: JNJ, TSLA, and JPM contributed the most to portfolio gains.
Goal: Maximize Sharpe Ratio.
Optimal Portfolio Weights:
| Ticker | Weight |
|---|---|
| MSFT | 0.7448 |
| JNJ | 0.1151 |
| TSLA | 0.0970 |
| JPM | 0.0381 |
| AAPL | 0.0050 |
Visualization:
- MSFT dominates the optimal portfolio (>74% allocation).
- Remaining stocks have minor allocations.
stock_prices.png→ Daily stock price movement
returns_histogram.png→ Daily returns distribution
correlation_heatmap.png→ Correlation between stocks
equal_weight_cum_returns.png→ Equal-weight portfolio cumulative returns
optimal_portfolio_allocation.png→ Optimal weights bar chart
- Efficient Frontier of Simulated Portfolios Each point represents a portfolio with different weight allocations.
- X-axis: Portfolio risk (volatility)
- Y-axis: Expected return
- Color: Sharpe ratio (risk-adjusted return)
Bright points represent portfolios with higher Sharpe ratios, indicating optimal trade-offs between risk and return.
7 Efficient Frontier Visualization The scatter plot shows all simulated portfolios with varying weights.
- X-axis: Portfolio Volatility (Risk)
- Y-axis: Portfolio Expected Return
- Color gradient: Sharpe Ratio (reward per unit risk)
- Red Star: Portfolio with highest Sharpe Ratio (Optimal Portfolio)


