Efficient Frontier Simulation

GPT4 Generated Code Summary

This Python code performs financial analysis and visualization on stock data using libraries such as Pandas, NumPy, yfinance, and Matplotlib. Here's a summary of its main components:

  1. Data Collection and Initial Plotting: The script imports necessary libraries and fetches adjusted closing price data for a set of stocks (Apple, Tesla, Nvidia, and Meta) from May 5, 2020, using the yfinance library. It then plots the historical prices and daily returns of these stocks.

  2. Portfolio Performance Functions: Two functions are defined:

    • portfolio_annualised_performance: Calculates the annualized standard deviation and return of a portfolio given weights, mean returns, and covariance matrix.

    • random_portfolios: Generates random portfolios with different weights, computing their standard deviation, return, and Sharpe ratio (return relative to risk).

  3. Simulation and Optimization: The display_simulated_ef_with_random function performs a Monte Carlo simulation to generate numerous random portfolios. It identifies the portfolio with the maximum Sharpe ratio and the one with minimum volatility, displaying their allocations and characteristics. It also plots these portfolios on a scatter plot to visualize the efficient frontier.

  4. Investment Simulation: The script calculates the profits made from an initial investment of $10,000 in each stock based on their historical performance. It prints the amount invested in each stock, the growth factor, and the profit made.

  5. Comparison with S&P 500 Index (SPY): The code downloads historical data for the SPY ETF, representing the S&P 500 index, to compare its performance with the custom portfolio. It calculates and plots the cumulative returns of both the portfolio and the SPY ETF.

Overall, the code is a comprehensive tool for analyzing stock data, simulating portfolio performance, and comparing it with a market benchmark. It includes data visualization, Monte Carlo simulation for portfolio optimization, and comparison of investment returns.