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_stat_normal_scores_exact (g01da)

## Purpose

nag_stat_normal_scores_exact (g01da) computes a set of Normal scores, i.e., the expected values of an ordered set of independent observations from a Normal distribution with mean $0.0$ and standard deviation $1.0$.

## Syntax

[pp, errest, ifail] = g01da(n, etol)
[pp, errest, ifail] = nag_stat_normal_scores_exact(n, etol)

## Description

If a sample of $n$ observations from any distribution (which may be denoted by ${x}_{1},{x}_{2},\dots ,{x}_{n}$), is sorted into ascending order, the $r$th smallest value in the sample is often referred to as the $r$th ‘order statistic’, sometimes denoted by ${x}_{\left(r\right)}$ (see Kendall and Stuart (1969)).
The order statistics therefore have the property
 $x1≤x2≤…≤xn.$
(If $n=2r+1$, ${x}_{r+1}$ is the sample median.)
For samples originating from a known distribution, the distribution of each order statistic in a sample of given size may be determined. In particular, the expected values of the order statistics may be found by integration. If the sample arises from a Normal distribution, the expected values of the order statistics are referred to as the ‘Normal scores’. The Normal scores provide a set of reference values against which the order statistics of an actual data sample of the same size may be compared, to provide an indication of Normality for the sample (see nag_stat_plot_scatter_normal (g01ah)). Normal scores have other applications; for instance, they are sometimes used as alternatives to ranks in nonparametric testing procedures.
nag_stat_normal_scores_exact (g01da) computes the $r$th Normal score for a given sample size $n$ as
 $Exr=∫-∞∞xrdGr,$
where
 $dGr=Arr- 1 1-Arn-r d Ar β r,n-r+ 1 , Ar=12π ∫-∞xre-t2/2 dt, r= 1,2,…,n,$
and $\beta$ denotes the complete beta function.
The function attempts to evaluate the scores so that the estimated error in each score is less than the value etol specified by you. All integrations are performed in parallel and arranged so as to give good speed and reasonable accuracy.

## References

Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, the size of the set.
Constraint: ${\mathbf{n}}>0$.
2:     $\mathrm{etol}$ – double scalar
The maximum value for the estimated absolute error in the computed scores.
Constraint: ${\mathbf{etol}}>0.0$.

None.

### Output Parameters

1:     $\mathrm{pp}\left({\mathbf{n}}\right)$ – double array
The Normal scores. ${\mathbf{pp}}\left(\mathit{i}\right)$ contains the value $E\left({x}_{\left(\mathit{i}\right)}\right)$, for $\mathit{i}=1,2,\dots ,n$.
2:     $\mathrm{errest}$ – double scalar
A computed estimate of the maximum error in the computed scores (see Accuracy).
3:     $\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{n}}<1$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{etol}}\le 0.0$.
W  ${\mathbf{ifail}}=3$
The function was unable to estimate the scores with estimated error less than etol. The best result obtained is returned together with the associated value of errest.
${\mathbf{ifail}}=4$
 On entry, if n is even, $\mathit{iw}<3×{\mathbf{n}}/2$; or if n is odd, $\mathit{iw}<3×\left({\mathbf{n}}-1\right)/2$.
${\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

Errors are introduced by evaluation of the functions $d{G}_{r}$ and errors in the numerical integration process. Errors are also introduced by the approximation of the true infinite range of integration by a finite range $\left[a,b\right]$ but $a$ and $b$ are chosen so that this effect is of lower order than that of the other two factors. In order to estimate the maximum error the functions $d{G}_{r}$ are also integrated over the range $\left[a,b\right]$. nag_stat_normal_scores_exact (g01da) returns the estimated maximum error as
 $errest=maxr maxa,b× ∫ab dGr-1.0 .$

The time taken by nag_stat_normal_scores_exact (g01da) depends on etol and n. For a given value of etol the timing varies approximately linearly with n.

## Example

The program below generates the Normal scores for samples of size $5$, $10$, $15$, and prints the scores and the computed error estimates.
```function g01da_example

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

n = int64(15);
etol = 0.001;

[pp, errest, ifail] = g01da(n, etol);

fprintf('Set size                = %5d\n', n);
fprintf('Error tolerance (input) = %13.3e\n', etol);
fprintf('Error estimate (output) = %13.3e\n', errest);
fprintf('Normal scores\n');
fprintf('   %10.3f%10.3f%10.3f%10.3f%10.3f\n',pp(1:n));

g01da_plot;

function g01da_plot
% This produces a Q-Q plot for a randomly generated set of data.
% The normal scores have been calculated using g01da and the sample
% quantiles obtained by sorting the observed data using m01ca.

% Initialize the base generator
seed = [int64(2324213)];
genid = int64(1);
subid = int64(1);

[state, ifail] = g05kf( ...
genid, subid, seed);

% Generate 50 variates from a Normal distribution
n = int64(50);
[state, x, ifail] = g05sk( ...
n, 0, 1, state);
% Sort x
m1 = int64(1);
[x, ifail] = m01ca(x, m1, 'Ascending');

% Calculate normal scores
etol = 0.001;
[pp, errest, ifail] = g01da(n, etol);

fig1 = figure;
hold on
plot(pp,x,'+r',[-2.25 2.25],[-2.25 2.25],'black');
axis([-2.5 2.5 -3 2.5]);
xlabel('Normal scores');
ylabel('sample Quantiles');
title({'Q-Q plot for a random set of data','using exact normal scores'});
hold off;
```
```g01da example results

Set size                =    15
Error tolerance (input) =     1.000e-03
Error estimate (output) =     2.218e-08
Normal scores
-1.736    -1.248    -0.948    -0.715    -0.516
-0.335    -0.165     0.000     0.165     0.335
0.516     0.715     0.948     1.248     1.736
```
This shows a Q-Q plot for a randomly generated set of data. The normal scores have been calculated using nag_stat_normal_scores_exact (g01da) and the sample quantiles obtained by sorting the observed data using nag_sort_realvec_sort (m01ca). A reference line at $y=x$ is also shown.