Search code examples
javascriptoptimizationlinear-algebraquantitative-financequadratic-programming

Portfolio Optimization Constraints wrong using quadprog in JavaScript


I cannot figure out what I am doing wrong with this portfolio optimization (find optimal weights) using quadprog in numeric.js

My portfolio constraints are simple: weights should sum up to 1 and all weights (for each of the 3 assets) should be between 0 and 1 (no short selling, no leverage). Constraints are not recognized and weights get very high (and also negative).

    var constraintsmatrix = [[0,0,0,0], [1,0,1,0], [1,0,0,0]]; 
    var covmatrix = [[0.00020817,0.00016281,0.00009747],[0.00016281,0.00026680,0.00009912],[0.00009747,0.00009912,0.00019958]]; 
    var returnsmatrix = [0.1,0.05,0.1];
    var bvec = 1; // [1,0,0,0,0,0,0,1,1,1];
    var result = numeric.solveQP(covmatrix, returnsmatrix, constraintsmatrix, bvec);

Any hint appreciated. Thanks


Solution

  • I believe your constraints are not correctly specified and the rhs is not passed on. We want the following constraints (equality first):

    x1+x2+x3 = 1
    x1 >= 0
    x2 >= 0
    x3 >= 0
    

    This corresponds to

    A=[[1,1,0,0],[1,0,1,0],[1,0,0,1]]
    b=[1,0,0,0]
    

    Here is what I get:

    enter image description here

    Note that the objective of this model is 0.5*x'Dx-d'x. Also note I pass on 5 arguments to solveQP.