# NAG Library Routine Document

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g07ddf ( n, x, tvar, wvar, k, sx,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: k Real (Kind=nag_wp), Intent (In) :: x(n), alpha Real (Kind=nag_wp), Intent (Out) :: tmean, wmean, tvar, wvar, sx(n)
#include nagmk26.h
 void g07ddf_ (const Integer *n, const double x[], const double *alpha, double *tmean, double *wmean, double *tvar, double *wvar, Integer *k, double sx[], Integer *ifail)

## 3Description

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

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
2:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the sample observations, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     $\mathbf{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:     $\mathbf{tmean}$ – Real (Kind=nag_wp)Output
On exit: the $\alpha$-trimmed mean, ${\stackrel{-}{x}}_{t}$.
5:     $\mathbf{wmean}$ – Real (Kind=nag_wp)Output
On exit: the $\alpha$-Winsorized mean, ${\stackrel{-}{x}}_{w}$.
6:     $\mathbf{tvar}$ – Real (Kind=nag_wp)Output
On exit: contains an estimate of the variance of the trimmed mean.
7:     $\mathbf{wvar}$ – Real (Kind=nag_wp)Output
On exit: contains an estimate of the variance of the Winsorized mean.
8:     $\mathbf{k}$ – IntegerOutput
On exit: contains the number of observations trimmed at each end, $k$.
9:     $\mathbf{sx}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: contains the sample observations sorted into ascending order.
10:   $\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{n}}\le 1$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{alpha}}<0.0$, or ${\mathbf{alpha}}\ge 0.5$.
${\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

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

## 8Parallelism and Performance

g07ddf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
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 is proportional to $n$.

## 10Example

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.

### 10.1Program Text

Program Text (g07ddfe.f90)

### 10.2Program Data

Program Data (g07ddfe.d)

### 10.3Program Results

Program Results (g07ddfe.r)

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