# NAG FL Interfaceg07dbf (robust_​1var_​mestim)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g07dbf ( n, x, ipsi, c, h1, h2, h3, dchi, tol, rs, nit, wrk,
 Integer, Intent (In) :: isigma, n, ipsi, maxit Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nit Real (Kind=nag_wp), Intent (In) :: x(n), c, h1, h2, h3, dchi, tol Real (Kind=nag_wp), Intent (Inout) :: theta, sigma Real (Kind=nag_wp), Intent (Out) :: rs(n), wrk(n)
#include <nag.h>
 void g07dbf_ (const Integer *isigma, const Integer *n, const double x[], const Integer *ipsi, const double *c, const double *h1, const double *h2, const double *h3, const double *dchi, double *theta, double *sigma, const Integer *maxit, const double *tol, double rs[], Integer *nit, double wrk[], Integer *ifail)
The routine may be called by the names g07dbf or nagf_univar_robust_1var_mestim.

## 3Description

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
 $F((xi-θ)/σ)$
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:
1. (a)Null Weights
 $\psi \left(t\right)=t$ $\chi \left(t\right)=\frac{{t}^{2}}{2}$
Use of these null functions leads to the mean and standard deviation of the data.
2. (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)$ $\chi \left(t\right)=\frac{{|t|}^{2}}{2}$ $|t|\le d$ $\chi \left(t\right)=\frac{{d}^{2}}{2}$ $|t|>d$
3. (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$ $0\le t\le {h}_{1}$ ${\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)$ ${h}_{2}\le t\le {h}_{3}$ ${\psi }_{{h}_{1},{h}_{2},{h}_{3}}\left(t\right)=0$ $t>{h}_{3}$ $\chi \left(t\right)=\frac{{|t|}^{2}}{2}$ $|t|\le d$ $\chi \left(t\right)=\frac{{d}^{2}}{2}$ $|t|>d$
4. (d)Andrew's Sine Wave Function
 $\psi \left(t\right)=\mathrm{sin}t$ $-\pi \le t\le \pi$ $\psi \left(t\right)=0$ otherwise $\chi \left(t\right)=\frac{{|t|}^{2}}{2}$ $|t|\le d$ $\chi \left(t\right)=\frac{{d}^{2}}{2}$ $|t|>d$
5. (e)Tukey's Bi-weight
 $\psi \left(t\right)=t{\left(1-{t}^{2}\right)}^{2}$ $|t|\le 1$ $\psi \left(t\right)=0$ otherwise $\chi \left(t\right)=\frac{{|t|}^{2}}{2}$ $|t|\le d$ $\chi \left(t\right)=\frac{{d}^{2}}{2}$ $|t|>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

## 5Arguments

1: $\mathbf{isigma}$Integer Input
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: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
3: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the vector of observations, ${x}_{1},{x}_{2},\dots ,{x}_{n}$.
4: $\mathbf{ipsi}$Integer Input
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: $\mathbf{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: $\mathbf{h1}$Real (Kind=nag_wp) Input
7: $\mathbf{h2}$Real (Kind=nag_wp) Input
8: $\mathbf{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: $\mathbf{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: $\mathbf{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: $\mathbf{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$, 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: $\mathbf{maxit}$Integer Input
On entry: the maximum number of iterations that should be used during the estimation.
Suggested value: ${\mathbf{maxit}}=50$.
Constraint: ${\mathbf{maxit}}>0$.
13: $\mathbf{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: $\mathbf{rs}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: the Winsorized residuals.
15: $\mathbf{nit}$Integer Output
On exit: the number of iterations that were used during the estimation.
16: $\mathbf{wrk}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: if ${\mathbf{sigma}}\le 0.0$ on entry, wrk will contain the $n$ observations in ascending order.
17: $\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{ipsi}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ipsi}}=0$, $1$, $2$, $3$ or $4$.
On entry, ${\mathbf{isigma}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{isigma}}=0$ or $1$.
On entry, ${\mathbf{maxit}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{maxit}}>0$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>1$.
On entry, ${\mathbf{tol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{tol}}>0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{c}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{c}}>0.0$.
On entry, ${\mathbf{dchi}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{dchi}}>0.0$.
On entry, ${\mathbf{h1}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{h2}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{h3}}=⟨\mathit{\text{value}}⟩$.
Constraint: $0\le {\mathbf{h1}}\le {\mathbf{h2}}\le {\mathbf{h3}}$ and ${\mathbf{h3}}>0.0$.
${\mathbf{ifail}}=3$
All elements of x are equal.
${\mathbf{ifail}}=4$
Current estimate of sigma is zero or negative: ${\mathbf{sigma}}=⟨\mathit{\text{value}}⟩$. This error exit is very unlikely, although it may be caused by too large an initial value of sigma.
${\mathbf{ifail}}=5$
Number of iterations required exceeds maxit: ${\mathbf{maxit}}=⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=6$
All winsorized residuals are 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, 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$.
${\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.

## 8Parallelism and Performance

g07dbf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g07dbf 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.

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).

## 10Example

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:
1. (a)g07dbf determines the starting values, $\sigma$ is estimated simultaneously.
2. (b)You must supply the starting values, $\sigma$ is estimated simultaneously.
3. (c)g07dbf determines the starting values, $\sigma$ is fixed.
4. (d)You must supply the starting values, $\sigma$ is fixed.

### 10.1Program Text

Program Text (g07dbfe.f90)

### 10.2Program Data

Program Data (g07dbfe.d)

### 10.3Program Results

Program Results (g07dbfe.r)