G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02HKF

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

G02HKF computes a robust estimate of the covariance matrix for an expected fraction of gross errors.

## 2  Specification

 SUBROUTINE G02HKF ( N, M, X, LDX, EPS, COV, THETA, MAXIT, NITMON, TOL, NIT, WK, IFAIL)
 INTEGER N, M, LDX, MAXIT, NITMON, NIT, IFAIL REAL (KIND=nag_wp) X(LDX,M), EPS, COV(M*(M+1)/2), THETA(M), TOL, WK(N+M*(M+5)/2)

## 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 -I=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.
G02HKF uses weight functions:
 $ut= aut2, if ​tbu2$
and
 $wt= 1, if ​t≤cw wt= cwt, if ​t>cw$
for constants ${a}_{u}$, ${b}_{u}$ and ${c}_{w}$.
These functions solve a minimax problem considered by Huber (see Huber (1981)). The values of ${a}_{u}$, ${b}_{u}$ and ${c}_{w}$ are calculated from the expected fraction of gross errors, $\epsilon$ (see Huber (1981) and Marazzi (1987)). The expected fraction of gross errors is the estimated proportion of outliers in the sample.
In order to make the estimate asymptotically unbiased under a Normal model a correction factor, ${\tau }^{2}$, is calculated, (see Huber (1981) and Marazzi (1987)).
The matrix $C$ is calculated using G02HLF. Initial estimates of ${\theta }_{j}$, for $j=1,2,\dots ,m$, are given by the median of the $j$th column of $X$ and the initial value of $A$ is based on the median absolute deviation (see Marazzi (1987)). G02HKF 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  Parameters

1:     $\mathrm{N}$ – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>1$.
2:     $\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}}$.
3:     $\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 for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
4:     $\mathrm{LDX}$ – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02HKF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
5:     $\mathrm{EPS}$ – REAL (KIND=nag_wp)Input
On entry: $\epsilon$, the expected fraction of gross errors expected in the sample.
Constraint: $0.0\le {\mathbf{EPS}}<1.0$.
6:     $\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. ${C}_{ij}$ is returned in ${\mathbf{COV}}\left(\left(j×\left(j-1\right)/2+i\right)\right)$, $i\le j$.
7:     $\mathrm{THETA}\left({\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayOutput
On exit: the robust estimate of the location parameters ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
8:     $\mathrm{MAXIT}$ – INTEGERInput
On entry: the maximum number of iterations that will be used during the calculation of the covariance matrix.
Constraint: ${\mathbf{MAXIT}}>0$.
9:     $\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 unit (see X04ABF).
10:   $\mathrm{TOL}$ – REAL (KIND=nag_wp)Input
On entry: the relative precision for the final estimates of the covariance matrix.
Constraint: ${\mathbf{TOL}}>0.0$.
11:   $\mathrm{NIT}$ – INTEGEROutput
On exit: the number of iterations performed.
12:   $\mathrm{WK}\left({\mathbf{N}}+{\mathbf{M}}×\left({\mathbf{M}}+5\right)/2\right)$ – REAL (KIND=nag_wp) arrayWorkspace
13:   $\mathrm{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}}\le 1$, or ${\mathbf{M}}<1$, or ${\mathbf{N}}<{\mathbf{M}}$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{EPS}}<0.0$, or ${\mathbf{EPS}}\ge 1.0$, or ${\mathbf{TOL}}\le 0.0$, or ${\mathbf{MAXIT}}\le 0$.
${\mathbf{IFAIL}}=2$
 On entry, a variable has a constant value, i.e., all elements in a column of $X$ are identical.
${\mathbf{IFAIL}}=3$
The iterative procedure to find $C$ has failed to converge in MAXIT iterations.
${\mathbf{IFAIL}}=4$
The iterative procedure to find $C$ has become unstable. This may happen if the value of EPS is too large for the sample.
${\mathbf{IFAIL}}=-99$
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction 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 the absolute relative change in $A$ (ii) $d2=\text{}$ the maximum absolute change in $u\left({‖{z}_{i}‖}_{2}\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

G02HKF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G02HKF 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$, and hence $C$, 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.

## 10  Example

A sample of $10$ observations on three variables is read in and the robust estimate of the covariance matrix is computed assuming 10% gross errors are to be expected. The robust covariance is then printed.

### 10.1  Program Text

Program Text (g02hkfe.f90)

### 10.2  Program Data

Program Data (g02hkfe.d)

### 10.3  Program Results

Program Results (g02hkfe.r)