Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_univar_robust_2var_ci (g07eb)

## Purpose

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

## Syntax

[theta, thetal, thetau, estcl, ulower, uupper, ifail] = g07eb(method, x, y, clevel, 'n', n, 'm', m)
[theta, thetal, thetau, estcl, ulower, uupper, ifail] = nag_univar_robust_2var_ci(method, x, y, clevel, 'n', n, 'm', m)

## Description

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)$.
nag_univar_robust_2var_ci (g07eb) 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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{method}$ – string (length ≥ 1)
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:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The observations of the first sample, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     $\mathrm{y}\left({\mathbf{m}}\right)$ – double array
The observations of the second sample, ${y}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,m$.
4:     $\mathrm{clevel}$ – double scalar
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$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the size of the first sample.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array y.
$m$, the size of the second sample.
Constraint: ${\mathbf{m}}\ge 1$.

### Output Parameters

1:     $\mathrm{theta}$ – double scalar
The estimate of the difference in the location of the two populations, $\stackrel{^}{\theta }$.
2:     $\mathrm{thetal}$ – double scalar
The estimate of the lower limit of the confidence interval, ${\theta }_{l}$.
3:     $\mathrm{thetau}$ – double scalar
The estimate of the upper limit of the confidence interval, ${\theta }_{u}$.
4:     $\mathrm{estcl}$ – double scalar
An estimate of the actual percentage confidence of the interval found, as a proportion between $\left(0.0,1.0\right)$.
5:     $\mathrm{ulower}$ – double scalar
The value of the Mann–Whitney $U$ statistic corresponding to the lower confidence limit, ${U}_{l}$.
6:     $\mathrm{uupper}$ – double scalar
The value of the Mann–Whitney $U$ statistic corresponding to the upper confidence limit, ${U}_{u}$.
7:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\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$.
W  ${\mathbf{ifail}}=2$
Each sample consists of identical values. All estimates are set to the common difference between the samples.
W  ${\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$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

nag_univar_robust_2var_ci (g07eb) 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)$.

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

## Example

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.
```function g07eb_example

fprintf('g07eb example results\n\n');

x = [-0.582;  0.157; -0.523; -0.769;  2.338;  1.664; -0.981;  1.549;
1.131; -0.460; -0.484;  1.932;  0.306; -0.602; -0.979;  0.132;
0.256; -0.094;  1.065; -1.084; -0.969; -0.524;  0.239;  1.512;
-0.782; -0.252; -1.163;  1.376;  1.674;  0.831;  1.478; -1.486;
-0.808; -0.429; -2.002;  0.482; -1.584; -0.105;  0.429;  0.568;
0.944;  2.558; -1.801;  0.242;  0.763; -0.461; -1.497; -1.353;
0.301;  1.941];
y = [ 1.995;  0.007;  0.997;  1.089;  2.004;  0.171;  0.294;  2.448;
0.214;  0.773;  2.960;  0.025;  0.638;  0.937; -0.568; -0.711;
0.931;  2.601;  1.121; -0.251; -0.050;  1.341;  2.282;  0.745;
1.633;  0.944;  2.370;  0.293;  0.895;  0.938;  0.199;  0.812;
1.253;  0.590;  1.522; -0.685;  1.259;  0.571;  1.579;  0.568;
0.381;  0.829;  0.277;  0.656;  2.497;  1.779;  1.922; -0.174;
2.132;  2.793;  0.102;  1.569;  1.267;  0.490;  0.077;  1.366;
0.056;  0.605;  0.628;  1.650;  0.104;  2.194;  2.869; -0.171;
-0.598;  2.134;  0.917;  0.630;  0.209;  1.328;  0.368;  0.756;
2.645;  1.161;  0.347;  0.920;  1.256; -0.052;  1.474;  0.510;
1.386;  3.550;  1.392; -0.358;  1.938;  1.727; -0.372;  0.911;
0.499;  0.066;  1.467;  1.898;  1.145;  0.501;  2.230;  0.212;
0.536;  1.690;  1.086;  0.494];

method = 'Approx';
clevel = 0.95;

[theta, thetal, thetau, estcl, ulower, uupper, ifail] = ...
g07eb(method, x, y, clevel);

fprintf(' Location estimator     Confidence Interval\n\n');
fprintf('%13.4f%12s%7.4f,%7.4f )\n\n', theta, '(', thetal, thetau);
fprintf(' Corresponding Mann-Whitney statistics\n\n');
fprintf('  Lower : %8.2f\n', ulower);
fprintf('  Upper : %8.2f\n', uupper);

```
```g07eb example results

Location estimator     Confidence Interval

0.9505           ( 0.5650, 1.3050 )

Corresponding Mann-Whitney statistics

Lower :  2007.00
Upper :  2993.00
```