# NAG CL Interfaceg02hdc (robustm_​user)

## 1Purpose

g02hdc performs bounded influence regression ($M$-estimates) using an iterative weighted least squares algorithm.

## 2Specification

 #include
void  g02hdc (Nag_OrderType order,
 double (*chi)(double t, Nag_Comm *comm),
 double (*psi)(double t, Nag_Comm *comm),
double psip0, double beta, Nag_RegType regtype, Nag_SigmaEst sigma_est, Integer n, Integer m, double x[], Integer pdx, double y[], double wgt[], double theta[], Integer *k, double *sigma, double rs[], double tol, double eps, Integer maxit, Integer nitmon, const char *outfile, Integer *nit, Nag_Comm *comm, NagError *fail)
The function may be called by the names: g02hdc, nag_correg_robustm_user or nag_robust_m_regsn_user_fn.

## 3Description

For the linear regression model
 $y=Xθ+ε,$
 where $y$ is a vector of length $n$ of the dependent variable, $X$ is an $n$ by $m$ matrix of independent variables of column rank $k$, $\theta$ is a vector of length $m$ of unknown parameters, and $\epsilon$ is a vector of length $n$ of unknown errors with var $\left({\epsilon }_{i}\right)={\sigma }^{2}$,
g02hdc calculates the M-estimates given by the solution, $\stackrel{^}{\theta }$, to the equation
 $∑i=1nψri/σwiwixij=0, j=1,2,…,m,$ (1)
 where ${r}_{i}$ is the $i$th residual, i.e., the $i$th element of the vector $r=y-X\stackrel{^}{\theta }$, $\psi$ is a suitable weight function, ${w}_{i}$ are suitable weights such as those that can be calculated by using output from g02hbc, and $\sigma$ may be estimated at each iteration by the median absolute deviation of the residuals $\stackrel{^}{\sigma }={\mathrm{med}}_{i}\left[\left|{r}_{i}\right|\right]/{\beta }_{1}$
or as the solution to
 $∑i=1nχri/σ^wiwi2=n-kβ2$
for a suitable weight function $\chi$, where ${\beta }_{1}$ and ${\beta }_{2}$ are constants, chosen so that the estimator of $\sigma$ is asymptotically unbiased if the errors, ${\epsilon }_{i}$, have a Normal distribution. Alternatively $\sigma$ may be held at a constant value.
The above describes the Schweppe type regression. If the ${w}_{i}$ are assumed to equal $1$ for all $i$, then Huber type regression is obtained. A third type, due to Mallows, replaces (1) by
 $∑i=1nψri/σwixij=0, j=1,2,…,m.$
This may be obtained by use of the transformations
 $wi* ←wi yi* ←yiwi xij* ←xijwi, j= 1,2,…,m$
(see Marazzi (1987)).
The calculation of the estimates of $\theta$ can be formulated as an iterative weighted least squares problem with a diagonal weight matrix $G$ given by
 $Gii= ψri/σwi ri/σwi , ri≠0 ψ′0, ri=0. .$
The value of $\theta$ at each iteration is given by the weighted least squares regression of $y$ on $X$. This is carried out by first transforming the $y$ and $X$ by
 $y~i =yiGii x~ij =xijGii, j=1,2,…,m$
and then using a least squares solver. If $X$ is of full column rank then an orthogonal-triangular ($QR$) decomposition is used; if not, a singular value decomposition is used.
Observations with zero or negative weights are not included in the solution.
Note:  there is no explicit provision in the function for a constant term in the regression model. However, the addition of a dummy variable whose value is $1.0$ for all observations will produce a value of $\stackrel{^}{\theta }$ corresponding to the usual constant term.
g02hdc is based on routines in ROBETH, see Marazzi (1987).

## 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
Marazzi A (1987) Subroutines for robust and bounded influence regression in ROBETH Cah. Rech. Doc. IUMSP, No. 3 ROB 2 Institut Universitaire de Médecine Sociale et Préventive, Lausanne

