G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08CHF

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

G08CHF calculates the Anderson–Darling goodness-of-fit test statistic.

## 2  Specification

 FUNCTION G08CHF ( N, ISSORT, Y, IFAIL)
 REAL (KIND=nag_wp) G08CHF
 INTEGER N, IFAIL REAL (KIND=nag_wp) Y(N) LOGICAL ISSORT

## 3  Description

Denote by ${A}^{2}$ the Anderson–Darling test statistic for $n$ observations ${y}_{1},{y}_{2},\dots ,{y}_{n}$ of a variable $Y$ assumed to be standard uniform and sorted in ascending order, then:
 $A2 = -n-S ;$
where:
 $S = ∑ i=1 n 2i-1 n ln⁡yi + ln 1- y n-i+1 .$
When observations of a random variable $X$ are non-uniformly distributed, the probability integral transformation (PIT):
 $Y=FX ,$
where $F$ is the cumulative distribution function of the distribution of interest, yields a uniformly distributed random variable $Y$. The PIT is true only if all parameters of a distribution are known as opposed to estimated; otherwise it is an approximation.

## 4  References

Anderson T W and Darling D A (1952) Asymptotic theory of certain ‘goodness-of-fit’ criteria based on stochastic processes Annals of Mathematical Statistics 23 193–212

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}>1$.
2:     ISSORT – LOGICALInput
On entry: set ${\mathbf{ISSORT}}=\mathrm{.TRUE.}$ if the observations are sorted in ascending order; otherwise the function will sort the observations.
3:     Y(N) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$, the $n$ observations.
On exit: if ${\mathbf{ISSORT}}=\mathrm{.FALSE.}$, the data sorted in ascending order; otherwise the array is unchanged.
Constraint: if ${\mathbf{ISSORT}}=\mathrm{.TRUE.}$, the values must be sorted in ascending order. Each ${y}_{i}$ must lie in the interval $\left(0,1\right)$.
4:     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}}<2$.
${\mathbf{IFAIL}}=3$
The data in Y is not sorted in ascending order.
${\mathbf{IFAIL}}=9$
The data in Y must lie in the interval $\left(0,1\right)$.

Not applicable.

None.

## 9  Example

This example calculates the ${A}^{2}$ statistic for data assumed to arise from an exponential distribution with a sample parameter estimate and simulates its $p$-value using the NAG basic random number generator.

### 9.1  Program Text

Program Text (g08chfe.f90)

### 9.2  Program Data

Program Data (g08chfe.d)

### 9.3  Program Results

Program Results (g08chfe.r)