G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG07GAF

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

G07GAF identifies outlying values using Peirce's criterion.

## 2  Specification

 SUBROUTINE G07GAF ( N, P, Y, MEAN, VAR, IOUT, NIOUT, LDIFF, DIFF, LLAMB, IFAIL)
 INTEGER N, P, IOUT(N), NIOUT, LDIFF, IFAIL REAL (KIND=nag_wp) Y(N), MEAN, VAR, DIFF(LDIFF), LLAMB(LDIFF)

## 3  Description

G07GAF flags outlying values in data using Peirce's criterion. Let
• $y$ denote a vector of $n$ observations (for example the residuals) obtained from a model with $p$ parameters,
• $m$ denote the number of potential outlying values,
• $\mu$ and ${\sigma }^{2}$ denote the mean and variance of $y$ respectively,
• $\stackrel{~}{y}$ denote a vector of length $n-m$ constructed by dropping the $m$ values from $y$ with the largest value of $\left|{y}_{i}-\mu \right|$,
• ${\stackrel{~}{\sigma }}^{2}$ denote the (unknown) variance of $\stackrel{~}{y}$,
• $\lambda$ denote the ratio of $\stackrel{~}{\sigma }$ and $\sigma$ with $\lambda =\frac{\stackrel{~}{\sigma }}{\sigma }$.
Peirce's method flags ${y}_{i}$ as a potential outlier if $\left|{y}_{i}-\mu \right|\ge x$, where $x={\sigma }^{2}z$ and $z$ is obtained from the solution of
 $Rm = λ m-n mm n-m n-m nn$ (1)
where
 $R = 2 exp z2 - 1 2 1- Φz$ (2)
and $\Phi$ is the cumulative distribution function for the standard Normal distribution.
As ${\stackrel{~}{\sigma }}^{2}$ is unknown an assumption is made that the relationship between ${\stackrel{~}{\sigma }}^{2}$ and ${\sigma }^{2}$, hence $\lambda$, depends only on the sum of squares of the rejected observations and the ratio estimated as
 $λ2 = n-p-m z2 n-p-m$
which gives
 $z2 = 1+ n-p-m m 1-λ2$ (3)
A value for the cutoff $x$ is calculated iteratively. An initial value of $R=0.2$ is used and a value of $\lambda$ is estimated using equation (1). Equation (3) is then used to obtain an estimate of $z$ and then equation (2) is used to get a new estimate for $R$. This process is then repeated until the relative change in $z$ between consecutive iterations is $\text{}\le \sqrt{\epsilon }$, where $\epsilon$ is machine precision.
By construction, the cutoff for testing for $m+1$ potential outliers is less than the cutoff for testing for $m$ potential outliers. Therefore Peirce's criterion is used in sequence with the existence of a single potential outlier being investigated first. If one is found, the existence of two potential outliers is investigated etc.
If one of a duplicate series of observations is flagged as an outlier, then all of them are flagged as outliers.

## 4  References

Gould B A (1855) On Peirce's criterion for the rejection of doubtful observations, with tables for facilitating its application The Astronomical Journal 45
Peirce B (1852) Criterion for the rejection of doubtful observations The Astronomical Journal 45

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 3$.
2:     P – INTEGERInput
On entry: $p$, the number of parameters in the model used in obtaining the $y$. If $y$ is an observed set of values, as opposed to the residuals from fitting a model with $p$ parameters, then $p$ should be set to $1$, i.e., as if a model just containing the mean had been used.
Constraint: $1\le {\mathbf{P}}\le {\mathbf{N}}-2$.
3:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: $y$, the data being tested.
4:     MEAN – REAL (KIND=nag_wp)Input
On entry: if ${\mathbf{VAR}}>0.0$, MEAN must contain $\mu$, the mean of $y$, otherwise MEAN is not referenced and the mean is calculated from the data supplied in Y.
5:     VAR – REAL (KIND=nag_wp)Input
On entry: if ${\mathbf{VAR}}>0.0$, VAR must contain ${\sigma }^{2}$, the variance of $y$, otherwise the variance is calculated from the data supplied in Y.
6:     IOUT(N) – INTEGER arrayOutput
On exit: the indices of the values in Y sorted in descending order of the absolute difference from the mean, therefore $\left|{\mathbf{Y}}\left({\mathbf{IOUT}}\left(\mathit{i}-1\right)\right)-\mu \right|\ge \left|{\mathbf{Y}}\left({\mathbf{IOUT}}\left(\mathit{i}\right)\right)-\mu \right|$, for $\mathit{i}=2,3,\dots ,{\mathbf{N}}$.
7:     NIOUT – INTEGEROutput
On exit: the number of potential outliers. The indices for these potential outliers are held in the first NIOUT elements of IOUT. By construction there can be at most ${\mathbf{N}}-{\mathbf{P}}-1$ values flagged as outliers.
8:     LDIFF – INTEGERInput
On entry: the maximum number of values to be returned in arrays DIFF and LLAMB.
If ${\mathbf{LDIFF}}\le 0$, arrays DIFF and LLAMB are not referenced.
9:     DIFF(LDIFF) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{DIFF}}\left(\mathit{i}\right)$ holds $\left|y-\mu \right|-{\sigma }^{2}z$ for observation ${\mathbf{Y}}\left({\mathbf{IOUT}}\left(\mathit{i}\right)\right)$, for $\mathit{i}=1,2,\dots ,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LDIFF}},{\mathbf{NIOUT}}+1,{\mathbf{N}}-{\mathbf{P}}-1\right)$.
10:   LLAMB(LDIFF) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{LLAMB}}\left(\mathit{i}\right)$ holds $\mathrm{log}\left({\lambda }^{2}\right)$ for observation ${\mathbf{Y}}\left({\mathbf{IOUT}}\left(\mathit{i}\right)\right)$, for $\mathit{i}=1,2,\dots ,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{LDIFF}},{\mathbf{NIOUT}}+1,{\mathbf{N}}-{\mathbf{P}}-1\right)$.
11:   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}}<3$.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{P}}\le 0$ or ${\mathbf{P}}>{\mathbf{N}}-2$.

## 7  Accuracy

Not applicable.

One problem with Peirce's algorithm as implemented in G07GAF is the assumed relationship between ${\sigma }^{2}$, the variance using the full dataset, and ${\stackrel{~}{\sigma }}^{2}$, the variance with the potential outliers removed. In some cases, for example if the data $y$ were the residuals from a linear regression, this assumption may not hold as the regression line may change significantly when outlying values have been dropped resulting in a radically different set of residuals. In such cases G07GBF should be used instead.

## 9  Example

This example reads in a series of data and flags any potential outliers.
The dataset used is from Peirce's original paper and consists of fifteen observations on the vertical semidiameter of Venus.

### 9.1  Program Text

Program Text (g07gafe.f90)

### 9.2  Program Data

Program Data (g07gafe.d)

### 9.3  Program Results

Program Results (g07gafe.r)