## 5Arguments

1: $\mathbf{order}$Nag_OrderType Input
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.1.3 in the Introduction to the NAG Library CL Interface for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2: $\mathbf{chi}$function, supplied by the user External Function
If ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$, chi must return the value of the weight function $\chi$ for a given value of its argument. The value of $\chi$ must be non-negative.
The specification of chi is:
 double chi (double t, Nag_Comm *comm)
1: $\mathbf{t}$double Input
On entry: the argument for which chi must be evaluated.
2: $\mathbf{comm}$Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to chi.
userdouble *
iuserInteger *
pPointer
The type Pointer will be void *. Before calling g02hdc you may allocate memory and initialize these pointers with various quantities for use by chi when called from g02hdc (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
Note: chi should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g02hdc. If your code inadvertently does return any NaNs or infinities, g02hdc is likely to produce unexpected results.
chi is required only if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$, otherwise it can be specified as a pointer with $0$ value.
3: $\mathbf{psi}$function, supplied by the user External Function
psi must return the value of the weight function $\psi$ for a given value of its argument.
The specification of psi is:
 double psi (double t, Nag_Comm *comm)
1: $\mathbf{t}$double Input
On entry: the argument for which psi must be evaluated.
2: $\mathbf{comm}$Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to psi.
userdouble *
iuserInteger *
pPointer
The type Pointer will be void *. Before calling g02hdc you may allocate memory and initialize these pointers with various quantities for use by psi when called from g02hdc (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
Note: psi should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by g02hdc. If your code inadvertently does return any NaNs or infinities, g02hdc is likely to produce unexpected results.
4: $\mathbf{psip0}$double Input
On entry: the value of ${\psi }^{\prime }\left(0\right)$.
5: $\mathbf{beta}$double Input
On entry: if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$, beta must specify the value of ${\beta }_{1}$.
For Huber and Schweppe type regressions, ${\beta }_{1}$ is the $75$th percentile of the standard Normal distribution (see g01fac). For Mallows type regression ${\beta }_{1}$ is the solution to
 $1n∑i=1nΦβ1/wi=0.75,$
where $\Phi$ is the standard Normal cumulative distribution function (see s15abc).
If ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$, beta must specify the value of ${\beta }_{2}$.
 $β2= ∫-∞∞χzϕzdz, in the Huber case; β2= 1n∑i=1nwi∫-∞∞χzϕzdz, in the Mallows case; β2= 1n∑i=1nwi2∫-∞∞χz/wiϕzdz, in the Schweppe case;$
where $\varphi$ is the standard normal density, i.e., $\frac{1}{\sqrt{2\pi }}\mathrm{exp}\left(-\frac{1}{2}{x}^{2}\right)$.
If ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$, beta is not referenced.
Constraint: if ${\mathbf{sigma_est}}\ne \mathrm{Nag_SigmaConst}$, ${\mathbf{beta}}>0.0$.
6: $\mathbf{regtype}$Nag_RegType Input
On entry: determines the type of regression to be performed.
${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$
Huber type regression.
${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$
Mallows type regression.
${\mathbf{regtype}}=\mathrm{Nag_SchweppeReg}$
Schweppe type regression.
Constraint: ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, $\mathrm{Nag_HuberReg}$ or $\mathrm{Nag_SchweppeReg}$.
7: $\mathbf{sigma_est}$Nag_SigmaEst Input
On entry: determines how $\sigma$ is to be estimated.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$
$\sigma$ is held constant at its initial value.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$
$\sigma$ is estimated by median absolute deviation of residuals.
${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$
$\sigma$ is estimated using the $\chi$ function.
Constraint: ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$, $\mathrm{Nag_SigmaConst}$ or $\mathrm{Nag_SigmaChi}$.
8: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}>1$.
9: $\mathbf{m}$Integer Input
On entry: $m$, the number of independent variables.
Constraint: $1\le {\mathbf{m}}<{\mathbf{n}}$.
10: $\mathbf{x}\left[\mathit{dim}\right]$double Input/Output
Note: the dimension, dim, of the array x must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdx}}×{\mathbf{m}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×{\mathbf{pdx}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
where ${\mathbf{X}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{pdx}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{x}}\left[\left(i-1\right)×{\mathbf{pdx}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the values of the $X$ matrix, i.e., the independent variables. ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}\mathit{j}$th element of ${\mathbf{x}}$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
If ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, during calculations the elements of x will be transformed as described in Section 3. Before exit the inverse transformation will be applied. As a result there may be slight differences between the input x and the output x.
On exit: unchanged, except as described above.
11: $\mathbf{pdx}$Integer Input
On entry: the stride separating row or column elements (depending on the value of order) in the array x.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdx}}\ge {\mathbf{n}}$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdx}}\ge {\mathbf{m}}$.
12: $\mathbf{y}\left[{\mathbf{n}}\right]$double Input/Output
On entry: the data values of the dependent variable.
${\mathbf{y}}\left[\mathit{i}-1\right]$ must contain the value of $y$ for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, during calculations the elements of y will be transformed as described in Section 3. Before exit the inverse transformation will be applied. As a result there may be slight differences between the input y and the output y.
On exit: unchanged, except as described above.
13: $\mathbf{wgt}\left[{\mathbf{n}}\right]$double Input/Output
On entry: the weight for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{regtype}}=\mathrm{Nag_MallowsReg}$, during calculations elements of wgt will be transformed as described in Section 3. Before exit the inverse transformation will be applied. As a result there may be slight differences between the input wgt and the output wgt.
If ${\mathbf{wgt}}\left[i-1\right]\le 0$, the $i$th observation is not included in the analysis.
If ${\mathbf{regtype}}=\mathrm{Nag_HuberReg}$, wgt is not referenced.
On exit: unchanged, except as described above.
14: $\mathbf{theta}\left[{\mathbf{m}}\right]$double Input/Output
On entry: starting values of the parameter vector $\theta$. These may be obtained from least squares regression. Alternatively if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaRes}$ and ${\mathbf{sigma}}=1$ or if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$ and sigma approximately equals the standard deviation of the dependent variable, $y$, then ${\mathbf{theta}}\left[\mathit{i}-1\right]=0.0$, for $\mathit{i}=1,2,\dots ,m$ may provide reasonable starting values.
On exit: the M-estimate of ${\theta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
15: $\mathbf{k}$Integer * Output
On exit: the column rank of the matrix $X$.
16: $\mathbf{sigma}$double * Input/Output
On entry: a starting value for the estimation of $\sigma$. sigma should be approximately the standard deviation of the residuals from the model evaluated at the value of $\theta$ given by theta on entry.
Constraint: ${\mathbf{sigma}}>0.0$.
On exit: the final estimate of $\sigma$ if ${\mathbf{sigma_est}}\ne \mathrm{Nag_SigmaConst}$ or the value assigned on entry if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaConst}$.
17: $\mathbf{rs}\left[{\mathbf{n}}\right]$double Output
On exit: the residuals from the model evaluated at final value of theta, i.e., rs contains the vector $\left(y-X\stackrel{^}{\theta }\right)$.
18: $\mathbf{tol}$double Input
On entry: the relative precision for the final estimates. Convergence is assumed when both the relative change in the value of sigma and the relative change in the value of each element of theta are less than tol.
It is advisable for tol to be greater than .
Constraint: ${\mathbf{tol}}>0.0$.
19: $\mathbf{eps}$double Input
On entry: a relative tolerance to be used to determine the rank of $X$.
If or ${\mathbf{eps}}>1.0$, machine precision will be used in place of tol.
A reasonable value for eps is $5.0×{10}^{-6}$ where this value is possible.
20: $\mathbf{maxit}$Integer Input
On entry: the maximum number of iterations that should be used during the estimation.
A value of ${\mathbf{maxit}}=50$ should be adequate for most uses.
Constraint: ${\mathbf{maxit}}>0$.
21: $\mathbf{nitmon}$Integer Input
On entry: determines the amount of information that is printed on each iteration.
${\mathbf{nitmon}}\le 0$
No information is printed.
${\mathbf{nitmon}}>0$
On the first and every nitmon iterations the values of sigma, theta and the change in theta during the iteration are printed.
22: $\mathbf{outfile}$const char * Input
On entry: a null terminated character string giving the name of the file to which results should be printed. If ${\mathbf{outfile}}=\mathbf{NULL}$ or an empty string then the stdout stream is used. Note that the file will be opened in the append mode.
23: $\mathbf{nit}$Integer * Output
On exit: the number of iterations that were used during the estimation.
24: $\mathbf{comm}$Nag_Comm *
The NAG communication argument (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
25: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_CHI
Value given by chi function $\text{}<0$: ${\mathbf{chi}}\left(〈\mathit{\text{value}}〉\right)=〈\mathit{\text{value}}〉$.
The value of chi must be non-negative.
NE_CONVERGENCE_SOL
Iterations to solve the weighted least squares equations failed to converge.
NE_CONVERGENCE_THETA
The function has failed to converge in maxit iterations.
NE_FULL_RANK
The weighted least squares equations are not of full rank. This may be due to the $X$ matrix not being of full rank, in which case the results will be valid. It may also occur if some of the ${G}_{ii}$ values become very small or zero, see Section 9. The rank of the equations is given by k. If the matrix just fails the test for nonsingularity then the result ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_FULL_RANK and ${\mathbf{k}}={\mathbf{m}}$ is possible .
NE_INT
On entry, ${\mathbf{maxit}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxit}}>0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>1$.
On entry, ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}>0$.
NE_INT_2
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{m}}<{\mathbf{n}}$.
On entry, ${\mathbf{pdx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdx}}\ge {\mathbf{m}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NOT_CLOSE_FILE
Cannot close file $〈\mathit{\text{value}}〉$.
NE_NOT_WRITE_FILE
Cannot open file $〈\mathit{\text{value}}〉$ for writing.
NE_REAL
On entry, ${\mathbf{beta}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{beta}}>0.0$.
On entry, ${\mathbf{sigma}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sigma}}>0.0$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}>0.0$.
NE_ZERO_DF
Having removed cases with zero weight, the value of ${\mathbf{n}}-{\mathbf{k}}\le 0$, i.e., no degree of freedom for error. This error will only occur if ${\mathbf{sigma_est}}=\mathrm{Nag_SigmaChi}$.
NE_ZERO_VALUE
Estimated value of sigma is zero.

## 7Accuracy

The accuracy of the results is controlled by tol.

## 8Parallelism and Performance

g02hdc is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02hdc makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

In cases when ${\mathbf{sigma_est}}\ne \mathrm{Nag_SigmaConst}$ it is important for the value of sigma to be of a reasonable magnitude. Too small a value may cause too many of the winsorized residuals, i.e., $\psi \left({r}_{i}/\sigma \right)$, to be zero, which will lead to convergence problems and may trigger the ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_FULL_RANK error.
By suitable choice of the functions chi and psi this function may be used for other applications of iterative weighted least squares.
For the variance-covariance matrix of $\theta$ see g02hfc.

## 10Example

Having input $X$, $Y$ and the weights, a Schweppe type regression is performed using Huber's $\psi$ function. The function BETCAL calculates the appropriate value of ${\beta }_{2}$.

### 10.1Program Text

Program Text (g02hdce.c)

### 10.2Program Data

Program Data (g02hdce.d)

### 10.3Program Results

Program Results (g02hdce.r)