NAG FL Interface
f01saf (real_​nmf)

1 Purpose

f01saf computes a non-negative matrix factorization for a real non-negative m by n matrix A.

2 Specification

Fortran Interface
Subroutine f01saf ( m, n, k, a, lda, w, ldw, h, ldh, seed, errtol, maxit, ifail)
Integer, Intent (In) :: m, n, k, lda, ldw, ldh, seed, maxit
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: a(lda,*), errtol
Real (Kind=nag_wp), Intent (Inout) :: w(ldw,*), h(ldh,*)
C Header Interface
#include <nag.h>
void  f01saf_ (const Integer *m, const Integer *n, const Integer *k, const double a[], const Integer *lda, double w[], const Integer *ldw, double h[], const Integer *ldh, const Integer *seed, const double *errtol, const Integer *maxit, Integer *ifail)
The routine may be called by the names f01saf or nagf_matop_real_nmf.

3 Description

The matrix A is factorized into the product of an m by k matrix W and a k by n matrix H, both with non-negative elements. The factorization is approximate, AWH, with W and H chosen to minimize the functional
fW,H = A-WH F 2 .  
You are free to choose any value for k, provided k<minm,n. The product WH will then be a low-rank approximation to A, with rank at most k.
f01saf finds W and H using an iterative method known as the Hierarchical Alternating Least Squares algorithm. You may specify initial values for W and H, or you may provide a seed value for f01saf to generate the initial values using a random number generator.

4 References

Cichocki A and Phan A–H (2009) Fast local algorithms for large scale nonnegative matrix and tensor factorizations IEICE Transactions on Fundamentals of Electronics, Communications and Computer Sciences E92–A 708–721
Cichocki A, Zdunek R and Amari S–I (2007) Hierarchical ALS algorithms for nonnegative matrix and 3D tensor factorization Lecture Notes in Computer Science 4666 Springer 169–176
Ho N–D (2008) Nonnegative matrix factorization algorithms and applications PhD Thesis Univ. Catholique de Louvain

5 Arguments

1: m Integer Input
On entry: m, the number of rows of the matrix A. Also the number of rows of the matrix W.
Constraint: m2.
2: n Integer Input
On entry: n, the number of columns of the matrix A. Also the number of columns of the matrix H.
Constraint: n2.
3: k Integer Input
On entry: k, the number of columns of the matrix W; the number of rows of the matrix H. See Section 9.2 for further details.
Constraint: 1k<minm,n.
4: alda* Real (Kind=nag_wp) array Input
Note: the second dimension of the array a must be at least n.
On entry: the m by n non-negative matrix A.
5: lda Integer Input
On entry: the first dimension of the array a as declared in the (sub)program from which f01saf is called.
Constraint: ldam.
6: wldw* Real (Kind=nag_wp) array Input/Output
Note: the second dimension of the array w must be at least k.
On entry:
  • if seed0, w should be set to an initial iterate for the non-negative matrix factor, W.
  • If seed1, w need not be set. f01saf will generate a random initial iterate.
On exit: the non-negative matrix factor, W.
7: ldw Integer Input
On entry: the first dimension of the array w as declared in the (sub)program from which f01saf is called.
Constraint: ldwm.
8: hldh* Real (Kind=nag_wp) array Input/Output
Note: the second dimension of the array h must be at least n.
On entry:
  • if seed0, h should be set to an initial iterate for the non-negative matrix factor, H.
  • If seed1, h need not be set. f01saf will generate a random initial iterate.
On exit: the non-negative matrix factor, H.
9: ldh Integer Input
On entry: the first dimension of the array h as declared in the (sub)program from which f01saf is called.
Constraint: ldhk.
10: seed Integer Input
On entry:
  • if seed0, the supplied values of W and H are used for the initial iterate.
  • If seed1, the value of seed is used to seed a random number generator for the initial iterates W and H. See Section 9.3 for further details.
11: errtol Real (Kind=nag_wp) Input
On entry: the convergence tolerance for when the Hierarchical Alternating Least Squares iteration has reached a stationary point. If errtol0.0, maxm,n×machine precision is used.
12: maxit Integer Input
On entry: specifies the maximum number of iterations to be used. If maxit0, 200 is used.
13: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 or 1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this argument, the recommended value is 0. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6 Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
ifail=1
On entry, m=value.
Constraint: m2.
ifail=2
On entry, n=value.
Constraint: n2.
ifail=3
On entry, k=value, m=value and n=value.
Constraint: 1k<minm,n.
ifail=4
On entry, lda=value and m=value.
Constraint: ldam.
ifail=5
On entry, ldw=value and m=value.
Constraint: ldwm.
ifail=6
On entry, ldh=value and k=value.
Constraint: ldhk.
ifail=7
The routine has failed to converge after value iterations. The factorization given by w and h may still be a good enough approximation to be useful. Alternatively an improved factorization may be obtained by increasing maxit or using different initial choices of w and h.
ifail=8
An internal error occurred when generating initial values for w and h. Please contact NAG.
ifail=9
On entry, one of more of the elements of a, w or h were negative.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

The Hierarchical Alternating Least Squares algorithm used by f01saf is locally convergent; it is guaranteed to converge to a stationary point of fW,H, but this may not be the global minimum. The iteration is deemed to have converged if the gradient of fW,H is less than errtol times the gradient at the initial values of W and H.
Due to the local convergence property, you may wish to run f01saf multiple times with different starting iterates. This can be done by explicitly providing the starting values of W and H each time, or by choosing a different random seed for each routine call.
Note that even if f01saf exits with ifail=7, the factorization given by W and H may still be a good enough approximation to be useful.

8 Parallelism and Performance

f01saf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
f01saf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

Each iteration of the Hierarchical Alternating Least Squares algorithm requires Omnk floating-point operations.
The real allocatable memory required is m×n+km+n.
If A is large and sparse, then f01sbf should be used to compute a non-negative matrix factorization.

9.1 Uniqueness

Note that non-negative matrix factorization is not unique. For a factorization given by the matrices W and H, an equally good solution is given by WD and D-1H, where D is any real non-negative k×k matrix whose inverse is also non-negative. In f01saf, W and H are normalized so that the columns of W have unit length.

9.2 Choice of k

The most appropriate choice of the factorization rank, k, is often problem dependent. Details of your particular application may help in guiding your choice of k, for example, it may be known a priori that the data in A naturally falls into a certain number of categories.
Alternatively, trial and error can be used. Compute non-negative matrix factorizations for several different values of k (typically with kminm,n) and select the one that performs the best.
Finally, it is also possible to use a singular value decomposition of A to guide your choice of k, by looking for an abrupt decay in the size of the singular values of A. The singular value decomposition can be computed using f08kbf.

9.3 Generating Random Initial Iterates

If seed1 on entry, then f01saf uses the routines g05kff and g05saf, with the NAG basic generator, to populate w and h. For further information on this random number generator see Section 2.1.1 in the G05 Chapter Introduction.
Note that this generator gives a repeatable sequence of random numbers, so if the value of seed is not changed between routine calls, then the same initial iterates will be generated.

10 Example

This example finds a non-negative matrix factorization for the matrix
A= 8 10 5 10 5 9 21 7 17 10 12 17 8 14 6 14 18 9 16 7 13 29 10 23 13 10 17 7 14 7 .  

10.1 Program Text

Program Text (f01safe.f90)

10.2 Program Data

Program Data (f01safe.d)

10.3 Program Results

Program Results (f01safe.r)