Package 'nnsolve'

Title: Fast Non-Negative Least Squares
Description: This package provides a fast algorithm for solving non-negative least squares problems. It implements the Fast Non-Negative Least Squares algorithm. of Bro and De Jong (1997).
Authors: Nikolaos Kontemeniotis [aut, cre], Michail Tsagris [aut]
Maintainer: Nikolaos Kontemeniotis <[email protected]>
License: GPL (>= 2)
Version: 0.0.2
Built: 2026-06-06 06:25:35 UTC
Source: https://github.com/nikolas-kk/nnsolve

Help Index


Fast Non-Negative Least Squares

Description

Solves the NNLS problem min ||y - x * w||^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

symmetric positive definite matrix of dimensions k x k

Xty

numeric vector of length k

tol

convergence tolerance. Default 1e-6

max_iter

maximum number of iterations. Default 1000

sum_to_constant

if TRUE all entries sum to 'constant'.Default FALSE

constant

if sum_to_constant is TRUE, all entries sum to this number. Default 1

lower_bound

if TRUE all entries bounded below by 'lb', otherwise they are nonnegative. Default FALSE

lb

if lower_bound is TRUE all entries are bounded below by 'lb'. Default 0

Value

non-negative numeric vector of length k

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)

Fast Non-Negative Least Squares Regression

Description

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

Usage

fnnls_reg(
  y,
  X,
  tol = 1e-06,
  max_iter = 1000,
  sum_to_constant = FALSE,
  constant = 1,
  lower_bound = FALSE,
  lb = 0
)

Arguments

y

A numeric vector of length n.

X

A numeric matrix of dimensions n x 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 list with two elements:

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

  • mse: The mean squared error of the fitted model.

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

n <- 100
k <- 10
X <- matrix(rnorm(n * k), nrow = n, ncol = k)
true_b <- abs(rnorm(k))
y <- X %*% true_b + rnorm(n, sd = 0.1)
result <- fnnls_reg(y, X)
result$b
result$mse

Fast Non-Negative Least Squares for Multiple Outputs

Description

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

Usage

fnnls_regs(
  Y,
  X,
  tol = 1e-06,
  max_iter = 1000,
  sum_to_constant = FALSE,
  constant = 1,
  lower_bound = FALSE,
  lb = 0,
  parallel = FALSE,
  ncores = -1
)

Arguments

Y

A numeric matrix of dimensions n x m.

X

A numeric matrix of dimensions n x p.

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 in each column of B sum to 'constant'. Default is FALSE.

constant

If sum_to_constant is TRUE, all entries in each column 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.

parallel

If TRUE, the columns of B are computed in parallel. The default value is FALSE.

ncores

If parallel is TRUE, this many cores are used in the parallel computations. Must be positive integer. The default value is -1 (use all available cores).

Value

A list with two elements:

  • B: A non-negative numeric matrix of dimensions p x m with the estimated coefficients.

  • mse: A numeric vector of length m with the mean squared error for each output column.

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

n <- 50
p <- 10
m <- 3
X <- matrix(rnorm(n * p), nrow = n, ncol = p)
Y <- matrix(runif(n * m, min = 0, max = 10), nrow = n, ncol = m)
result <- fnnls_regs(Y, X, tol = 1e-8, max_iter = 1000)
result$B
result$mse