# NAG Library Routine Document

## 1Purpose

g07ebf calculates a rank based (nonparametric) estimate and confidence interval for the difference in location between two independent populations.

## 2Specification

Fortran Interface
 Subroutine g07ebf ( n, x, m, y, wrk, iwrk,
 Integer, Intent (In) :: n, m Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: iwrk(3*n) Real (Kind=nag_wp), Intent (In) :: x(n), y(m), clevel Real (Kind=nag_wp), Intent (Out) :: theta, thetal, thetau, estcl, ulower, uupper, wrk(3*(m+n)) Character (1), Intent (In) :: method
#include nagmk26.h
 void g07ebf_ (const char *method, const Integer *n, const double x[], const Integer *m, const double y[], const double *clevel, double *theta, double *thetal, double *thetau, double *estcl, double *ulower, double *uupper, double wrk[], Integer iwrk[], Integer *ifail, const Charlen length_method)

## 3Description

Consider two random samples from two populations which have the same continuous distribution except for a shift in the location. Let the random sample, $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$, have distribution $F\left(x\right)$ and the random sample, $y={\left({y}_{1},{y}_{2},\dots ,{y}_{m}\right)}^{\mathrm{T}}$, have distribution $F\left(x-\theta \right)$.
g07ebf finds a point estimate, $\stackrel{^}{\theta }$, of the difference in location $\theta$ together with an associated confidence interval. The estimates are based on the ordered differences ${y}_{j}-{x}_{i}$. The estimate $\stackrel{^}{\theta }$ is defined by
 $θ^ = median yj-xi , i=1,2,…,n ; j=1,2,…,m .$
Let ${d}_{\mathit{k}}$, for $\mathit{k}=1,2,\dots ,nm$, denote the $nm$ (ascendingly) ordered differences ${y}_{\mathit{j}}-{x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$. Then
• if $nm$ is odd, $\stackrel{^}{\theta }={d}_{k}$ where $k=\left(nm-1\right)/2$;
• if $nm$ is even, $\stackrel{^}{\theta }=\left({d}_{k}+{d}_{k+1}\right)/2$ where $k=nm/2$.
This estimator arises from inverting the two sample Mann–Whitney rank test statistic, $U\left({\theta }_{0}\right)$, for testing the hypothesis that $\theta ={\theta }_{0}$. Thus $U\left({\theta }_{0}\right)$ is the value of the Mann–Whitney $U$ statistic for the two independent samples $\left\{\left({x}_{i}+{\theta }_{0}\right)\text{, for ​}i=1,2,\dots ,n\right\}$ and $\left\{{y}_{j}\text{, for ​}j=1,2,\dots ,m\right\}$. Effectively $U\left({\theta }_{0}\right)$ is a monotonically increasing step function of ${\theta }_{0}$ with
 $mean ​U=μ= nm2, varU=σ2 nmn+m+112.$
The estimate $\stackrel{^}{\theta }$ is the solution to the equation $U\left(\stackrel{^}{\theta }\right)=\mu$; two methods are available for solving this equation. These methods avoid the computation of all the ordered differences ${d}_{k}$; this is because for large $n$ and $m$ both the storage requirements and the computation time would be high.
The first is an exact method based on a set partitioning procedure on the set of all differences ${y}_{\mathit{j}}-{x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$. This is adapted from the algorithm proposed by Monahan (1984) for the computation of the Hodges–Lehmann estimator for a single population.
The second is an iterative algorithm, based on the Illinois method which is a modification of the regula falsi method, see McKean and Ryan (1977). This algorithm has proved suitable for the function $U\left({\theta }_{0}\right)$ which is asymptotically linear as a function of ${\theta }_{0}$.
The confidence interval limits are also based on the inversion of the Mann–Whitney test statistic.
Given a desired percentage for the confidence interval, $1-\alpha$, expressed as a proportion between $0.0$ and $1.0$ initial estimates of the upper and lower confidence limits for the Mann–Whitney $U$ statistic are found;
 $Ul=μ-0.5+σ×Φ-1α/2 Uu=μ+0.5+σ×Φ-11-α/2$
where ${\Phi }^{-1}$ is the inverse cumulative Normal distribution function.
${U}_{l}$ and ${U}_{u}$ are rounded to the nearest integer values. These estimates are refined using an exact method, without taking ties into account, if $n+m\le 40$ and $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(n,m\right)\le 30$ and a Normal approximation otherwise, to find ${U}_{l}$ and ${U}_{u}$ satisfying
 $PU≤Ul≤α/2 PU≤Ul+1>α/2$
and
 $PU≥Uu≤α /2 PU≥Uu- 1>α /2.$
The function $U\left({\theta }_{0}\right)$ is a monotonically increasing step function. It is the number of times a score in the second sample, ${y}_{j}$, precedes a score in the first sample, ${x}_{i}+\theta$, where we only count a half if a score in the second sample actually equals a score in the first.
Let ${U}_{l}=k$; then ${\theta }_{l}={d}_{k+1}$. This is the largest value ${\theta }_{l}$ such that $U\left({\theta }_{l}\right)={U}_{l}$.
Let ${U}_{u}=nm-k$; then ${\theta }_{u}={d}_{nm-k}$. This is the smallest value ${\theta }_{u}$ such that $U\left({\theta }_{u}\right)={U}_{u}$.
As in the case of $\stackrel{^}{\theta }$, these equations may be solved using either the exact or iterative methods to find the values ${\theta }_{l}$ and ${\theta }_{u}$.
Then $\left({\theta }_{l},{\theta }_{u}\right)$ is the confidence interval for $\theta$. The confidence interval is thus defined by those values of ${\theta }_{0}$ such that the null hypothesis, $\theta ={\theta }_{0}$, is not rejected by the Mann–Whitney two sample rank test at the $\left(100×\alpha \right)%$ level.

## 4References

Lehmann E L (1975) Nonparametrics: Statistical Methods Based on Ranks Holden–Day
McKean J W and Ryan T A (1977) Algorithm 516: An algorithm for obtaining confidence intervals and point estimates based on ranks in the two-sample location problem ACM Trans. Math. Software 10 183–185
Monahan J F (1984) Algorithm 616: Fast computation of the Hodges–Lehman location estimator ACM Trans. Math. Software 10 265–270

## 5Arguments

1:     $\mathbf{method}$ – Character(1)Input
On entry: specifies the method to be used.
${\mathbf{method}}=\text{'E'}$
The exact algorithm is used.
${\mathbf{method}}=\text{'A'}$
The iterative algorithm is used.
Constraint: ${\mathbf{method}}=\text{'E'}$ or $\text{'A'}$.
2:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the size of the first sample.
Constraint: ${\mathbf{n}}\ge 1$.
3:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the observations of the first sample, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
4:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the size of the second sample.
Constraint: ${\mathbf{m}}\ge 1$.
5:     $\mathbf{y}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the observations of the second sample, ${y}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
6:     $\mathbf{clevel}$ – Real (Kind=nag_wp)Input
On entry: the confidence interval required, $1-\alpha$; e.g., for a $95%$ confidence interval set ${\mathbf{clevel}}=0.95$.
Constraint: $0.0<{\mathbf{clevel}}<1.0$.
7:     $\mathbf{theta}$ – Real (Kind=nag_wp)Output
On exit: the estimate of the difference in the location of the two populations, $\stackrel{^}{\theta }$.
8:     $\mathbf{thetal}$ – Real (Kind=nag_wp)Output
On exit: the estimate of the lower limit of the confidence interval, ${\theta }_{l}$.
9:     $\mathbf{thetau}$ – Real (Kind=nag_wp)Output
On exit: the estimate of the upper limit of the confidence interval, ${\theta }_{u}$.
10:   $\mathbf{estcl}$ – Real (Kind=nag_wp)Output
On exit: an estimate of the actual percentage confidence of the interval found, as a proportion between $\left(0.0,1.0\right)$.
11:   $\mathbf{ulower}$ – Real (Kind=nag_wp)Output
On exit: the value of the Mann–Whitney $U$ statistic corresponding to the lower confidence limit, ${U}_{l}$.
12:   $\mathbf{uupper}$ – Real (Kind=nag_wp)Output
On exit: the value of the Mann–Whitney $U$ statistic corresponding to the upper confidence limit, ${U}_{u}$.
13:   $\mathbf{wrk}\left(3×\left({\mathbf{m}}+{\mathbf{n}}\right)\right)$ – Real (Kind=nag_wp) arrayWorkspace
14:   $\mathbf{iwrk}\left(3×{\mathbf{n}}\right)$ – Integer arrayWorkspace
15:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. 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 $-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 argument, 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).

## 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{method}}\ne \text{'E'}$ or $\text{'A'}$, or ${\mathbf{n}}<1$, or ${\mathbf{m}}<1$, or ${\mathbf{clevel}}\le 0.0$, or ${\mathbf{clevel}}\ge 1.0$.
${\mathbf{ifail}}=2$
Each sample consists of identical values. All estimates are set to the common difference between the samples.
${\mathbf{ifail}}=3$
For at least one of the estimates $\stackrel{^}{\theta }$, ${\theta }_{l}$ and ${\theta }_{u}$, the underlying iterative algorithm (when ${\mathbf{method}}=\text{'A'}$) failed to converge. This is an unlikely exit but the estimate should still be a reasonable approximation.
${\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

g07ebf should return results accurate to five significant figures in the width of the confidence interval, that is the error for any one of the three estimates should be less than $0.00001×\left({\mathbf{thetau}}-{\mathbf{thetal}}\right)$.

## 8Parallelism and Performance

g07ebf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g07ebf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken increases with the sample sizes $n$ and $m$.

## 10Example

The following program calculates a 95% confidence interval for the difference in location between the two populations from which the two samples of sizes $50$ and $100$ are drawn respectively.

### 10.1Program Text

Program Text (g07ebfe.f90)

### 10.2Program Data

Program Data (g07ebfe.d)

### 10.3Program Results

Program Results (g07ebfe.r)

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