G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG07DDF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G07DDF calculates the trimmed and Winsorized means of a sample and estimates of the variances of the two means.

## 2  Specification

 SUBROUTINE G07DDF ( N, X, ALPHA, TMEAN, WMEAN, TVAR, WVAR, K, SX, IFAIL)
 INTEGER N, K, IFAIL REAL (KIND=nag_wp) X(N), ALPHA, TMEAN, WMEAN, TVAR, WVAR, SX(N)

## 3  Description

G07DDF 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 .$
G07DDF 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 .$

## 4  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

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 2$.
2:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sample observations, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     ALPHA – REAL (KIND=nag_wp)Input
On entry: $\alpha$, the proportion of observations to be trimmed at each end of the sorted sample.
Constraint: $0.0\le {\mathbf{ALPHA}}<0.5$.
4:     TMEAN – REAL (KIND=nag_wp)Output
On exit: the $\alpha$-trimmed mean, ${\stackrel{-}{x}}_{t}$.
5:     WMEAN – REAL (KIND=nag_wp)Output
On exit: the $\alpha$-Winsorized mean, ${\stackrel{-}{x}}_{w}$.
6:     TVAR – REAL (KIND=nag_wp)Output
On exit: contains an estimate of the variance of the trimmed mean.
7:     WVAR – REAL (KIND=nag_wp)Output
On exit: contains an estimate of the variance of the Winsorized mean.
8:     K – INTEGEROutput
On exit: contains the number of observations trimmed at each end, $k$.
9:     SX(N) – REAL (KIND=nag_wp) arrayOutput
On exit: contains the sample observations sorted into ascending order.
10:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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 parameter, 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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}}\le 1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{ALPHA}}<0.0$, or ${\mathbf{ALPHA}}\ge 0.5$.

## 7  Accuracy

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

The time taken is proportional to $n$.

## 9  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.

### 9.1  Program Text

Program Text (g07ddfe.f90)

### 9.2  Program Data

Program Data (g07ddfe.d)

### 9.3  Program Results

Program Results (g07ddfe.r)