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_1var_trimmed (g07dd)

## Purpose

nag_univar_robust_1var_trimmed (g07dd) calculates the trimmed and Winsorized means of a sample and estimates of the variances of the two means.

## Syntax

[tmean, wmean, tvar, wvar, k, sx, ifail] = g07dd(x, alpha, 'n', n)
[tmean, wmean, tvar, wvar, k, sx, ifail] = nag_univar_robust_1var_trimmed(x, alpha, 'n', n)

## Description

nag_univar_robust_1var_trimmed (g07dd) calculates the $\alpha$-trimmed mean and $\alpha$-Winsorized mean for a given $\alpha$, as described below.
Let ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$ represent the $n$ sample observations sorted into ascending order. Let $k=\left[\alpha n\right]$ where $\left[y\right]$ represents the integer nearest to $y$; if $2k=n$ then $k$ is reduced by $1$.
Then the trimmed mean is defined as:
 $x-t = 1 n-2k ∑ i=k+1 n-k xi ,$
and the Winsorized mean is defined as:
 $x-w = 1n ∑ i=k+ 1 n-k xi + k ⁢ x k+1 + k ⁢ x n-k .$
nag_univar_robust_1var_trimmed (g07dd) then calculates the Winsorized variance about the trimmed and Winsorized means respectively and divides by $n$ to obtain estimates of the variances of the above two means.
Thus we have;
 $Estimate of ​ var x-t = 1n2 ∑ i=k+1 n-k xi - x-t 2 + k xk+1 - x-t 2 + k xn-k - x-t 2$
and
 $Estimate of ​ var x-w = 1 n2 ∑ i=k+ 1 n-k xi - x-w 2 + k xk+ 1 - x-w 2 + k xn-k - x-w 2 .$

## References

Hampel F R, Ronchetti E M, Rousseeuw P J and Stahel W A (1986) Robust Statistics. The Approach Based on Influence Functions Wiley
Huber P J (1981) Robust Statistics Wiley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The sample observations, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:     $\mathrm{alpha}$ – double scalar
$\alpha$, the proportion of observations to be trimmed at each end of the sorted sample.
Constraint: $0.0\le {\mathbf{alpha}}<0.5$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.

### Output Parameters

1:     $\mathrm{tmean}$ – double scalar
The $\alpha$-trimmed mean, ${\stackrel{-}{x}}_{t}$.
2:     $\mathrm{wmean}$ – double scalar
The $\alpha$-Winsorized mean, ${\stackrel{-}{x}}_{w}$.
3:     $\mathrm{tvar}$ – double scalar
Contains an estimate of the variance of the trimmed mean.
4:     $\mathrm{wvar}$ – double scalar
Contains an estimate of the variance of the Winsorized mean.
5:     $\mathrm{k}$int64int32nag_int scalar
Contains the number of observations trimmed at each end, $k$.
6:     $\mathrm{sx}\left({\mathbf{n}}\right)$ – double array
Contains the sample observations sorted into ascending order.
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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 1$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{alpha}}<0.0$, or ${\mathbf{alpha}}\ge 0.5$.
${\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

The results should be accurate to within a small multiple of machine precision.

The time taken is proportional to $n$.

## Example

The following program finds the $\alpha$-trimmed mean and $\alpha$-Winsorized mean for a sample of $16$ observations where $\alpha =0.15$. The estimates of the variances of the above two means are also calculated.
```function g07dd_example

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

x = [26; 12;  9;  2;  5;  6;  8; 14;
7;  3;  1; 11; 10;  4; 17; 21];
alpha = 0.15;

[tmean, wmean, tvar, wvar, k, sx, ifail] = ...
g07dd(x, alpha);

% Calculate proportion of data cut
propn = 100*(1-2*double(k)/numel(x));

fprintf('Statistics from middle %6.2f%% of data\n\n',propn);
fprintf('               Trimmed-mean = %11.4f\n', tmean);
fprintf('   Variance of Trimmed-mean = %11.4f\n\n', tvar);
fprintf('            Winsorized-mean = %11.4f\n', wmean);
fprintf('Variance of Winsorized-mean = %11.4f\n', wvar);

```
```g07dd example results

Statistics from middle  75.00% of data

Trimmed-mean =      8.8333
Variance of Trimmed-mean =      1.5434

Winsorized-mean =      9.1250
Variance of Winsorized-mean =      1.5381
```