g02hl calculates a robust estimate of the covariance matrix for user-supplied weight functions and their derivatives.

# Syntax

C#
```public static void g02hl(
G02..::..G02HL_UCV ucv,
int indm,
int n,
int m,
double[,] x,
double[] cov,
double[] a,
double[] wt,
double[] theta,
double bl,
double bd,
int maxit,
int nitmon,
double tol,
out int nit,
out int ifail
)```
Visual Basic
```Public Shared Sub g02hl ( _
ucv As G02..::..G02HL_UCV, _
indm As Integer, _
n As Integer, _
m As Integer, _
x As Double(,), _
cov As Double(), _
a As Double(), _
wt As Double(), _
theta As Double(), _
bl As Double, _
bd 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 g02hl(
G02..::..G02HL_UCV^ ucv,
int indm,
int n,
int m,
array<double,2>^ x,
array<double>^ cov,
array<double>^ a,
array<double>^ wt,
array<double>^ theta,
double bl,
double bd,
int maxit,
int nitmon,
double tol,
[OutAttribute] int% nit,
[OutAttribute] int% ifail
)```
F#
```static member g02hl :
ucv : G02..::..G02HL_UCV *
indm : int *
n : int *
m : int *
x : float[,] *
cov : float[] *
a : float[] *
wt : float[] *
theta : float[] *
bl : float *
bd : float *
maxit : int *
nitmon : int *
tol : float *
nit : int byref *
ifail : int byref -> unit
```

#### Parameters

ucv
Type: NagLibrary..::..G02..::..G02HL_UCV
ucv must return the values of the functions $u$ and $w$ and their derivatives for a given value of its argument.

A delegate of type G02HL_UCV.

indm
Type: System..::..Int32
On entry: indicates which form of the function $v$ will be used.
${\mathbf{indm}}=1$
$v=1$.
${\mathbf{indm}}\ne 1$
$v=u$.
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 on the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
cov
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{m}}×\left({\mathbf{m}}+1\right)/2$]
On exit: contains 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), ${C}_{ij}$ is returned in ${\mathbf{cov}}\left[\left(j×\left(j-1\right)/2+i\right)-1\right]$, $i\le j$.
a
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{m}}×\left({\mathbf{m}}+1\right)/2$]
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}=0,1,\dots ,m-1$.
On exit: the lower triangular elements of the inverse of the matrix $A$, stored row-wise.
wt
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: ${\mathbf{wt}}\left[\mathit{i}-1\right]$ contains the weights, ${\mathit{wt}}_{\mathit{i}}=u\left({‖{z}_{\mathit{i}}‖}_{2}\right)$, for $\mathit{i}=1,2,\dots ,n$.
theta
Type: array<System..::..Double>[]()[][]
An array of size [m]
On entry: an initial estimate of the location parameter, ${\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 not in this release).
On exit: contains the robust estimate of the location parameter, ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
bl
Type: System..::..Double
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$.
bd
Type: System..::..Double
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$.
maxit
Type: System..::..Int32
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$.
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. Iteration will stop when maximum $\delta$ (see [Accuracy]) is less than tol.
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-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.
g02hl 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)).
g02hl finds $A$ using the iterative procedure as given by Huber.
 $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/D3,-BL,BL,j>l-minmaxhjj/2D3-D4/D2,-BD,BD,j=l,$
where
• ${D}_{1}=\sum _{i=1}^{n}\left\{w\left({‖{z}_{i}‖}_{2}\right)+\frac{1}{m}{w}^{\prime }\left({‖{z}_{i}‖}_{2}\right){‖{z}_{i}‖}_{2}\right\}$
• ${D}_{2}=\sum _{i=1}^{n}\left\{\frac{1}{p}\left({u}^{\prime }\left({‖{z}_{i}‖}_{2}\right){‖{z}_{i}‖}_{2}+2u\left({‖{z}_{i}‖}_{2}\right)\right){‖{z}_{i}‖}_{2}-{v}^{\prime }\left({‖{z}_{i}‖}_{2}\right)\right\}{‖{z}_{i}‖}_{2}$
• ${D}_{3}=\frac{1}{m+2}\sum _{i=1}^{n}\left\{\frac{1}{m}\left({u}^{\prime }\left({‖{z}_{i}‖}_{2}\right){‖{z}_{i}‖}_{2}+2u\left({‖{z}_{i}‖}_{2}\right)\right)+u\left({‖{z}_{i}‖}_{2}\right)\right\}{{‖{z}_{i}‖}_{2}}^{2}$
• ${D}_{4}=\sum _{i=1}^{n}\left\{\frac{1}{m}u\left({‖{z}_{i}‖}_{2}\right){{‖{z}_{i}‖}_{2}}^{2}-v\left({{‖{z}_{i}‖}_{2}}^{2}\right)\right\}$
• ${h}_{jl}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right){z}_{ij}{z}_{il}$, for $j>l$
• ${h}_{jj}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right)\left({z}_{ij}^{2}-{{‖{z}_{ij}‖}_{2}}^{2}/m\right)$
• ${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.
g02hl 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}}$,
${\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 method has failed to converge in maxit iterations.
${\mathbf{ifail}}=6$
One of the following is zero: ${D}_{1}$, ${D}_{2}$ or ${D}_{3}$.
This may be caused by the functions $u$ or $w$ being too strict for the current estimate of $A$ (or $C$). You should try either a larger initial estimate of $A$ or make $u$ and $w$ less strict.
${\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 $\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}}$.

# Parallelism and Performance

None.

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.

# Example

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

Example program (C#): g02hle.cs

Example program data: g02hle.d

Example program results: g02hle.r