okama.EfficientFrontier.get_monte_carlo
- EfficientFrontier.get_monte_carlo(n=100, kind='mean')
Generate N random portfolios with Monte Carlo simulation.
Risk (annualized standard deviation) and Return (CAGR) are calculated for a set of random weights.
- Parameters:
- nint, default 100
Number of random portfolios to generate with Monte Carlo simulation.
- kind{‘mean’,’cagr’}, default ‘mean’
Use CAGR for return if kind=’cagr’, or annualized arithmetic mean if kind=’mean’.
- Returns:
- DataFrame
Table with Return, Risk and weights values for random portfolios.
Examples
>>> assets = ['SPY.US', 'AGG.US', 'GLD.US'] >>> last_date='2021-07' >>> base_currency = 'EUR' >>> y = ok.EfficientFrontier(assets, ccy=base_currency, last_date=last_date) >>> y.get_monte_carlo(n=10) # generate 10 random portfolios Risk Return SPY.US AGG.US GLD.US 0 0.099168 0.101667 0.470953 0.205227 0.323819 1 0.099790 0.076282 0.070792 0.558928 0.370280 2 0.129312 0.106620 0.274261 0.050524 0.675215 3 0.102031 0.083311 0.129375 0.443444 0.427182 4 0.102956 0.105136 0.489213 0.146174 0.364614 5 0.095690 0.091834 0.297122 0.335066 0.367812 6 0.103747 0.090285 0.203408 0.334694 0.461898 7 0.148311 0.099617 0.082660 0.120871 0.796470 8 0.115780 0.082983 0.042871 0.422335 0.534794 9 0.093903 0.088553 0.266303 0.387332 0.346365
To plot Monte Carlo simulation result it’s useful to combine in with the Efficien Frontier chart. Additionaly assets points could be plotted with ‘Plot.plot_assets()’.
>>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> # Plot the assets points (optional). >>> # The same first and last dates, base currency and return type should be used. >>> y.plot_assets(kind='cagr') >>> ax = plt.gca() >>> # Plot random portfolios risk-return points. >>> mc = y.get_monte_carlo(n=1000, kind='cagr') >>> ax.scatter(mc.Risk, mc.CAGR, linewidth=0, color='green') >>> # Plot the Efficient (optional) >>> df = y.ef_points >>> ax.plot(df['Risk'], df['CAGR'], color='black', linestyle='dashed', linewidth=3) >>> # Set the title and axis labels >>> ax.set_title('Single period Efficient Frontier & Monte Carlo simulation') >>> ax.set_xlabel('Risk (Standard Deviation)') >>> ax.set_ylabel('CAGR') >>> ax.legend() >>> plt.show()