As recounted in Portfolio Theory is Dead, Now What?, the minimum variance portfolio is the optimal portfolio in a world with zero risk premium. This post expands on the topic via practical implementation in R, preceded by walk through of the corresponding mathematics.

Recall the classic Markowitz mean-variance optimal portfolio is the solution to:

\begin{aligned} & \underset{w}{\text{min}} & &w^{\mathrm{T}} \hat{\Omega} w - \frac{1}{\gamma} \hat{\mu}^{\mathrm{T}} w \\ & \text{subject to} & & \sum_i{w_i} = 1, \\ \end{aligned}

where $w \in \bold{\mathbb{R}^n}$ are vector of portfolio weights, $\hat{\mu}^\mathrm{T} w$ is sample mean portfolio return, and $w^\mathrm{T} \hat{\Omega} w$ is sample covariance of portfolio returns. The summation to one constraint ensures weight percentages sum to 100%, meaning full investment. An additional common requirement is no short selling (e.g. 401k, IRAs, and mutual funds), which adds the following non-negative optimization constraint:

$\forall i : w_i \ge 0$

From this starting point, consider two assumptions which lead to the minimum variance portfolio:

• Aversion to risk is infinite, thus $\gamma = \infty$

With those assumptions, the above optimization reduces to the following covariance minimization:

\begin{aligned} & \underset{w}{\text{min}} & &w^\mathrm{T} \hat{\Omega} w \\ & \text{subject to} & & \sum_i{w_i} = 1, \\ \end{aligned}

For portfolios constructed from individual stocks (as opposed to sectors, for example), an upper bound $\epsilon$ constraint on the weights is appropriate to minimize idiosyncratic risk:

$\forall i : 0 \le w_i < \epsilon.$

Jagannathan and Ma (2002) show this constraint improves estimation by amplifying shrinkage efficacy. The authors demonstrate a similar statistical shrinkage benefit provided by the zero lower bound.

Although this is a rudimentary constrained optimization problem, what makes it fun is the “Markowitz optimization enigma”: robustly estimating the covariance matrix $\hat{\Omega}$. Consider three estimators from the literature, the first two following Falkenblog and Estimation Risk and the final being a more recent estimator. All estimators ensure the covariance matrix is positive definite (as required to ensure the optimization is convex).

• Asymptotic Components with Hetersokedastic Factor Residuals

Following Falkenstein, use Connor and Korajczyk (1993) and Jones (2001) to estimate $\hat{\Omega}$:

$\hat{\Omega} = B^\mathrm{T} (F F^\mathrm{T}) B$
$B = (F F^\mathrm{T})^{-1} (F R^\mathrm{T})$

Where $R$ ($N \times T$) is historical returns for T periods on N instruments and $F$ ($T \times K$) are the Jones factors.

• Bayesian Sample Shrinkage Estimator

Similar to Nogales, use Ledoit and Wolf (2004) to estimate $\hat{\Omega}$ via shrinkage with a Bayesian prior and sample covariance matrix:

$\hat{\Omega} = \hat{\delta} F + (1 - \hat{\delta})S$
$\hat{\delta} = \max(0, \min(\frac{\hat{\kappa}}{T}, 1))$
$\hat{\kappa} = \frac{\hat{\pi} - \hat{\phi}}{\hat{\gamma}}$

where $T$ is length of time-series, $S$ is the sample covariance matrix, $\hat{\delta}$ is the shrinkage constant, and the elements of the estimator $F$ are composed:

$f_{ii} = s_{ii} : \forall i$
$f_{ij} = \bar{r} \sqrt{s_{ii}s_{jj}} : \forall i \neq j$

• Sample Variance and Correlation Shrinkage Estimator

Following Schäfer and Strimmer (2005) and Opgen-Rhein and Strimmer (2007) to estimate $\hat{\Omega}$ via shrinkage of both variance and correlation:

$\hat{s}_{kl} = \hat{r}_{kl} \sqrt{\hat{v_k}\hat{v_l}}$
$\hat{r}_{kl} = (1 - \hat{\lambda_1}) r_{kl}$
$\hat{v_k} = \hat{\lambda_2}v_{\mathrm{median}} + (1 - \hat{\lambda_2}) v_k$
$\hat{\lambda_1} = \min(1, \frac{\sum\limits_{k \neq l} \hat{var}(r_{kl})}{\sum\limits_{k \neq l} r^2_{kl}})$
$\hat{\lambda_2} = \min(1, \frac{\sum\limits_{k=1}^p \hat{var}(v_k)}{\sum\limits_{k=1}^p (v_k - v_{\mathrm{median}})^2})$

