# NAG CL Interfaceg02hkc (robustm_​corr_​huber)

Settings help

CL Name Style:

## 1Purpose

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

## 2Specification

 #include
 void g02hkc (Integer n, Integer m, const double x[], Integer tdx, double eps, double cov[], double theta[], Integer max_iter, Integer print_iter, const char *outfile, double tol, Integer *iter, NagError *fail)
The function may be called by the names: g02hkc, nag_correg_robustm_corr_huber or nag_robust_corr_estim.

## 3Description

For a set $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.
 $z i = A ( x i -θ)$
 $1 n ∑ i=1 n w ( ‖ z i ‖ 2 ) z i = 0$
and
 $1 n ∑ i=1 n u ( ‖ z i ‖ 2 ) z i 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.
g02hkc uses weight functions:
 $u (t) = a u t 2 , if ​ t < a u 2 u (t) = 1 , if ​ a u 2 ≤ t ≤ b u 2 u (t) = b u t 2 , if ​ t > b u 2$
and
 $w (t) = 1 , if ​ t ≤ c w w (t) = c w t , if ​ t > c w$
for constants ${a}_{u}$, ${b}_{u}$ and ${c}_{w}$.
These functions solve a minimax problem considered by 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)).
Initial estimates of ${\theta }_{\mathit{j}}$, for $\mathit{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)). g02hkc is based on routines in ROBETH, (see Marazzi (1987)).
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: the number of observations, $n$.
Constraint: ${\mathbf{n}}>1$.
2: $\mathbf{m}$Integer Input
On entry: the number of columns of the matrix $X$, i.e., number of independent variables, $m$.
Constraint: $1\le {\mathbf{m}}\le {\mathbf{n}}$.
3: $\mathbf{x}\left[{\mathbf{n}}×{\mathbf{tdx}}\right]$const double Input
On entry: ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+\mathit{j}-1\right]$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
4: $\mathbf{tdx}$Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
5: $\mathbf{eps}$double Input
On entry: the expected fraction of gross errors expected in the sample, $\epsilon$.
Constraint: $0.0\le {\mathbf{eps}}<1.0$.
6: $\mathbf{cov}\left[{\mathbf{m}}×\left({\mathbf{m}}+1\right)/2\right]$double Output
On exit: the ${\mathbf{m}}×\left({\mathbf{m}}+1\right)$/2 elements of cov contain the upper triangular part of the covariance matrix. They are stored packed by column, i.e., ${C}_{\mathit{i}\mathit{j}}$, $\mathit{j}\ge \mathit{i}$, is stored in ${\mathbf{cov}}\left[\mathit{j}\left(\mathit{j}+1\right)/2+\mathit{i}\right]$, for $\mathit{i}=0,1,\dots ,{\mathbf{m}}-1$ and $\mathit{j}=i,\dots ,{\mathbf{m}}-1$.
7: $\mathbf{theta}\left[{\mathbf{m}}\right]$double Output
On exit: the robust estimate of the location arguments ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
8: $\mathbf{max_iter}$Integer Input
On entry: the maximum number of iterations that will be used during the calculation of the covariance matrix.
Suggested value: ${\mathbf{max_iter}}=150$.
Constraint: ${\mathbf{max_iter}}>0$.
On entry: indicates if the printing of information on the iterations is required and the rate at which printing is produced.
${\mathbf{print_iter}}\le 0$
No iteration monitoring is printed.
${\mathbf{print_iter}}>0$
The value of $A$, $\theta$ and $\delta$ (see Section 9) will be printed at the first and every print_iter iterations.
10: $\mathbf{outfile}$const char * Input
On entry: a null terminated character string giving the name of the file to which results should be printed. If outfile is NULL or an empty string then the stdout stream is used. Note that the file will be opened in the append mode.
11: $\mathbf{tol}$double Input
On entry: the relative precision for the final estimates of the covariance matrix.
Constraint: ${\mathbf{tol}}>0.0$.
12: $\mathbf{iter}$Integer * Output
On exit: the number of iterations performed.
13: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{m}}\le {\mathbf{n}}$.
NE_2_INT_ARG_LT
On entry, ${\mathbf{tdx}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_C_ITER_UNSTABLE
The iterative procedure to find $C$ has become unstable. This may happen if the value of eps is too large.
NE_CONST_COL
On entry, column $⟨\mathit{\text{value}}⟩$ of array x has constant value.
NE_INT_ARG_LE
On entry, max_iter must not be less than or equal to 0: ${\mathbf{max_iter}}=⟨\mathit{\text{value}}⟩$.
NE_INT_ARG_LT
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_NOT_APPEND_FILE
Cannot open file $⟨\mathit{string}⟩$ for appending.
NE_NOT_CLOSE_FILE
Cannot close file $⟨\mathit{string}⟩$.
NE_REAL_ARG_GE
On entry, eps must be not be greater than or equal to 1.0: ${\mathbf{eps}}=⟨\mathit{\text{value}}⟩$.
NE_REAL_ARG_LE
On entry, tol must not be less than or equal to 0.0: ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
NE_REAL_ARG_LT
On entry, eps must not be less than 0.0: ${\mathbf{eps}}=⟨\mathit{\text{value}}⟩$.
NE_TOO_MANY
Too many iterations($⟨\mathit{\text{value}}⟩$ ).
The iterative procedure to find the co-variance matrix $C$, has failed to converge in max_iter iterations.

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

g02hkc 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 function. 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 (g02hkce.c)

### 10.2Program Data

Program Data (g02hkce.d)

### 10.3Program Results

Program Results (g02hkce.r)