G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08CGF

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

G08CGF computes the test statistic for the ${\chi }^{2}$ goodness-of-fit test for data with a chosen number of class intervals.

## 2  Specification

 SUBROUTINE G08CGF ( NCLASS, IFREQ, CB, DIST, PAR, NPEST, PROB, CHISQ, P, NDF, EVAL, CHISQI, IFAIL)
 INTEGER NCLASS, IFREQ(NCLASS), NPEST, NDF, IFAIL REAL (KIND=nag_wp) CB(NCLASS-1), PAR(2), PROB(NCLASS), CHISQ, P, EVAL(NCLASS), CHISQI(NCLASS) CHARACTER(1) DIST

## 3  Description

The ${\chi }^{2}$ goodness-of-fit test performed by G08CGF is used to test the null hypothesis that a random sample arises from a specified distribution against the alternative hypothesis that the sample does not arise from the specified distribution.
Given a sample of size $n$, denoted by ${x}_{1},{x}_{2},\dots ,{x}_{n}$, drawn from a random variable $X$, and that the data has been grouped into $k$ classes,
 $x≤c1, ci-1ck-1,$
then the ${\chi }^{2}$ goodness-of-fit test statistic is defined by
 $X2=∑i=1k Oi-Ei 2Ei,$
where ${O}_{i}$ is the observed frequency of the $i$th class, and ${E}_{i}$ is the expected frequency of the $i$th class.
The expected frequencies are computed as
 $Ei=pi×n,$
where ${p}_{i}$ is the probability that $X$ lies in the $i$th class, that is
 $p1=PX≤c1, pi=Pci-1ck-1.$
These probabilities are either taken from a common probability distribution or are supplied by you. The available probability distributions within this routine are:
• Normal distribution with mean $\mu$, variance ${\sigma }^{2}$;
• uniform distribution on the interval $\left[a,b\right]$;
• exponential distribution with probability density function $\left(\mathrm{pdf}\right)=\lambda {e}^{-\lambda x}$;
• ${\chi }^{2}$-distribution with $f$ degrees of freedom; and
• gamma distribution with $\mathrm{pdf}=\frac{{x}^{\alpha -1}{e}^{-x/\beta }}{\Gamma \left(\alpha \right){\beta }^{\alpha }}$.
You must supply the frequencies and classes. Given a set of data and classes the frequencies may be calculated using G01AEF.
G08CGF returns the ${\chi }^{2}$ test statistic, ${X}^{2}$, together with its degrees of freedom and the upper tail probability from the ${\chi }^{2}$-distribution associated with the test statistic. Note that the use of the ${\chi }^{2}$-distribution as an approximation to the distribution of the test statistic improves as the expected values in each class increase.

## 4  References

Conover W J (1980) Practical Nonparametric Statistics Wiley
Kendall M G and Stuart A (1973) The Advanced Theory of Statistics (Volume 2) (3rd Edition) Griffin
Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill

## 5  Parameters

