Skip to content

Instantly share code, notes, and snippets.

@ganeumann
Last active April 8, 2021 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganeumann/0133b0ac7bf18a58ae6312cfc4e8c4b5 to your computer and use it in GitHub Desktop.
Save ganeumann/0133b0ac7bf18a58ae6312cfc4e8c4b5 to your computer and use it in GitHub Desktop.
Probability of Exceeding Return Chart
import numpy as np
def vcdist(alpha):
# generates one pick from the VC distribution
# prob density distribution = 1/3 0x, 1/3 1x, 1/3 power law with alpha=alpha
# returns one draw from the distribution as a float
pn=np.random.randint(0,3)
if pn==2:
td = powerlaw.Power_Law(xmin=1.0, parameters=[alpha])
return td.generate_random(1)[0]
return pn
def vcportfolios(alpha,n):
# creates 10,000 random portfolios, each of size n and finds average mean
# of those portfolios and the percent of those portfolios above 1x,2x,3x,4x
# returns tuple: (mean, %>1x, %>2x, %>3x, %>4x)
runs=10000.
portfolioavgs=[average([vcdist(alpha) for i in range(n)]) for j in range(int(runs))]
return (average(portfolioavgs),
(len([i for i in portfolioavgs if i>1.])/runs),
(len([i for i in portfolioavgs if i>2.])/runs),
(len([i for i in portfolioavgs if i>3.])/runs),
(len([i for i in portfolioavgs if i>4.])/runs))
def plt_vcports(alpha):
# runs vcportfolios at portfolio sizes from 1 to 100
# charts result
# no return value
xm,xm1,xm2,xm3,xm4=[],[],[],[],[]
X=range(1,101,1)
for i in X:
m,m1,m2,m3,m4 = vcportfolios(alpha,i)
xm.append(m) #mean, not charted here
xm1.append(m1) #more than 1x
xm2.append(m2) #more than 2x
xm3.append(m3) #more than 3x
xm4.append(m4) #more than 4x
# chart results
ax = plt.figure(figsize=(10, 10)).add_subplot(211)
ax.plot(X,xm1,X,xm2,X,xm3,X,xm4)
ax.set(xlim=[0, 100], ylim=[0,1],
title='Probability of Exceeding Benchmark Return by Portfolio Size',
xlabel='Portfolio Size', yticks=np.arange(0,1.1,.1))
ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
ax.grid(True)
ax.legend( ('% more than 1x','% more than 2x','% more than 3x','% more than 4x'),loc='upper left')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment