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

# Syntax

C#
```public static void g01nb(
string dncase,
string mean,
int n,
double[,] a,
double[,] b,
double[,] c,
double[] ela,
double[] emu,
double[,] sigma,
int l1,
int l2,
out int lmax,
double[] rmom,
out double abserr,
double eps,
out int ifail
)```
Visual Basic
```Public Shared Sub g01nb ( _
dncase As String, _
mean As String, _
n As Integer, _
a As Double(,), _
b As Double(,), _
c As Double(,), _
ela As Double(), _
emu As Double(), _
sigma As Double(,), _
l1 As Integer, _
l2 As Integer, _
<OutAttribute> ByRef lmax As Integer, _
rmom As Double(), _
<OutAttribute> ByRef abserr As Double, _
eps As Double, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g01nb(
String^ dncase,
String^ mean,
int n,
array<double,2>^ a,
array<double,2>^ b,
array<double,2>^ c,
array<double>^ ela,
array<double>^ emu,
array<double,2>^ sigma,
int l1,
int l2,
[OutAttribute] int% lmax,
array<double>^ rmom,
[OutAttribute] double% abserr,
double eps,
[OutAttribute] int% ifail
)```
F#
```static member g01nb :
dncase : string *
mean : string *
n : int *
a : float[,] *
b : float[,] *
c : float[,] *
ela : float[] *
emu : float[] *
sigma : float[,] *
l1 : int *
l2 : int *
lmax : int byref *
rmom : float[] *
abserr : float byref *
eps : float *
ifail : int byref -> unit
```

#### Parameters

dncase
Type: System..::..String
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"}$.
mean
Type: System..::..String
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"}$.
n
Type: System..::..Int32
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
a
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
b
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
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.
c
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, dim2]
Note: dim1 must satisfy the constraint:
• if ${\mathbf{case}}=\text{"Q"}$, $\mathrm{dim1}\ge {\mathbf{n}}$;
• otherwise $\mathrm{dim1}\ge 1$.
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.
ela
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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.
emu
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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.
sigma
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: the $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
l1
Type: System..::..Int32
On entry: the first moment to be computed, ${l}_{1}$.
Constraint: $0<{\mathbf{l1}}\le {\mathbf{l2}}$.
l2
Type: System..::..Int32
On entry: the last moment to be computed, ${l}_{2}$.
Constraint: ${\mathbf{l1}}\le {\mathbf{l2}}\le 12$.
lmax
Type: System..::..Int32%
On exit: the highest moment computed, ${l}_{\mathrm{MAX}}$. This will be ${l}_{2}$ if ${\mathbf{ifail}}={0}$ on exit.
rmom
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{l2}}-{\mathbf{l1}}+1$]
On exit: the ${l}_{1}$ to ${l}_{\mathrm{MAX}}$ moments.
abserr
Type: System..::..Double%
On exit: the estimated maximum absolute error in any computed moment.
eps
Type: System..::..Double
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 .
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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$, g01nb 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 method 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 method is based on the method 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 d01am.

# 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

# Error Indicators and Warnings

Note: g01nb may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDA, LDB, LDC, LDSIG) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 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.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

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

None.

# 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 g01nb. 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.

Example program (C#): g01nbe.cs

Example program data: g01nbe.d

Example program results: g01nbe.r