Reputation: 11
I am trying to achieve a plot, where for different values for the expected return on the y-axis, the corresponding variance of the portfolio fulfilling is given.
To construct the plot, I need to apply the Markowitz variance for different values of the expected return. In order to do so, I have to write a function which takes as input:
The function should return a vector containing the variances of the portfolio for the corresponding values of the expected return.
The minimum value for the portfolio return should be 0.001 and the maximum 0.2 with 20 number of points in-between. I have already calculated the sigma and the expected returns from my data set. I think I have to write some kind of a loop function, but I'm stuck here.
The markowitz variance i computed with:
markowitz.variance <- function(mu.target, mu, sigma) {
n <- nrow(sigma)
ones <- rep(1,n)
sigma.inv <- solve(sigma)
A <- t(ones)%*%sigma.inv%*%ones
B <- t(ones)%*%sigma.inv%*%mu
C <- t(mu)%*%sigma.inv%*%mu
delta <- A%*%C-B%*%B
variance <- (A*mu.target^2 - 2*B*mu.target + C) / delta
return(variance)
}
The covariance matrix and the expected return with:
sigma <- cov(constituents.return[,-1])
mu <- apply(constituents.return[,-1],2, mean)
I know that i have to somehow create a loop where the expected return is cycled 20 times by an increment of 0.01 from 0.001 to 0.2 to get to the points and have the variance as the return, but I haven't been able to quite understand how to input the return into the cycle.
Upvotes: 1
Views: 138
Reputation: 1666
If I understand your question correctly,
min.expected.return <- 0.001
max.expected.return <- 0.2
num.points <- 20
mu.targets <- seq(min.expected.return, max.expected.return, length=num.points)
sapply(mu.targets, function (mu.target) markowitz.variance(mu.target, mu, sigma))
Upvotes: 0