G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01NBF

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

G01NBF computes the moments of ratios of quadratic forms in Normal variables and related statistics.

## 2  Specification

 SUBROUTINE G01NBF ( CASE, MEAN, N, A, LDA, B, LDB, C, LDC, ELA, EMU, SIGMA, LDSIG, L1, L2, LMAX, RMOM, ABSERR, EPS, WK, IFAIL)
 INTEGER N, LDA, LDB, LDC, LDSIG, L1, L2, LMAX, IFAIL REAL (KIND=nag_wp) A(LDA,N), B(LDB,N), C(LDC,*), ELA(*), EMU(*), SIGMA(LDSIG,N), RMOM(L2-L1+1), ABSERR, EPS, WK(3*N*N+(8+L2)*N) CHARACTER(1) CASE, MEAN

## 3  Description

Let $x$ have an $n$-dimensional multivariate Normal distribution with mean $\mu$ and variance-covariance matrix $\Sigma$. Then for a symmetric matrix $A$ and symmetric positive semidefinite matrix $B$, G01NBF computes a subset, ${l}_{1}$ to ${l}_{2}$, of the first $12$ moments of the ratio of quadratic forms
 $R=xTAx/xTBx.$
The $s$th moment (about the origin) is defined as
 $ERs,$ (1)
where $E$ denotes the expectation. Alternatively, this routine will compute the following expectations:
 $ERsaTx$ (2)
and
 $ERsxTCx,$ (3)
where $a$ is a vector of length $n$ and $C$ is a $n$ by $n$ symmetric matrix, if they exist. In the case of (2) the moments are zero if $\mu =0$.
The conditions of theorems 1, 2 and 3 of Magnus (1986) and Magnus (1990) are used to check for the existence of the moments. If all the requested moments do not exist, the computations are carried out for those moments that are requested up to the maximum that exist, ${l}_{\mathrm{MAX}}$.
This routine is based on the routine QRMOM written by Magnus and Pesaran (1993a) and based on the theory given by Magnus (1986) and Magnus (1990). The computation of the moments requires first the computation of the eigenvectors of the matrix ${L}^{\mathrm{T}}BL$, where $L{L}^{\mathrm{T}}=\Sigma$. The matrix ${L}^{\mathrm{T}}BL$ must be positive semidefinite and not null. Given the eigenvectors of this matrix, a function which has to be integrated over the range zero to infinity can be computed. This integration is performed using D01AMF.

## 4  References

Magnus J R (1986) The exact moments of a ratio of quadratic forms in Normal variables Ann. Économ. Statist. 4 95–109
Magnus J R (1990) On certain moments relating to quadratic forms in Normal variables: Further results Sankhyā, Ser. B 52 1–13
Magnus J R and Pesaran B (1993a) The evaluation of cumulants and moments of quadratic forms in Normal variables (CUM): Technical description Comput. Statist. 8 39–45
Magnus J R and Pesaran B (1993b) The evaluation of moments of quadratic forms and ratios of quadratic forms in Normal variables: Background, motivation and examples Comput. Statist. 8 47–55

## 5  Parameters

