G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03BCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G03BCF computes Procrustes rotations in which an orthogonal rotation is found so that a transformed matrix best matches a target matrix.

## 2  Specification

 SUBROUTINE G03BCF ( STAND, PSCALE, N, M, X, LDX, Y, LDY, YHAT, R, LDR, ALPHA, RSS, RES, WK, IFAIL)
 INTEGER N, M, LDX, LDY, LDR, IFAIL REAL (KIND=nag_wp) X(LDX,M), Y(LDY,M), YHAT(LDY,M), R(LDR,M), ALPHA, RSS, RES(N), WK(M*M+7*M) CHARACTER(1) STAND, PSCALE

## 3  Description

Let $X$ and $Y$ be $n$ by $m$ matrices. They can be considered as representing sets of $n$ points in an $m$-dimensional space. The $X$ matrix may be a matrix of loadings from say factor or canonical variate analysis, and the $Y$ matrix may be a postulated pattern matrix or the loadings from a different sample. The problem is to relate the two sets of points without disturbing the relationships between the points in each set. This can be achieved by translating, rotating and scaling the sets of points. The $Y$ matrix is considered as the target matrix and the $X$ matrix is rotated to match that matrix.
First the two sets of points are translated so that their centroids are at the origin to give ${X}_{c}$ and ${Y}_{c}$, i.e., the matrices will have zero column means. Then the rotation of the translated ${X}_{c}$ matrix which minimizes the sum of squared distances between corresponding points in the two sets is found. This is computed from the singular value decomposition of the matrix:
 $XcTYc=UDVT,$
where $U$ and $V$ are orthogonal matrices and $D$ is a diagonal matrix. The matrix of rotations, $R$, is computed as:
 $R=UVT.$
After rotation, a scaling or dilation factor, $\alpha$, may be estimated by least squares. Thus, the final set of points that best match ${Y}_{c}$ is given by:
 $Y^c=αXcR.$
Before rotation, both sets of points may be normalized to have unit sums of squares or the $X$ matrix may be normalized to have the same sum of squares as the $Y$ matrix. After rotation, the results may be translated to the original $Y$ centroid.
The $i$th residual, ${r}_{i}$, is given by the distance between the point given in the $i$th row of $Y$ and the point given in the $i$th row of $\stackrel{^}{Y}$. The residual sum of squares is also computed.

## 4  References

Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press
Lawley D N and Maxwell A E (1971) Factor Analysis as a Statistical Method (2nd Edition) Butterworths

## 5  Parameters

