# NAG FL Interfacef04yaf (real_​gen_​lsq_​covmat)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

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

## 3Description

The estimated variance-covariance matrix $C$ of the sample regression coefficients is given by
 $C = σ 2 ( X TX) - 1 , X T X nonsingular,$
where ${X}^{\mathrm{T}}X$ is the normal matrix for the linear least squares regression problem
 $min:‖y-Xb‖2,$ (1)
${\sigma }^{2}$ is the estimated variance of the residual vector $r=y-Xb$, and $X$ is an $n×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, 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 the argument svd should be set to .TRUE.. The argument p of this routine corresponds to the argument n in routines f04jgf and f08kaf.
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}$Integer Input
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}$Integer Input
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) array Input/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}$Integer Input
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}$Logical Input
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}$Integer Input
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) array Input
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) array Output
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) array Workspace
If ${\mathbf{job}}>0$, work is not referenced.
11: $\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{irank}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{svd}}=\mathrm{.TRUE.}$, ${\mathbf{irank}}\ge 0$ and ${\mathbf{irank}}\le {\mathbf{p}}$.
On entry, ${\mathbf{job}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{lda}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{irank}}=⟨\mathit{\text{value}}⟩$.
Constraint: 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)$.
On entry, ${\mathbf{job}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{job}}\ge -1$ and ${\mathbf{job}}\le {\mathbf{p}}$.
On entry, ${\mathbf{lda}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{svd}}=\mathrm{.FALSE.}$, ${\mathbf{lda}}\ge {\mathbf{p}}$.
On entry, ${\mathbf{lda}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{svd}}=\mathrm{.TRUE.}$ and ${\mathbf{job}}=-1$, ${\mathbf{lda}}\ge {\mathbf{p}}$.
On entry, ${\mathbf{p}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{p}}\ge 1$.
On entry, ${\mathbf{sigma}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{sigma}}\ge 0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{irank}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{svd}}=\mathrm{.TRUE.}$, ${\mathbf{irank}}>0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{svd}}=\mathrm{.FALSE.}$ and overflow would occur in calculating an element of $C$. The upper triangular matrix $U$ must be very nearly singular.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{svd}}=\mathrm{.FALSE.}$ and one of the first irank singular values is zero. Either the first irank singular values or irank must be incorrect: ${\mathbf{irank}}=⟨\mathit{\text{value}}⟩$.
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$
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 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)