NAG CPP Interface
nagcpp::matop::real_nmf_rcomm (f01sb)

Settings help

CPP Name Style:



1 Purpose

real_nmf_rcomm computes a non-negative matrix factorization for a real non-negative m×n matrix A. It uses reverse communication for evaluating matrix products, so that the matrix A is not accessed explicitly.

2 Specification

#include "f01/nagcpp_f01sb.hpp"
template <typename W, typename H, typename HT>

void function real_nmf_rcomm(types::f77_integer &irevcm, W &&w, H &&h, HT &&ht, CopyableComm &comm, OptionalF01SB opt)
template <typename W, typename H, typename HT>

void function real_nmf_rcomm(types::f77_integer &irevcm, W &&w, H &&h, HT &&ht, CopyableComm &comm)

3 Description

The matrix A is factorized into the product of an m×k matrix W and a k×n matrix H, both with non-negative elements. The factorization is approximate, AWH, with W and H chosen to minimize the functional
f(W,H) = A-WH F 2 .  
You are free to choose any value for k, provided k<min(m,n). The product WH will then be a low-rank approximation to A, with rank at most k.
real_nmf_rcomm 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 real_nmf_rcomm to generate the initial values using a random number generator.
real_nmf_rcomm does not explicitly need to access the elements of A; it only requires the result of matrix multiplications of the form AX or ATY. A reverse communication interface is used, in which control is returned to the calling program whenever a matrix product is required.

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

Note: this function uses reverse communication. Its use involves an initial entry, intermediate exits and re-entries, and a final exit, as indicated by the argument irevcm. Between intermediate exits and re-entries, all arguments other than w and ht must remain unchanged.
1: irevcm types::f77_integer Input/Output
On initial entry: must be set to 0.
On intermediate exit: specifies what action you must take before re-entering real_nmf_rcomm with irevcm unchanged. The value of irevcm should be interpreted as follows:
irevcm=1
Indicates the start of a new iteration. No action is required by you, but w and h are available for printing, and a limit on the number of iterations can be applied.
irevcm=2
Indicates that before re-entry to real_nmf_rcomm, the product ATW must be computed and stored in ht.
irevcm=3
Indicates that before re-entry to real_nmf_rcomm, the product AHT must be computed and stored in w.
On final exit: irevcm=0.
2: w(m,k) double array Input/Output
On initial 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. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit: if irevcm=1 or 2, w contains the current iterate of the m×k non-negative matrix W.
On intermediate re-entry:
  • if irevcm=3, w must contain AHT, where HT is stored in ht.
  • If irevcm=0, 1 or 2, w must not be changed.
On final exit: w contains the m×k non-negative matrix W.
3: h(k,n) double array Input/Output
On initial 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. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit: if irevcm=1, h contains the current iterate of the k×n non-negative matrix H.
On intermediate re-entry: h must not be changed.
On final exit: h contains the k×n non-negative matrix H.
4: ht(n,k) double array Input/Output
On initial entry: ht need not be set.
On intermediate exit: if irevcm=3, ht contains the n×k non-negative matrix HT, which is required in order to form AHT.
On intermediate re-entry: if irevcm=2, ht must contain ATW.
If irevcm=0, 1 or 3, ht must not be changed.
On final exit: ht is undefined.
5: comm CopyableComm Input/Output
Communication structure. On initial entry: need not be set.
Container for:
commdouble array
This optional parameter may be set using the method CopyableComm::comm and accessed via CopyableComm::get_comm.
icommtypes::f77_integer array
This optional parameter may be set using the method CopyableComm::icomm and accessed via CopyableComm::get_icomm.
6: opt OptionalF01SB Input/Output
Optional parameter container, derived from Optional.
Container for:
seedtypes::f77_integer
This optional parameter may be set using the method OptionalF01SB::seed and accessed via OptionalF01SB::get_seed.
Default: 1
On initial 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.
errtoldouble
This optional parameter may be set using the method OptionalF01SB::errtol and accessed via OptionalF01SB::get_errtol.
Default: 0.0
On entry: the convergence tolerance for when the Hierarchical Alternating Least Squares iteration has reached a stationary point. If errtol0.0, max(m,n)×machine precision is used.

