I am trying to solve a optimization portfolio in R in which I do the following constraints:

  • Set weight sum to within a boundary
  • Set return to a certain value
  • Set portfolio beta to 0

The purpose is then to minimize risk subject to the constraints above.

While I have no trouble in doing that my issue comes next. I want to make the weights such that the portfolio's total exposure to a factor is 0. Imagine asset a has a beta of 0.3, asset b has 0.7 and asset c -0.3. How can I set this constraint? My issue is that using quadprog I can only add an external parameter vector(mean returns in this case). Is there a way to go around this issue or am i seeing thing s the wrong way?

The code I have so far is as follows :

6 assets and six mean returns. Also, a 6*6 var-cov matrix. The upper weight bound is lev_ub and the lower bound is lev_lb. Returns_full is the vector with returns.

dvec = matrix(colMeans(returns_full),ncol = 1)
Dmat = cov(returns_full)
A.Constraint1 <- matrix(c(1,1,1,1,1,1), ncol=1)
A.Constraint2 = matrix(c(1,1,1,1,1,1), ncol=1)
Amat <- cbind(A.Equality1,A.Equality2, dvec)
bvec <- c(-lev_ub,lev_lb,target_return)
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=0)

My issue comes now from the fact that assume I have another vector of length 6 with one beta for each asset. I want to make it such that the sum of the product of weights and betas is zero or,in other words, set the total portfolio beta to 0. How can I add this constraint ?


3 Answers 3


Use PortfolioAnalytics

See my previous response here: https://quant.stackexchange.com/a/16002/2154 , you will find links to the documentation there.

You can use the constraint function to add a factor exposure constraint of 0.

use add.constraint(your_portfolio_name,type='factor_exposure',B = your_vector_of_betas,lower=0,upper=0)


Say we have $n$ assets. Suppose that the covariance matrix is $\Sigma$. Now suppose that $x$ is the vector of weights of your assets, $\mu$ is the vector of mean returns on the assets, $\mathbb{1}$ is the vector of all ones, $B$ is the vector of betas on each asset, and $\tau$ your target return. You want to solve the following quadratic program $$Min_x\;\; \frac{1}{2}x^T\Sigma x$$ Under the constraints $$x^T \mathbb{1}=1$$ $$\mu^Tx \geq \tau$$ $$B^Tx=0$$ The constraint $B^Tx=0$ will ensure that the portfolios beta is zero.

  • $\begingroup$ Sure, I guet this but how do I solve such problem? $\endgroup$ Commented Jan 18, 2015 at 0:39
  • $\begingroup$ Lagrange Multiplier $\endgroup$
    – pppp_prs
    Commented Jan 10 at 7:48

You have the budget constaint on the sum of weights, the constraint that the portfolio return equal the target return, and the constraint on beta. With three equality constraints, you can set a row in Amat for each constraint, and then take the transpose to pass to solve.QP. bvec is set to the right-hand side of the constraint equations and the parameter meq equals the number of equality constraints. An example for you problem is shown below:

target_return <-  0.0002
num_assets <- 6
dvec <- rep(0,num_assets)                        #  dvec = 0 when minimizing only the variance
Dmat <- cov(return_full)
Amat <- matrix(rep(1, num_assets), ncol=num_assets)      #  budget constraint: sum of wts = 1
Amat <- rbind(Amat, c(.3, .7, -.3, 1, 1.1, 1.3) )        #  beta constraint: sum of wt*beta = 0
Amat <- rbind(Amat, colMeans(return_full) )              #  portfolio return = target_return
# Amat <- rbind(Amat, diag(1,nrow=num_assets, ncol=num_assets) )    #  would be used for long-only solutions: wts >= 0
Amat <- t(Amat)
bvec <- c(budget = 1, beta = 0, return = target_return )         # rhs of constraint eqns
# bvec <- c(bvec, rep(0,num_assets) )       # would be used with long-only constraint
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.