F04 Chapter Contents
F04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentF04YAF

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

F04YAF returns elements of the estimated variance-covariance matrix of the sample regression coefficients for the solution of a linear least squares problem.
The routine can be used to find the estimated variances of the sample regression coefficients.

## 2  Specification

 SUBROUTINE F04YAF ( JOB, P, SIGMA, A, LDA, SVD, IRANK, SV, CJ, WORK, IFAIL)
 INTEGER JOB, P, LDA, IRANK, IFAIL REAL (KIND=nag_wp) SIGMA, A(LDA,P), SV(P), CJ(P), WORK(P) LOGICAL SVD

## 3  Description

The estimated variance-covariance matrix $C$ of the sample regression coefficients is given by
 $C = σ 2 XT X - 1 , XT X nonsingular,$
where ${X}^{\mathrm{T}}X$ is the normal matrix for the linear least squares regression problem
 $min:y-Xb2,$ (1)
${\sigma }^{2}$ is the estimated variance of the residual vector $r=y-Xb$, and $X$ is an $n$ by $p$ observation matrix.
When ${X}^{\mathrm{T}}X$ is singular, $C$ is taken to be
 $C=σ2 XTX †,$
where ${\left({X}^{\mathrm{T}}X\right)}^{†}$ is the pseudo-inverse of ${X}^{\mathrm{T}}X$; this assumes that the minimal least squares solution of (1) has been found.
The diagonal elements of $C$ are the estimated variances of the sample regression coefficients, $b$.
The routine can be used to find either the diagonal elements of $C$, or the elements of the $j$th column of $C$, or the upper triangular part of $C$.
This routine must be preceded by a routine that returns either the upper triangular matrix $U$ of the $QU$ factorization of $X$ or of the Cholesky factorization of ${X}^{\mathrm{T}}X$, or the singular values and right singular vectors of $X$. In particular this routine can be preceded by one of the routines F04JGF or F08KAF (DGELSS), which return the parameters IRANK, SIGMA, A and SV in the required form. F04JGF returns the parameter SVD, but when this routine is used following routine F08KAF (DGELSS) the parameter SVD should be set to .TRUE.. The parameter P of this routine corresponds to the parameter N in routines F04JGF and F08KAF (DGELSS).

## 4  References

Anderson T W (1958) An Introduction to Multivariate Statistical Analysis Wiley
Lawson C L and Hanson R J (1974) Solving Least-squares Problems Prentice–Hall

## 5  Parameters

1:     JOB – INTEGERInput
On entry: specifies which elements of $C$ are required.
${\mathbf{JOB}}=-1$
The upper triangular part of $C$ is required.
${\mathbf{JOB}}=0$
The diagonal elements of $C$ are required.
${\mathbf{JOB}}>0$
The elements of column JOB of $C$ are required.
Constraint: $-1\le {\mathbf{JOB}}\le {\mathbf{P}}$.
2:     P – INTEGERInput
On entry: $p$, the order of the variance-covariance matrix $C$.
Constraint: ${\mathbf{P}}\ge 1$.
3:     SIGMA – REAL (KIND=nag_wp)Input
On entry: $\sigma$, the standard error of the residual vector given by
 $σ=rTr/n-k,n>kσ=0,n=k,$
