Constrained Portfolio Optimization with a Volatility Limit in R
Summary
The document describes an R approach to minimizing a linear portfolio objective while imposing limits on net exposure, individual asset weights, total absolute exposure, and portfolio volatility. It suggests using the nonlinear optimizer in Rsolnp, even though the other constraints can be handled with linear optimization techniques. The volatility constraint is evaluated from the covariance matrix as the square root of portfolio variance.
The proposed setup passes the net exposure, gross exposure, and volatility measures to the optimizer as inequality constraints, while applying individual weight limits through variable bounds. It also notes that the objective is a vector of asset-level coefficients multiplied by portfolio weights. The answer is an untested code example rather than a demonstrated solution: convergence depends on a suitable starting point and constraint bounds, and the author explicitly has no data with which to validate it. The document does not compare solvers or discuss covariance estimation and numerical conditioning.
Key ideas
- A quadratic volatility constraint can be included in a nonlinear optimization formulation for portfolio weights.
- Rsolnp is presented as one possible solver for the combined constraints.
- Net exposure and gross exposure constraints can be supplied as inequality functions.
- Individual asset bounds can be represented as optimizer parameter limits.
- Solver convergence and output should be treated cautiously because the example is untested.
Tags
Full text
# Portfolio optimization in R with factor tilting while constraining volatility
# Portfolio optimization in R with factor tilting while constraining volatility
what optimizer I can use in R to solve the following portfolio optimization problem:
$min(f^Tx)$ st: 1. $ -a \le \sum_{i=1} ^{n} x(i) \le b$ 2. $ -c \le x(i) \le d$ 3. $ e \le \sum _{i=1} ^n |x(i)| \le f$ 4. $\sqrt{x^t\Sigma x} \le g$
a,b,c,d,e,f,g - positive. f is a vector. x - weights. $\Sigma$ var-covar matrix estimated from historical data. The problem without constrain 4 can be solved with linear solver ( with some tricks for condition 3). Condition 4 makes constrain non-linear, but quadratic. Any suggestion would be helpful. Thanks.
## Answer by Eldioo (score 2)
https://quant.stackexchange.com/a/34686
You can try the `solnp()` function from the package `Rsolnp`, which is a solver for nonlinear optimization. First, you define your function that is to be minimized:
```
fun1 <- function(x) ( return(as.numeric(fvec %*% x)))
```
`fvec` is your fixed vector $f$.
Then you define the inequality function for the inequalities 1,3 and 4 - inequality 2 is covered by the parameter boundaries in the solver.
```
ineqfun1 <- function(x){
ineq1 <- sum(x);
ineq2 <- sum(abs(x));
ineq3 <- sqrt(x %*% vcovmat %*% x);
return(c(ineq1,ineq2,ineq3))
}
```
`vcovmat` is your var-cov matrix $\Sigma$.
After additionally defining the boundaries `a` to `g` and a starting estimate for x, which we call `xstart`, you can do the optimization:
```
sol <- solnp(pars=xstart, fun=fun1, ineqfun=ineqfun1,
ineqLB=c(-a,e,-Inf),ineqUB=c(b,f,g),
LB=-c, UB=d)
```
If the solver converges, you can get the resulting weight estimates with `sol$pars`. I have no data to try it, but the code should work. You will have to choose a sensible starting value and appropriate boundaries a-g, though.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.