1:     NCLASS – INTEGERInput
On entry: $k$, the number of classes into which the data is divided.
Constraint: ${\mathbf{NCLASS}}\ge 2$.
2:     IFREQ(NCLASS) – INTEGER arrayInput
On entry: ${\mathbf{IFREQ}}\left(\mathit{i}\right)$ must specify the frequency of the $\mathit{i}$th class, ${O}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
Constraint: ${\mathbf{IFREQ}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,k$.
3:     CB(${\mathbf{NCLASS}}-1$) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{CB}}\left(\mathit{i}\right)$ must specify the upper boundary value for the $\mathit{i}$th class, for $\mathit{i}=1,2,\dots ,k-1$.
Constraint: ${\mathbf{CB}}\left(1\right)<{\mathbf{CB}}\left(2\right)<\cdots <{\mathbf{CB}}\left({\mathbf{NCLASS}}-1\right)$. For the exponential, gamma and ${\chi }^{2}$-distributions ${\mathbf{CB}}\left(1\right)\ge 0.0$.
4:     DIST – CHARACTER(1)Input
On entry: indicates for which distribution the test is to be carried out.
${\mathbf{DIST}}=\text{'N'}$
The Normal distribution is used.
${\mathbf{DIST}}=\text{'U'}$
The uniform distribution is used.
${\mathbf{DIST}}=\text{'E'}$
The exponential distribution is used.
${\mathbf{DIST}}=\text{'C'}$
The ${\chi }^{2}$-distribution is used.
${\mathbf{DIST}}=\text{'G'}$
The gamma distribution is used.
${\mathbf{DIST}}=\text{'A'}$
You must supply the class probabilities in the array PROB.
Constraint: ${\mathbf{DIST}}=\text{'N'}$, $\text{'U'}$, $\text{'E'}$, $\text{'C'}$, $\text{'G'}$ or $\text{'A'}$.
5:     PAR($2$) – REAL (KIND=nag_wp) arrayInput
On entry: must contain the parameters of the distribution which is being tested. If you supply the probabilities (i.e., ${\mathbf{DIST}}=\text{'A'}$) the array PAR is not referenced.
If a Normal distribution is used then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the mean, $\mu$, and the variance, ${\sigma }^{2}$, respectively.
If a uniform distribution is used then ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the boundaries $a$ and $b$ respectively.
If an exponential distribution is used then ${\mathbf{PAR}}\left(1\right)$ must contain the parameter $\lambda$. ${\mathbf{PAR}}\left(2\right)$ is not used.
If a ${\chi }^{2}$-distribution is used then ${\mathbf{PAR}}\left(1\right)$ must contain the number of degrees of freedom. ${\mathbf{PAR}}\left(2\right)$ is not used.
If a gamma distribution is used ${\mathbf{PAR}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)$ must contain the parameters $\alpha$ and $\beta$ respectively.
Constraints:
• if ${\mathbf{DIST}}=\text{'N'}$, ${\mathbf{PAR}}\left(2\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'U'}$, ${\mathbf{PAR}}\left(1\right)<{\mathbf{PAR}}\left(2\right)$ and ${\mathbf{PAR}}\left(1\right)\le {\mathbf{CB}}\left(1\right)$ and ${\mathbf{PAR}}\left(2\right)\ge {\mathbf{CB}}\left({\mathbf{NCLASS}}-1\right)$;
• if ${\mathbf{DIST}}=\text{'E'}$, ${\mathbf{PAR}}\left(1\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'C'}$, ${\mathbf{PAR}}\left(1\right)>0.0$;
• if ${\mathbf{DIST}}=\text{'G'}$, ${\mathbf{PAR}}\left(1\right)>0.0$ and ${\mathbf{PAR}}\left(2\right)>0.0$.
6:     NPEST – INTEGERInput
On entry: the number of estimated parameters of the distribution.
Constraint: $0\le {\mathbf{NPEST}}<{\mathbf{NCLASS}}-1$.
7:     PROB(NCLASS) – REAL (KIND=nag_wp) arrayInput
On entry: if you are supplying the probability distribution (i.e., ${\mathbf{DIST}}=\text{'A'}$) then ${\mathbf{PROB}}\left(i\right)$ must contain the probability that $X$ lies in the $i$th class.
If ${\mathbf{DIST}}\ne \text{'A'}$, PROB is not referenced.
Constraint: if ${\mathbf{DIST}}=\text{'A'}$, $\sum _{i=1}^{k}{\mathbf{PROB}}\left(i\right)=1.0$, ${\mathbf{PROB}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,k$.
8:     CHISQ – REAL (KIND=nag_wp)Output
On exit: the test statistic, ${X}^{2}$, for the ${\chi }^{2}$ goodness-of-fit test.
9:     P – REAL (KIND=nag_wp)Output
On exit: the upper tail probability from the ${\chi }^{2}$-distribution associated with the test statistic, ${X}^{2}$, and the number of degrees of freedom.
10:   NDF – INTEGEROutput
On exit: contains $\left({\mathbf{NCLASS}}-1-{\mathbf{NPEST}}\right)$, the degrees of freedom associated with the test.
11:   EVAL(NCLASS) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{EVAL}}\left(\mathit{i}\right)$ contains the expected frequency for the $\mathit{i}$th class, ${E}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
12:   CHISQI(NCLASS) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{CHISQI}}\left(\mathit{i}\right)$ contains the contribution from the $\mathit{i}$th class to the test statistic, that is, ${\left({O}_{\mathit{i}}-{E}_{\mathit{i}}\right)}^{2}/{E}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
13:   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, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).
Note: G08CGF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{NCLASS}}<2$.
${\mathbf{IFAIL}}=2$
 On entry, DIST is invalid.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{NPEST}}<0$, or ${\mathbf{NPEST}}\ge {\mathbf{NCLASS}}-1$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{IFREQ}}\left(\mathit{i}\right)<0.0$ for some $\mathit{i}$, for $\mathit{i}=1,2,\dots ,k$.
${\mathbf{IFAIL}}=5$
On entry, the elements of CB are not in ascending order. That is, ${\mathbf{CB}}\left(\mathit{i}\right)\le {\mathbf{CB}}\left(\mathit{i}-1\right)$ for some $\mathit{i}$, for $\mathit{i}=2,3,\dots ,k-1$.
${\mathbf{IFAIL}}=6$
On entry, ${\mathbf{DIST}}=\text{'E'}$, $\text{'C'}$ or $\text{'G'}$ and ${\mathbf{CB}}\left(1\right)<0.0$. No negative class boundary values are valid for the exponential, gamma or ${\chi }^{2}$-distributions.
${\mathbf{IFAIL}}=7$
 On entry, the values provided in PAR are invalid.
${\mathbf{IFAIL}}=8$
 On entry, with ${\mathbf{DIST}}=\text{'A'}$, ${\mathbf{PROB}}\left(i\right)\le 0.0$ for some $i$, for $i=1,2,\dots ,k$, or $\sum _{i=1}^{k}{\mathbf{PROB}}\left(i\right)\ne 1.0$.
${\mathbf{IFAIL}}=9$
An expected frequency is equal to zero when the observed frequency was not.
${\mathbf{IFAIL}}=10$
This is a warning that expected values for certain classes are less than $1.0$. This implies that we cannot be confident that the ${\chi }^{2}$-distribution is a good approximation to the distribution of the test statistic.
${\mathbf{IFAIL}}=11$
The solution obtained when calculating the probability for a certain class for the gamma or ${\chi }^{2}$-distribution did not converge in $600$ iterations. The solution may be an adequate approximation.

## 7  Accuracy

The computations are believed to be stable.

The time taken by G08CGF is dependent both on the distribution chosen and on the number of classes, $k$.

## 9  Example

This example applies the ${\chi }^{2}$ goodness-of-fit test to test whether there is evidence to suggest that a sample of $100$ randomly generated observations do not arise from a uniform distribution $U\left(0,1\right)$. The class intervals are calculated such that the interval $\left(0,1\right)$ is divided into five equal classes. The frequencies for each class are calculated using G01AEF.

### 9.1  Program Text

Program Text (g08cgfe.f90)

### 9.2  Program Data

Program Data (g08cgfe.d)

### 9.3  Program Results

Program Results (g08cgfe.r)