where $k$ is the rank of $X$.
Constraint: ${\mathbf{SIGMA}}\ge 0.0$.
4:     A(LDA,P) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{SVD}}=\mathrm{.FALSE.}$, A must contain the upper triangular matrix $U$ of the $QU$ factorization of $X$, or of the Cholesky factorization of ${X}^{\mathrm{T}}X$; elements of the array below the diagonal need not be set.
If ${\mathbf{SVD}}=\mathrm{.TRUE.}$, $A$ must contain the first $k$ rows of the matrix ${V}^{\mathrm{T}}$, where $k$ is the rank of $X$ and $V$ is the right-hand orthogonal matrix of the singular value decomposition of $X$. Thus the $i$th row must contain the $i$th right-hand singular vector of $X$.
On exit: if ${\mathbf{JOB}}\ge 0$, $A$ is unchanged.
If ${\mathbf{JOB}}=-1$, A contains the upper triangle of the symmetric matrix $C$.
If ${\mathbf{SVD}}=\mathrm{.TRUE.}$, elements of the array below the diagonal are used as workspace.
If ${\mathbf{SVD}}=\mathrm{.FALSE.}$, they are unchanged.
5:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which F04YAF is called.
Constraints:
• if ${\mathbf{SVD}}=\mathrm{.FALSE.}$ or ${\mathbf{JOB}}=-1$, ${\mathbf{LDA}}\ge {\mathbf{P}}$;
• if ${\mathbf{SVD}}=\mathrm{.TRUE.}$ and ${\mathbf{JOB}}\ge 0$, ${\mathbf{LDA}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{IRANK}}\right)$.
6:     SVD – LOGICALInput
On entry: must be .TRUE. if the least squares solution was obtained from a singular value decomposition of $X$. SVD must be .FALSE. if the least squares solution was obtained from either a $QU$ factorization of $X$ or a Cholesky factorization of ${X}^{\mathrm{T}}X$. In the latter case the rank of $X$ is assumed to be $p$ and so is applicable only to full rank problems with $n\ge p$.
7:     IRANK – INTEGERInput
On entry: if ${\mathbf{SVD}}=\mathrm{.TRUE.}$, IRANK must specify the rank $k$ of the matrix $X$.
If ${\mathbf{SVD}}=\mathrm{.FALSE.}$, IRANK is not referenced and the rank of $X$ is assumed to be $p$.
Constraint: $0<{\mathbf{IRANK}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(n,{\mathbf{P}}\right)$.
8:     SV(P) – REAL (KIND=nag_wp) arrayInput
On entry: if ${\mathbf{SVD}}=\mathrm{.TRUE.}$, SV must contain the first IRANK singular values of $X$.
If ${\mathbf{SVD}}=\mathrm{.FALSE.}$, SV is not referenced.
9:     CJ(P) – REAL (KIND=nag_wp) arrayOutput
On exit: if ${\mathbf{JOB}}=0$, CJ returns the diagonal elements of $C$.
If ${\mathbf{JOB}}=j>0$, CJ returns the $j$th column of $C$.
If ${\mathbf{JOB}}=-1$, CJ is not referenced.
10:   WORK(P) – REAL (KIND=nag_wp) arrayWorkspace
If ${\mathbf{JOB}}>0$, WORK is not referenced.
11:   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{P}}<1$, or ${\mathbf{SIGMA}}<0.0$, or ${\mathbf{JOB}}<-1$, or ${\mathbf{JOB}}>{\mathbf{P}}$, or ${\mathbf{SVD}}=\mathrm{.TRUE.}$ and (${\mathbf{IRANK}}<0$ or ${\mathbf{IRANK}}>{\mathbf{P}}$) or (${\mathbf{JOB}}\ge 0$ and ${\mathbf{LDA}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{IRANK}}\right)$) or (${\mathbf{JOB}}=-1$ and ${\mathbf{LDA}}<{\mathbf{P}}$)), or ${\mathbf{SVD}}=\mathrm{.FALSE.}$ and ${\mathbf{LDA}}<{\mathbf{P}}$.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{SVD}}=\mathrm{.TRUE.}$ and ${\mathbf{IRANK}}=0$.
${\mathbf{IFAIL}}=3$
On entry, ${\mathbf{SVD}}=\mathrm{.FALSE.}$ and overflow would occur in computing an element of $C$. The upper triangular matrix $U$ must be very nearly singular.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{SVD}}=\mathrm{.TRUE.}$ and one of the first IRANK singular values is zero. Either the first IRANK singular values or IRANK must be incorrect.
$\mathbf{\text{overflow}}$
If overflow occurs then either an element of $C$ is very large, or more likely, either the rank, or the upper triangular matrix, or the singular values or vectors have been incorrectly supplied.

## 7  Accuracy

The computed elements of $C$ will be the exact covariances of a closely neighbouring least squares problem, so long as a numerically stable method has been used in the solution of the least squares problem.

When ${\mathbf{JOB}}=-1$ the time taken by F04YAF is approximately proportional to $p{k}^{2}$, where $k$ is the rank of $X$. When ${\mathbf{JOB}}=0$ and ${\mathbf{SVD}}=\mathrm{.FALSE.}$, the time taken by the routine is approximately proportional to $p{k}^{2}$, otherwise the time taken is approximately proportional to $pk$.

## 9  Example

This example finds the estimated variances of the sample regression coefficients (the diagonal elements of $C$) for the linear least squares problem
 $min⁡rTr , where ​ r=y-Xb and$
 $X= 0.6 1.2 3.9 5.0 4.0 2.5 1.0 -4.0 -5.5 -1.0 -2.0 -6.5 -4.2 -8.4 -4.8 , b= 3.0 4.0 -1.0 -5.0 -1.0 ,$
following a solution obtained by F04JGF. See the routine document for F04JGF for further information.

### 9.1  Program Text

Program Text (f04yafe.f90)

### 9.2  Program Data

Program Data (f04yafe.d)

### 9.3  Program Results

Program Results (f04yafe.r)