G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG07DBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G07DBF computes an $M$-estimate of location with (optional) simultaneous estimation of the scale using Huber's algorithm.

## 2  Specification

 SUBROUTINE G07DBF ( ISIGMA, N, X, IPSI, C, H1, H2, H3, DCHI, THETA, SIGMA, MAXIT, TOL, RS, NIT, WRK, IFAIL)
 INTEGER ISIGMA, N, IPSI, MAXIT, NIT, IFAIL REAL (KIND=nag_wp) X(N), C, H1, H2, H3, DCHI, THETA, SIGMA, TOL, RS(N), WRK(N)

## 3  Description

The data consists of a sample of size $n$, denoted by ${x}_{1},{x}_{2},\dots ,{x}_{n}$, drawn from a random variable $X$.
The ${x}_{i}$ are assumed to be independent with an unknown distribution function of the form
 $Fxi-θ/σ$
where $\theta$ is a location parameter, and $\sigma$ is a scale parameter. $M$-estimators of $\theta$ and $\sigma$ are given by the solution to the following system of equations:
 $∑i=1nψ xi-θ^ /σ^=0$ (1)
 $∑i=1nχ xi-θ^ /σ^=n-1β$ (2)
where $\psi$ and $\chi$ are given functions, and $\beta$ is a constant, such that $\stackrel{^}{\sigma }$ is an unbiased estimator when ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$ has a Normal distribution. Optionally, the second equation can be omitted and the first equation is solved for $\stackrel{^}{\theta }$ using an assigned value of $\sigma ={\sigma }_{c}$.
The values of $\psi \left(\frac{{x}_{i}-\stackrel{^}{\theta }}{\stackrel{^}{\sigma }}\right)\stackrel{^}{\sigma }$ are known as the Winsorized residuals.
The following functions are available for $\psi$ and $\chi$ in G07DBF.
(a) Null Weights
 $\psi \left(t\right)=t\phantom{\frac{{t}^{2}}{2}}$ $\chi \left(t\right)=\frac{{t}^{2}}{2}$
Use of these null functions leads to the mean and standard deviation of the data.
(b) Huber's Function
 $\psi \left(t\right)=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(-c,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(c,t\right)\right)\phantom{\frac{{t}^{2}}{2}}$ $\chi \left(t\right)=\frac{{‖t‖}^{2}}{2}‖t‖\le d$ $\chi \left(t\right)=\frac{{d}^{2}}{2}‖t‖>d$
(c) Hampel's Piecewise Linear Function
 ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)=-{\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(-t\right)$ ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)=t\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $0\le t\le {h}_{1}\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $\chi \left(t\right)=\frac{{\left|t\right|}^{2}}{2}\left|t\right|\le d$ ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)={h}_{1}$ ${h}_{1}\le t\le {h}_{2}$ ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)={h}_{1}\left({h}_{3}-t\right)/\left({h}_{3}-{h}_{2}\right)\phantom{\frac{{\left|t\right|}^{2}}{2}}$ ${h}_{2}\le t\le {h}_{3}\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $\chi \left(t\right)=\frac{{d}^{2}}{2}\left|t\right|>d$ ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)=0$ $t>{h}_{3}$
(d) Andrew's Sine Wave Function
 $\psi \left(t\right)=\mathrm{sin}t\phantom{\frac{{d}^{2}}{2}}$ $-\pi \le t\le \pi \phantom{\frac{{d}^{2}}{2}}$ $\chi \left(t\right)=\frac{{\left|t\right|}^{2}}{2}\left|t\right|\le d$ $\psi \left(t\right)=0\phantom{\frac{{d}^{2}}{2}}$ otherwise $\phantom{\frac{{d}^{2}}{2}}$ $\chi \left(t\right)=\frac{{d}^{2}}{2}\left|t\right|>d$
(e) Tukey's Bi-weight
 $\psi \left(t\right)=t{\left(1-{t}^{2}\right)}^{2}\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $\left|t\right|\le 1\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $\chi \left(t\right)=\frac{{\left|t\right|}^{2}}{2}\left|t\right|\le d$ $\psi \left(t\right)=t{\left(1-{t}^{2}\right)}^{2}=0\phantom{\frac{{\left|t\right|}^{2}}{2}}$ otherwise $\phantom{\frac{{\left|t\right|}^{2}}{2}}$ $\chi \left(t\right)=\frac{{d}^{2}}{2}\left|t\right|>d$
where $c$, ${h}_{1}$, ${h}_{2}$, ${h}_{3}$ and $d$ are constants.
Equations (1) and (2) are solved by a simple iterative procedure suggested by Huber:
 $σ^k=1βn-1 ∑i=1nχ xi-θ^k-1σ^k-1 σ^k-12$
and
 $θ^k=θ^k- 1+1n∑i= 1nψ xi-θ^k- 1σ^k σ^k$
