1 | mathEfrontRisky(returns, npoints = 100, efront.only = T, display = T, digits = NULL)
|
returns |
|
npoints |
|
efront.only |
|
display |
|
digits |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (returns, npoints = 100, efront.only = T, display = T,
digits = NULL)
{
V = var(returns)
mu = apply(returns, 2, mean)
one = rep(1, nrow(V))
z = solve(V, one)
a = as.numeric(t(mu) %*% z)
cc = as.numeric(t(one) %*% z)
z = solve(V, mu)
b = as.numeric(t(mu) %*% z)
d = b * cc - a^2
gmv = mathGmv(returns)
sigma.gmv = gmv$vol
mu.stocks = apply(returns, 2, mean)
sigma.stocks = apply(returns, 2, var)^0.5
mu.max = 2 * max(mu.stocks)
sigma.max = (1/cc + (cc/d) * (mu.max - a/cc)^2)^0.5
sigma.max = 1.2 * sigma.max
sigma = seq(sigma.gmv, sigma.max, length = npoints)
mu.efront = a/cc + ((d * sigma^2)/cc - d/cc^2)^0.5
if (!efront.only) {
mu.front = a/cc - ((d * sigma^2)/cc - d/cc^2)^0.5
}
mu[1] = a/cc
xlim = c(0, max(sigma))
if (efront.only) {
ylim = range(mu.efront, mu.stocks)
}
else {
ylim = range(mu.efront, mu.front)
}
if (display) {
plot(sigma, mu.efront, type = "l", lwd = 3, xlim = xlim,
ylim = ylim, xlab = "VOL", ylab = "MEAN RETURN",
main = "PORTFOLIO FRONTIER")
if (!efront.only) {
lines(sigma, mu.front)
}
points(gmv$vol, gmv$mu, pch = 19)
text(gmv$vol, gmv$mu, "GMV", cex = 1.2, pos = 2)
points(sigma.stocks, mu.stocks, pch = 20)
text(sigma.stocks, mu.stocks, names(returns), cex = 0.5,
pos = 4)
text(0.07, 0.06, "EFFICIENT FRONTIER")
arrows(0.07, 0.056, 0.09, 0.038, length = 0.1)
}
if (is.null(digits)) {
out = list(mu.efront = mu.efront, vol.efront = sigma)
}
else {
vol.efront = sigma
out = rbind(mu.efront, vol.efront)
out = round(out, digits = digits)
}
out
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.