G08CGF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G08CGF

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.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G08CGF computes the test statistic for the χ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 χ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 x1,x2,,xn, drawn from a random variable X, and that the data has been grouped into k classes,
xc1, ci-1<xci, i=2,3,,k-1, x>ck-1,  
then the χ2 goodness-of-fit test statistic is defined by
X2=i=1k Oi-Ei 2Ei,  
where Oi is the observed frequency of the ith class, and Ei is the expected frequency of the ith class.
The expected frequencies are computed as
Ei=pi×n,  
where pi is the probability that X lies in the ith class, that is
p1=PXc1, pi=Pci-1<Xci, i=2,3,,k-1, pk=PX>ck-1.  
These probabilities are either taken from a common probability distribution or are supplied by you. The available probability distributions within this routine are:
You must supply the frequencies and classes. Given a set of data and classes the frequencies may be calculated using G01AEF.
G08CGF returns the χ2 test statistic, X2, together with its degrees of freedom and the upper tail probability from the χ2-distribution associated with the test statistic. Note that the use of the χ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: NCLASS2.
2:     IFREQNCLASS – INTEGER arrayInput
On entry: IFREQi must specify the frequency of the ith class, Oi, for i=1,2,,k.
Constraint: IFREQi0, for i=1,2,,k.
3:     CBNCLASS-1 – REAL (KIND=nag_wp) arrayInput
On entry: CBi must specify the upper boundary value for the ith class, for i=1,2,,k-1.
Constraint: CB1<CB2<<CBNCLASS-1. For the exponential, gamma and χ2-distributions CB10.0.
4:     DIST – CHARACTER(1)Input
On entry: indicates for which distribution the test is to be carried out.
DIST='N'
The Normal distribution is used.
DIST='U'
The uniform distribution is used.
DIST='E'
The exponential distribution is used.
DIST='C'
The χ2-distribution is used.
DIST='G'
The gamma distribution is used.
DIST='A'
You must supply the class probabilities in the array PROB.
Constraint: DIST='N', 'U', 'E', 'C', 'G' or 'A'.
5:     PAR2 – 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., DIST='A') the array PAR is not referenced.
If a Normal distribution is used then PAR1 and PAR2 must contain the mean, μ, and the variance, σ2, respectively.
If a uniform distribution is used then PAR1 and PAR2 must contain the boundaries a and b respectively.
If an exponential distribution is used then PAR1 must contain the parameter λ. PAR2 is not used.
If a χ2-distribution is used then PAR1 must contain the number of degrees of freedom. PAR2 is not used.
If a gamma distribution is used PAR1 and PAR2 must contain the parameters α and β respectively.
Constraints:
  • if DIST='N', PAR2>0.0;
  • if DIST='U', PAR1<PAR2 and PAR1CB1 and PAR2CBNCLASS-1;
  • if DIST='E', PAR1>0.0;
  • if DIST='C', PAR1>0.0;
  • if DIST='G', PAR1>0.0 and PAR2>0.0.
6:     NPEST – INTEGERInput
On entry: the number of estimated parameters of the distribution.
Constraint: 0NPEST<NCLASS-1.
7:     PROBNCLASS – REAL (KIND=nag_wp) arrayInput
On entry: if you are supplying the probability distribution (i.e., DIST='A') then PROBi must contain the probability that X lies in the ith class.
If DIST'A', PROB is not referenced.
Constraint: if DIST='A', i=1kPROBi=1.0, PROBi>0.0, for i=1,2,,k.
8:     CHISQ – REAL (KIND=nag_wp)Output
On exit: the test statistic, X2, for the χ2 goodness-of-fit test.
9:     P – REAL (KIND=nag_wp)Output
On exit: the upper tail probability from the χ2-distribution associated with the test statistic, X2, and the number of degrees of freedom.
10:   NDF – INTEGEROutput
On exit: contains NCLASS-1-NPEST, the degrees of freedom associated with the test.
11:   EVALNCLASS – REAL (KIND=nag_wp) arrayOutput
On exit: EVALi contains the expected frequency for the ith class, Ei, for i=1,2,,k.
12:   CHISQINCLASS – REAL (KIND=nag_wp) arrayOutput
On exit: CHISQIi contains the contribution from the ith class to the test statistic, that is, Oi-Ei 2/Ei, for i=1,2,,k.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ 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​ 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 IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -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:
IFAIL=1
On entry,NCLASS<2.
IFAIL=2
On entry,DIST is invalid.
IFAIL=3
On entry,NPEST<0,
orNPESTNCLASS-1.
IFAIL=4
On entry,IFREQi<0.0 for some i, for i=1,2,,k.
IFAIL=5
On entry, the elements of CB are not in ascending order. That is, CBiCBi-1 for some i, for i=2,3,,k-1.
IFAIL=6
On entry, DIST='E', 'C' or 'G' and CB1<0.0. No negative class boundary values are valid for the exponential, gamma or χ2-distributions.
IFAIL=7
On entry,the values provided in PAR are invalid.
IFAIL=8
On entry,with DIST='A', PROBi0.0 for some i, for i=1,2,,k,
ori=1kPROBi1.0.
IFAIL=9
An expected frequency is equal to zero when the observed frequency was not.
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 χ2-distribution is a good approximation to the distribution of the test statistic.
IFAIL=11
The solution obtained when calculating the probability for a certain class for the gamma or χ2-distribution did not converge in 600 iterations. The solution may be an adequate approximation.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

The computations are believed to be stable.

8  Parallelism and Performance

Not applicable.

9  Further Comments

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

10  Example

This example applies the χ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 U0,1. The class intervals are calculated such that the interval 0,1 is divided into five equal classes. The frequencies for each class are calculated using G01AEF.

10.1  Program Text

Program Text (g08cgfe.f90)

10.2  Program Data

Program Data (g08cgfe.d)

10.3  Program Results

Program Results (g08cgfe.r)


G08CGF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015