Python for Finance: Analyze Big Financial Data

(Elle) #1
LANGUAGE

The MPT example shows again how efficient it is with Python to translate mathematical concepts, like portfolio

return or portfolio variance, into executable, vectorized code (an argument made in Chapter 1).

This mainly completes the tool set for mean-variance portfolio selection. Of paramount


interest to investors is what risk-return profiles are possible for a given set of securities,


and their statistical characteristics. To this end, we implement a Monte Carlo simulation


(cf. Chapter 10) to generate random portfolio weight vectors on a larger scale. For every


simulated allocation, we record the resulting expected portfolio return and variance:


In  [ 45 ]: prets   =   []
pvols = []
for p in range ( 2500 ):
weights = np.random.random(noa)
weights /= np.sum(weights)
prets.append(np.sum(rets.mean() * weights) * 252 )
pvols.append(np.sqrt(np.dot(weights.T,
np.dot(rets.cov() * 252 , weights))))
prets = np.array(prets)
pvols = np.array(pvols)

Figure 11-12 illustrates the results of the Monte Carlo simulation. In addition it provides


results for the so-called Sharpe ratio, defined as (i.e., the expected excess return


of the portfolio) over the risk-free short rate rf divided by the expected standard deviation


of the portfolio. For simplicity, we assume rf = 0:


In  [ 46 ]: plt.figure(figsize=( 8 ,     4 ))
plt.scatter(pvols, prets, c=prets / pvols, marker=‘o’)
plt.grid(True)
plt.xlabel(‘expected volatility’)
plt.ylabel(‘expected return’)
plt.colorbar(label=‘Sharpe ratio’)

Figure 11-12. Expected return and volatility for different/random portfolio weights

It is clear by inspection of Figure 11-12 that not all weight distributions perform well


when measured in terms of mean and variance. For example, for a fixed risk level of, say,


20%, there are multiple portfolios that all show different returns. As an investor one is


generally interested in the maximum return given a fixed risk level or the minimum risk


given a fixed return expectation. This set of portfolios then makes up the so-called


efficient frontier. This is what we derive later in the section.


Portfolio Optimizations


To make our lives a bit easier, first we have a convenience function giving back the major

Free download pdf