Plotting the Line
Solution
import matplotlib.pyplot as plt
def CAL(ret1,ret2,var1,var2,cov):
returns = [(1-x/100)*ret1+(x/100)*ret2 for x in range(0,101)]
variances = [(1-x/100)**2*var1+(x/100)**2*var2+2*(1-x/100)*(x/100)*cov for x in range(0,101)]
standardDevs = [x**.5 for x in variances]
plt.plot(standardDevs,returns)
plt.xlabel("Standard Deviation")
plt.ylabel("Return")
plt.title("Capital Allocation Line ")
plt.show()
CAL(.03,.09,0,.35,0)
I will now introduce a new equation, the Sharpe Ratio. What this equation measures is the reward for risk ratio of an investment or portfolio
Equation
p
-r
f
)/(σ
p
)
r
p
= Portfolio Return
r
f
= Risk-free Rate
σ
p
= Standard Deviation of Portfolio
Let’s turn the equation into a function.
def SharpeRatio(rf,ret,SD):
return (ret-rf)/SD
The sharpe ratio is also the slope of the CAL, which makes sense because it is a measure of risk-reward trade off.
CAL(.03,.09,0,.35,0)
print(SharpeRatio(.03,.15,.35**.5*2))
I’m going to now modify the CAL function so that we can visualize how stock’s with a covariance work. We will return two graphs, one that shows return versus allocation in the assets, and the other that shows standard deviation versus allocations.
def CAL(ret1,ret2,var1,var2,cov):
returns = [(1-x/100)*ret1+(x/100)*ret2 for x in range(0,101)]
variances = [(1-x/100)**2*var1+(x/100)**2*var2+2*(1-x/100)*(x/100)*cov for x in range(0,101)]
standardDevs = [x**.5 for x in variances]
allocations = [(1-x/100) for x in range(0,101)]
plt.plot(allocations,returns)
plt.xlabel("Amount in Asset 1")
plt.ylabel("Return")
plt.title("Capital Allocation Line ")
plt.show()
plt.plot(allocations,standardDevs)
plt.xlabel("Amount in Asset 1")
plt.ylabel("Standard Deviation")
plt.title("Capital Allocation Line ")
plt.show()
CAL(.03,.09,.25,.35,.1)
When we model the two stocks, you’ll notice that the standard deviation is a parabola. There is a section of the graph that has a lower standard deviation than either of the stocks. This is what we call diversification, when we add stocks to our portfolio, we get less variation. Why is this? It is because with more stocks, it is much more unlikely that every single one of them has a negative return or a positive return. So, for example, if one company had a -50% return it would destroy an investor who only put money in it, but if an investor had it with 99 other stocks it would only drop the portfolio value .5%.