or
 $σ^k=σc, if σ is fixed.$
The initial values for $\stackrel{^}{\theta }$ and $\stackrel{^}{\sigma }$ may either be user-supplied or calculated within G07DBF as the sample median and an estimate of $\sigma$ based on the median absolute deviation respectively.
G07DBF is based upon subroutine LYHALG within the ROBETH library, see Marazzi (1987).
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 estimation of location and scale in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 1 Institut Universitaire de Médecine Sociale et Préventive, Lausanne

## 5  Parameters

1:     ISIGMA – INTEGERInput
On entry: the value assigned to ISIGMA determines whether $\stackrel{^}{\sigma }$ is to be simultaneously estimated.
${\mathbf{ISIGMA}}=0$
The estimation of $\stackrel{^}{\sigma }$ is bypassed and SIGMA is set equal to ${\sigma }_{c}$.
${\mathbf{ISIGMA}}=1$
$\stackrel{^}{\sigma }$ is estimated simultaneously.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>1$.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the vector of observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
4:     IPSI – INTEGERInput
On entry: which $\psi$ function is to be used.
${\mathbf{IPSI}}=0$
$\psi \left(t\right)=t$.
${\mathbf{IPSI}}=1$
Huber's function.
${\mathbf{IPSI}}=2$
Hampel's piecewise linear function.
${\mathbf{IPSI}}=3$
Andrew's sine wave,
${\mathbf{IPSI}}=4$
Tukey's bi-weight.
5:     C – REAL (KIND=nag_wp)Input
On entry: if ${\mathbf{IPSI}}=1$, C must specify the parameter, $c$, of Huber's $\psi$ function. C is not referenced if ${\mathbf{IPSI}}\ne 1$.
Constraint: if ${\mathbf{IPSI}}=1$, ${\mathbf{C}}>0.0$.
6:     H1 – REAL (KIND=nag_wp)Input
7:     H2 – REAL (KIND=nag_wp)Input
8:     H3 – REAL (KIND=nag_wp)Input
On entry: if ${\mathbf{IPSI}}=2$, H1, H2 and H3 must specify the parameters, ${h}_{1}$, ${h}_{2}$, and ${h}_{3}$, of Hampel's piecewise linear $\psi$ function. H1, H2 and H3 are not referenced if ${\mathbf{IPSI}}\ne 2$.
Constraint: $0\le {\mathbf{H1}}\le {\mathbf{H2}}\le {\mathbf{H3}}$ and ${\mathbf{H3}}>0.0$ if ${\mathbf{IPSI}}=2$.
9:     DCHI – REAL (KIND=nag_wp)Input
On entry: $d$, the parameter of the $\chi$ function. DCHI is not referenced if ${\mathbf{IPSI}}=0$.
Constraint: if ${\mathbf{IPSI}}\ne 0$, ${\mathbf{DCHI}}>0.0$.
10:   THETA – REAL (KIND=nag_wp)Input/Output
On entry: if ${\mathbf{SIGMA}}>0$ then THETA must be set to the required starting value of the estimation of the location parameter $\stackrel{^}{\theta }$. A reasonable initial value for $\stackrel{^}{\theta }$ will often be the sample mean or median.
On exit: the $M$-estimate of the location parameter, $\stackrel{^}{\theta }$.
11:   SIGMA – REAL (KIND=nag_wp)Input/Output
On entry: the role of SIGMA depends on the value assigned to ISIGMA, as follows:
• if ${\mathbf{ISIGMA}}=1$, SIGMA must be assigned a value which determines the values of the starting points for the calculations of $\stackrel{^}{\theta }$ and $\stackrel{^}{\sigma }$. If ${\mathbf{SIGMA}}\le 0.0$ then G07DBF will determine the starting points of $\stackrel{^}{\theta }$ and $\stackrel{^}{\sigma }$. Otherwise the value assigned to SIGMA will be taken as the starting point for $\stackrel{^}{\sigma }$, and THETA must be assigned a value before entry, see above;
• if ${\mathbf{ISIGMA}}=0$, SIGMA must be assigned a value which determines the value of ${\sigma }_{c}$, which is held fixed during the iterations, and the starting value for the calculation of $\stackrel{^}{\theta }$. If ${\mathbf{SIGMA}}\le 0$, then G07DBF will determine the value of ${\sigma }_{c}$ as the median absolute deviation adjusted to reduce bias (see G07DAF) and the starting point for $\stackrel{^}{\theta }$. Otherwise, the value assigned to SIGMA will be taken as the value of ${\sigma }_{c}$ and THETA must be assigned a relevant value before entry, see above.
On exit: contains the $M$-estimate of the scale parameter, $\stackrel{^}{\sigma }$, if ISIGMA was assigned the value $1$ on entry, otherwise SIGMA will contain the initial fixed value ${\sigma }_{c}$.
12:   MAXIT – INTEGERInput
On entry: the maximum number of iterations that should be used during the estimation.
Suggested value: ${\mathbf{MAXIT}}=50$.
Constraint: ${\mathbf{MAXIT}}>0$.
13:   TOL – REAL (KIND=nag_wp)Input
On entry: the relative precision for the final estimates. Convergence is assumed when the increments for THETA, and SIGMA are less than ${\mathbf{TOL}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1.0,{\sigma }_{k-1}\right)$.
Constraint: ${\mathbf{TOL}}>0.0$.
14:   RS(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the Winsorized residuals.
15:   NIT – INTEGEROutput
On exit: the number of iterations that were used during the estimation.
16:   WRK(N) – REAL (KIND=nag_wp) arrayOutput
On exit: if ${\mathbf{SIGMA}}\le 0.0$ on entry, WRK will contain the $n$ observations in ascending order.
17:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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{N}}\le 1$, or ${\mathbf{MAXIT}}\le 0$, or ${\mathbf{TOL}}\le 0.0$, or ${\mathbf{ISIGMA}}\ne 0$ or $1$, or ${\mathbf{IPSI}}<0$, or ${\mathbf{IPSI}}>4$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{C}}\le 0.0$ and ${\mathbf{IPSI}}=1$, or ${\mathbf{H1}}<0.0$ and ${\mathbf{IPSI}}=2$, or ${\mathbf{H1}}={\mathbf{H2}}={\mathbf{H3}}=0.0$ and ${\mathbf{IPSI}}=2$, or ${\mathbf{H1}}>{\mathbf{H2}}$ and ${\mathbf{IPSI}}=2$, or ${\mathbf{H1}}>{\mathbf{H3}}$ and ${\mathbf{IPSI}}=2$, or ${\mathbf{H2}}>{\mathbf{H3}}$ and ${\mathbf{IPSI}}=2$, or ${\mathbf{DCHI}}\le 0.0$ and ${\mathbf{IPSI}}\ne 0$.
${\mathbf{IFAIL}}=3$
 On entry, all elements of the input array X are equal.
