G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02HMF

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

G02HMF computes a robust estimate of the covariance matrix for user-supplied weight functions. The derivatives of the weight functions are not required.

## 2  Specification

 SUBROUTINE G02HMF ( UCV, RUSER, INDM, N, M, X, LDX, COV, A, WT, THETA, BL, BD, MAXIT, NITMON, TOL, NIT, WK, IFAIL)
 INTEGER INDM, N, M, LDX, MAXIT, NITMON, NIT, IFAIL REAL (KIND=nag_wp) RUSER(*), X(LDX,M), COV(M*(M+1)/2), A(M*(M+1)/2), WT(N), THETA(M), BL, BD, TOL, WK(2*M) EXTERNAL UCV

## 3  Description

For a set of $n$ observations on $m$ variables in a matrix $X$, a robust estimate of the covariance matrix, $C$, and a robust estimate of location, $\theta$, are given by
 $C=τ2ATA-1,$
where ${\tau }^{2}$ is a correction factor and $A$ is a lower triangular matrix found as the solution to the following equations.
 $zi=Axi-θ$
 $1n ∑i= 1nwzi2zi=0$
and
 $1n∑i=1nuzi2zi ziT -vzi2I=0,$
 where ${x}_{i}$ is a vector of length $m$ containing the elements of the $i$th row of $X$, ${z}_{i}$ is a vector of length $m$, $I$ is the identity matrix and $0$ is the zero matrix. and $w$ and $u$ are suitable functions.
G02HMF covers two situations:
 (i) $v\left(t\right)=1$ for all $t$, (ii) $v\left(t\right)=u\left(t\right)$.
The robust covariance matrix may be calculated from a weighted sum of squares and cross-products matrix about $\theta$ using weights ${\mathit{wt}}_{i}=u\left(‖{z}_{i}‖\right)$. In case (i) a divisor of $n$ is used and in case (ii) a divisor of $\sum _{i=1}^{n}{\mathit{wt}}_{i}$ is used. If $w\left(.\right)=\sqrt{u\left(.\right)}$, then the robust covariance matrix can be calculated by scaling each row of $X$ by $\sqrt{{\mathit{wt}}_{i}}$ and calculating an unweighted covariance matrix about $\theta$.
In order to make the estimate asymptotically unbiased under a Normal model a correction factor, ${\tau }^{2}$, is needed. The value of the correction factor will depend on the functions employed (see Huber (1981) and Marazzi (1987)).
G02HMF finds $A$ using the iterative procedure as given by Huber; see Huber (1981).
 $Ak=Sk+IAk-1$
and
 $θjk=bjD1+θjk- 1,$
where ${S}_{k}=\left({s}_{jl}\right)$, for $\mathit{j}=1,2,\dots ,m$ and $\mathit{l}=1,2,\dots ,m$ is a lower triangular matrix such that
 $sjl= -minmaxhjl/D2,-BL,BL, j>l -minmax12hjj/D2-1,-BD,BD, j=l ,$
where
• ${D}_{1}=\sum _{i=1}^{n}w\left({‖{z}_{i}‖}_{2}\right)$
• ${D}_{2}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right)$
• ${h}_{jl}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right){z}_{ij}{z}_{il}$, for $j\ge l$
• ${b}_{j}=\sum _{i=1}^{n}w\left({‖{z}_{i}‖}_{2}\right)\left({x}_{ij}-{b}_{j}\right)$
and $\mathit{BD}$ and $\mathit{BL}$ are suitable bounds.
The value of $\tau$ may be chosen so that $C$ is unbiased if the observations are from a given distribution.
G02HMF is based on routines in ROBETH; see Marazzi (1987).

## 4  References

Huber P J (1981) Robust Statistics Wiley
Marazzi A (1987) Weights for bounded influence regression in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 3 Institut Universitaire de Médecine Sociale et Préventive, Lausanne

## 5  Arguments

1:     $\mathrm{UCV}$ – SUBROUTINE, supplied by the user.External Procedure
UCV must return the values of the functions $u$ and $w$ for a given value of its argument.
The specification of UCV is:
 SUBROUTINE UCV ( T, RUSER, U, W)
 REAL (KIND=nag_wp) T, RUSER(*), U, W
