library(data.table) library(scales) library(ggplot2) #######DATA er_x=4/100 er_y=5/100 er_z=6/100 sd_x=2/100 sd_y=3/100 sd_z=4/100 cor_xy=0 cor_xz=0 cor_yz=-1 cov_xy=cor_xy*sd_y*sd_y cov_xz=cor_xz*sd_x*sd_z cov_yz=cor_yz*sd_y*sd_z # create portfolio weights (omegas) x_weights <- seq(from = 0, to = 1, length.out = 1000) # create a data.table that contains the weights for the three assets three_assets <- data.table(wx = rep(x_weights, each = length(x_weights)), wy = rep(x_weights, length(x_weights))) three_assets three_assets[, wz := 1 - wx - wy] # calculate the expected returns and standard deviations for the 1000 possible portfolios three_assets[, ':=' (er_p = wx * er_x + wy * er_y + wz * er_z, sd_p = sqrt(wx^2 * sd_x^2 + wy^2 * sd_y^2 + wz^2 * sd_z^2 + 2 * wx * wy * cov_xy + 2 * wx * wz * cov_xz + 2 * wy * wz * cov_yz))] # take out cases where we have negative weights (shortselling) three_assets <- three_assets[wx >= 0 & wy >= 0 & wz >= 0] three_assets[er_p<0.045 & sd_p<0.015] # lastly plot the values ggplot() + geom_point(data = three_assets, aes(x = sd_p, y = er_p, color = wx - wz)) + geom_point(data = data.table(sd = c(sd_x, sd_y, sd_z), mean = c(er_x, er_y, er_z)), aes(x = sd, y = mean), color = "red", size = 3, shape = 18) + # Miscellaneous Formatting theme_bw() + ggtitle("Possible Portfolios with Three Risky Assets") + xlab("Volatility") + ylab("Expected Returns") + scale_y_continuous(label = percent, limits = c(0, max(three_assets$er_p) * 1.2)) + scale_x_continuous(label = percent, limits = c(0, max(three_assets$sd_p) * 1.2)) + scale_color_gradientn(colors = c("red", "blue", "yellow"), name = expression(omega[x] - omega[z]), labels = percent) # # # # #