g02hf calculates an estimate of the asymptotic variance-covariance matrix for the bounded influence regression estimates (M-estimates). It is intended for use with g02hd.

# Syntax

C#
```public static void g02hf(
G02..::..G02HF_PSI psi,
G02..::..G02HF_PSP psp,
int indw,
int indc,
double sigma,
int n,
int m,
double[,] x,
double[] rs,
double[] wgt,
double[,] c,
double[] diag_d,
double[] diag_p,
out int ifail
)```
Visual Basic
```Public Shared Sub g02hf ( _
psi As G02..::..G02HF_PSI, _
psp As G02..::..G02HF_PSP, _
indw As Integer, _
indc As Integer, _
sigma As Double, _
n As Integer, _
m As Integer, _
x As Double(,), _
rs As Double(), _
wgt As Double(), _
c As Double(,), _
diag_d As Double(), _
diag_p As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02hf(
G02..::..G02HF_PSI^ psi,
G02..::..G02HF_PSP^ psp,
int indw,
int indc,
double sigma,
int n,
int m,
array<double,2>^ x,
array<double>^ rs,
array<double>^ wgt,
array<double,2>^ c,
array<double>^ diag_d,
array<double>^ diag_p,
[OutAttribute] int% ifail
)```
F#
```static member g02hf :
psi : G02..::..G02HF_PSI *
psp : G02..::..G02HF_PSP *
indw : int *
indc : int *
sigma : float *
n : int *
m : int *
x : float[,] *
rs : float[] *
wgt : float[] *
c : float[,] *
diag_d : float[] *
diag_p : float[] *
ifail : int byref -> unit
```

#### Parameters

psi
Type: NagLibrary..::..G02..::..G02HF_PSI
psi must return the value of the $\psi$ function for a given value of its argument.

A delegate of type G02HF_PSI.

psp
Type: NagLibrary..::..G02..::..G02HF_PSP
psp must return the value of ${\psi }^{\prime }\left(t\right)=\frac{d}{dt}\psi \left(t\right)$ for a given value of its argument.

A delegate of type G02HF_PSP.

indw
Type: System..::..Int32
On entry: the type of regression for which the asymptotic variance-covariance matrix is to be calculated.
${\mathbf{indw}}=-1$
Mallows type regression.
${\mathbf{indw}}=0$
Huber type regression.
${\mathbf{indw}}=1$
Schweppe type regression.
indc
Type: System..::..Int32
On entry: if ${\mathbf{indw}}\ne 0$, indc must specify the approximation to be used.
If ${\mathbf{indc}}=1$, averaging over residuals.
If ${\mathbf{indc}}=0$, replacing expected by observed.
If ${\mathbf{indw}}=0$, indc is not referenced.
sigma
Type: System..::..Double
On entry: the value of $\stackrel{^}{\sigma }$, as given by g02hd.
Constraint: ${\mathbf{sigma}}>0.0$.
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 independent variables.
Constraint: $1\le {\mathbf{m}}<{\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: the values of the $X$ matrix, i.e., the independent variables. ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\right]$ must contain the $\mathit{i}\mathit{j}$th element of $X$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
rs
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: the residuals from the bounded influence regression. These are given by g02hd.
wgt
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: if ${\mathbf{indw}}\ne 0$, wgt must contain the vector of weights used by the bounded influence regression. These should be used with g02hd.
If ${\mathbf{indw}}=0$, wgt is not referenced.
c
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On exit: the estimate of the variance-covariance matrix.
diag_d
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: diag_d will contain the diagonal elements of the matrix _d
diag_p
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: diag_p will contain the diagonal elements of the matrix _p
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 description of bounded influence regression see g02hd. Let $\theta$ be the regression parameters and let $C$ be the asymptotic variance-covariance matrix of $\stackrel{^}{\theta }$. Then for Huber type regression
 $C=fHXTX-1σ^2,$
where
 $fH=1n-m∑i=1nψ2ri/σ^1n∑ψ′riσ^2κ2$
 $κ2=1+mn1n∑i=1nψ′ri/σ^-1n∑i=1nψ′ri/σ^21n∑i=1nψ′riσ^2,$
