# NAG FL Interfaceg07ddf (robust_​1var_​trimmed)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 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 <nag.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)
The routine may be called by the names g07ddf or nagf_univar_robust_1var_trimmed.

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

## 4References

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}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
2: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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, ${\overline{x}}_{t}$.
5: $\mathbf{wmean}$Real (Kind=nag_wp) Output
On exit: the $\alpha$-Winsorized mean, ${\overline{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}$Integer Output
On exit: contains the number of observations trimmed at each end, $k$.
9: $\mathbf{sx}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: contains the sample observations sorted into ascending order.
10: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\le 1$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{alpha}}=⟨\mathit{\text{value}}⟩$.
Constraint: $0.0\le {\mathbf{alpha}}<0.5$.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface 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)