Created
December 2, 2017 03:30
-
-
Save ganeumann/2a2e3e898132af8729b9a3772e262eee to your computer and use it in GitHub Desktop.
Monte Carlo Simulation of the Basic Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def pickaportfolio(n): | |
# possible outcomes, from Fred's post | |
# portfolios of size n | |
# returns the mean of a single random portfolio | |
possible_outcomes = [0,0,0,0,1,1,1,3,3,10] | |
# pick n random elements from the list and take the mean of the outcome | |
return np.mean(np.random.choice(possible_outcomes,n)) | |
def lotsofportfolios(m,n): | |
# m portfolios of n picks each | |
# returns a list of m means of portfolio size n | |
return [pickaportfolio(n) for _ in range(m)] | |
def chartports(n): | |
# Plot 10,000 runs with portfolio size n | |
# no return value, chart is side-effect | |
portfolios = lotsofportfolios(10000,n) | |
plt.title('Distribution of returns, portfolio size %d' % n) | |
tallest=max(plt.hist(portfolios,bins=[0,1,2,3,4,5,6,7,8], rwidth=0.7,align='mid')[0]) | |
plt.grid(True) | |
plt.xlabel('Portfolio Return Multiple') | |
plt.yticks(np.arange(0,tallest-tallest%-len(portfolios)/10.,len(portfolios)/10.), | |
('0%','10%','20%','30%','40%','50%','60%','70%','80%','90%','100%') ) | |
plt.show() | |
print "Mean = %.3f" % np.mean(portfolios) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment