# NAG FL Interfaceg02hmf (robustm_​corr_​user)

## 1Purpose

g02hmf computes a robust estimate of the covariance matrix for user-supplied weight functions. The derivatives of the weight functions are not required.

## 2Specification

Fortran Interface
 Subroutine g02hmf ( ucv, indm, n, m, x, ldx, cov, a, wt, bl, bd, tol, nit, wk,
 Integer, Intent (In) :: indm, n, m, ldx, maxit, nitmon Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nit Real (Kind=nag_wp), Intent (In) :: x(ldx,m), bl, bd, tol Real (Kind=nag_wp), Intent (Inout) :: ruser(*), a(m*(m+1)/2), theta(m) Real (Kind=nag_wp), Intent (Out) :: cov(m*(m+1)/2), wt(n), wk(2*m) External :: ucv
#include <nag.h>
 void g02hmf_ (void (NAG_CALL *ucv)(const double *t, double ruser[], double *u, double *w),double ruser[], const Integer *indm, const Integer *n, const Integer *m, const double x[], const Integer *ldx, double cov[], double a[], double wt[], double theta[], const double *bl, const double *bd, const Integer *maxit, const Integer *nitmon, const double *tol, Integer *nit, double wk[], Integer *ifail)
The routine may be called by the names g02hmf or nagf_correg_robustm_corr_user.

## 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=τ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=1nuzi2zi ziT -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.
g02hmf covers two situations:
1. (i)$v\left(t\right)=1$ for all $t$,
2. (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)).
g02hmf finds $A$ using the iterative procedure as given by Huber; see Huber (1981).
 $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/D2,-BL,BL, j>l -minmax12hjj/D2-1,-BD,BD, j=l ,$
where
• ${D}_{1}=\sum _{i=1}^{n}w\left({‖{z}_{i}‖}_{2}\right)$
• ${D}_{2}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right)$
• ${h}_{jl}=\sum _{i=1}^{n}u\left({‖{z}_{i}‖}_{2}\right){z}_{ij}{z}_{il}$, for $j\ge l$
• ${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.
The value of $\tau$ may be chosen so that $C$ is unbiased if the observations are from a given distribution.
g02hmf 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{ucv}$Subroutine, supplied by the user. External Procedure
ucv must return the values of the functions $u$ and $w$ for a given value of its argument.
The specification of ucv is:
Fortran Interface
 Subroutine ucv ( t, u, w)
 Real (Kind=nag_wp), Intent (In) :: t Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Real (Kind=nag_wp), Intent (Out) :: u, w
 void ucv_ (const double *t, double ruser[], double *u, double *w)
1: $\mathbf{t}$Real (Kind=nag_wp) Input
On entry: the argument for which the functions $u$ and $w$ must be evaluated.
2: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
ucv is called with the argument ruser as supplied to g02hmf. You should use the array ruser to supply information to ucv.
3: $\mathbf{u}$Real (Kind=nag_wp) Output
On exit: the value of the $u$ function at the point t.
Constraint: ${\mathbf{u}}\ge 0.0$.
4: $\mathbf{w}$Real (Kind=nag_wp) Output
On exit: the value of the $w$ function at the point t.
Constraint: ${\mathbf{w}}\ge 0.0$.
ucv must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which g02hmf is called. Arguments denoted as Input must not be changed by this procedure.
Note: ucv should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g02hmf. If your code inadvertently does return any NaNs or infinities, g02hmf is likely to produce unexpected results.
2: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
ruser is not used by g02hmf, but is passed directly to ucv and may be used to pass information to this routine.
3: $\mathbf{indm}$Integer Input
On entry: indicates which form of the function $v$ will be used.
${\mathbf{indm}}=1$
$v=1$.
${\mathbf{indm}}\ne 1$
$v=u$.
4: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
5: $\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}}$.
6: $\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 on the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
7: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g02hmf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
8: $\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 (lower triangular stored by rows), that is ${C}_{ij}$ is returned in ${\mathbf{cov}}\left(j×\left(j-1\right)/2+i\right)$, $i\le j$.
9: $\mathbf{a}\left({\mathbf{m}}×\left({\mathbf{m}}+1\right)/2\right)$Real (Kind=nag_wp) array Input/Output
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}=1,2,\dots ,m$.
On exit: the lower triangular elements of the inverse of the matrix $A$, stored row-wise.
10: $\mathbf{wt}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\mathbf{wt}}\left(\mathit{i}\right)$ contains the weights, ${\mathit{wt}}_{\mathit{i}}=u\left({‖{z}_{\mathit{i}}‖}_{2}\right)$, for $\mathit{i}=1,2,\dots ,n$.
11: $\mathbf{theta}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Input/Output
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.
On exit: contains the robust estimate of the location parameter, ${\theta }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
12: $\mathbf{bl}$Real (Kind=nag_wp) Input
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$.
13: $\mathbf{bd}$Real (Kind=nag_wp) Input
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$.
14: $\mathbf{maxit}$Integer Input
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$.
15: $\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 channel (See x04abf.)
16: $\mathbf{tol}$Real (Kind=nag_wp) Input
On entry: the relative precision for the final estimate of the covariance matrix. Iteration will stop when maximum $\delta$ (see Section 7) is less than tol.
Constraint: ${\mathbf{tol}}>0.0$.
17: $\mathbf{nit}$Integer Output
On exit: the number of iterations performed.
18: $\mathbf{wk}\left(2×{\mathbf{m}}\right)$Real (Kind=nag_wp) array Workspace
19: $\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{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{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}}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{bd}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{bd}}>0.0$.
On entry, ${\mathbf{bl}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{bl}}>0.0$.
On entry, $i=〈\mathit{\text{value}}〉$ and the $i$th diagonal element of $A$ is $0$.
Constraint: all diagonal elements of $A$ must be non-zero.
On entry, ${\mathbf{maxit}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxit}}>0$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=3$
On entry, a variable has a constant value, i.e., all elements in column $〈\mathit{\text{value}}〉$ of x are identical.
${\mathbf{ifail}}=4$
$u$ value returned by ${\mathbf{ucv}}<0.0$: $u\left(〈\mathit{\text{value}}〉\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{u}}\ge 0.0$.
$w$ value returned by ${\mathbf{ucv}}<0.0$: $w\left(〈\mathit{\text{value}}〉\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{w}}\ge 0.0$.
${\mathbf{ifail}}=5$
Iterations to calculate weights failed to converge.
${\mathbf{ifail}}=6$
The sum ${D}_{1}$ is zero. Try either a larger initial estimate of $A$ or make $u$ and $w$ less strict.
The sum ${D}_{2}$ is zero. Try either a larger initial estimate of $A$ or make $u$ and $w$ less strict.
${\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 $\left|{s}_{jl}\right|$
2. (ii)$d2=\text{}$ the maximum absolute change in $wt\left(i\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

g02hmf 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$ 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.
If derivatives of the $u$ and $w$ functions are available then the method used in g02hlf will usually give much faster convergence.

## 10Example

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 g02hmf is printed along with the robust estimate of $\theta$.
ucv computes the Huber's weight functions:
 $ut=1, if t≤cu2 ut= cut2, if t>cu2$
and
 $wt= 1, if t≤cw wt= cwt, if t>cw.$

### 10.1Program Text

Program Text (g02hmfe.f90)

### 10.2Program Data

Program Data (g02hmfe.d)

### 10.3Program Results

Program Results (g02hmfe.r)