Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

PLEASE GIVE PYTHON CODE FOR EACH ANSWER. Thank you!

PROBLEM 2: Based on the program L4_Allocation5CC.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 once we impose restrictions on the weights that they are nonnegative (no short sell) and be no greater than 30% (never invest more than 30% of your money into any of the assets)?

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

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

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

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

L4_Allocation5CC.ipynb looks like this:

image text in transcribed

image text in transcribed

image text in transcribedAnd Indu10_July26_July11.xlsx looks like this:

image text in transcribed

In [8]: import pandas as pd # To Load data, we use the package pandas import numpy as np import matplotlib.pyplot as plt import cvxopt # install it first by running: "pip install cvxopt" via Spyder from cvxopt import matrix, solvers %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 109 due to data are in % rf = df.loc[:,"rate"]/100 R1 - df2.loc[:, 'Indul' : 'Indus']/100 # extract the 5 industry returns, R1 is T by 5. R1 - np.array(R1) rf - np.array(rf) T = len(df) Re - np.ones((1,5)) # convert list to array to apply np.functions # convert list to array to apply np.functions # The number of obs # creat storage for excess returns for i in range(5): Re[:,i] - R1[:,i] - rf # the excess return: each indu substracts riskfree rate, Re[:,i]-rf mus - np.mean(Re, axis - ) mu5 - mus.T V5 = np.cov(Re.T) # the mean taking each column of the matrix, a row vector of 25 # make it a column vector # the covariance estimate, 25 by 25 VI = np. linalg.inv(V5) # The inverse of v # The optimal weights on the 5 risky aasets 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 ') print("Rsik avrersion and Optimal wight on the market ') print {@:.4f} {1:.4f) In '.format(gamma,w)) print( ') print("The Optimal wights on the 5 assets ') print(5) w_rf - 1 - np.dot(w5, np.ones((5,1))) print( ') print("The rest is on riskfree asset ') print(w_rf) Rsik avrersion and Optimal wight on the market 3.eeee 0.6998 The Optimal wights on the 5 assets [ 6.69542253 0.6684779 8.05889521 0.54013127 -0.93355514) The rest is on riskfree asset [-@.02137177] In [10]: # Compute the the Opt Port under bound constraints # the mapping of utility paramters into quadratic programming Q = gamma * V5 Q- matrix(Q) 9-mus 9 - matrix(9) # Lower bound #upper bound 1b = np.ones((1, 5)) * 0.8 ub = np.ones((1, 5)) * 0.4 h - np.append(ub, lb, axis - 1) h-h.T h - matrix(h) # identiy matrix of order 5 5 G1 = np.eye(5) G2 = - np.eye(5) G - np.append(G1, G2, axis - ) G - matrix() solvers.options['show_progress'] = False # this prevent print progress data # we do not really need sol - solvers.qp(Q. 9, G, h) # Format: solvers.gp@, q, , h, A, b) sola - np.array(sol['x']) print(sola.T) soli - solvers.qp(, 4) # using the solver for the unconstrained solution solb - np.array( soll('x']) print (solb.T) [[3.33321566e-01 1.44867e97e-01 8.83505305e-64 3.99992818e-e1 3.15638814-07]] [[ 0.69542253 0.6604779 0.85889521 0.54013127 -0.93355514]] In [11]: * compute the the Opt Port under bound constraints & weights sum to 1 # make sure it is float, not integer A = np.ones((1,5), np.float64) A = matrix(A) b = 1.0 b-matrix(b) # Format: solvers.qp(Q, q, g, h, A, b) sol - solvers.qp(Q. q, G, H,A,b) solc = np.array(sol['x']) print(solc.T) C- - np.dot(A, solc) #double check the weights sum to 1 print(c) [[3.99127287e-81 1.89707362e-01 1.11655970e-82 3.999996812-01 7.26166637e-08]] [[1.]] In [12]: # Compute the Sharpe Ratio of the Opt Port and Met w5 = np.array((sola) w5.shape - (1,5) Port-np.ones((T)) #define this T vector to store the returns on the portfolio Port[] = np.dot (w5, Re[e]) + rf[@] # return in the first period 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 # For the met Sharpe = np.sqrt(12) *mu/sigma print('The Annualized Sharpe ratios of the Opt Port and Mkt ') print {@:.4f} {1:.4} '.format(Sharpe, Sharpe) The Annualized Sharpe ratios of the Opt Port and Mkt 0.4856 0.3963 In [13]: # Compute the accu returns of the opt port and the mkt CC_Port = np.ones((1,)) CC = np.ones((1)) # to store the accumulative returns # to store the accumulative returns mkt2 = mkt + rf # Add back riskfree rate to get pure mkt return CC_Port[@] - 1+Port[@] CC[@] = 1 + mkt2[@] # initial accu return # 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(CC[T-1]) Terminal wealth in Opt Port and Mkt 4739.873038799331 2540.3983705076002 In [14]: # plot the accu returns p=plt.plot(CC_Port) p1 = plt.plot(CC) 5000 4000 3000 2000 1000 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 In [8]: import pandas as pd # To Load data, we use the package pandas import numpy as np import matplotlib.pyplot as plt import cvxopt # install it first by running: "pip install cvxopt" via Spyder from cvxopt import matrix, solvers %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 109 due to data are in % rf = df.loc[:,"rate"]/100 R1 - df2.loc[:, 'Indul' : 'Indus']/100 # extract the 5 industry returns, R1 is T by 5. R1 - np.array(R1) rf - np.array(rf) T = len(df) Re - np.ones((1,5)) # convert list to array to apply np.functions # convert list to array to apply np.functions # The number of obs # creat storage for excess returns for i in range(5): Re[:,i] - R1[:,i] - rf # the excess return: each indu substracts riskfree rate, Re[:,i]-rf mus - np.mean(Re, axis - ) mu5 - mus.T V5 = np.cov(Re.T) # the mean taking each column of the matrix, a row vector of 25 # make it a column vector # the covariance estimate, 25 by 25 VI = np. linalg.inv(V5) # The inverse of v # The optimal weights on the 5 risky aasets 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 ') print("Rsik avrersion and Optimal wight on the market ') print {@:.4f} {1:.4f) In '.format(gamma,w)) print( ') print("The Optimal wights on the 5 assets ') print(5) w_rf - 1 - np.dot(w5, np.ones((5,1))) print( ') print("The rest is on riskfree asset ') print(w_rf) Rsik avrersion and Optimal wight on the market 3.eeee 0.6998 The Optimal wights on the 5 assets [ 6.69542253 0.6684779 8.05889521 0.54013127 -0.93355514) The rest is on riskfree asset [-@.02137177] In [10]: # Compute the the Opt Port under bound constraints # the mapping of utility paramters into quadratic programming Q = gamma * V5 Q- matrix(Q) 9-mus 9 - matrix(9) # Lower bound #upper bound 1b = np.ones((1, 5)) * 0.8 ub = np.ones((1, 5)) * 0.4 h - np.append(ub, lb, axis - 1) h-h.T h - matrix(h) # identiy matrix of order 5 5 G1 = np.eye(5) G2 = - np.eye(5) G - np.append(G1, G2, axis - ) G - matrix() solvers.options['show_progress'] = False # this prevent print progress data # we do not really need sol - solvers.qp(Q. 9, G, h) # Format: solvers.gp@, q, , h, A, b) sola - np.array(sol['x']) print(sola.T) soli - solvers.qp(, 4) # using the solver for the unconstrained solution solb - np.array( soll('x']) print (solb.T) [[3.33321566e-01 1.44867e97e-01 8.83505305e-64 3.99992818e-e1 3.15638814-07]] [[ 0.69542253 0.6604779 0.85889521 0.54013127 -0.93355514]] In [11]: * compute the the Opt Port under bound constraints & weights sum to 1 # make sure it is float, not integer A = np.ones((1,5), np.float64) A = matrix(A) b = 1.0 b-matrix(b) # Format: solvers.qp(Q, q, g, h, A, b) sol - solvers.qp(Q. q, G, H,A,b) solc = np.array(sol['x']) print(solc.T) C- - np.dot(A, solc) #double check the weights sum to 1 print(c) [[3.99127287e-81 1.89707362e-01 1.11655970e-82 3.999996812-01 7.26166637e-08]] [[1.]] In [12]: # Compute the Sharpe Ratio of the Opt Port and Met w5 = np.array((sola) w5.shape - (1,5) Port-np.ones((T)) #define this T vector to store the returns on the portfolio Port[] = np.dot (w5, Re[e]) + rf[@] # return in the first period 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 # For the met Sharpe = np.sqrt(12) *mu/sigma print('The Annualized Sharpe ratios of the Opt Port and Mkt ') print {@:.4f} {1:.4} '.format(Sharpe, Sharpe) The Annualized Sharpe ratios of the Opt Port and Mkt 0.4856 0.3963 In [13]: # Compute the accu returns of the opt port and the mkt CC_Port = np.ones((1,)) CC = np.ones((1)) # to store the accumulative returns # to store the accumulative returns mkt2 = mkt + rf # Add back riskfree rate to get pure mkt return CC_Port[@] - 1+Port[@] CC[@] = 1 + mkt2[@] # initial accu return # 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(CC[T-1]) Terminal wealth in Opt Port and Mkt 4739.873038799331 2540.3983705076002 In [14]: # plot the accu returns p=plt.plot(CC_Port) p1 = plt.plot(CC) 5000 4000 3000 2000 1000 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 to Expert-Tailored 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

Fundamentals Of Investments Valuation And Management

Authors: Bradford Jordan, Thomas Miller, Steve Dolvin

9th Edition

1260013979, 9781260013979

More Books

Students also viewed these Finance questions

Question

Describe voluntary benefits.

Answered: 1 week ago

Question

Describe the major job evaluation systems.

Answered: 1 week ago