### Install quadprog with npm Source: https://github.com/albertosantini/quadprog/blob/master/README.md Use npm to install the quadprog package for Node.js projects. ```bash npm install quadprog ``` -------------------------------- ### Solve Portfolio Optimization with Equality Constraints in Quadprog Source: https://context7.com/albertosantini/quadprog/llms.txt Use the `meq` parameter in `solveQP` to specify equality constraints, such as portfolio weights summing to 1. This example demonstrates portfolio optimization with 7 assets, including sum-to-one, non-negativity, and upper bound constraints. ```javascript import { solveQP } from "quadprog"; // Portfolio optimization example with 7 assets // Minimize portfolio variance subject to: // - Weights sum to 1 (equality constraint) // - Each weight >= 0 (non-negativity) // - Each weight <= 0.5 (upper bounds) // Covariance matrix (7x7, 1-indexed) const Dmat = [, [, 0.0004523, 0.0005097, 0.0005725, 0.0005050, -0.0000113, 0.0001956, 0.0003307], [, 0.0005097, 0.0006951, 0.0006418, 0.0006697, -0.0000019, 0.0002421, 0.0003855], [, 0.0005725, 0.0006418, 0.0008402, 0.0006540, -0.0000125, 0.0002540, 0.0004705], [, 0.0005050, 0.0006697, 0.0006540, 0.0008528, 0.0000168, 0.0002643, 0.0003591], [, -0.0000113, -0.0000019, -0.0000125, 0.0000168, 0.0000603, 0.0000283, -0.0000274], [, 0.0001956, 0.0002421, 0.0002540, 0.0002643, 0.0000283, 0.0001418, 0.0001441], [, 0.0003307, 0.0003855, 0.0004705, 0.0003591, -0.0000274, 0.0001441, 0.0022755] ]; // Expected returns vector const dvec = [, 0.0000671, 0.0000422, 0.0000713, 0.0000290, 0.0000280, 0.0000404, 0.0000458]; // Constraint matrix: sum=1, lower bounds, upper bounds const Amat = [, [, 1, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0], [, 1, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0], [, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0], [, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0], [, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0], [, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0], [, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1] ]; // Constraint bounds: [sum=1, lower>=0, upper<=-0.5 (negated)] const bvec = [, 1, 0, 0, 0, 0, 0, 0, 0, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5]; const meq = 1; // First constraint is equality (weights sum to 1) const result = solveQP(Dmat, dvec, Amat, bvec, meq); console.log("Portfolio weights:", result.solution.slice(1)); // [0.0905, ~0, ~0, ~0, 0.5, 0.4001, 0.0094] console.log("Iterations:", result.iterations.slice(1)); // [7, 1] console.log("Error:", result.message); // '' (empty = success) ``` -------------------------------- ### Solve Quadratic Programming Problem in R Source: https://github.com/albertosantini/quadprog/blob/master/README.md Example demonstrating how to use the solve.QP function in R to solve a quadratic programming problem with specified constraints and objective function. ```r ## Assume we want to minimize: -(0 5 0) %*% b + 1/2 b^T b ## under the constraints: A^T b >= b0 ## with b0 = (-8,2,0)^T ## and ## (-4 2 0) ## A = (-3 1 -2) ## ( 0 0 1) ## we can use solve.QP as follows: ## require(quadprog) Dmat <- matrix(0, 3, 3) diag(Dmat) <- 1 dvec <- c(0, 5 ,0) Amat <- matrix(c(-4, -3, 0, 2, 1, 0, 0, -2, 1), 3, 3) bvec <- c(-8, 2 ,0) solve.QP(Dmat, dvec, Amat, bvec=bvec) # $solution # [1] 0.4761905 1.0476190 2.0952381 # $value # [1] -2.380952 # $unconstrained.solution # [1] 0 5 0 # $iterations # [1] 3 0 # $Lagrangian # [1] 0.0000000 0.2380952 2.0952381 # $iact ``` -------------------------------- ### Solve Quadratic Programming Problem Source: https://github.com/albertosantini/quadprog/blob/master/README.md Use this function to solve a quadratic programming problem. Ensure that array indices start from 1, as this implementation is a direct port of Fortran code. ```javascript import { solveQP } from "quadprog"; const Dmat = [], dvec = [], Amat = [], bvec = []; Dmat[1] = []; Dmat[2] = []; Dmat[3] = []; Dmat[1][1] = 1; Dmat[2][1] = 0; Dmat[3][1] = 0; Dmat[1][2] = 0; Dmat[2][2] = 1; Dmat[3][2] = 0; Dmat[1][3] = 0; Dmat[2][3] = 0; Dmat[3][3] = 1; dvec[1] = 0; dvec[2] = 5; dvec[3] = 0; Amat[1] = []; Amat[2] = []; Amat[3] = []; Amat[1][1] = -4; Amat[2][1] = -3; Amat[3][1] = 0; Amat[1][2] = 2; Amat[2][2] = 1; Amat[3][2] = 0; Amat[1][3] = 0; Amat[2][3] = -2; Amat[3][3] = 1; bvec[1] = -8; bvec[2] = 2; bvec[3] = 0; solveQP(Dmat, dvec, Amat, bvec) ``` -------------------------------- ### CommonJS Usage of Quadprog (JavaScript) Source: https://context7.com/albertosantini/quadprog/llms.txt Demonstrates how to import and use `solveQP` in a CommonJS environment using `require`. Note the array initialization syntax, which is typical for CommonJS when dealing with sparse or 1-indexed arrays. ```javascript // CommonJS import const { solveQP } = require("quadprog"); const Dmat = []; Dmat[1] = [, 1, 0, 0]; Dmat[2] = [, 0, 1, 0]; Dmat[3] = [, 0, 0, 1]; const dvec = [, 0, 5, 0]; const Amat = []; Amat[1] = [, -4, 2, 0]; Amat[2] = [, -3, 1, -2]; Amat[3] = [, 0, 0, 1]; const bvec = [, -8, 2, 0]; const result = solveQP(Dmat, dvec, Amat, bvec); console.log("Solution:", result.solution.slice(1)); // [0.476190476190476, 1.047619047619047, 2.095238095238095] ``` -------------------------------- ### Solve QP with Factorized Matrix (JavaScript) Source: https://context7.com/albertosantini/quadprog/llms.txt Demonstrates using `solveQP` with a pre-computed Cholesky factor for `Dmat`. Set `factorized` to `[0, 1]` to enable this mode. This can improve performance for repeated optimizations with the same D matrix. ```javascript import { solveQP } from "quadprog"; // Using pre-factorized Cholesky decomposition // If D = R'R, pass R instead of D const Dmat = [, [, 1, 0, 0], [, 0, 1, 0], [, 0, 0, 1] ]; const dvec = [, 0, 5, 0]; const Amat = [, [, -4, 2, 0], [, -3, 1, -2], [, 0, 0, 1] ]; const bvec = [, -8, 2, 0]; const meq = 0; const factorized = [0, 1]; // [0, 1] means factorized=true const result = solveQP(Dmat, dvec, Amat, bvec, meq, factorized); if (result.message === '') { console.log("Optimization successful"); console.log("Solution:", result.solution.slice(1)); } else { console.log("Error:", result.message); // Possible errors: // - "constraints are inconsistent, no solution!" // - "matrix D in quadratic function is not positive definite!" } ``` -------------------------------- ### Run Project Tests Source: https://github.com/albertosantini/quadprog/blob/master/CONTRIBUTING.md Execute the project's test suite to ensure a clean slate before making changes. This command is essential for verifying the integrity of your contributions. ```bash npm test ``` -------------------------------- ### Solve Quadratic Programming Problems with Quadprog Source: https://context7.com/albertosantini/quadprog/llms.txt Use solveQP for standard quadratic programming problems. Ensure arrays are 1-indexed for compatibility with the Fortran implementation. The result object contains the solution, Lagrangian values, optimal value, and other diagnostic information. ```javascript import { solveQP } from "quadprog"; // Problem: minimize -(0,5,0)'b + 1/2 b'Db // Subject to: A'b >= b0 // where b0 = (-8, 2, 0)' // and A = [[-4, 2, 0], [-3, 1, -2], [0, 0, 1]] // Create 1-indexed arrays (index 0 is unused) const Dmat = []; Dmat[1] = [, 1, 0, 0]; // Identity matrix (3x3) Dmat[2] = [, 0, 1, 0]; Dmat[3] = [, 0, 0, 1]; const dvec = [, 0, 5, 0]; // Objective vector const Amat = []; Amat[1] = [, -4, 2, 0]; // Constraint matrix Amat[2] = [, -3, 1, -2]; Amat[3] = [, 0, 0, 1]; const bvec = [, -8, 2, 0]; // Constraint bounds const result = solveQP(Dmat, dvec, Amat, bvec); console.log(result); // { // solution: [<1 empty>, 0.476190476190476, 1.047619047619047, 2.095238095238095], // Lagrangian: [<1 empty>, 0, 0.238095238095238, 2.095238095238095], // value: [<1 empty>, -2.380952380952381], // unconstrained_solution: [<1 empty>, 0, 5, 0], // iterations: [<1 empty>, 3, 0], // iact: [<1 empty>, 3, 2, 0], // message: '' // } // Access solution values (remember 1-indexed) console.log("Solution:", result.solution.slice(1)); // [0.476..., 1.047..., 2.095...] console.log("Optimal value:", result.value[1]); // -2.380952380952381 console.log("Active constraints:", result.iact.slice(1).filter(x => x > 0)); // [3, 2] ``` -------------------------------- ### Import solveQP function (CJS) Source: https://github.com/albertosantini/quadprog/blob/master/README.md Import the solveQP function using CommonJS syntax. ```javascript const { solveQP } = require("quadprog"); // CJS ``` -------------------------------- ### solveQP with Equality Constraints Source: https://context7.com/albertosantini/quadprog/llms.txt The `solveQP` function can handle equality constraints by specifying the `meq` parameter. The first `meq` constraints in `Amat` and `bvec` are treated as equalities (`A'b = b0`), while the rest are inequalities (`A'b >= b0`). This is useful for problems like portfolio optimization where weights must sum to a specific value. ```APIDOC ## POST /solveQP (with equality constraints) ### Description Solves quadratic programming problems with a mix of equality and inequality constraints. The first `meq` constraints are treated as equalities. ### Method POST ### Endpoint /solveQP ### Parameters #### Request Body - **Dmat** (Array>) - Required - The positive definite matrix D. - **dvec** (Array) - Required - The objective vector d. - **Amat** (Array>) - Required - The constraint matrix A. - **bvec** (Array) - Required - The constraint bounds b0. - **meq** (number) - Required - The number of equality constraints. ### Request Example ```json { "Dmat": [ [, 0.0004523, 0.0005097, ...], [, 0.0005097, 0.0006951, ...], ... ], "dvec": [, 0.0000671, 0.0000422, ...], "Amat": [ [, 1, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0], [, 1, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0], ... ], "bvec": [, 1, 0, 0, 0, 0, 0, 0, 0, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5], "meq": 1 } ``` ### Response #### Success Response (200) - **solution** (Array) - The solution vector b. - **Lagrangian** (Array) - The Lagrangian multipliers. - **value** (Array) - The optimal value of the objective function. - **unconstrained_solution** (Array) - The solution without constraints. - **iterations** (Array) - Number of iterations performed. - **iact** (Array) - Indices of active constraints. - **message** (string) - Status message. #### Response Example ```json { "solution": [, 0.0905, 0, 0, 0, 0.5, 0.4001, 0.0094], "Lagrangian": [...], "value": [...], "unconstrained_solution": [...], "iterations": [, 7, 1], "iact": [...], "message": "" } ``` ``` -------------------------------- ### Import solveQP function (ESM) Source: https://github.com/albertosantini/quadprog/blob/master/README.md Import the solveQP function using ECMAScript module syntax. ```javascript import { solveQP } from "quadprog"; // ESM ``` -------------------------------- ### solveQP Function Source: https://github.com/albertosantini/quadprog/blob/master/README.md The `solveQP` function is the core of the quadprog library, used to solve quadratic programming problems. It takes matrices and vectors defining the quadratic objective function and constraints, and returns the solution, value, and other relevant information. ```APIDOC ## solveQP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE) ### Description Solves a quadratic programming problem. ### Method `solveQP` ### Parameters #### Arguments - **Dmat** (matrix) - Matrix appearing in the quadratic function to be minimized. - **dvec** (vector) - Vector appearing in the quadratic function to be minimized. - **Amat** (matrix) - Matrix defining the constraints under which we want to minimize the quadratic function. - **bvec** (vector) - Vector holding the values of b0 (defaults to zero). - **meq** (number) - The first `meq` constraints are treated as equality constraints, all further as inequality constraints (defaults to 0). - **factorized** (boolean) - Logical flag: if TRUE, then we are passing R1 (where D = RT R) instead of the matrix D in the argument Dmat (defaults to FALSE). ### Return Value An object with the following properties: - **solution** (vector) - Vector containing the solution of the quadratic programming problem. - **value** (scalar) - The value of the quadratic function at the solution. - **unconstrained.solution** (vector) - Vector containing the unconstrained minimizer of the quadratic function. - **iterations** (vector) - Vector of length 2, the first component contains the number of iterations the algorithm needed, the second indicates how often constraints became inactive after becoming active first. - **Lagrangian** (vector) - Vector with the Lagrangian multipliers at the solution. - **iact** (vector) - Vector with the indices of the active constraints at the solution. - **message** (string) - String containing an error message, if the call failed, otherwise empty. ### Request Example ```javascript import { solveQP } from "quadprog"; const Dmat = []; const dvec = []; const Amat = []; const bvec = []; Dmat[1] = []; Dmat[2] = []; Dmat[3] = []; Dmat[1][1] = 1; Dmat[2][1] = 0; Dmat[3][1] = 0; Dmat[1][2] = 0; Dmat[2][2] = 1; Dmat[3][2] = 0; Dmat[1][3] = 0; Dmat[2][3] = 0; Dmat[3][3] = 1; dvec[1] = 0; dvec[2] = 5; dvec[3] = 0; Amat[1] = []; Amat[2] = []; Amat[3] = []; Amat[1][1] = -4; Amat[2][1] = -3; Amat[3][1] = 0; Amat[1][2] = 2; Amat[2][2] = 1; Amat[3][2] = 0; Amat[1][3] = 0; Amat[2][3] = -2; Amat[3][3] = 1; bvec[1] = -8; bvec[2] = 2; bvec[3] = 0; solveQP(Dmat, dvec, Amat, bvec); ``` ### Response Example ```json { "solution": [ null, 0.47619047619047616, 1.0476190476190477, 2.0952380952380953 ], "Lagrangian": [ null, 0, 0.23809523809523808, 2.0952380952380953 ], "value": [ null, -2.380952380952381 ], "unconstrained_solution": [ null, 0, 5, 0 ], "iterations": [ null, 3, 0 ], "iact": [ null, 3, 2, 0 ], "message": "" } ``` ``` -------------------------------- ### Handle Validation Errors in solveQP (JavaScript) Source: https://context7.com/albertosantini/quadprog/llms.txt Shows how `solveQP` handles input validation errors, such as incompatible dimensions between `Dmat` and `dvec`, and infeasible constraints. Check the `message` property of the result for error details. ```javascript import { solveQP } from "quadprog"; // Example: Handling validation errors // Incompatible dimensions const badDmat = [, [, 1, 0], [, 0, 1] ]; const badDvec = [, 0, 5, 0]; const Amat = [, [, 1], [, 1], [, 1]]; const bvec = [, 0]; let result = solveQP(badDmat, badDvec, Amat, bvec); console.log(result.message); // "Dmat and dvec are incompatible!" // Example: Infeasible constraints const Dmat = [, [, 1, 0], [, 0, 1]]; const dvec = [, 0, 0]; const infeasibleAmat = [, [, 1, -1], [, -1, 1] ]; const infeasibleBvec = [, 1, 1]; result = solveQP(Dmat, dvec, infeasibleAmat, infeasibleBvec); if (result.message) { console.log("Problem:", result.message); // "constraints are inconsistent, no solution!" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.