1:     STAND – CHARACTER(1)Input
On entry: indicates if translation/normalization is required.
${\mathbf{STAND}}=\text{'N'}$
There is no translation or normalization.
${\mathbf{STAND}}=\text{'Z'}$
There is translation to the origin (i.e., to zero).
${\mathbf{STAND}}=\text{'C'}$
There is translation to origin and then to the $Y$ centroid after rotation.
${\mathbf{STAND}}=\text{'U'}$
There is unit normalization.
${\mathbf{STAND}}=\text{'S'}$
There is translation and normalization (i.e., there is standardization).
${\mathbf{STAND}}=\text{'M'}$
There is translation and normalization to $Y$ scale, then translation to the $Y$ centroid after rotation (i.e., they are matched).
Constraint: ${\mathbf{STAND}}=\text{'N'}$, $\text{'Z'}$, $\text{'C'}$, $\text{'U'}$, $\text{'S'}$ or $\text{'M'}$.
2:     PSCALE – CHARACTER(1)Input
On entry: indicates if least squares scaling is to be applied after rotation.
${\mathbf{PSCALE}}=\text{'S'}$
Scaling is applied.
${\mathbf{PSCALE}}=\text{'U'}$
No scaling is applied.
Constraint: ${\mathbf{PSCALE}}=\text{'S'}$ or $\text{'U'}$.
3:     N – INTEGERInput
On entry: $n$, the number of points.
Constraint: ${\mathbf{N}}\ge {\mathbf{M}}$.
4:     M – INTEGERInput
On entry: $m$, the number of dimensions.
Constraint: ${\mathbf{M}}\ge 1$.
5:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: $X$, the matrix to be rotated.
On exit: if ${\mathbf{STAND}}=\text{'N'}$, X will be unchanged.
If ${\mathbf{STAND}}=\text{'Z'}$, $\text{'C'}$, $\text{'S'}$ or $\text{'M'}$, X will be translated to have zero column means.
If ${\mathbf{STAND}}=\text{'U'}$ or $\text{'S'}$, X will be scaled to have unit sum of squares.
If ${\mathbf{STAND}}=\text{'M'}$, X will be scaled to have the same sum of squares as Y.
6:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03BCF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
7:     Y(LDY,M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the target matrix, ${\mathbf{Y}}$.
On exit: if ${\mathbf{STAND}}=\text{'N'}$, Y will be unchanged.
If ${\mathbf{STAND}}=\text{'Z'}$ or $\text{'S'}$, Y will be translated to have zero column means.
If ${\mathbf{STAND}}=\text{'U'}$ or $\text{'S'}$, Y will be scaled to have unit sum of squares.
If ${\mathbf{STAND}}=\text{'C'}$ or $\text{'M'}$, Y will be translated and then after rotation translated back. The output Y should be the same as the input Y except for rounding errors.
8:     LDY – INTEGERInput
On entry: the first dimension of the arrays Y and YHAT as declared in the (sub)program from which G03BCF is called.
Constraint: ${\mathbf{LDY}}\ge {\mathbf{N}}$.
9:     YHAT(LDY,M) – REAL (KIND=nag_wp) arrayOutput
On exit: the fitted matrix, $\stackrel{^}{Y}$.
10:   R(LDR,M) – REAL (KIND=nag_wp) arrayOutput
On exit: the matrix of rotations, $R$, see Section 8.
11:   LDR – INTEGERInput
On entry: the first dimension of the array R as declared in the (sub)program from which G03BCF is called.
Constraint: ${\mathbf{LDR}}\ge {\mathbf{M}}$.
12:   ALPHA – REAL (KIND=nag_wp)Output
On exit: if ${\mathbf{PSCALE}}=\text{'S'}$ the scaling factor, $\alpha$; otherwise ALPHA is not set.
On exit: the residual sum of squares.
14:   RES(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
15:   WK(${\mathbf{M}}×{\mathbf{M}}+7×{\mathbf{M}}$) – REAL (KIND=nag_wp) arrayWorkspace
16:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<{\mathbf{M}}$, or ${\mathbf{M}}<1$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{LDY}}<{\mathbf{N}}$, or ${\mathbf{LDR}}<{\mathbf{M}}$, or ${\mathbf{STAND}}\ne \text{'N'}$, $\text{'Z'}$, $\text{'C'}$, $\text{'U'}$, $\text{'S'}$ or $\text{'M'}$, or ${\mathbf{PSCALE}}\ne \text{'S'}$ or $\text{'U'}$.
${\mathbf{IFAIL}}=2$
On entry, either X or Y contain only zero-points (possibly after translation) when normalization is to be applied.
${\mathbf{IFAIL}}=3$
The $\stackrel{^}{Y}$ matrix contains only zero-points when least squares scaling is applied.
${\mathbf{IFAIL}}=4$
The singular value decomposition has failed to converge. This is an unlikely error exit.

## 7  Accuracy

The accuracy of the calculation of the rotation matrix largely depends upon the singular value decomposition. See the F08 Chapter Introduction for further details.

Note that if the matrix ${X}_{c}^{\mathrm{T}}Y$ is not of full rank, then the matrix of rotations, $R$, may not be unique even if there is a unique solution in terms of the rotated matrix, ${\stackrel{^}{Y}}_{c}$. The matrix $R$ may also include reflections as well as pure rotations, see Krzanowski (1990).
If the column dimensions of the $X$ and $Y$ matrices are not equal, the smaller of the two should be supplemented by columns of zeros. Adding a column of zeros to both $X$ and $Y$ will have the effect of allowing reflections as well as rotations.

## 9  Example

Three points representing the vertices of a triangle in two dimensions are input. The points are translated and rotated to match the triangle given by $\left(0,0\right)$, $\left(1,0\right)$, $\left(0,2\right)$ and scaling is applied after rotation. The target matrix and fitted matrix are printed along with additional information.

### 9.1  Program Text

Program Text (g03bcfe.f90)

### 9.2  Program Data

Program Data (g03bcfe.d)

### 9.3  Program Results

Program Results (g03bcfe.r)