Demo entry 6284940

GGGGG

   

Submitted by anonymous on Oct 18, 2016 at 04:22
Language: S. Code size: 8.1 kB.

#Homework 5 Evan Bagley (Aka The Greatest)
C = matrix(c(.04,.02,.05,.02,.16,.04,.05,.04,.25),nrow=3,byrow=T)
m = matrix(c(.05,.1,.2),nrow=1,byrow=T)
u = matrix(c(1,1,1),nrow=1,byrow=T)
CI <- solve(C)
# 1. For the covariance matrix C and expected return matrix m given 
#    above, find each of the following:
### CODE QUESTION 1:
#   (a) The weights for the minimum variance portfolio
W <- (u%*%CI)/((u%*%CI%*%t(u))[1,1])
W
#(b) The expected return and risk for the minimum variance portfolio.
mu <- W%*%t(m)
mu
sig <- (W%*%C%*%t(W))^.5
sig
#(c) The weights for the portfolio on the minimum variance line with expected return of 10%.
coef1 <- (m%*%CI%*%t(m))[1,1]
coef2 <- (u%*%CI%*%t(m))[1,1]
coef3 <- (m%*%CI%*%t(u))[1,1]
coef4 <- (u%*%CI%*%t(u))[1,1]
M = matrix(c(coef1,coef2,coef3,coef4),nrow=2,byrow=T)
muv = matrix(c(.1,1),nrow=2,byrow=1) 
lambda <- (2*solve(M))%*%muv
l1 <- lambda[1,1]
l2 <- lambda[2,1]
wr <- ((l1%*%m%*%CI)+(l2%*%u%*%CI))/2
wr
#(d) The risk for the portfolio found in (c)
risk1 <- (wr%*%C%*%t(wr))^.5
risk1
#(e) Assume the risk-free return is R = 4%.  Find the weights for the market portfolio.
r = .04
v <- m - r*u
Wm <- (v%*%CI)/(v%*%CI%*%t(u))[1,1]
Wm
#(f) Find the expected return and risk for the market portfolio
mum <- Wm %*% t(m)
mum
sigm <- (Wm %*% C %*% t(Wm))^.5
sigm
#(g) Find the maximum expected return possible with a risk of 0.80.
rr <- .8
maxmu <- r + ((mum-r)/sigm)*rr
maxmu

#(h) Find the weights for the portfolio that includes the bond and risky assets from (g).
x <- (1-(rr/sigm))[1,1]
ws <- (1-x) * Wm
ws <- append(ws, x, after = 3)
ws
### OUTPUT QUESTION 1:
> #   (a) The weights for the minimum variance portfolio
  > W <- (u%*%CI)/((u%*%CI%*%t(u))[1,1])
> W
[,1]      [,2]        [,3]
[1,] 0.9306931 0.1287129 -0.05940594
> #(b) The expected return and risk for the minimum variance portfolio.
  > mu <- W%*%t(m)
> mu
[,1]
[1,] 0.04752475
> sig <- (W%*%C%*%t(W))^.5
> sig
[,1]
[1,] 0.1919158
> #(c) The weights for the portfolio on the minimum variance line with expected return of 10%.
  > coef1 <- (m%*%CI%*%t(m))[1,1]
> coef2 <- (u%*%CI%*%t(m))[1,1]
> coef3 <- (m%*%CI%*%t(u))[1,1]
> coef4 <- (u%*%CI%*%t(u))[1,1]
> M = matrix(c(coef1,coef2,coef3,coef4),nrow=2,byrow=T)
> muv = matrix(c(.1,1),nrow=2,byrow=1)
> lambda <- (2*solve(M))%*%muv
> l1 <- lambda[1,1]
> l2 <- lambda[2,1]
> wr <- ((l1%*%m%*%CI)+(l2%*%u%*%CI))/2
> wr
[,1]      [,2]      [,3]
[1,] 0.5095541 0.2356688 0.2547771
> #(d) The risk for the portfolio found in (c)
  > risk1 <- (wr%*%C%*%t(wr))^.5
