# NAG Library Routine Document

## 1Purpose

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.

## 2Specification

Fortran Interface
 Subroutine f04yaf ( job, p, a, lda, svd, sv, cj, work,
 Integer, Intent (In) :: job, p, lda, irank Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: sigma, sv(p) Real (Kind=nag_wp), Intent (Inout) :: a(lda,p) Real (Kind=nag_wp), Intent (Out) :: cj(p), work(p) Logical, Intent (In) :: svd
#include nagmk26.h
 void f04yaf_ (const Integer *job, const Integer *p, const double *sigma, double a[], const Integer *lda, const logical *svd, const Integer *irank, const double sv[], double cj[], double work[], Integer *ifail)

## 3Description

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 arguments irank, sigma, a and sv in the required form. f04jgf returns the argument svd, but when this routine is used following routine f08kaf (dgelss) the argument svd should be set to .TRUE.. The argument p of this routine corresponds to the argument n in routines f04jgf and f08kaf (dgelss).
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

## 5Arguments

1:     $\mathbf{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:     $\mathbf{p}$ – IntegerInput
On entry: $p$, the order of the variance-covariance matrix $C$.
Constraint: ${\mathbf{p}}\ge 1$.
3:     $\mathbf{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:     $\mathbf{a}\left({\mathbf{lda}},{\mathbf{p}}\right)$ – 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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{sv}\left({\mathbf{p}}\right)$ – 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:     $\mathbf{cj}\left({\mathbf{p}}\right)$ – 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:   $\mathbf{work}\left({\mathbf{p}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
If ${\mathbf{job}}>0$, work is not referenced.
11:   $\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{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.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
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 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.

## 8Parallelism and Performance

f04yaf 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 routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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

## 10Example

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.

### 10.1Program Text

Program Text (f04yafe.f90)

### 10.2Program Data

Program Data (f04yafe.d)

### 10.3Program Results

Program Results (f04yafe.r)

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