FE 825 Numerical Mean-Variance Optimization in R
Numerical Mean-Variance Optimization in R
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
FE 825
Numerical Mean-Variance Optimization in R
The three classic problems in mean-variance portfolio optimization, minimum variance,
maximum expected utility, maximum Sharpe Ratio, can all be written as quadratic pro-
gramming problems. With only expected return, and full investment constraints, they
have analytical solutions. If we add no-short-sales and other bound constraints, one has to
resort to numerical optimization. It is easily done in the EXCEL solver when it is only a
few optimizations. Larger scale projects require programmable languages like R or Matlab.
This note describes the use of solve.QP (package quadprog) and portfolio.optim (pack-
age tseries). It does not repeat the help file you can get from R.
solve.QP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE) solves:
min
b
−d′b+ 12b′Db (1)
S.T. A′b ≥ b0 (2)
For portfolio optimization, b is the N-vector of weights, it is an output of the routine, not
an input. D is proportional to the returns covariance matrix. The inputs needed are
• dvec: N-vector, set to zero if not needed.
• Dmat: holds D, a N ×N matrix.
• Amat: holds A, a N ×m matrix of coefficients needed for the m constraints. Note
that solve.QP use A′, not A. Don’t ask why!
• bvec: holds b0 m-vector needed for the m constraints
• meq: number of strict equality constraints. They must be the first (meq) constraints
listed. The remaining (m - meq) constraints are ≥ 0 constraints.
1
1 Minimum variance portfolio
min
w
w′V w
S.T. µ′w = µp
i′w = 1, (3)
where i is a vector of 1s, µp is the desired portfolio mean return (a scalar), and V is the
covariance matrix.
One needs to lump all the constraints in the A′b ≥ b0 formulation needed by solve.QP.
We also input the number of strict equality constraints. We have:
• meq = 2, the mean and full investment constraints are equalities.
• dvec = 0, not needed here
• Dmat = V (or 2V, it does not matter here)
• b0 =
(
µp
1
)
• A′ =
(
µ′
i′
)
, so we have A′w =
(
µp
1
)
.
1.1 No-short-sales constraint
Input them as N additional positivity constraints. Together with the full investment con-
straint, it guarantees that no single weight is above 100% as well.
• Extend the A′ matrix, stacking an identity matrix below the first two rows.
• Extend the b0 vector, adding N zeros.
Write it to see that it does the trick.
1.2 Global MVP
Remove the first constraint (first row of A′ and first element of b0). Don’t forget to change
meq.
1.3 Other constraints
• Incorporate any additional constraint by an additional row to A′ and an additional
element to b0.
• For ≤ constraints, transform them into ≥ constraints by multiplying by −1.
• Put all the equality constraints first and set meq = correctly.
2
1.4 Command portfolio.optim
For the MVP problem subject to a desired return, the command portfolio.optim (package
tseries) is a convenient substitute. It is however a bit limited in scope.
portfolio.optim(x, pm = mean(x), riskless = FALSE, shorts = FALSE, rf =
0.0, reslow = NULL, reshigh = NULL, covmat = cov(x))
• It can’t have sophisticated constraints, but you can directly input bounds on weights
by using shorts = F. You can also use shorts = T. You can alternatively specify
N-vectors of lower bounds and upper bounds for the parameters by setting reslow
and reshigh.
• It computes mean and covariance matrix from the data matrix (input x in the com-
mand). So you can’t use your own vector of means different from the sample mean.
You can however supply an alternative covariance matrix instead of cov(x) by using
covmat = .
• It can’t do a simple global MVP. It always minimizes variance subject to a desired
mean return, pm in the inputs. If you don’t supply pm, it uses the global mean of
all the returns.
• You can set a risk-free rate, using riskless=T, rf = .
2 Maximum Certainty Equivalent Problem
To maximize the CE, with no risk free rate:
max
w
µ′w − 12Aw′V w
S.T. 1′w = 1
Compare to
min
b
−d′b+ 12 b′Db
S.T. A′b ≥ b0
• meq = 1, only the full investment constraints is an equality.
• dvec = µ, the vector of means
• Dmat = γV , where γ is the risk aversion used.
• b0 = 1
• A′ = i′.
• Add the no-short-sales constraint if required.
3
3 Maximum Sharpe Ratio
3.1 Maximum Sharpe Ratio Problem
With the existence of a risk-free asset, the frontier is the CAL. All portfolios on the CAL
have the same Sharpe Ratio as the Tangency Portfolio. Maximizing the Sharpe Ratio
amounts to identifying any one portfolio on the frontier. See the notes. We solve:
min
w
w′V w
S.T (µ−Rf i)′w = µp −Rf ,
where Rf is the risk-free rate.
Choose µp > Rf and use solve.QP with only one equality constraint. Rescale the re-
sulting risky weights so they sum to one to get the Tangency portfolio.
One can also formulate a maximum CE problem with risk-free rate (choose an arbitrary
risk aversion, 2 for example) to find a portfolio on the CAL. The differences with the
previous section are that (1) there is no full investment constraint, and (2) dvec is the
expected return µ−Rf i
4 Beyond
If you’re interested in general numerical optimization, the book Convex Optimization by
Stephen Boyd and Lieven Vandenberghe is a good starting point.
If you need more complex constraints (non-linear constraints) and alternate objective
functions, you will need to use more expansive packages. The two packages below cover a
wide range of portfolio optimization objectives and constraints.
1. Portfolio Analytics
2. fPortfolio
Also, for more optimization package choices in R, take a look at R’s official CRAN Task
page of Optimization and Mathematical Programming.