1:     $\mathrm{T}$ – REAL (KIND=nag_wp)Input
On entry: the argument for which the functions $u$ and $w$ must be evaluated.
2:     $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace
UCV is called with the argument RUSER as supplied to G02HMF. You should use the array RUSER to supply information to UCV.
3:     $\mathrm{U}$ – REAL (KIND=nag_wp)Output
On exit: the value of the $u$ function at the point T.
Constraint: ${\mathbf{U}}\ge 0.0$.
4:     $\mathrm{W}$ – REAL (KIND=nag_wp)Output
On exit: the value of the $w$ function at the point T.
Constraint: ${\mathbf{W}}\ge 0.0$.
UCV must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which G02HMF is called. Arguments denoted as Input must not be changed by this procedure.
2:     $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace
RUSER is not used by G02HMF, but is passed directly to UCV and should be used to pass information to this routine.
3:     $\mathrm{INDM}$ – INTEGERInput
On entry: indicates which form of the function $v$ will be used.
${\mathbf{INDM}}=1$
$v=1$.
${\mathbf{INDM}}\ne 1$
$v=u$.
4:     $\mathrm{N}$ – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>1$.
5:     $\mathrm{M}$ – INTEGERInput
On entry: $m$, the number of columns of the matrix $X$, i.e., number of independent variables.
Constraint: $1\le {\mathbf{M}}\le {\mathbf{N}}$.
6:     $\mathrm{X}\left({\mathbf{LDX}},{\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation on the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
7:     $\mathrm{LDX}$ – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02HMF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
8:     $\mathrm{COV}\left({\mathbf{M}}×\left({\mathbf{M}}+1\right)/2\right)$ – REAL (KIND=nag_wp) arrayOutput
On exit: a robust estimate of the covariance matrix, $C$. The upper triangular part of the matrix $C$ is stored packed by columns (lower triangular stored by rows), that is ${C}_{ij}$ is returned in ${\mathbf{COV}}\left(j×\left(j-1\right)/2+i\right)$, $i\le j$.
9:     $\mathrm{A}\left({\mathbf{M}}×\left({\mathbf{M}}+1\right)/2\right)$ – REAL (KIND=nag_wp) arrayInput/Output
On entry: an initial estimate of the lower triangular real matrix $A$. Only the lower triangular elements must be given and these should be stored row-wise in the array.
The diagonal elements must be $\text{}\ne 0$, and in practice will usually be $\text{}>0$. If the magnitudes of the columns of $X$ are of the same order, the identity matrix will often provide a suitable initial value for $A$. If the columns of $X$ are of different magnitudes, the diagonal elements of the initial value of $A$ should be approximately inversely proportional to the magnitude of the columns of $X$.
Constraint: ${\mathbf{A}}\left(\mathit{j}×\left(\mathit{j}-1\right)/2+\mathit{j}\right)\ne 0.0$, for $\mathit{j}=1,2,\dots ,m$.
On exit: the lower triangular elements of the inverse of the matrix $A$, stored row-wise.
10:   $\mathrm{WT}\left({\mathbf{N}}\right)$ – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{WT}}\left(\mathit{i}\right)$ contains the weights, ${\mathit{wt}}_{\mathit{i}}=u\left({‖{z}_{\mathit{i}}‖}_{2}\right)$, for $\mathit{i}=1,2,\dots ,n$.
11:   $\mathrm{THETA}\left({\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayInput/Output
On entry: an initial estimate of the location argument, ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
In many cases an initial estimate of ${\theta }_{\mathit{j}}=0$, for $\mathit{j}=1,2,\dots ,m$, will be adequate. Alternatively medians may be used as given by G07DAF.
On exit: contains the robust estimate of the location argument, ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
12:   $\mathrm{BL}$ – REAL (KIND=nag_wp)Input
On entry: the magnitude of the bound for the off-diagonal elements of ${S}_{k}$, $BL$.
Suggested value: ${\mathbf{BL}}=0.9$.
Constraint: ${\mathbf{BL}}>0.0$.
13:   $\mathrm{BD}$ – REAL (KIND=nag_wp)Input
On entry: the magnitude of the bound for the diagonal elements of ${S}_{k}$, $BD$.
Suggested value: ${\mathbf{BD}}=0.9$.
Constraint: ${\mathbf{BD}}>0.0$.
14:   $\mathrm{MAXIT}$ – INTEGERInput
On entry: the maximum number of iterations that will be used during the calculation of $A$.
Suggested value: ${\mathbf{MAXIT}}=150$.
Constraint: ${\mathbf{MAXIT}}>0$.
15:   $\mathrm{NITMON}$ – INTEGERInput
On entry: indicates the amount of information on the iteration that is printed.
${\mathbf{NITMON}}>0$
The value of $A$, $\theta$ and $\delta$ (see Section 7) will be printed at the first and every NITMON iterations.
${\mathbf{NITMON}}\le 0$
No iteration monitoring is printed.
When printing occurs the output is directed to the current advisory message channel (See X04ABF.)
16:   $\mathrm{TOL}$ – REAL (KIND=nag_wp)Input
On entry: the relative precision for the final estimate of the covariance matrix. Iteration will stop when maximum $\delta$ (see Section 7) is less than TOL.
Constraint: ${\mathbf{TOL}}>0.0$.
17:   $\mathrm{NIT}$ – INTEGEROutput
On exit: the number of iterations performed.
18:   $\mathrm{WK}\left(2×{\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayWorkspace
19:   $\mathrm{IFAIL}$ – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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}}=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:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 1$, or ${\mathbf{M}}<1$, or ${\mathbf{N}}<{\mathbf{M}}$, or ${\mathbf{LDX}}<{\mathbf{N}}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{TOL}}\le 0.0$, or ${\mathbf{MAXIT}}\le 0$, or diagonal element of ${\mathbf{A}}=0.0$, or ${\mathbf{BL}}\le 0.0$, or ${\mathbf{BD}}\le 0.0$.
${\mathbf{IFAIL}}=3$
A column of X has a constant value.
${\mathbf{IFAIL}}=4$
Value of U or W returned by ${\mathbf{UCV}}<0$.
${\mathbf{IFAIL}}=5$
The routine has failed to converge in MAXIT iterations.
${\mathbf{IFAIL}}=6$
Either the sum ${D}_{1}$ or the sum ${D}_{2}$ is zero. This may be caused by the functions $u$ or $w$ being too strict for the current estimate of $A$ (or $C$). You should either try a larger initial estimate of $A$ or make the $u$ and $w$ functions less strict.
${\mathbf{IFAIL}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7  Accuracy

On successful exit the accuracy of the results is related to the value of TOL; see Section 5. At an iteration let
 (i) $d1=\text{}$ the maximum value of $\left|{s}_{jl}\right|$ (ii) $d2=\text{}$ the maximum absolute change in $wt\left(i\right)$ (iii) $d3=\text{}$ the maximum absolute relative change in ${\theta }_{j}$
and let $\delta =\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(d1,d2,d3\right)$. Then the iterative procedure is assumed to have converged when $\delta <{\mathbf{TOL}}$.

## 8  Parallelism and Performance

G02HMF 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.

The existence of $A$ will depend upon the function $u$ (see Marazzi (1987)); also if $X$ is not of full rank a value of $A$ will not be found. If the columns of $X$ are almost linearly related, then convergence will be slow.
If derivatives of the $u$ and $w$ functions are available then the method used in G02HLF will usually give much faster convergence.

## 10  Example

A sample of $10$ observations on three variables is read in along with initial values for $A$ and $\theta$ and argument values for the $u$ and $w$ functions, ${c}_{u}$ and ${c}_{w}$. The covariance matrix computed by G02HMF is printed along with the robust estimate of $\theta$.
UCV computes the Huber's weight functions:
 $ut=1, if t≤cu2 ut= cut2, if t>cu2$
and
 $wt= 1, if t≤cw wt= cwt, if t>cw.$

### 10.1  Program Text

Program Text (g02hmfe.f90)

### 10.2  Program Data

Program Data (g02hmfe.d)

### 10.3  Program Results

Program Results (g02hmfe.r)