Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE GIVE PYTHON CODE FOR EACH ANSWER. Thank you! PROBLEM 1: Based on the program L3_Allocation5.ipynb (you have to make some changes) and the data,

PLEASE GIVE PYTHON CODE FOR EACH ANSWER. Thank you!

PROBLEM 1: Based on the program L3_Allocation5.ipynb (you have to make some changes) and the data, Indu10_July26_July11.xlsx, answer the following questions:

1) With the same risk aversion parameter 3, what are the optimal portfolio weights on the 10 industry assets?

2) What are the mean, std and Sharpe ratio of the optimal portfolio?

3) What are the mean, std and Sharpe ratio of the mkt index portfolio?

4) What are the accumulative returns of investing $1 into the mkt index portfolio and the optimal portfolio respectively for the relevant time period?

5) Re-do 4) with risk aversion parameter=1 and 5, respectively.

L3_Allocation5.ipynb look like this:

image text in transcribed

image text in transcribed

And the Indu10_July26_July11.xlsx looks like this:

image text in transcribed

# To Load data, we use the package pandas In [21] : import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline # Load the data - df - pd.read_excel('Factors_July26_July11.xlsx') # It has 5 columns : date, mkt, size, b/m, riskree rate # downloaded from Ken French's website df2 = pd. read_excel('Indus_July26_July11.xlsx') # return on 5 industry portfolios from Ken French's web mkt - df.loc[:, "mkt"]/100 # Mkt excess return; divided by 180 due to data are in % rf - df.loc[:,"rate"]/100 , R1 - df2.loc[:, 'Indul' : 'Indus']/100 # extract the 5 industry returns, RI is T by 5. print(R1[0:31) # check a couple of values R1 = np.array(R1) # convert list to array to apply np. functions, T x 5 rf - np.array(rf) # convert list to array to apply np.functions, a T-vector # note: T, is not txi in Python as the latter is 2-dim T-len(df) Re = np.ones((1,5)) # The number of observations # creat storage for excess returns # no need, but more clear for i in range(5): Re[:,i] - R1[:,1] - rf # the excess return: each indu substracts riskfree rate, Re[:,1]-rf mus - np.mean(Re, axis - e) mu5 = mu5.1 V5 = np.cov(Re.T) # the mean taking each column of the matrix, a row vector of 5 # make it a column vector # the covariance estimate, 5 by 5 VI - np.linalg.inv(V5) # The inverse of V # The optimal weights on the 5 risky casets gamma = 3 # The risk-averse coeff. W5 - 1/gamma np.matmul(VI, mu5) mu - mkt.mean() = ( sig2 = mkt. var sigma = np.sqrt(sig2) # The expected mkt excess return # The var of the mkt excess return # Its vol W- (1/gamma)*mu/sig2 # The optimal weight on mkt print(" In') print("Rsik avrersion and Optimal wight on the market ") print( {@:.4} {1:.4f} In '.format(gamma,w)) print( " (' In print('The Optimal wights on the 5 industries ') print(5) w_rf - 1 - np.dot(w5, np.ones((5,1))) print( ') print('The rest is on riskfree asset ') print(w_rf) Indus Indu2 Indu3 Indu4 Indus @ @.410 @.0273 0.0183 0.0164 0.0212 1 0.0258 @.0226 0.6241 0.8451 0.8436 2 @ 0.0206 -0.0039 0.0106 0.0061 0.8829 Rsik avrersion and Optimal wight on the market 3.0008 0.6998 The Optimal wights on the 5 industries [ 0.69542253 0.6684779 0.85889521 0.54013127 -0.93355514] The rest is on riskfree asset [-0.02137177] In [32]: # Compute the Sharpe Ratio of the Opt Port and Met Port-np.ones((1)) # define this T-vector to store the returns on the portfolio # to be compatible with rf port[@] - np.dot(W5, Re[@]) + rf[e] # return in the first period, the weight on rf is absorbed # into the previous excess return term, see formulas in the slides for t in range(T): Port[t] = np.dot (w5, Re[t]) + rf[t] # excess return of the optima portfolio ExPort = Port - rf mup - Export.mean() sig2P = Export.var() sigP = np.sqrt(sig2P) SharpeP = np.sqrt(12) "mup/sigp print(Export.shape, Port.shape, rf.shape) # double check the vectors are cpmpatible # For the met Sharpe = np.sqrt(12) "mu/sigma = ( print('The Annulized Sharpe ratios of the Opt Port and Mkt ") print {:.5f} {1:.5f} in.format(Sharpe, Sharpe)) (1021,) (1021,) (1821,) 0.888366526157297762 0.052783620295316525 The Annulized Sharpe ratios of the Opt Port and Mkt 0.54988 0.39627 In [31]: # Compute the accu returns of the opt port and the mkt CC_Port - np.ones((1)) # to store the accumulative returns CC - np.ones((1)) # to store the accumulative returns mkt2 - mkt + rf # Add back riskfree rate to get pure mkt return CC_Port[@] = 1+Port[@] # initial accu return CC[0] = 1 + mkt2[0] # initial accu return for t in range(T-1): CC_Port[t+1] = CC_Port[t]*(1+Port[t+1]) CC(t+1] = CC[t]*(1+mkt2(t+1]) print('Terminal wealth in Opt Port and Mkt ') print(CC_Port(T-1]) print(CCT-1]) Terminal wealth in Opt Port and Mkt = 24082.92871232563 2540.3983705076002 In [68]: # plot the accu returns P=plt.plot(CC_Port) pl - p1 = plt.plot(CC) 25000 20000 15000 10000 5000 0 0 200 400 600 800 1000 L Nm + L A B D E F G H K 1 Date Indu1 Indu2 Indu3 Indu4 Indu5 Indu6 Indu7 Indu8 Indu9 Indu10 192607 1.43 15.61 4.6 -1.17 2.9 0.83 -2.81 1.64 7.1 2.12 192608 4 3.64 2.79 3.31 2.66 2.17 -0.54 4.51 -1.74 4.36 192609 1.2 4.88 1.22 -3.37 -0.38 2.41 0.14 0.61 1.96 0.29 192610 -1.34 -8.22 -3.61 -0.78 -4.58 -0.11 -5.31 -0.73 -2.67 -2.85 192611 5.15 -0.2 4.14 -0.01 4.71 1.63 4.05 5.53 3.76 2.13 7 192612 0.84 9.91 3.7 2.81 -0.02 1.99 2.89 0.15 -0.12 3.39 8 192701 -0.64 -0.91 -0.06 1.2 -1.14 1.88 -3.19 4.91 -1.78 1.52 9 192702 3.37 8.88 5.8 1.42 4.45 3.97 4.55 1.65 4.57 5.04 10 192703 2.71 1.65 1.47 -5.95 1.45 5.56 2.85 0.88 0.44 1.26 11 192704 3.31 3.17 0.78 -5.11 5.4 -2.13 2.21 3.25 1.65 0.9 12 192705 8.17 5.92 5.09 4.8 7.37 3.35 0.63 4.01 9.31 6.4 13 192706 -1.74 -2.95 -2.79 -3.2 3.28 -2.14 0.21 0.45 -3.64 -2.1 14 192707 4.81 13.13 12.08 2.9 11.89 3.73 9.46 9.84 5.31 6.18 15 192708 2.74 7.02 2.89 0 5.79 0.43 13.08 0.28 3.43 -1.21 16 192709 5.31 5.73 4.76 3.33 1.42 6.53 5.12 5.65 8.29 4.75 17 192710 -0.81 -4.38 -6.69 -2.06 -7.19 -2.09 -6.01 5.13 -4.34 -3.8 18 192711 6.78 6.35 10.84 4.15 10.62 2.83 10.92 3.68 6.32 4.53 19 192712 2.47 7.78 3.05 0.89 1.18 1.33 -0.18 -0.46 2.24 1.99 20 192801 0.57 -3.34 -0.55 -1.86 -0.4 0.3 -0.56 2.69 5.87 -1.17 21 192802 -3.99 1.28 -1.9 -0.21 -0.45 -0.3 -1.67 -1.38 -0.68 -1.94 22 192803 4.36 27.63 8.21 5.37 19.22 1.96 8.78 10.54 6.62 6.05 23 192804 -0.45 1.42 1.49 15.12 2.23 3.22 0.52 2.35 10.94 4.09 24 192805 0.88 5.64 3.5 -5.26 2.57 6.61 3.97 9.27 1.51 0.09 25 192806 -4.35 -4.04 -4.91 -4.05 -5.13 -5.22 -4.1 -3.15 -3.43 -4.23 26 192807 1.84 0.87 2 0.98 0.63 -1.28 4.85 2.68 -1.15 -0.32 27 192808 5.95 9.06 8.6 2.38 10.06 4.55 11.68 15.11 6.46 4.77 28 192809 1.22 7.78 4.14 5.21 0.73 0.15 1.54 -1.5 3.35 1.94 29 192810 0.79 0.14 4.32 1.91 1.82 2.97 4.05 -0.08 -1.64 -0.32 30 192811 6.57 4.47 13.11 17.29 19.67 6.68 14.19 5.05 21.98 10.06 31 192812 -0.46 -1.25 3.04 -4.34 7.14 -0.38 -1.42 -0.28 1.52 0.47 32 192901 3.46 1.27 11.04 -7.68 10.44 13.38 -4.3 3.25 12.96 5.67 102nnn 227 non F Indu 10_July26_July11 # To Load data, we use the package pandas In [21] : import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline # Load the data - df - pd.read_excel('Factors_July26_July11.xlsx') # It has 5 columns : date, mkt, size, b/m, riskree rate # downloaded from Ken French's website df2 = pd. read_excel('Indus_July26_July11.xlsx') # return on 5 industry portfolios from Ken French's web mkt - df.loc[:, "mkt"]/100 # Mkt excess return; divided by 180 due to data are in % rf - df.loc[:,"rate"]/100 , R1 - df2.loc[:, 'Indul' : 'Indus']/100 # extract the 5 industry returns, RI is T by 5. print(R1[0:31) # check a couple of values R1 = np.array(R1) # convert list to array to apply np. functions, T x 5 rf - np.array(rf) # convert list to array to apply np.functions, a T-vector # note: T, is not txi in Python as the latter is 2-dim T-len(df) Re = np.ones((1,5)) # The number of observations # creat storage for excess returns # no need, but more clear for i in range(5): Re[:,i] - R1[:,1] - rf # the excess return: each indu substracts riskfree rate, Re[:,1]-rf mus - np.mean(Re, axis - e) mu5 = mu5.1 V5 = np.cov(Re.T) # the mean taking each column of the matrix, a row vector of 5 # make it a column vector # the covariance estimate, 5 by 5 VI - np.linalg.inv(V5) # The inverse of V # The optimal weights on the 5 risky casets gamma = 3 # The risk-averse coeff. W5 - 1/gamma np.matmul(VI, mu5) mu - mkt.mean() = ( sig2 = mkt. var sigma = np.sqrt(sig2) # The expected mkt excess return # The var of the mkt excess return # Its vol W- (1/gamma)*mu/sig2 # The optimal weight on mkt print(" In') print("Rsik avrersion and Optimal wight on the market ") print( {@:.4} {1:.4f} In '.format(gamma,w)) print( " (' In print('The Optimal wights on the 5 industries ') print(5) w_rf - 1 - np.dot(w5, np.ones((5,1))) print( ') print('The rest is on riskfree asset ') print(w_rf) Indus Indu2 Indu3 Indu4 Indus @ @.410 @.0273 0.0183 0.0164 0.0212 1 0.0258 @.0226 0.6241 0.8451 0.8436 2 @ 0.0206 -0.0039 0.0106 0.0061 0.8829 Rsik avrersion and Optimal wight on the market 3.0008 0.6998 The Optimal wights on the 5 industries [ 0.69542253 0.6684779 0.85889521 0.54013127 -0.93355514] The rest is on riskfree asset [-0.02137177] In [32]: # Compute the Sharpe Ratio of the Opt Port and Met Port-np.ones((1)) # define this T-vector to store the returns on the portfolio # to be compatible with rf port[@] - np.dot(W5, Re[@]) + rf[e] # return in the first period, the weight on rf is absorbed # into the previous excess return term, see formulas in the slides for t in range(T): Port[t] = np.dot (w5, Re[t]) + rf[t] # excess return of the optima portfolio ExPort = Port - rf mup - Export.mean() sig2P = Export.var() sigP = np.sqrt(sig2P) SharpeP = np.sqrt(12) "mup/sigp print(Export.shape, Port.shape, rf.shape) # double check the vectors are cpmpatible # For the met Sharpe = np.sqrt(12) "mu/sigma = ( print('The Annulized Sharpe ratios of the Opt Port and Mkt ") print {:.5f} {1:.5f} in.format(Sharpe, Sharpe)) (1021,) (1021,) (1821,) 0.888366526157297762 0.052783620295316525 The Annulized Sharpe ratios of the Opt Port and Mkt 0.54988 0.39627 In [31]: # Compute the accu returns of the opt port and the mkt CC_Port - np.ones((1)) # to store the accumulative returns CC - np.ones((1)) # to store the accumulative returns mkt2 - mkt + rf # Add back riskfree rate to get pure mkt return CC_Port[@] = 1+Port[@] # initial accu return CC[0] = 1 + mkt2[0] # initial accu return for t in range(T-1): CC_Port[t+1] = CC_Port[t]*(1+Port[t+1]) CC(t+1] = CC[t]*(1+mkt2(t+1]) print('Terminal wealth in Opt Port and Mkt ') print(CC_Port(T-1]) print(CCT-1]) Terminal wealth in Opt Port and Mkt = 24082.92871232563 2540.3983705076002 In [68]: # plot the accu returns P=plt.plot(CC_Port) pl - p1 = plt.plot(CC) 25000 20000 15000 10000 5000 0 0 200 400 600 800 1000 L Nm + L A B D E F G H K 1 Date Indu1 Indu2 Indu3 Indu4 Indu5 Indu6 Indu7 Indu8 Indu9 Indu10 192607 1.43 15.61 4.6 -1.17 2.9 0.83 -2.81 1.64 7.1 2.12 192608 4 3.64 2.79 3.31 2.66 2.17 -0.54 4.51 -1.74 4.36 192609 1.2 4.88 1.22 -3.37 -0.38 2.41 0.14 0.61 1.96 0.29 192610 -1.34 -8.22 -3.61 -0.78 -4.58 -0.11 -5.31 -0.73 -2.67 -2.85 192611 5.15 -0.2 4.14 -0.01 4.71 1.63 4.05 5.53 3.76 2.13 7 192612 0.84 9.91 3.7 2.81 -0.02 1.99 2.89 0.15 -0.12 3.39 8 192701 -0.64 -0.91 -0.06 1.2 -1.14 1.88 -3.19 4.91 -1.78 1.52 9 192702 3.37 8.88 5.8 1.42 4.45 3.97 4.55 1.65 4.57 5.04 10 192703 2.71 1.65 1.47 -5.95 1.45 5.56 2.85 0.88 0.44 1.26 11 192704 3.31 3.17 0.78 -5.11 5.4 -2.13 2.21 3.25 1.65 0.9 12 192705 8.17 5.92 5.09 4.8 7.37 3.35 0.63 4.01 9.31 6.4 13 192706 -1.74 -2.95 -2.79 -3.2 3.28 -2.14 0.21 0.45 -3.64 -2.1 14 192707 4.81 13.13 12.08 2.9 11.89 3.73 9.46 9.84 5.31 6.18 15 192708 2.74 7.02 2.89 0 5.79 0.43 13.08 0.28 3.43 -1.21 16 192709 5.31 5.73 4.76 3.33 1.42 6.53 5.12 5.65 8.29 4.75 17 192710 -0.81 -4.38 -6.69 -2.06 -7.19 -2.09 -6.01 5.13 -4.34 -3.8 18 192711 6.78 6.35 10.84 4.15 10.62 2.83 10.92 3.68 6.32 4.53 19 192712 2.47 7.78 3.05 0.89 1.18 1.33 -0.18 -0.46 2.24 1.99 20 192801 0.57 -3.34 -0.55 -1.86 -0.4 0.3 -0.56 2.69 5.87 -1.17 21 192802 -3.99 1.28 -1.9 -0.21 -0.45 -0.3 -1.67 -1.38 -0.68 -1.94 22 192803 4.36 27.63 8.21 5.37 19.22 1.96 8.78 10.54 6.62 6.05 23 192804 -0.45 1.42 1.49 15.12 2.23 3.22 0.52 2.35 10.94 4.09 24 192805 0.88 5.64 3.5 -5.26 2.57 6.61 3.97 9.27 1.51 0.09 25 192806 -4.35 -4.04 -4.91 -4.05 -5.13 -5.22 -4.1 -3.15 -3.43 -4.23 26 192807 1.84 0.87 2 0.98 0.63 -1.28 4.85 2.68 -1.15 -0.32 27 192808 5.95 9.06 8.6 2.38 10.06 4.55 11.68 15.11 6.46 4.77 28 192809 1.22 7.78 4.14 5.21 0.73 0.15 1.54 -1.5 3.35 1.94 29 192810 0.79 0.14 4.32 1.91 1.82 2.97 4.05 -0.08 -1.64 -0.32 30 192811 6.57 4.47 13.11 17.29 19.67 6.68 14.19 5.05 21.98 10.06 31 192812 -0.46 -1.25 3.04 -4.34 7.14 -0.38 -1.42 -0.28 1.52 0.47 32 192901 3.46 1.27 11.04 -7.68 10.44 13.38 -4.3 3.25 12.96 5.67 102nnn 227 non F Indu 10_July26_July11

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Accounting

Authors: Joe Hoyle, Thomas Schaefer, Timothy Doupnik

10th edition

0-07-794127-6, 978-0-07-79412, 978-0077431808

Students also viewed these Finance questions