5.1Additional Quantities

1: m
m, the number of rows of the matrix A
2: n
n, the number of columns of the matrix A
3: k
k, the number of columns of the matrix W

6 Exceptions and Warnings

Errors or warnings detected by the function:
All errors and warnings have an associated numeric error code field, errorid, stored either as a member of the thrown exception object (see errorid), or as a member of opt.ifail, depending on how errors and warnings are being handled (see Error Handling for more details).
Raises: ErrorException
errorid=1
On intermediate re-entry, irevcm = value.
Constraint: irevcm = 1,2​ or ​3.
errorid=1
On initial entry, irevcm = value.
Constraint: irevcm=0.
errorid=2
On entry, m = value.
Constraint: m2.
errorid=3
On entry, n = value.
Constraint: n2.
errorid=4
On entry, k = value, m = value and
n = value.
Constraint: 1k<min(m,n).
errorid=8
An internal error occurred when generating initial values for w and h.
Please contact NAG.
errorid=9
On entry, one of more of the elements of w or h were negative.
errorid=10601
On entry, argument value must be a value x value array.
Supplied argument has value dimensions.
errorid=10601
On entry, argument value must be a value x value array.
Supplied argument was a value x value array.
errorid=10601
On entry, argument value must be a value x value array.
Not all of the sizes for the supplied array could be ascertained.
errorid=10602
On entry, the raw data component of value is null.
errorid=10603
On entry, unable to ascertain a value for value.
errorid=10604
On entry, the data in value is stored in value Major Order.
The data was expected to be in value Major Order.
errorid=10605
On entry, the communication class value has not been initialized correctly.
errorid=−99
An unexpected error has been triggered by this routine.
errorid=−399
Your licence key may have expired or may not have been installed correctly.
errorid=−999
Dynamic memory allocation failed.

7 Accuracy

The Hierarchical Alternating Least Squares algorithm used by real_nmf_rcomm is locally convergent; it is guaranteed to converge to a stationary point of f(W,H), but this may not be the global minimum. The iteration is deemed to have converged if the gradient of f(W,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 real_nmf_rcomm 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 each time.
Note that even if real_nmf_rcomm exits with errorid=7, the factorization given by W and H may still be a good enough approximation to be useful.

8 Parallelism and Performance

Please see the description for the underlying computational routine in this section of the FL Interface documentation.

9 Further Comments

real_nmf_rcomm is designed to be used when A is large and sparse. Whenever a matrix multiplication is required, the function will return control to the calling program so that the multiplication can be done in the most efficient way possible. Note that W and H will not, in general, be sparse even if A is sparse.
If A is small and dense, then f01saf (no CPP interface) can be used to compute W and H without the use of a reverse communication interface.

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 real_nmf_rcomm, 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 kmin(m,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 f12fbf (no CPP interface).

9.3 Generating Random Initial Iterates

If seed1 on entry, then real_nmf_rcomm uses the functions g05kff (no CPP interface) and g05saf (no CPP interface), 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 function calls, then the same initial iterates will be generated.

9.4 Use in Conjunction with NAG Library Functions

To compute the non-negative matrix factorization, the following logic can normally be used:
The code used to compute the matrix products will vary depending on the way A is stored. If all the elements of A are stored explicitly, then dgemm) can be used. If A is triangular, then f06yff (no CPP interface) should be used. If A is symmetric, then f06ycf (no CPP interface) should be used. For sparse A stored in coordinate storage format real_​gen_​matvec and f11xef (no CPP interface) can be used. Alternatively, if A is stored in compressed column format direct_​real_​gen_​matmul can be used.

10 Example

This example finds a non-negative matrix factorization for the matrix
A= ( 0 1 4 0 0 2 6 0 0 2 0 0 0 7 2 0 6 1 3 0 2 1 7 1 6 2 0 2 3 0 3 0 6 1 0 3 0 2 3 0 1 0 ) .  

10.1 Example Program

Source File Data Results
ex_f01sb.cpp None ex_f01sb.r