1 | mathEfrontCashRisky(returns, rf = 0.005, scalex = 1.2, scaley = 2, display.stocks = T, stock.names = T, risk.tol = F, npoints = 10, plot.efront = T, wts.plot = T, equal.wts = T, bar.ylim = c(0, 1), digits = NULL)
|
returns |
|
rf |
|
scalex |
|
scaley |
|
display.stocks |
|
stock.names |
|
risk.tol |
|
npoints |
|
plot.efront |
|
wts.plot |
|
equal.wts |
|
bar.ylim |
|
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | ##---- 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, rf = 0.005, scalex = 1.2, scaley = 2, display.stocks = T,
stock.names = T, risk.tol = F, npoints = 10, plot.efront = T,
wts.plot = T, equal.wts = T, bar.ylim = c(0, 1), digits = NULL)
{
V = var(returns)
mu.stocks = apply(returns, 2, mean)
sigma.stocks = apply(returns, 2, sd)
mue = mu.stocks - rf
a = solve(V, mue)
b = as.numeric(t(mue) %*% a)
sr.opt = sqrt(as.numeric(b))
bsr = sqrt(b)
mu.max = scaley * max(mu.stocks)
muvals = seq(0, mu.max, length.out = npoints)
sigmavals = c(muvals/bsr)
muvalse = seq(0, mu.max, length.out = npoints)
muvals = rf + muvalse
inv.lambdavals = (muvals - rf)/b
if (plot.efront) {
if (wts.plot) {
par(mfrow = c(1, 2))
}
if (risk.tol) {
x = inv.lambdavals
y = muvals
xlab = "Risk Tolerance"
par(mfrow = c(1, 1))
}
else {
x = sigmavals
y = muvals
xlab = "Portfolio Standard Deviation"
}
xlim = c(0, scalex * max(sigma.stocks, x))
ylim = c(min(mu.stocks), max(muvals))
plot(x, y, type = "l", xaxs = "i", lwd = 2, xlim = xlim,
ylim = ylim, xlab = xlab, ylab = "Portfolio Mean Return")
if (risk.tol) {
display.stocks = F
stock.names = F
}
if (display.stocks) {
points(sigma.stocks, mu.stocks, pch = 20)
if (stock.names) {
text(sigma.stocks + 0.02 * xlim[2], mu.stocks,
names(returns), cex = 0.5, adj = 0)
}
}
x <- xlim[1] + 0.05 * (xlim[2] - xlim[1])
y <- ylim[2] - 0.02 * (ylim[2] - ylim[1])
text(x, y, paste("Rf = ", round(rf, 3), sep = ""), pos = 4)
y = y - 0.05 * (ylim[2] - ylim[1])
text(x, y, paste("SR = ", round(sr.opt, 2), sep = ""),
pos = 4)
if (equal.wts) {
n = length(mu.stocks)
wts = rep(1/n, n)
mu.eq = mean(mu.stocks)
sigma.eq = as.numeric((t(wts) %*% V %*% wts)^0.5)
sr.eq = mu.eq/sigma.eq
points(sigma.eq, mu.eq, pch = 15, cex = 1.2)
text(sigma.eq, mu.eq, "EQ.WTS", pos = 4, cex = 0.7)
}
}
wts.risky = (sum(a)/b) * muvalse
wts.cash = 1 - wts.risky
wts.efront = rbind(wts.cash, wts.risky)
row.names(wts.efront) = c("Cash", "Risky Assets")
if (wts.plot) {
barplot.wts(wts.efront, legend.text = T, col = topo.colors(2),
ylab = "Weights", xlab = xlab, bar.ylim = bar.ylim)
par(mfrow = c(1, 1))
}
if (is.null(digits)) {
if (!risk.tol) {
wts.efront
}
}
else {
if (equal.wts) {
out = list(MU.EQ.WT = mu.eq, STDEV.EQ.WT = sigma.eq,
SR.EQ.WT = sr.eq, SR.EFRONT = sr.opt)
out = lapply(out, round, digits = digits)
}
else {
out = list(SR.EFRONT = sr.opt)
out = lapply(out, round, digits = digits)
}
out
}
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.