Optimizing a Portfolio with Monte Carlo Simulations and the Efficient Frontier

Introduction

Welcome to my latest project—an exploration of portfolio optimization using both Monte Carlo simulations and Modern Portfolio Theory. I’ve just open-sourced the code on GitHub. In this blog post, I’ll walk through what the project does, why it’s interesting, and how you can try it yourself.


What is Portfolio Optimization?

Portfolio optimization is all about finding the ideal blend of assets (stocks, funds, or other instruments) that maximizes returns while minimizing risk. One classic method to visualize this is the Efficient Frontier, a concept introduced by Nobel laureate Harry Markowitz. The Efficient Frontier is the curve of “optimal” portfolios offering the highest return per unit of risk.

Risk

We often measure risk by volatility (standard deviation) of returns.

Return

Typically measured by annualized mean of returns.

Sharpe Ratio

A measure of risk-adjusted return, calculated as the (portfolio return – risk-free rate) / (portfolio volatility).


Project Overview

Monte Carlo Simulation Approach

  1. Random Weights
    We generate thousands (often 100K+) of random weight allocations across our chosen assets.
  2. Performance Metrics
    For each allocation, we calculate the annualized return, annualized volatility, and Sharpe ratio.
  3. Best Found Portfolio
    We highlight the random portfolio that yields the highest Sharpe ratio among all simulations (i.e., the “green star” in the resulting plot).


Analytical Efficient Frontier

In contrast to random allocations, we also compute the Efficient Frontier by systematic optimization. We use optimization routines (like scipy.optimize) to find the portfolio with the minimum volatility for each target return. This yields a smooth curve which is the theoretical solution to Markowitz’s Modern Portfolio Theory.

Visualizing the Results

The script generates a scatter plot of all random portfolios, color-coded by Sharpe ratio. It then overlays the Efficient Frontier curve and highlights notable points:

  • Market Benchmark (S&P 500) performance (red dot)
  • Optimal Portfolio from Monte Carlo (green star)
  • Potentially other reference portfolios like the Global Minimum Volatility or an Equal-Weighted portfolio.

Why Dividend Kings?

By default, the repository focuses on the Dividend Kings—stocks that have increased dividends for at least 50 consecutive years. I wanted to apply the technique to a realistic, relatively conservative set of stocks. Of course, you can specify any set of tickers if you’d prefer growth stocks, ETFs, or other instruments.


Getting Started

  1. Clone or Download the repository: 
  1. Install Dependencies:
            pip install -r requirements.txt
  1. Run the Script: python monte_carlo_portfolio.py --assets AAPL MSFT TSLA --start 2020-01-01 --end 2025-01-01
  • By default, it saves a PNG of the Efficient Frontier (efficient_frontier_monter_carlo.png) and a CSV of the historical data (market_data.csv).

Pro Tip: Adjust parameters like risk_free_rate, portfolios, or the date range in the command line to see how different assumptions impact the results.


Key Highlights

  1. Modular Code
    • The repository separates data fetching, Monte Carlo simulations, and plotting into distinct functions for clarity.
  2. Argument Parsing
    • You can easily change inputs via --assets, --start, --end, and other flags.
  3. Analytical Frontier + Monte Carlo
    • See how a purely random approach might miss the theoretical optimum (the frontier line) unless it samples very densely.
  4. Saving Outputs
    • The script automatically saves the final chart. Perfect for including in reports or presentations.

Results: Example Chart

Below is a sample chart from the script, showing:

  • A cloud of random portfolio allocations color-coded by Sharpe.
  • A black line representing the Efficient Frontier.
  • Markers for the market (in red) and the optimal Monte Carlo portfolio (green star).

In a real market scenario, you might see how dividend-paying stocks cluster toward lower volatility with moderate returns, while certain growth stocks occupy a higher-risk, higher-return domain.




Potential Next Steps

  • Incorporate Additional Factors
    You could add constraints like “no short selling” or “limit to 20% in any single asset” to your optimization.
  • Use Different Benchmarks
    Compare your portfolio to an international index (like IXUS), or to bonds (like AGG).
  • Evaluate Performance Over Time
    Extend the script to backtest the chosen “optimal” allocation through time, rebalancing periodically.

Conclusion

With this project, I’ve combined two powerful approaches—Monte Carlo and analytical optimization—to visualize and compare different portfolio allocations. By default, it focuses on Dividend Kings, but you can easily switch to any assets you like. Whether you’re an aspiring quant investor or just curious about portfolio construction, I hope this provides a helpful practical example of Modern Portfolio Theory in action.

For more details, check out the GitHub repository. Feel free to fork it, play with the parameters, and share your results. Happy investing!


References


Thank you for reading! If you have any questions or suggestions, feel free to open an issue or comment below.

Comments

Popular Posts