fnnls: Fast Non-Negative Least Squares

View source: R/fnnls.R

fnnlsR Documentation

Fast Non-Negative Least Squares

Description

Solves the NNLS problem min ||y - Xw||^2 subject to w >= 0 using the Fast Non-Negative Least Squares algorithm of Bro & de Jong (1997).

Usage

fnnls(
  XtX,
  Xty,
  tol = 1e-06,
  max_iter = 1000,
  sum_to_constant = FALSE,
  constant = 1,
  lower_bound = FALSE,
  lb = 0
)

Arguments

XtX

A symmetric positive definite matrix of dimensions k x k.

Xty

A numeric vector of length k.

tol

The convergence tolerance, default is 1e-6.

max_iter

The maximum number of iterations, default is 1000.

sum_to_constant

If TRUE all entries sum to 'constant', Default is FALSE.

constant

If sum_to_constant is TRUE, all entries sum to this number. The default value is 1.

lower_bound

If TRUE all entries bounded below by 'lb', otherwise they are nonnegative. The default value is FALSE.

lb

If lower_bound is TRUE all entries are bounded below by 'lb'. The default value is 0.

Value

A non-negative numeric vector of length k with the estimated coefficients.

References

Bro, Rasmus & Jong, Sijmen. (1997). A Fast Non-negativity-constrained Least Squares Algorithm. Journal of Chemometrics. 11. 393-401. 10.1002/(SICI)1099-128X(199709/10)11:53.0.CO;2-L.

Examples

k <- 10
D <- 100
H <- matrix(rnorm(k * D), nrow = k, ncol = D)
x <- rnorm(D)
XtX <- H %*% t(H) + diag(1e-8, k)
Xty <- as.vector(H %*% x)
w <- fnnls(XtX, Xty)


nnsolve documentation built on April 12, 2026, 5:06 p.m.