8
Efficient Equity Portfolios using Mean Variance Optimization Gregg Barrett ======================== Efficient Equity Portfolios ======================== Data setwd("~/R/datasets") dat = read.csv("Stock_Bond.csv", header = T) prices = cbind(dat$GM_AC, dat$F_AC, dat$CAT_AC, dat$UTX_AC, dat$MRK_AC, dat$IBM_AC) n = dim(prices)[1] returns = 100 * (prices[2:n, ] / prices[1:(n-1), ] - 1) pairs(returns) mean_vect = colMeans(returns) cov_mat = cov(returns) sd_vect = sqrt(diag(cov_mat))

Efficient equity portfolios using mean variance optimisation in R

Embed Size (px)

Citation preview

Efficient Equity Portfolios using Mean Variance OptimizationGregg Barrett======================== Efficient Equity Portfolios ========================

Datasetwd("~/R/datasets")dat = read.csv("Stock_Bond.csv", header = T)prices = cbind(dat$GM_AC, dat$F_AC, dat$CAT_AC, dat$UTX_AC,

dat$MRK_AC, dat$IBM_AC)n = dim(prices)[1]returns = 100 * (prices[2:n, ] / prices[1:(n-1), ] - 1)pairs(returns)

mean_vect = colMeans(returns)cov_mat = cov(returns)sd_vect = sqrt(diag(cov_mat))

Functionlibrary(quadprog)

## Warning: package 'quadprog' was built under R version 3.2.3

eff_front_fn = function(returns, muP, mu_free, lower_limit_weight = -Inf, upper_limit_weight = +Inf){n_stocks = dim(returns)[2]mean_vect = apply(returns, 2, mean)cov_mat = cov(returns)sd_vect = sqrt(diag(cov_mat))Amat = cbind(rep(1,n_stocks),mean_vect)bvec = c(1,NaN)if( is.finite(lower_limit_weight) ){Amat = cbind(Amat, diag(1, nrow = n_stocks))bvec = c(bvec, lower_limit_weight * rep(1, n_stocks))

}if( is.finite(upper_limit_weight) ){Amat = cbind(Amat, -diag(1, nrow = n_stocks))bvec = c(bvec, -upper_limit_weight * rep(1, n_stocks))

}sdP = muPweights = matrix(0, nrow = length(muP), ncol = n_stocks)for( i in 1:length(muP) ){bvec[2] = +muP[i]result = solve.QP(Dmat = 2*cov_mat, dvec = rep(0, n_stocks), Amat = Ama

t, bvec = bvec, meq=2)sdP[i] = sqrt(result$value)weights[i,] = result$solution

}sharpe = ( muP - mu_free ) / sdPind_ms = (sharpe == max(sharpe))ind_mv = (sdP == min(sdP))

list( muP = muP, sdP = sdP, weights = weights, sharpe = sharpe, max_sharpe = ind_ms, min_variance = ind_mv )}

Setting the risk free rate, the target porfolio return and constraintsmu_free = 3.0/365muP = seq(min(mean_vect), max(mean_vect), length.out=300)mvo = eff_front_fn(returns, muP, mu_free = mu_free, lower_limit_weight = -0.1, upper_limit_weight = 0.5)sdP = mvo$sdP

Plotplot(sdP, muP, type = "l", xlim = c(0, 1.1 * max(sd_vect)),ylim = c(0, 1.1 * max(mean_vect)),lty=3)points(0, mu_free, cex = 4, pch = "*")sharpe = ( muP - mu_free ) / sdPind_ms = (sharpe == max(sharpe))print(mvo$weights[ind_ms,] )

## [1] -0.091164544 -0.002905131 0.335298962 0.383700258 0.319482622 ## [6] 0.055587833

lines(c(0, 2), mu_free + c(0, 2) * (muP[ind_ms] - mu_free) / sdP[ind_ms],lwd = 4, lty = 1, col = "blue")points(sdP[ind_ms], muP[ind_ms], cex = 4, pch = "*")ind_mv = (sdP == min(sdP))points(sdP[ind_mv], muP[ind_mv], cex = 2, pch = "+")ind3 = (muP > muP[ind_mv])lines(sdP[ind3], muP[ind3], type = "l", xlim = c(0, 0.25),ylim = c(0, 0.3), lwd = 3, col = "red")text(sd_vect[1], mean_vect[1], "GM", cex = 1.15)text(sd_vect[2], mean_vect[2], "F", cex = 1.15)text(sd_vect[3], mean_vect[3], "CAT", cex = 1.15)text(sd_vect[4], mean_vect[4], "UTX", cex = 1.15)text(sd_vect[5], mean_vect[5], "MRK", cex = 1.15)text(sd_vect[6], mean_vect[6], "IBM", cex = 1.15)

======== Efficient Equity Portfolios using the package “fPortfolio” from Rmetrics ========

Datalibrary(fPortfolio)

## Warning: package 'fPortfolio' was built under R version 3.2.3

## Loading required package: timeDate

## Warning: package 'timeDate' was built under R version 3.2.3

## Loading required package: timeSeries

## Warning: package 'timeSeries' was built under R version 3.2.3

## Loading required package: fBasics

## Warning: package 'fBasics' was built under R version 3.2.3

## ## ## Rmetrics Package fBasics ## Analysing Markets and calculating Basic Statistics ## Copyright (C) 2005-2014 Rmetrics Association Zurich ## Educational Software for Financial Engineering and Computational Science ## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY. ## https://www.rmetrics.org --- Mail to: [email protected] ## Loading required package: fAssets

## Warning: package 'fAssets' was built under R version 3.2.3

## ## ## Rmetrics Package fAssets ## Analysing and Modeling Financial Assets ## Copyright (C) 2005-2014 Rmetrics Association Zurich ## Educational Software for Financial Engineering and Computational Science ## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY. ## https://www.rmetrics.org --- Mail to: [email protected] ## ## ## Rmetrics Package fPortfolio ## Portfolio Optimization ## Copyright (C) 2005-2014 Rmetrics Association Zurich ## Educational Software for Financial Engineering and Computational Science ## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY. ## https://www.rmetrics.org --- Mail to: [email protected]

setwd("~/R/datasets")dat = read.csv("Stock_Bond.csv", header = T)prices = cbind(dat$GM_AC, dat$F_AC, dat$CAT_AC, dat$UTX_AC,

dat$MRK_AC, dat$IBM_AC)n = dim(prices)[1]returns = 100 * (prices[2:n, ] / prices[1:(n-1), ] - 1)pairs(returns)

mean_vect = colMeans(returns)cov_mat = cov(returns)sd_vect = sqrt(diag(cov_mat))

Constraintsreturns3 = as.timeSeries(returns)names(returns3) = c("GM", "F", "CAT", "UTX", "MRK", "IBM")Constraints = c("minW[1:nAssets] = rep(-0.10, times = nAssets)","maxW[1:nAssets] = rep(0.50, times = nAssets)")

Specspec1 = portfolioSpec()setRiskFreeRate(spec1) = 0.0082192

Model and plotmvo2 = portfolioFrontier(returns3, spec1, Constraints)frontierPlot(mvo2, frontier = c("both"),

col = c("black", "grey"), labels = TRUE,return = c("mean"), risk = c("Sigma"))

minvariancePoints(mvo2, col = "red")tangencyPoints(mvo2, col = "green")tangencyLines(mvo2, col = "purple")

Examination of weightsweightsSlider(mvo2)