> risk1
[,1]
[1,] 0.241017
> #(e) Assume the risk-free return is R = 4%.  Find the weights for the market portfolio.
  > r = .04
> v <- m - r*u
> Wm <- (v%*%CI)/(v%*%CI%*%t(u))[1,1]
> Wm
[,1]     [,2]     [,3]
[1,] -4.157895 1.421053 3.736842
> #(f) Find the expected return and risk for the market portfolio
  > mum <- Wm %*% t(m)
> mum
[,1]
[1,] 0.6815789
> sigm <- (Wm %*% C %*% t(Wm))^.5
> sigm
[,1]
[1,] 1.772106
> #(g) Find the maximum expected return possible with a risk of 0.80.
  > rr <- .8
> maxmu <- r + ((mum-r)/sigm)*rr
> maxmu
[,1]
[1,] 0.3296345
> #(h) Find the weights for the portfolio that includes the bond and risky assets from (g).
  > x <- (1-(rr/sigm))[1,1]
> ws <- (1-x) * Wm
> ws <- append(ws, x, after = 3)
> ws
[1] -1.8770410  0.6415203  1.6869609  0.5485598

### ANSWERS QUESTION 1
# (a) W = [.9307 .1287 -.0594]
# (b) Expected Return = .0475  , Risk = .1919
# (c) W = [.510 .2357 .2548]
# (d) Risk = .2410
# (e) W = [-4.1579 1.4211 3.7368]
# (f) Expected Return = .6816  , Risk = 1.7721
# (g) Expected Return = .3296
# (h) Weights with Bond = [-1.877 .6415 1.6870 .5486]
#

# 2. For the given covariance matrix C and expected return matrix m, 
#    find (a)-(h).
### CODE FOR QUESTION 2
x = c(.15,-.18,.23,.10,.30,
      -.18,.35,.30,-.15,-.45,
      .23,.30,.71,-.25,.11,
      .10,-.15,-.25,.22,.40,
      .30,-.45,.11,.40,1.04)
C = matrix(x,nrow=5,byrow=T)
m = matrix(c(.12,.15,.28,.17,.32),nrow=1,byrow=T)
u = matrix(c(1,1,1,1,1),nrow=1,byrow=T)
CI <- solve(C)
# (a) The weights for the minimum variance portfolio
W <- (u%*%CI)/((u%*%CI%*%t(u))[1,1])
W
# (b) The expected return and risk for the minimum variance portfolio
mu <- W%*%t(m)
mu
sig <- (W%*%C%*%t(W))^.5
sig
#   (c) The weights for the portfolio on the minimum variance line with expected return of 15%.
coef1 <- (m%*%CI%*%t(m))[1,1]
coef2 <- (u%*%CI%*%t(m))[1,1]
coef3 <- (m%*%CI%*%t(u))[1,1]
coef4 <- (u%*%CI%*%t(u))[1,1]
M = matrix(c(coef1,coef2,coef3,coef4),nrow=2,byrow=T)
muv = matrix(c(.15,1),nrow=2,byrow=1) 
lambda <- (2*solve(M))%*%muv
l1 <- lambda[1,1]
l2 <- lambda[2,1]
wr <- ((l1%*%m%*%CI)+(l2%*%u%*%CI))/2
wr
# (d) The risk for the portfolio found in (c)
risk1 <- (wr%*%C%*%t(wr))^.5
risk1
# (e) Assume the risk-free return is R = 4%.  Find the weights for the market portfolio.
r = .04
v <- m - r*u
Wm <- (v%*%CI)/(v%*%CI%*%t(u))[1,1]
Wm
# (f) Find the expected return and risk for the market portfolio.
mum <- Wm %*% t(m)
mum
sigm <- (Wm %*% C %*% t(Wm))^.5
sigm
# (g) Find the maximum expected return possible with a risk of 0.14.
rr <- .14
maxmu <- r + ((mum-r)/sigm)*rr
maxmu
#(h) Find the weights for the portfolio that includes the bond and risky assets from (g).
x <- (1-(rr/sigm))[1,1]
ws <- (1-x) * Wm
ws <- append(ws, x, after = 5)
ws

