# NAG FL Interfaceg02hkf (robustm_​corr_​huber)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g02hkf ( n, m, x, ldx, eps, cov, tol, nit, wk,
 Integer, Intent (In) :: n, m, ldx, maxit, nitmon Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nit Real (Kind=nag_wp), Intent (In) :: x(ldx,m), eps, tol Real (Kind=nag_wp), Intent (Out) :: cov(m*(m+1)/2), theta(m), wk(n+m*(m+5)/2)
#include <nag.h>
 void g02hkf_ (const Integer *n, const Integer *m, const double x[], const Integer *ldx, const double *eps, double cov[], double theta[], const Integer *maxit, const Integer *nitmon, const double *tol, Integer *nit, double wk[], Integer *ifail)
The routine may be called by the names g02hkf or nagf_correg_robustm_corr_huber.

## 3Description

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=τ2(ATA)-1,$
where ${\tau }^{2}$ is a correction factor and $A$ is a lower triangular matrix found as the solution to the following equations:
 $zi=A(xi-θ),$
 $1n ∑i= 1nw(‖zi‖2)zi=0,$
and
 $1n∑i=1nu(‖zi‖2)zi 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:
 $u(t)= aut2, if ​tbu2$
and
 $w(t)= 1, if ​t≤cw w(t)= 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).

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
2: $\mathbf{m}$Integer Input
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: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{ldx}$Integer Input
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: $\mathbf{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: $\mathbf{cov}\left({\mathbf{m}}×\left({\mathbf{m}}+1\right)/2\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{theta}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: the robust estimate of the location parameters ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
8: $\mathbf{maxit}$Integer Input
On entry: the maximum number of iterations that will be used during the calculation of the covariance matrix.
Suggested value: $150$.
Constraint: ${\mathbf{maxit}}>0$.
9: $\mathbf{nitmon}$Integer Input
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: $\mathbf{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: $\mathbf{nit}$Integer Output
On exit: the number of iterations performed.
12: $\mathbf{wk}\left({\mathbf{n}}+{\mathbf{m}}×\left({\mathbf{m}}+5\right)/2\right)$Real (Kind=nag_wp) array Workspace
13: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error 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{eps}}=⟨\mathit{\text{value}}⟩$.
Constraint: $0.0\le {\mathbf{eps}}\le 1.0$.
On entry, ${\mathbf{ldx}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{maxit}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxit}}>0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=2$
On entry, a variable has a constant value, i.e., all elements in column $⟨\mathit{\text{value}}⟩$ 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 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

On successful exit the accuracy of the results is related to the value of tol; see Section 5. At an iteration let
1. (i)$d1=\text{}$ the maximum value of the absolute relative change in $A$
2. (ii)$d2=\text{}$ the maximum absolute change in $u\left({‖{z}_{i}‖}_{2}\right)$
3. (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}}$.

## 8Parallelism 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.

## 10Example

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

Program Text (g02hkfe.f90)

### 10.2Program Data

Program Data (g02hkfe.d)

### 10.3Program Results

Program Results (g02hkfe.r)