|
| 1 | +# Import numpy |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +# store the variables in arrays |
| 5 | +prob = np.array([0.25, 0.5, 0.25]) |
| 6 | +rate_1 = np.array([0.05, 0.075, 0.10]) |
| 7 | +rate_2 = np.array([0.2, 0.15, 0.1]) |
| 8 | + |
| 9 | +# expected return of each investment |
| 10 | +expected_return1 = np.sum(prob * rate_1) |
| 11 | +expected_return2 = np.sum(prob * rate_2) |
| 12 | + |
| 13 | +# expected return of the equally weighted portfolio |
| 14 | +weights = np.array([0.5, 0.5]) |
| 15 | +individual_returns = np.array([expected_return1, expected_return2]) |
| 16 | +portfolio_returns = np.dot(weights, individual_returns) |
| 17 | + |
| 18 | +# covariance matrix given probabilities |
| 19 | +cov_matrix = np.cov(rate_1, rate_2, ddof=0, aweights=prob) |
| 20 | + |
| 21 | +# variance and standard deviation of each investment |
| 22 | +var1 = cov_matrix[0,0] |
| 23 | +var2 = cov_matrix[1,1] |
| 24 | +std1 = np.sqrt(var1) |
| 25 | +std2 = np.sqrt(var2) |
| 26 | + |
| 27 | +# correlation between Asset 1 & 2's returns |
| 28 | +cov = cov_matrix[0,1] |
| 29 | +corr = cov / (std1 * std2) |
| 30 | + |
| 31 | +# variance of portfolio |
| 32 | +portfolio_var = np.dot(weights.T, np.dot(cov_matrix, weights)) |
| 33 | + |
| 34 | +# standard deviation (volatility of the portfolio) |
| 35 | +portfolio_vols = np.sqrt(portfolio_var) |
| 36 | + |
| 37 | +def percentage (number): |
| 38 | + return str(round(number, 4) * 100) + '%' |
| 39 | + |
| 40 | +print('Expected Return of Investment 1 = {}'.format(percentage(expected_return1))) |
| 41 | +print('Expected Return of Investment 2 = {}'.format(percentage(expected_return2))) |
| 42 | +print('Expected Return of Portfolio = {}'.format(percentage(portfolio_returns))) |
| 43 | +print('Standard Deviation of Investment 1 = {}'.format(percentage(std1))) |
| 44 | +print('Standard Deviation of Investment 1 = {}'.format(percentage(std2))) |
| 45 | +print('Correlation between Returns of 1 & 2 = {}'.format(round(corr, 4))) |
| 46 | +print('Risk of Portfilio = {}'.format(percentage(portfolio_vols))) |
0 commit comments