see Huber (1981) and Marazzi (1987).
For Mallows and Schweppe type regressions, $C$ is of the form
 $σ^n2S1-1S2S1-1,$
where ${S}_{1}=\frac{1}{n}{X}^{\mathrm{T}}DX$ and ${S}_{2}=\frac{1}{n}{X}^{\mathrm{T}}PX$.
$D$ is a diagonal matrix such that the $i$th element approximates $E\left({\psi }^{\prime }\left({r}_{i}/\left(\sigma {w}_{i}\right)\right)\right)$ in the Schweppe case and $E\left({\psi }^{\prime }\left({r}_{i}/\sigma \right){w}_{i}\right)$ in the Mallows case.
$P$ is a diagonal matrix such that the $i$th element approximates $E\left({\psi }^{2}\left({r}_{i}/\left(\sigma {w}_{i}\right)\right){w}_{i}^{2}\right)$ in the Schweppe case and $E\left({\psi }^{2}\left({r}_{i}/\sigma \right){w}_{i}^{2}\right)$ in the Mallows case.
Two approximations are available in g02hf:
1. Average over the ${r}_{i}$
 $SchweppeMallowsDi=1n∑j=1nψ′rjσ^wiwi Di=1n∑j=1nψ′rjσ^wiPi=1n∑j=1nψ2rjσ^wiwi2 Pi=1n∑j=1nψ2rjσ^wi2$
2. Replace expected value by observed
 $SchweppeMallowsDi=ψ′riσ^wiwi Di=ψ′riσ^wiPi=ψ2riσ^wiwi2 Pi=ψ2riσ^wi2$
In all cases $\stackrel{^}{\sigma }$ is a robust estimate of $\sigma$.
g02hf is based on routines in ROBETH; see Marazzi (1987).

# References

Hampel F R, Ronchetti E M, Rousseeuw P J and Stahel W A (1986) Robust Statistics. The Approach Based on Influence Functions Wiley
Huber P J (1981) Robust Statistics Wiley
Marazzi A (1987) Subroutines for robust and bounded influence regression in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 2 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, LDC, WK) 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}}\le {\mathbf{m}}$,
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{sigma}}\le 0.0$.
${\mathbf{ifail}}=3$
If ${\mathbf{indw}}=0$ then the matrix ${X}^{\mathrm{T}}X$ is either not positive definite, possibly due to rounding errors, or is ill-conditioned.
If ${\mathbf{indw}}\ne 0$ then the matrix ${S}_{1}$ is singular or almost singular. This may be due to many elements of $D$ being zero.
${\mathbf{ifail}}=4$
Either the value of $\frac{1}{n}\sum _{i=1}^{n}{\psi }^{\prime }\left(\frac{{r}_{i}}{\stackrel{^}{\sigma }}\right)=0$,
or $\kappa =0$,
or $\sum _{i=1}^{n}{\psi }^{2}\left(\frac{{r}_{i}}{\stackrel{^}{\sigma }}\right)=0$.
In this situation g02hf returns $C$ as ${\left({X}^{\mathrm{T}}X\right)}^{-1}$.
${\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

In general, the accuracy of the variance-covariance matrix will depend primarily on the accuracy of the results from g02hd.

# Parallelism and Performance

None.

g02hf is only for situations in which $X$ has full column rank.
Care has to be taken in the choice of the $\psi$ function since if ${\psi }^{\prime }\left(t\right)=0$ for too wide a range then either the value of ${f}_{H}$ will not exist or too many values of ${D}_{i}$ will be zero and it will not be possible to calculate $C$.

# Example

The asymptotic variance-covariance matrix is calculated for a Schweppe type regression. The values of $X$, $\stackrel{^}{\sigma }$ and the residuals and weights are read in. The averaging over residuals approximation is used.

Example program (C#): g02hfe.cs

Example program data: g02hfe.d

Example program results: g02hfe.r