1:     CASE – CHARACTER(1)Input
On entry: indicates the moments of which function are to be computed.
${\mathbf{CASE}}=\text{'R'}$ (Ratio)
$E\left({R}^{s}\right)$ is computed.
${\mathbf{CASE}}=\text{'L'}$ (Linear with ratio)
$E\left({R}^{s}\left({a}^{\mathrm{T}}x\right)\right)$ is computed.
${\mathbf{CASE}}=\text{'Q'}$ (Quadratic with ratio)
$E\left({R}^{s}\left({x}^{\mathrm{T}}Cx\right)\right)$ is computed.
Constraint: ${\mathbf{CASE}}=\text{'R'}$, $\text{'L'}$ or $\text{'Q'}$.
2:     MEAN – CHARACTER(1)Input
On entry: indicates if the mean, $\mu$, is zero.
${\mathbf{MEAN}}=\text{'Z'}$
$\mu$ is zero.
${\mathbf{MEAN}}=\text{'M'}$
The value of $\mu$ is supplied in EMU.
Constraint: ${\mathbf{MEAN}}=\text{'Z'}$ or $\text{'M'}$.
3:     N – INTEGERInput
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{N}}>1$.
4:     A(LDA,N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
5:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which G01NBF is called.
Constraint: ${\mathbf{LDA}}\ge {\mathbf{N}}$.
6:     B(LDB,N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ by $n$ positive semidefinite symmetric matrix $B$. Only the lower triangle is referenced.
Constraint: the matrix $B$ must be positive semidefinite.
7:     LDB – INTEGERInput
On entry: the first dimension of the array B as declared in the (sub)program from which G01NBF is called.
Constraint: ${\mathbf{LDB}}\ge {\mathbf{N}}$.
8:     C(LDC,$*$) – REAL (KIND=nag_wp) arrayInput
Note: the second dimension of the array C must be at least ${\mathbf{N}}$ if ${\mathbf{CASE}}=\text{'Q'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{CASE}}=\text{'Q'}$, C must contain the $n$ by $n$ symmetric matrix $C$; only the lower triangle is referenced.
If ${\mathbf{CASE}}\ne \text{'Q'}$, C is not referenced.
9:     LDC – INTEGERInput
On entry: the first dimension of the array C as declared in the (sub)program from which G01NBF is called.
Constraints:
• if ${\mathbf{CASE}}=\text{'Q'}$, ${\mathbf{LDC}}\ge {\mathbf{N}}$;
• otherwise ${\mathbf{LDC}}\ge 1$.
10:   ELA($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array ELA must be at least ${\mathbf{N}}$ if ${\mathbf{CASE}}=\text{'L'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{CASE}}=\text{'L'}$, ELA must contain the vector $a$ of length $n$, otherwise A is not referenced.
11:   EMU($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array EMU must be at least ${\mathbf{N}}$ if ${\mathbf{MEAN}}=\text{'M'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{MEAN}}=\text{'M'}$, EMU must contain the $n$ elements of the vector $\mu$.
If ${\mathbf{MEAN}}=\text{'Z'}$, EMU is not referenced.
12:   SIGMA(LDSIG,N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
13:   LDSIG – INTEGERInput
On entry: the first dimension of the array SIGMA as declared in the (sub)program from which G01NBF is called.
Constraint: ${\mathbf{LDSIG}}\ge {\mathbf{N}}$.
14:   L1 – INTEGERInput
On entry: the first moment to be computed, ${l}_{1}$.
Constraint: $0<{\mathbf{L1}}\le {\mathbf{L2}}$.
15:   L2 – INTEGERInput
On entry: the last moment to be computed, ${l}_{2}$.
Constraint: ${\mathbf{L1}}\le {\mathbf{L2}}\le 12$.
16:   LMAX – INTEGEROutput
On exit: the highest moment computed, ${l}_{\mathrm{MAX}}$. This will be ${l}_{2}$ if ${\mathbf{IFAIL}}={\mathbf{0}}$ on exit.
17:   RMOM(${\mathbf{L2}}-{\mathbf{L1}}+1$) – REAL (KIND=nag_wp) arrayOutput
On exit: the ${l}_{1}$ to ${l}_{\mathrm{MAX}}$ moments.
18:   ABSERR – REAL (KIND=nag_wp)Output
On exit: the estimated maximum absolute error in any computed moment.
19:   EPS – REAL (KIND=nag_wp)Input
On entry: the relative accuracy required for the moments, this value is also used in the checks for the existence of the moments.
If ${\mathbf{EPS}}=0.0$, a value of $\sqrt{\epsilon }$ where $\epsilon$ is the machine precision used.
Constraint: ${\mathbf{EPS}}=0.0$ or .
20:   WK($3×{\mathbf{N}}×{\mathbf{N}}+\left(8+{\mathbf{L2}}\right)×{\mathbf{N}}$) – REAL (KIND=nag_wp) arrayWorkspace
21:   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, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).
Note: G01NBF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 1$, or ${\mathbf{LDA}}<{\mathbf{N}}$, or ${\mathbf{LDB}}<{\mathbf{N}}$, or ${\mathbf{LDSIG}}<{\mathbf{N}}$, or ${\mathbf{CASE}}=\text{'Q'}$ and ${\mathbf{LDC}}<{\mathbf{N}}$, or ${\mathbf{CASE}}\ne \text{'Q'}$ and ${\mathbf{LDC}}<1$, or ${\mathbf{L1}}<1$, or ${\mathbf{L1}}>{\mathbf{L2}}$, or ${\mathbf{L2}}>12$, or ${\mathbf{CASE}}\ne \text{'R'}$, $\text{'L'}$ or $\text{'Q'}$, or ${\mathbf{MEAN}}\ne \text{'M'}$ or $\text{'Z'}$, or ${\mathbf{EPS}}\ne 0.0$ and .
${\mathbf{IFAIL}}=2$
 On entry, $\Sigma$ is not positive definite, or ${\mathbf{B}}$ is not positive semidefinite or is null.
${\mathbf{IFAIL}}=3$
None of the required moments can be computed.
${\mathbf{IFAIL}}=4$
The matrix ${L}^{\mathrm{T}}BL$ is not positive semidefinite or is null.
${\mathbf{IFAIL}}=5$
The computation to compute the eigenvalues required in the calculation of moments has failed to converge: this is an unlikely error exit.
${\mathbf{IFAIL}}=6$
Only some of the required moments have been computed, the highest is given by LMAX.
${\mathbf{IFAIL}}=7$
The required accuracy has not been achieved in the integration. An estimate of the accuracy is returned in ABSERR.

## 7  Accuracy

The relative accuracy is specified by EPS and an estimate of the maximum absolute error for all computed moments is returned in ABSERR.

None.

## 9  Example

This example is given by Magnus and Pesaran (1993b) and considers the simple autoregression:
 $yt=βyt-1+ut, t=1,2,…,n,$
where $\left\{{u}_{t}\right\}$ is a sequence of independent Normal variables with mean zero and variance one, and ${y}_{0}$ is known. The least squares estimate of $\beta$, $\stackrel{^}{\beta }$, is given by
 $β^=∑t=2nytyt-1 ∑t=2nyt2 .$
Thus $\stackrel{^}{\beta }$ can be written as a ratio of quadratic forms and its moments computed using G01NBF. The matrix $A$ is given by
 $Ai+1,i=12, i=1,2,…n-1; Ai,j=0, otherwise,$
and the matrix $B$ is given by
 $Bi,i=1, i=1,2,…n-1; Bi,j=0, otherwise.$
The value of $\Sigma$ can be computed using the relationships
 $varyt=β2varyt-1+1$
and
 $covytyt+k=β covytyt+k- 1$
for $k\ge 0$ and $\mathrm{var}\left({y}_{1}\right)=1$.
The values of $\beta$, ${y}_{0}$, $n$, and the number of moments required are read in and the moments computed and printed.

### 9.1  Program Text

Program Text (g01nbfe.f90)

### 9.2  Program Data

Program Data (g01nbfe.d)

### 9.3  Program Results

Program Results (g01nbfe.r)