Now consider the R necessary to generate these portfolios. Readers seeking more background on portfolio optimization in R are referred to R Tools for Portfolio Optimization by Yollin (R/Finance 2009).

The most important detail is choice of covariance estimator, including Schäfer-Strimmer-Opgen-Rhein from corpcor package and Ledoit-Wolf from tawny package (both functions confusingly named cov.shrink). The Ledoit-Wolf estimator will be used in this and subsequent posts.

These optimizations can be formulated as quadratic programming, and solved using solve.QP from quadprog, via formulating the above optimizations into the following functional form (following Goldfarb and Idnani (1982, 1983)) with a zero $d^\mathrm{T}$:

\begin{aligned} & \underset{b}{\text{min}} & & \frac{1}{2} b^{\mathrm{T}} D b - d^{\mathrm{T}} b \\ & \text{subject to} & & A^{\mathrm{T}} b \ge b_0 \end{aligned}

Assume X is a matrix of log returns, then generate optimization inputs:

<code>covar <- cov.shrink(X)
N <- ncol(X)
zeros <- array(0, dim = c(N,1))


Evaluate the optimization to generate minimum variance portfolio without short selling constraint:

aMat  <- t(array(1, dim = c(1,N)))
res <- solve.QP(covar, zeros, aMat, bvec=1, meq = 1)


Or, similar optimization with short selling constraint (i.e. non-negative weights):

aMat <- cbind(t(array(1, dim = c(1,N))), diag(N))
b0 <- as.matrix(c(1, rep.int(0,N)))
res <- solve.QP(covar, zeros, aMat, bvec=b0, meq = 1)


Finally, return portfolio attributes (similar to portfolio.optim):

y <- X %*% res$solution port <- list(pw = round(res$solution,3), px = y, pm = mean(y), ps = sd(y))


One minor numerical analysis reminder: use of solve.QP with short selling constraints may generate weights with a negative sign (e.g. -3.417223e-17), due to floating point precision and numerical stability. Such weights are equal to zero when rounded to a reasonable number of digits.

A subsequent post will apply these minimum variance optimization to building and evaluating risk for rotation strategies.

April 24, 2011 9:08 am

Hi,

I tried the same procedure to get a minimum variance portfolio, but I am getting large weights for 1 or 2 individual stocks.

Is there a way to run solveQP with the constraint that the absolute value of the weights are less than some positive number?

Thanks…

April 24, 2011 10:27 am

@agathocles: no, an epsilon constraint makes the problem not suitable for solve.QP. If you are interested, I can write a follow-up post that walks through adding such constraints and corresponding R.

Btw: I concur with your intuition to avoid portfolios composed of individual equities with large weights (as opposed to sectors or countries, for example), due to idiosyncratic risk.

December 14, 2011 7:05 am

I would be interested in the follow up post of the problem mentioned above.i.e how to optimise a portfolio in R, with constraints on the weights of the individual assets held in the portfolio and the constraint of the sum of the weights in the portfolio summing to 1.

I have tried to do this using the optim function in R, setting the minimum & maximum tolerances as lower and upper bounds but once I set the constraint of the weights summing to 1 in the function, it creates an error.

2. May 23, 2011 7:52 pm

I’ve just started playing about with modifications to the optimal mean-variance portfolio in Excel (an initial spreadsheet is here http://optimizeyourportfolio.blogspot.com/2011/05/markowitz-portfolio-optimization.html). This is a good summary of the theory, and motivates me to try R.

December 10, 2012 5:29 am

Hi, nice post and blog! I’m sorry to comment on this now but would you mind posting the R code for the constrained optimization problem for your 130/30 strategy in the same way that you did in this post? it would be great. Many thanks in any case.

June 30, 2013 12:19 am

Very nice straightforward coding!

For the questioners looking to apply constraints, check out rMetrics (fPortfolio package) which supports all sorts of adaptation of MVO approaches.