Creating the Line
Let’s first set up an equation that will print the return and standard deviation of a 2 asset portfolio.
def CAL(ret1,ret2,var1,var2,cov,p1,p2):
retEquation = (p1*ret1+p2*ret2)
variance = p1**2*var1+p2**2*var2+2*p1*p2*cov
print(retEquation)
print(variance)
print("")
Now, let’s simulate a stock with expected return of 8% and a risk-free asset like government bonds with an expected return of 3%. Because it is risk free, it has no variance, nor does it have a covariance with the stock. We will loop through 1%-100% by using a for loop, and dividing the number by 100.
for x in range(0,101):
fract = x/100
CAL(.03,.08,0,.35,0,1-fract,fract)
The first weight goes into the risk-free asset, and the stock gets whatever is left over (hence 1-fract)
Challenge
Create a function which creates two arrays representing the returns and standard deviations at any point, and then plot them with standard deviation on the x-axis.