g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_robust_corr_estim (g02hkc)

## 1  Purpose

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

## 2  Specification

 #include #include
 void nag_robust_corr_estim (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)

## 3  Description

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 AT A -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.
nag_robust_corr_estim (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)). nag_robust_corr_estim (g02hkc) 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  Arguments

1:     nIntegerInput
On entry: the number of observations, $n$.
Constraint: ${\mathbf{n}}>1$.
2:     mIntegerInput
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:     x[${\mathbf{n}}×{\mathbf{tdx}}$]const doubleInput
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:     tdxIntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
5:     epsdoubleInput
On entry: the expected fraction of gross errors expected in the sample, $\epsilon$.
Constraint: $0.0\le {\mathbf{eps}}<1.0$.
6:     cov[${\mathbf{m}}×\left({\mathbf{m}}+1\right)/2$]doubleOutput
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:     theta[m]doubleOutput
On exit: the robust estimate of the location arguments ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
8:     max_iterIntegerInput
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 8) will be printed at the first and every print_iter iterations.
10:   outfileconst 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:   toldoubleInput
On entry: the relative precision for the final estimates of the covariance matrix.
Constraint: ${\mathbf{tol}}>0.0$.
12:   iterInteger *Output
On exit: the number of iterations performed.
13:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

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

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

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.

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

### 9.1  Program Text

Program Text (g02hkce.c)

### 9.2  Program Data

Program Data (g02hkce.d)

### 9.3  Program Results

Program Results (g02hkce.r)