## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g01nbf ( case, mean, n, a, lda, b, ldb, c, ldc, ela, emu, l1, l2, lmax, rmom, eps, wk,
 Integer, Intent (In) :: n, lda, ldb, ldc, ldsig, l1, l2 Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: lmax Real (Kind=nag_wp), Intent (In) :: a(lda,n), b(ldb,n), c(ldc,*), ela(*), emu(*), sigma(ldsig,n), eps Real (Kind=nag_wp), Intent (Out) :: rmom(l2-l1+1), abserr, wk(3*n*n+(8+l2)*n) Character (1), Intent (In) :: case, mean
#include <nag.h>
 void g01nbf_ (const char *cas, const char *mean, const Integer *n, const double a[], const Integer *lda, const double b[], const Integer *ldb, const double c[], const Integer *ldc, const double ela[], const double emu[], const double sigma[], const Integer *ldsig, const Integer *l1, const Integer *l2, Integer *lmax, double rmom[], double *abserr, const double *eps, double wk[], Integer *ifail, const Charlen length_cas, const Charlen length_mean)
The routine may be called by the names g01nbf or nagf_stat_moments_ratio_quad_forms.

## 3Description

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

## 4References

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

## 5Arguments

1: $\mathbf{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: $\mathbf{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: $\mathbf{n}$Integer Input
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
4: $\mathbf{a}\left({\mathbf{lda}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
5: $\mathbf{lda}$Integer Input
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: $\mathbf{b}\left({\mathbf{ldb}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{ldb}$Integer Input
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: $\mathbf{c}\left({\mathbf{ldc}},*\right)$Real (Kind=nag_wp) array Input
Note: the second dimension of the array c must be at least ${\mathbf{n}}$ if ${\mathbf{case}}=\text{'Q'}$.
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: $\mathbf{ldc}$Integer Input
On entry: the first dimension of the array c as declared in the (sub)program from which g01nbf is called.
Constraint: if ${\mathbf{case}}=\text{'Q'}$, ${\mathbf{ldc}}\ge {\mathbf{n}}$.
10: $\mathbf{ela}\left(*\right)$Real (Kind=nag_wp) array Input
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 ela is not referenced.
11: $\mathbf{emu}\left(*\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{sigma}\left({\mathbf{ldsig}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{ldsig}$Integer Input
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: $\mathbf{l1}$Integer Input
On entry: the first moment to be computed, ${l}_{1}$.
Constraint: $0<{\mathbf{l1}}\le {\mathbf{l2}}$.
15: $\mathbf{l2}$Integer Input
On entry: the last moment to be computed, ${l}_{2}$.
Constraint: ${\mathbf{l1}}\le {\mathbf{l2}}\le 12$.
16: $\mathbf{lmax}$Integer Output
On exit: the highest moment computed, ${l}_{\mathrm{MAX}}$. This will be ${l}_{2}$ if ${\mathbf{ifail}}={\mathbf{0}}$ on exit.
17: $\mathbf{rmom}\left({\mathbf{l2}}-{\mathbf{l1}}+1\right)$Real (Kind=nag_wp) array Output
On exit: the ${l}_{1}$ to ${l}_{\mathrm{MAX}}$ moments.
18: $\mathbf{abserr}$Real (Kind=nag_wp) Output
On exit: the estimated maximum absolute error in any computed moment.
19: $\mathbf{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: $\mathbf{wk}\left(3×{\mathbf{n}}×{\mathbf{n}}+\left(8+{\mathbf{l2}}\right)×{\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
21: $\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 $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. 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:
Note: in some cases g01nbf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{case}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{case}}=\text{'R'}$, $\text{'L'}$ or $\text{'Q'}$.
On entry, ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{eps}}\ne 0.0$, .
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l1}}\ge 1$.
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l2}}\ge {\mathbf{l1}}$.
On entry, ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l2}}\le 12$.
On entry, ${\mathbf{lda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldb}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldsig}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldsig}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{mean}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mean}}=\text{'Z'}$ or $\text{'M'}$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>1$.
${\mathbf{ifail}}=2$
On entry, b is not positive semidefinite or is null.
On entry, sigma is not positive definite.
${\mathbf{ifail}}=3$
Only $〈\mathit{\text{value}}〉$ moments exist, less than ${\mathbf{l1}}=〈\mathit{\text{value}}〉$, therefore 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.
${\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 relative accuracy is specified by eps and an estimate of the maximum absolute error for all computed moments is returned in abserr.

## 8Parallelism and Performance

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

None.

## 10Example

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.

### 10.1Program Text

Program Text (g01nbfe.f90)

### 10.2Program Data

Program Data (g01nbfe.d)

### 10.3Program Results

Program Results (g01nbfe.r)