# Quadratic problems with norm constraints in R

I'm trying to reproduce the results of DeMiguel et al. (2009), i.e. trying to obtain norm-constrained portfolios. To do so, I need to solve two optimization programs.

Notations : $$N$$ is the number of assets in the portfolio, $$w$$ is the Nx1 vector of weights, $$\delta$$ is exogenously fixed (real and positive, typically $$1$$ or $$1/N$$), $$\Sigma$$ is a symmetric NxN covariance matrix, $$A$$ is a NxN identity matrix.

1) 1-Norm constraint where $$||w|| = \sum_{i=1}^N |w_i|$$,

2) 2-Norm constraint where $$A$$ is a NxN identity matrix.

I'm writing my thesis in R, and I'm looking for ways to solve these two problems in R. These problems are not standard quadratic programs (QP) because of the nonlinear constraint in the first program ($$||w|| \leq \delta$$) and the quadratic constraint in the second one ($$w'Aw\leq\delta$$), making it a QCQP (quadratically constrained QP). Hence, the well-known quadprog package is not useful since it only handles linear constraints.

I've already spend some time looking for answers in different forums, but I didn't yet find what I'm looking for, i.e. an "R only" implementation, as I'd very much like to avoid using other optimization softwares.

Concerning the first problem, I've heard of the quadprogXT package extending quadprog to problems with absolute values in OF/constraints, but I didn't manage yet to succesfully implement it. Same goes for the CLSOCP package for QCQP problems. Of course, if any other packages are more adapted, please let me know.

If anyone is interested in giving this a shot, please find below the only data you'll need, i.e. the covariance matrix of the securities' returns.

covmatrix = matrix(c(0.004468923,0.003229201,0.003091624,0.002521959,0.002076048,0.002233386,0.003229201,0.002688044,0.002666069,0.001890422,0.001813305,0.002021806,0.003091624, 0.002666069, 0.002811833,0.001790847,0.001821353,0.002131751,0.002521959,0.001890422,0.001790847,0.002080253,0.001701675,0.001720723,0.002076048,0.001813305,0.001821353,0.001701675,0.001854355,0.001856546,0.002233386,0.002021806,0.002131751,0.001720723,0.001856546,0.002318369),nrow=6,ncol=6)


Many thanks in advance!

PS. This is my first post on this forum, please let me know if smth is unclear! :-) PS2. An example of implementation would be awesome!!

• Aren't these just Lasso and Ridge regression problems, respectively? The glmnet library efficiently implements solutions to both.
– whuber
Apr 5, 2019 at 17:47

Here is how to do with CVXR.

Sigma <- matrix(
c(0.004468923,0.003229201,0.003091624,0.002521959,0.002076048,0.002233386,
0.003229201,0.002688044,0.002666069,0.001890422,0.001813305,0.002021806,
0.003091624,0.002666069,0.002811833,0.001790847,0.001821353,0.002131751,
0.002521959,0.001890422,0.001790847,0.002080253,0.001701675,0.001720723,
0.002076048,0.001813305,0.001821353,0.001701675,0.001854355,0.001856546,
0.002233386,0.002021806,0.002131751,0.001720723,0.001856546,0.002318369),
nrow=6, ncol=6)
delta <- 1

library(CVXR)
w <- Variable(6)
objective <- Minimize(quad_form(w, Sigma))

# first problem
constraint1 <- sum(abs(w)) < delta  # or  norm1(w) < delta
constraint2 <- sum(w) == 1
problem <- Problem(objective, constraints = list(constraint1, constraint2))
result <- solve(problem)
result$$getValue(w) # optimal w # [,1] # [1,] 8.437165e-08 # [2,] 1.205772e-06 # [3,] 1.008583e-05 # [4,] 2.873903e-01 # [5,] 7.125944e-01 # [6,] 4.001052e-06 result$$value # value of objective at optimal w
# [1] 0.001810476

# second problem
constraint1 <- quad_form(w, diag(6)) < delta  # or  norm2(w) < sqrt(delta)
constraint2 <- sum(w) == 1
problem <- Problem(objective, constraints = list(constraint1, constraint2))
result <- solve(problem)
result$$getValue(w) # [,1] # [1,] -0.53215099 # [2,] 0.41212600 # [3,] 0.18344919 # [4,] 0.61207111 # [5,] 0.36965414 # [6,] -0.04514945 result$$value
# [1] 0.001542726

• Brilliant solution! Thanks! Works like a charm.
– Rvdk
Apr 9, 2019 at 17:15

Leaving aside the ability of the glmnet library to solve these particular problems, as per @whuber 's comment above, I recommend you consider installing CVXR https://cvxr.rbind.io/ , a package for Disciplined Convex Programming in R. This is similar to CVX (MATLAB) and CVXPY (Python), but for R.

Not only can CVXR be used to easily formulate and solve these problems, it can also be used to easily formulate and solve many variations on these problems which glmnet will not be able to handle. For instance, use any combination of norms in objective function and constraints, include other constraints, such as linear, quadratic, or constraining one or more variables (parameters) to be estimated to be integer, or matrix to be symmetric positive semidefinite (equivalent to constraining it to be a covariance matrix).

The execution time will not be as fast as specialized solvers for particular problems,but the human time can be much less, especially for new or novel formulations.