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

# Syntax

C#
```public static void g02hk(
int n,
int m,
double[,] x,
double eps,
double[] cov,
double[] theta,
int maxit,
int nitmon,
double tol,
out int nit,
out int ifail
)```
Visual Basic
```Public Shared Sub g02hk ( _
n As Integer, _
m As Integer, _
x As Double(,), _
eps As Double, _
cov As Double(), _
theta As Double(), _
maxit As Integer, _
nitmon As Integer, _
tol As Double, _
<OutAttribute> ByRef nit As Integer, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02hk(
int n,
int m,
array<double,2>^ x,
double eps,
array<double>^ cov,
array<double>^ theta,
int maxit,
int nitmon,
double tol,
[OutAttribute] int% nit,
[OutAttribute] int% ifail
)```
F#
```static member g02hk :
n : int *
m : int *
x : float[,] *
eps : float *
cov : float[] *
theta : float[] *
maxit : int *
nitmon : int *
tol : float *
nit : int byref *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
m
Type: System..::..Int32
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}}$.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\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}}$.
eps
Type: System..::..Double
On entry: $\epsilon$, the expected fraction of gross errors expected in the sample.
Constraint: $0.0\le {\mathbf{eps}}<1.0$.
cov
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{m}}×\left({\mathbf{m}}+1\right)/2$]
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)-1\right]$, $i\le j$.
theta
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: the robust estimate of the location parameters ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
maxit
Type: System..::..Int32
On entry: the maximum number of iterations that will be used during the calculation of the covariance matrix.
Constraint: ${\mathbf{maxit}}>0$.
nitmon
Type: System..::..Int32
On entry: indicates the amount of information on the iteration that is printed.
${\mathbf{nitmon}}>0$
The value of $A$, $\theta$ and $\delta$ (see [Accuracy]) 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 not in this release)).
tol
Type: System..::..Double
On entry: the relative precision for the final estimates of the covariance matrix.
Constraint: ${\mathbf{tol}}>0.0$.
nit
Type: System..::..Int32%
On exit: the number of iterations performed.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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=1nuzi2ziziT-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.
g02hk uses weight functions:
 $ut=aut2,if ​tbu2$
and
 $wt=1,if ​t≤cwwt=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 g02hl. 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)). g02hk is based on routines in ROBETH; see Marazzi (1987).

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 1$, or ${\mathbf{m}}<1$, or ${\mathbf{n}}<{\mathbf{m}}$, 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}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

On successful exit the accuracy of the results is related to the value of tol; see [Parameters]. 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}}$.

# Parallelism and Performance

None.

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.

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

Example program (C#): g02hke.cs

Example program data: g02hke.d

Example program results: g02hke.r