# NAG CPP Interfacenagcpp::matop::real_nmf_rcomm (f01sb)

Settings help

CPP Name Style:

## 1Purpose

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.

## 2Specification

```#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)```

## 3Description

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, $A\approx WH$, 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<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$. 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 ${A}^{\mathrm{T}}Y$. A reverse communication interface is used, in which control is returned to the calling program whenever a matrix product is required.
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

## 5Arguments

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: $\mathbf{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:
${\mathbf{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.
${\mathbf{irevcm}}=2$
Indicates that before re-entry to real_nmf_rcomm, the product ${A}^{\mathrm{T}}W$ must be computed and stored in ht.
${\mathbf{irevcm}}=3$
Indicates that before re-entry to real_nmf_rcomm, the product $A{H}^{\mathrm{T}}$ must be computed and stored in w.
On final exit: ${\mathbf{irevcm}}=0$.
2: $\mathbf{w}\left({\mathbf{m}},{\mathbf{k}}\right)$double array Input/Output
On initial entry:
• if ${\mathbf{seed}}\le 0$, w should be set to an initial iterate for the non-negative matrix factor, $W$.
• If ${\mathbf{seed}}\ge 1$, w need not be set. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit: if ${\mathbf{irevcm}}=1$ or $2$, w contains the current iterate of the $m×k$ non-negative matrix $W$.
On intermediate re-entry:
• if ${\mathbf{irevcm}}=3$, w must contain $A{H}^{\mathrm{T}}$, where ${H}^{\mathrm{T}}$ is stored in $\mathit{ht}$.
• If ${\mathbf{irevcm}}=0$, $1$ or $2$, w must not be changed.
On final exit: w contains the $m×k$ non-negative matrix $W$.
3: $\mathbf{h}\left({\mathbf{k}},{\mathbf{n}}\right)$double array Input/Output
On initial entry:
• if ${\mathbf{seed}}\le 0$, h should be set to an initial iterate for the non-negative matrix factor, $H$.
• If ${\mathbf{seed}}\ge 1$, h need not be set. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit: if ${\mathbf{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: $\mathbf{ht}\left({\mathbf{n}},{\mathbf{k}}\right)$double array Input/Output
On initial entry: ht need not be set.
On intermediate exit: if ${\mathbf{irevcm}}=3$, ht contains the $n×k$ non-negative matrix ${H}^{\mathrm{T}}$, which is required in order to form $A{H}^{\mathrm{T}}$.
On intermediate re-entry: if ${\mathbf{irevcm}}=2$, ht must contain ${A}^{\mathrm{T}}W$.
If ${\mathbf{irevcm}}=0$, $1$ or $3$, ht must not be changed.
On final exit: ht is undefined.
5: $\mathbf{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: $\mathbf{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 ${\mathbf{seed}}\le 0$, the supplied values of $W$ and $H$ are used for the initial iterate.
• If ${\mathbf{seed}}\ge 1$, 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 ${\mathbf{errtol}}\le 0.0$, is used.

### 5.1Additional Quantities

1: $\mathbf{m}$
$m$, the number of rows of the matrix $A$
2: $\mathbf{n}$
$n$, the number of columns of the matrix $A$
3: $\mathbf{k}$
$k$, the number of columns of the matrix $W$

## 6Exceptions 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
$\mathbf{errorid}=1$
On intermediate re-entry, ${\mathbf{irevcm}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{irevcm}}=1,2\text{​ or ​}3$.
$\mathbf{errorid}=1$
On initial entry, ${\mathbf{irevcm}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{irevcm}}=0$.
$\mathbf{errorid}=2$
On entry, ${\mathbf{m}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{m}}\ge 2$.
$\mathbf{errorid}=3$
On entry, ${\mathbf{n}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
$\mathbf{errorid}=4$
On entry, ${\mathbf{k}}=⟨\mathit{value}⟩$, ${\mathbf{m}}=⟨\mathit{value}⟩$ and
${\mathbf{n}}=⟨\mathit{value}⟩$.
Constraint: $1\le {\mathbf{k}}<\mathrm{min}\left({\mathbf{m}},{\mathbf{n}}\right)$.
$\mathbf{errorid}=8$
An internal error occurred when generating initial values for w and h.
Please contact NAG.
$\mathbf{errorid}=9$
On entry, one of more of the elements of w or h were negative.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a $⟨\mathit{\text{value}}⟩$ x $⟨\mathit{\text{value}}⟩$ array.
Supplied argument has $⟨\mathit{\text{value}}⟩$ dimensions.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a $⟨\mathit{\text{value}}⟩$ x $⟨\mathit{\text{value}}⟩$ array.
Supplied argument was a $⟨\mathit{\text{value}}⟩$ x $⟨\mathit{\text{value}}⟩$ array.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a $⟨\mathit{\text{value}}⟩$ x $⟨\mathit{\text{value}}⟩$ array.
Not all of the sizes for the supplied array could be ascertained.
$\mathbf{errorid}=10602$
On entry, the raw data component of $⟨\mathit{\text{value}}⟩$ is null.
$\mathbf{errorid}=10603$
On entry, unable to ascertain a value for $⟨\mathit{\text{value}}⟩$.
$\mathbf{errorid}=10604$
On entry, the data in $⟨\mathit{\text{value}}⟩$ is stored in $⟨\mathit{\text{value}}⟩$ Major Order.
The data was expected to be in $⟨\mathit{\text{value}}⟩$ Major Order.
$\mathbf{errorid}=10605$
On entry, the communication class $⟨\mathit{\text{value}}⟩$ has not been initialized correctly.
$\mathbf{errorid}=-99$
An unexpected error has been triggered by this routine.
$\mathbf{errorid}=-399$
Your licence key may have expired or may not have been installed correctly.
$\mathbf{errorid}=-999$
Dynamic memory allocation failed.

## 7Accuracy

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\left(W,H\right)$, but this may not be the global minimum. The iteration is deemed to have converged if the gradient of $f\left(W,H\right)$ 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 $\mathbf{errorid}={\mathbf{7}}$, the factorization given by $W$ and $H$ may still be a good enough approximation to be useful.

## 8Parallelism and Performance

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

## 9Further 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.1Uniqueness

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}^{-1}H$, 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.2Choice of $\mathbit{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 $k\ll \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$) 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.3Generating Random Initial Iterates

If ${\mathbf{seed}}\ge 1$ 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.4Use in Conjunction with NAG Library Functions

To compute the non-negative matrix factorization, the following logic can normally be used:
• set ${\mathbf{irevcm}}=0$
• start looping
• call real_​nmf_​rcomm
• if ${\mathbf{irevcm}}=0$ then
• stop looping
• else if ${\mathbf{irevcm}}=1$ then
• print the $W$ and $H$ matrices if required and check the number of iterations
• else if ${\mathbf{irevcm}}=2$ then
• set ${\mathbf{ht}}={A}^{\mathrm{T}}W$
• else if ${\mathbf{irevcm}}=3$ then
• set ${\mathbf{w}}=A{H}^{\mathrm{T}}$
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.

## 10Example

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.1Example Program

Source File Data Results
ex_f01sb.cpp None ex_f01sb.r