Finance
Portfolio Optimization in Python
Build an efficient frontier, compute the Sharpe-optimal portfolio, and visualize portfolio weights using Monte Carlo simulation.
What
This AI Data Analyst workflow downloads two years of daily prices for AAPL, MSFT, GOOGL, AMZN, and JPM (or generates correlated synthetic returns if needed) and computes daily returns. It calculates and visualizes the return correlation matrix across the five assets. It runs a Monte Carlo simulation of 5,000 random portfolios to build an efficient frontier, then identifies the maximum Sharpe ratio portfolio and visualizes the resulting weights.
Who
This is for analysts and students who want a reproducible, code-generating example of mean-variance portfolio optimization in Python. It helps users compare risk, return, and diversification effects across multiple stocks and extract an interpretable set of portfolio weights.
Tools
- yfinance
- pandas
- numpy
- matplotlib
- seaborn
Outcomes
- Price DataFrame for the selected tickers over ~2 years
- Daily returns and a 5x5 correlation heatmap
- Efficient frontier scatter plot of risk vs return colored by Sharpe ratio
- Maximum Sharpe portfolio metrics (Sharpe, expected return, volatility) and printed optimal weights
- Portfolio weights visualization (pie chart)
Quality Score
7/10
Last scored: Apr 7, 2026
Task Completion: 1/2
Needs workDownloaded prices, computed returns/correlation, ran 5000-portfolio Monte Carlo, and identified max-Sharpe weights. However, the expected optimal-weights pie chart is not present, and the price dataframe is not in the expected ~(504, 5) form (it includes 30 OHLCV fields).
Execution Correctness: 2/2
ExcellentCode shown is coherent and likely runnable: yfinance download, adjusted-close extraction, returns/covariance, simulation loop, plotting, and summary extraction are syntactically correct.
Output Quality: 2/3
GoodEfficient frontier scatter (risk vs return colored by Sharpe) and a 5x5 correlation matrix are produced, and max-Sharpe metrics/weights are printed. But the correlation heatmap is not shown (only a table), and the weights pie chart is missing; prices output shape differs from the expected single-price 5-column dataframe.
Reasoning Quality: 1/2
Needs workReasoning about using adjusted close and annualization is correct, but several assistant messages claim outputs are already present rather than directly providing the requested artifacts (notably the missing heatmap/pie chart).
Reliability: 1/1
ExcellentWorkflow uses standard, deterministic steps (seeded simulation) and avoids hallucinated APIs; outputs align with the code provided, though it omits some requested visualizations.