# NAG Library Routine Document

## 1Purpose

g08baf performs Mood's and David's tests for dispersion differences between two independent samples of possibly unequal size.

## 2Specification

Fortran Interface
 Subroutine g08baf ( x, n, n1, r, w, v, pw, pv,
 Integer, Intent (In) :: n, n1, itest Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Out) :: r(n), w, v, pw, pv
#include <nagmk26.h>
 void g08baf_ (const double x[], const Integer *n, const Integer *n1, double r[], const Integer *itest, double *w, double *v, double *pw, double *pv, Integer *ifail)

## 3Description

Mood's and David's tests investigate the difference between the dispersions of two independent samples of sizes ${n}_{1}$ and ${n}_{2}$, denoted by
 $x1,x2,…,xn1$
and
 $xn1+ 1,xn1+ 2,…,xn, n=n1+n2.$
The hypothesis under test, ${H}_{0}$, often called the null hypothesis, is that the dispersion difference is zero, and this is to be tested against a one- or two-sided alternative hypothesis ${H}_{1}$ (see below).
Both tests are based on the rankings of the sample members within the pooled sample formed by combining both samples. If there is some difference in dispersion, more of the extreme ranks will tend to be found in one sample than in the other.
Let the rank of ${x}_{\mathit{i}}$ be denoted by ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
(a) Mood's test.
The test statistic $W=\sum _{i=1}^{{n}_{1}}{\left({r}_{i}-\frac{n+1}{2}\right)}^{2}$ is found.
$W$ is the sum of squared deviations from the average rank in the pooled sample. For large $n$, $W$ approaches normality, and so an approximation, ${p}_{w}$, to the probability of observing $W$ not greater than the computed value, may be found.
g08baf returns $W$ and ${p}_{w}$ if Mood's test is selected.
(b) David's test.
The disadvantage of Mood's test is that it assumes that the means of the two samples are equal. If this assumption is unjustified a high value of $W$ could merely reflect the difference in means. David's test reduces this effect by using the variance of the ranks of the first sample about their mean rank, rather than the overall mean rank.
The test statistic for David's test is
 $V=1n1-1 ∑i=1n1 ri-r- 2$
where
 $r-=∑i= 1n1rin1.$
For large $n$, $V$ approaches normality, enabling an approximate probability ${p}_{v}$ to be computed, similarly to ${p}_{w}$.
g08baf returns $V$ and ${p}_{v}$ if David's test is selected.
Suppose that a significance test of a chosen size $\alpha$ is to be performed (i.e., $\alpha$ is the probability of rejecting ${H}_{0}$ when ${H}_{0}$ is true; typically $\alpha$ is a small quantity such as $0.05$ or $0.01$).
The returned value $p$ ($={p}_{v}$ or ${p}_{w}$) can be used to perform a significance test, against various alternative hypotheses ${H}_{1}$, as follows.
 (i) ${H}_{1}$: dispersions are unequal. ${H}_{0}$ is rejected if $2×\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(p,1-p\right)<\alpha$. (ii) ${H}_{1}$: dispersion of sample $1>\text{}$ dispersion of sample $2$. ${H}_{0}$ is rejected if $1-p<\alpha$. (iii) ${H}_{1}$: dispersion of sample $2>\text{}$ dispersion of sample $1$. ${H}_{0}$ is rejected if $p<\alpha$.
Cooper B E (1975) Statistics for Experimentalists Pergamon Press

## 5Arguments

1:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the first ${n}_{1}$ elements of x must be set to the data values in the first sample, and the next ${n}_{2}$ ($\text{}={\mathbf{n}}-{n}_{1}$) elements to the data values in the second sample.
2:     $\mathbf{n}$ – IntegerInput
On entry: the total of the two sample sizes, $n$ ($\text{}={n}_{1}+{n}_{2}$).
Constraint: ${\mathbf{n}}>2$.
3:     $\mathbf{n1}$ – IntegerInput
On entry: the size of the first sample, ${n}_{1}$.
Constraint: $1<{\mathbf{n1}}<{\mathbf{n}}$.
4:     $\mathbf{r}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the ranks ${r}_{\mathit{i}}$, assigned to the data values ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
5:     $\mathbf{itest}$ – IntegerInput
On entry: the test(s) to be carried out.
${\mathbf{itest}}=0$
Both Mood's and David's tests.
${\mathbf{itest}}=1$
David's test only.
${\mathbf{itest}}=2$
Mood's test only.
Constraint: ${\mathbf{itest}}=0$, $1$ or $2$.
6:     $\mathbf{w}$ – Real (Kind=nag_wp)Output
On exit: Mood's test statistic, $W$, if requested.
7:     $\mathbf{v}$ – Real (Kind=nag_wp)Output
On exit: David's test statistic, $V$, if requested.
8:     $\mathbf{pw}$ – Real (Kind=nag_wp)Output
On exit: the lower tail probability, ${p}_{w}$, corresponding to the value of $W$, if Mood's test was requested.
9:     $\mathbf{pv}$ – Real (Kind=nag_wp)Output
On exit: the lower tail probability, ${p}_{v}$, corresponding to the value of $V$, if David's test was requested.
10:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  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{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $1<{\mathbf{n1}}<{\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{itest}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{itest}}=0$, $1$ or $2$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

All computations are believed to be stable. The statistics $V$ and $W$ should be accurate enough for all practical uses.

## 8Parallelism and Performance

g08baf is not threaded in any implementation.

The time taken by g08baf is small, and increases with $n$.

## 10Example

This example is taken from page 280 of Cooper (1975). The data consists of two samples of six observations each. Both Mood's and David's test statistics and significances are computed. Note that Mood's statistic is inflated owing to the difference in location of the two samples, the median ranks differing by a factor of two.

### 10.1Program Text

Program Text (g08bafe.f90)

### 10.2Program Data

Program Data (g08bafe.d)

### 10.3Program Results

Program Results (g08bafe.r)