### OUTPUT FOR QUESTION 2:
> x = c(.15,-.18,.23,.10,.30,
        + -.18,.35,.30,-.15,-.45,
        + .23,.30,.71,-.25,.11,
        + .10,-.15,-.25,.22,.40,
        + .30,-.45,.11,.40,1.04)
> C = matrix(x,nrow=5,byrow=T)
> m = matrix(c(.12,.15,.28,.17,.32),nrow=1,byrow=T)
> u = matrix(c(1,1,1,1,1),nrow=1,byrow=T)
> CI <- solve(C)
> # (a) The weights for the minimum variance portfolio
  > W <- (u%*%CI)/((u%*%CI%*%t(u))[1,1])
> W
[,1]      [,2]       [,3]      [,4]       [,5]
[1,] 0.4249648 0.1878899 0.05930747 0.5647671 -0.2369293
> # (b) The expected return and risk for the minimum variance portfolio
  > mu <- W%*%t(m)
> mu
[,1]
[1,] 0.1159784
> sig <- (W%*%C%*%t(W))^.5
> sig
[,1]
[1,] 0.1701857
> #   (c) The weights for the portfolio on the minimum variance line with expected return of 15%.
  > coef1 <- (m%*%CI%*%t(m))[1,1]
> coef2 <- (u%*%CI%*%t(m))[1,1]
> coef3 <- (m%*%CI%*%t(u))[1,1]
> coef4 <- (u%*%CI%*%t(u))[1,1]
> M = matrix(c(coef1,coef2,coef3,coef4),nrow=2,byrow=T)
> muv = matrix(c(.15,1),nrow=2,byrow=1)
> lambda <- (2*solve(M))%*%muv
> l1 <- lambda[1,1]
> l2 <- lambda[2,1]
> wr <- ((l1%*%m%*%CI)+(l2%*%u%*%CI))/2
> wr
[,1]      [,2]       [,3]      [,4]        [,5]
[1,] 0.2408733 0.2777456 0.07442993 0.4775426 -0.07059143
> # (d) The risk for the portfolio found in (c)
  > risk1 <- (wr%*%C%*%t(wr))^.5
> risk1
[,1]
[1,] 0.1905715
> # (e) Assume the risk-free return is R = 4%.  Find the weights for the market portfolio.
  > r = .04
> v <- m - r*u
> Wm <- (v%*%CI)/(v%*%CI%*%t(u))[1,1]
> Wm
[,1]      [,2]       [,3]      [,4]       [,5]
[1,] 0.1003248 0.3463478 0.08597549 0.4109491 0.05640278
> # (f) Find the expected return and risk for the market portfolio.
  > mum <- Wm %*% t(m)
> mum
[,1]
[1,] 0.1759745
> sigm <- (Wm %*% C %*% t(Wm))^.5
> sigm
[,1]
[1,] 0.2276705
> # (g) Find the maximum expected return possible with a risk of 0.14.
  > rr <- .14
> maxmu <- r + ((mum-r)/sigm)*rr
> maxmu
[,1]
[1,] 0.123614
> #(h) Find the weights for the portfolio that includes the bond and risky assets from (g).
  > x <- (1-(rr/sigm))[1,1]
> ws <- (1-x) * Wm
> ws <- append(ws, x, after = 5)
> ws
[1] 0.06169207 0.21297749 0.05286837 0.25270238 0.03468341 0.38507628


### ANSWERS TO QUESTION 2
# (a) W = [.4250 .1879 .0593 .5648 -.2369]
# (b) Expected Return = .1160  , Risk = .1702
# (c) W = [0.2409 0.2777 0.0744 0.4775 -0.07059]
# (d) Risk = .1906
# (e) W = [0.1003 0.3463 0.0859 0.4109 0.0564]
# (f) Expected Return = .1760  , Risk = .2277
# (g) Expected Return = .1236
# (h) Weights with Bond = [0.0617 0.2130 0.0529 0.2527 0.0347 0.3851]
#

                      ~fin~

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).