Title: | Quadratic Programming using Cyclic Projections |
---|---|
Description: | Solves quadratic programming problems using Richard L. Dykstra's cyclic projection algorithm. Routine allows for a combination of equality and inequality constraints. See Dykstra (1983) <doi:10.1080/01621459.1983.10477029> for details. |
Authors: | Nathaniel E. Helwig <[email protected]> |
Maintainer: | Nathaniel E. Helwig <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0-0 |
Built: | 2024-10-31 21:12:25 UTC |
Source: | CRAN |
This function uses Dykstra's cyclic projection algorithm to solve quadratic programming problems of the form
subject to where
is a positive definite (or positive semidefinite) matrix.
dykstra(Dmat, dvec, Amat, bvec, meq = 0, factorized = FALSE, maxit = NULL, eps = NULL)
dykstra(Dmat, dvec, Amat, bvec, meq = 0, factorized = FALSE, maxit = NULL, eps = NULL)
Dmat |
Quadratic program matrix |
dvec |
Quadratic program vector |
Amat |
Constraint matrix |
bvec |
Constraint vector |
meq |
First |
factorized |
If |
maxit |
Maximum number of iterations (cycles). Defaults to |
eps |
Numeric tolerance. Defaults to |
Arguments 1-6 of the dykstra
function are inspired by (and identical to) the corresponding arguments of the solve.QP
function in the quadprog package.
solution |
Vector |
value |
Value of quadratic function at |
unconstrained |
Vector |
iterations |
Number of iterations (cycles) of the algorithm. |
converged |
|
For positive semidefinite , a small constant is added to each eigenvalue of
before solving the quadratic programming problem.
Nathaniel E. Helwig <[email protected]>
Dykstra, Richard L. (1983). An algorithm for restricted least squares regression. Journal of the American Statistical Association, Volume 78, Issue 384, 837-842. doi: 10.1080/01621459.1983.10477029
### EXAMPLE 1: Generic Quadratic Programming Problem ### # constraint 1 (equality): coefficients sum to 1 # constraints 2-4 (inequality): coefficients non-negative # define QP problem Dmat <- diag(3) dvec <- c(1, 1.5, 1) Amat <- cbind(rep(1, 3), diag(3)) bvec <- c(1, 0, 0, 0) # solve QP problem dykstra(Dmat, dvec, Amat, bvec, meq = 1) # solve QP problem (factorized = TRUE) dykstra(Dmat, dvec, Amat, bvec, meq = 1, factorized = TRUE) ### EXAMPLE 2: Regression with Non-Negative Coefficients ### # generate regression data set.seed(1) nobs <- 100 nvar <- 5 X <- matrix(rnorm(nobs*nvar), nobs, nvar) beta <- c(0, 1, 0.3, 0.7, 0.1) y <- X %*% beta + rnorm(nobs) # define QP problem Dmat <- crossprod(X) dvec <- crossprod(X, y) Amat <- diag(nvar) # solve QP problem dykstra(Dmat, dvec, Amat) # solve QP problem (factorized = TRUE) Rmat <- chol(Dmat) Rinv <- solve(Rmat) dykstra(Rinv, dvec, Amat, factorized = TRUE) ### EXAMPLE 3: Isotonic Regression ### # generate regression data set.seed(1) n <- 50 x <- 1:n y <- log(x) + rnorm(n) # define QP problem Dmat <- diag(n) Amat <- Dmat[, 2:n] - Dmat[, 1:(n-1)] # solve QP problem dyk <- dykstra(Dmat, y, Amat) dyk # plot results plot(x, y) lines(x, dyk$solution) ### EX 4: Large Non-Negative Quadratic Program ### # define QP problem set.seed(1) n <- 1000 Dmat <- Amat <- diag(n) dvec <- runif(n, min = -2) # solve QP problem with dykstra dyk <- dykstra(Dmat, dvec, Amat) dyk
### EXAMPLE 1: Generic Quadratic Programming Problem ### # constraint 1 (equality): coefficients sum to 1 # constraints 2-4 (inequality): coefficients non-negative # define QP problem Dmat <- diag(3) dvec <- c(1, 1.5, 1) Amat <- cbind(rep(1, 3), diag(3)) bvec <- c(1, 0, 0, 0) # solve QP problem dykstra(Dmat, dvec, Amat, bvec, meq = 1) # solve QP problem (factorized = TRUE) dykstra(Dmat, dvec, Amat, bvec, meq = 1, factorized = TRUE) ### EXAMPLE 2: Regression with Non-Negative Coefficients ### # generate regression data set.seed(1) nobs <- 100 nvar <- 5 X <- matrix(rnorm(nobs*nvar), nobs, nvar) beta <- c(0, 1, 0.3, 0.7, 0.1) y <- X %*% beta + rnorm(nobs) # define QP problem Dmat <- crossprod(X) dvec <- crossprod(X, y) Amat <- diag(nvar) # solve QP problem dykstra(Dmat, dvec, Amat) # solve QP problem (factorized = TRUE) Rmat <- chol(Dmat) Rinv <- solve(Rmat) dykstra(Rinv, dvec, Amat, factorized = TRUE) ### EXAMPLE 3: Isotonic Regression ### # generate regression data set.seed(1) n <- 50 x <- 1:n y <- log(x) + rnorm(n) # define QP problem Dmat <- diag(n) Amat <- Dmat[, 2:n] - Dmat[, 1:(n-1)] # solve QP problem dyk <- dykstra(Dmat, y, Amat) dyk # plot results plot(x, y) lines(x, dyk$solution) ### EX 4: Large Non-Negative Quadratic Program ### # define QP problem set.seed(1) n <- 1000 Dmat <- Amat <- diag(n) dvec <- runif(n, min = -2) # solve QP problem with dykstra dyk <- dykstra(Dmat, dvec, Amat) dyk