${\mathbf{IFAIL}}=4$
SIGMA, the current estimate of $\sigma$, is zero or negative. This error exit is very unlikely, although it may be caused by too large an initial value of SIGMA.
${\mathbf{IFAIL}}=5$
The number of iterations required exceeds MAXIT.
${\mathbf{IFAIL}}=6$
On completion of the iterations, the Winsorized residuals were all zero. This may occur when using the ${\mathbf{ISIGMA}}=0$ option with a redescending $\psi$ function, i.e., Hampel's piecewise linear function, Andrew's sine wave, and Tukey's biweight.
If the given value of $\sigma$ is too small, then the standardized residuals $\frac{{x}_{i}-{\stackrel{^}{\theta }}_{k}}{{\sigma }_{c}}$, will be large and all the residuals may fall into the region for which $\psi \left(t\right)=0$. This may incorrectly terminate the iterations thus making THETA and SIGMA invalid.
Re-enter the routine with a larger value of ${\sigma }_{c}$ or with ${\mathbf{ISIGMA}}=1$.

## 7  Accuracy

On successful exit the accuracy of the results is related to the value of TOL, see Section 5.

When you supply the initial values, care has to be taken over the choice of the initial value of $\sigma$. If too small a value of $\sigma$ is chosen then initial values of the standardized residuals $\frac{{x}_{i}-{\stackrel{^}{\theta }}_{k}}{\sigma }$ will be large. If the redescending $\psi$ functions are used, i.e., Hampel's piecewise linear function, Andrew's sine wave, or Tukey's bi-weight, then these large values of the standardized residuals are Winsorized as zero. If a sufficient number of the residuals fall into this category then a false solution may be returned, see page 152 of Hampel et al. (1986).

## 9  Example

The following program reads in a set of data consisting of eleven observations of a variable $X$.
For this example, Hampel's Piecewise Linear Function is used (${\mathbf{IPSI}}=2$), values for ${h}_{1}$, ${h}_{2}$ and ${h}_{3}$ along with $d$ for the $\chi$ function, being read from the data file.
Using the following starting values various estimates of $\theta$ and $\sigma$ are calculated and printed along with the number of iterations used:
 (a) G07DBF determines the starting values, $\sigma$ is estimated simultaneously. (b) You must supply the starting values, $\sigma$ is estimated simultaneously. (c) G07DBF determines the starting values, $\sigma$ is fixed. (d) You must supply the starting values, $\sigma$ is fixed.

### 9.1  Program Text

Program Text (g07dbfe.f90)

### 9.2  Program Data

Program Data (g07dbfe.d)

### 9.3  Program Results

Program Results (g07dbfe.r)