g01 Chapter Contents
g01 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_moments_ratio_quad_forms (g01nbc)

## 1  Purpose

nag_moments_ratio_quad_forms (g01nbc) computes the moments of ratios of quadratic forms in Normal variables and related statistics.

## 2  Specification

 #include #include
 void nag_moments_ratio_quad_forms (Nag_OrderType order, Nag_MomentType ratio_type, Nag_IncludeMean mean, Integer n, const double a[], Integer pda, const double b[], Integer pdb, const double c[], Integer pdc, const double ela[], const double emu[], const double sigma[], Integer pdsig, Integer l1, Integer l2, Integer *lmax, double rmom[], double *abserr, double eps, NagError *fail)

## 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$, nag_moments_ratio_quad_forms (g01nbc) 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 function 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 function is based on the function 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 nag_1d_quad_inf_1 (d01smc).

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

1:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or Nag_ColMajor.
2:     ratio_typeNag_MomentTypeInput
On entry: indicates the moments of which function are to be computed.
${\mathbf{ratio_type}}=\mathrm{Nag_RatioMoments}$ (Ratio)
$E\left({R}^{s}\right)$ is computed.
${\mathbf{ratio_type}}=\mathrm{Nag_LinearRatio}$ (Linear with ratio)
$E\left({R}^{s}\left({a}^{\mathrm{T}}x\right)\right)$ is computed.
${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$ (Quadratic with ratio)
$E\left({R}^{s}\left({x}^{\mathrm{T}}Cx\right)\right)$ is computed.
Constraint: ${\mathbf{ratio_type}}=\mathrm{Nag_RatioMoments}$, $\mathrm{Nag_LinearRatio}$ or $\mathrm{Nag_QuadRatio}$.
3:     meanNag_IncludeMeanInput
On entry: indicates if the mean, $\mu$, is zero.
${\mathbf{mean}}=\mathrm{Nag_MeanZero}$
$\mu$ is zero.
${\mathbf{mean}}=\mathrm{Nag_MeanInclude}$
The value of $\mu$ is supplied in emu.
Constraint: ${\mathbf{mean}}=\mathrm{Nag_MeanZero}$ or $\mathrm{Nag_MeanInclude}$.
4:     nIntegerInput
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
5:     a[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array a must be at least ${\mathbf{pda}}×{\mathbf{n}}$.
The $\left(i,j\right)$th element of the matrix $A$ is stored in
• ${\mathbf{a}}\left[\left(j-1\right)×{\mathbf{pda}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{a}}\left[\left(i-1\right)×{\mathbf{pda}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
6:     pdaIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array a.
Constraint: ${\mathbf{pda}}\ge {\mathbf{n}}$.
7:     b[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array b must be at least ${\mathbf{pdb}}×{\mathbf{n}}$.
The $\left(i,j\right)$th element of the matrix $B$ is stored in
• ${\mathbf{b}}\left[\left(j-1\right)×{\mathbf{pdb}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{b}}\left[\left(i-1\right)×{\mathbf{pdb}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
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.
8:     pdbIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array b.
Constraint: ${\mathbf{pdb}}\ge {\mathbf{n}}$.
9:     c[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array c must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdc}}×{\mathbf{n}}\right)$ when ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$;
• $1$ otherwise.
The $\left(i,j\right)$th element of the matrix $C$ is stored in
• ${\mathbf{c}}\left[\left(j-1\right)×{\mathbf{pdc}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{pdc}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: if ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$, c must contain the $n$ by $n$ symmetric matrix $C$; only the lower triangle is referenced.
If ${\mathbf{ratio_type}}\ne \mathrm{Nag_QuadRatio}$, c is not referenced.
10:   pdcIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array c.
Constraints:
• if ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$, ${\mathbf{pdc}}\ge {\mathbf{n}}$;
• otherwise ${\mathbf{pdc}}\ge 1$.
11:   ela[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array ela must be at least
• ${\mathbf{n}}$ when ${\mathbf{ratio_type}}=\mathrm{Nag_LinearRatio}$;
• $1$ otherwise.
On entry: if ${\mathbf{ratio_type}}=\mathrm{Nag_LinearRatio}$, ela must contain the vector $a$ of length $n$, otherwise a is not referenced.
12:   emu[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array emu must be at least
• ${\mathbf{n}}$ when ${\mathbf{mean}}=\mathrm{Nag_MeanInclude}$;
• $1$ otherwise.
On entry: if ${\mathbf{mean}}=\mathrm{Nag_MeanInclude}$, emu must contain the $n$ elements of the vector $\mu$.
If ${\mathbf{mean}}=\mathrm{Nag_MeanZero}$, emu is not referenced.
13:   sigma[$\mathit{dim}$]const doubleInput
Note: the dimension, dim, of the array sigma must be at least ${\mathbf{pdsig}}×{\mathbf{n}}$.
The $\left(i,j\right)$th element of the matrix is stored in
• ${\mathbf{sigma}}\left[\left(j-1\right)×{\mathbf{pdsig}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{sigma}}\left[\left(i-1\right)×{\mathbf{pdsig}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
14:   pdsigIntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array sigma.
Constraint: ${\mathbf{pdsig}}\ge {\mathbf{n}}$.
15:   l1IntegerInput
On entry: the first moment to be computed, ${l}_{1}$.
Constraint: $0<{\mathbf{l1}}\le {\mathbf{l2}}$.
16:   l2IntegerInput
On entry: the last moment to be computed, ${l}_{2}$.
Constraint: ${\mathbf{l1}}\le {\mathbf{l2}}\le 12$.
17:   lmaxInteger *Output
On exit: the highest moment computed, ${l}_{\mathrm{MAX}}$. This will be ${l}_{2}$ on successful exit.
18:   rmom[${\mathbf{l2}}-{\mathbf{l1}}+1$]doubleOutput
On exit: the ${l}_{1}$ to ${l}_{\mathrm{MAX}}$ moments.
19:   abserrdouble *Output
On exit: the estimated maximum absolute error in any computed moment.
20:   epsdoubleInput
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 .
21:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ACCURACY
Full accuracy not achieved in integration.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_EIGENVALUES
Failure in computing eigenvalues.
NE_ENUM_INT
On entry, ${\mathbf{ratio_type}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
NE_ENUM_INT_2
On entry, ${\mathbf{ratio_type}}=〈\mathit{\text{value}}〉$, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$, ${\mathbf{pdc}}\ge {\mathbf{n}}$;
otherwise ${\mathbf{pdc}}\ge 1$.
NE_INT
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l1}}\ge 1$.
On entry, ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l2}}\le 12$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>1$.
On entry, ${\mathbf{pda}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pda}}>0$.
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}>0$.
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}>0$.
On entry, ${\mathbf{pdsig}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdsig}}>0$.
NE_INT_2
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: $0<{\mathbf{l1}}\le {\mathbf{l2}}$.
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l1}}\le {\mathbf{l2}}\le 12$.
On entry, ${\mathbf{l1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l2}}\ge {\mathbf{l1}}$.
On entry, ${\mathbf{pda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pda}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{pdsig}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdsig}}\ge {\mathbf{n}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_MOMENTS
Only $〈\mathit{\text{value}}〉$ moments exist, less than ${\mathbf{l1}}=〈\mathit{\text{value}}〉$.
NE_POS_DEF
On entry, sigma is not positive definite.
NE_POS_SEMI_DEF
On entry, b is not positive semidefinite or is null.
The matrix ${L}^{\mathrm{T}}BL$ is not positive semidefinite or is null.
NE_REAL
On entry, ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{eps}}\ne 0.0$, .
NE_SOME_MOMENTS
Only $〈\mathit{\text{value}}〉$ moments exist, less than ${\mathbf{l2}}=〈\mathit{\text{value}}〉$.

## 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 nag_moments_ratio_quad_forms (g01nbc). 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 (g01nbce.c)

### 9.2  Program Data

Program Data (g01nbce.d)

### 9.3  Program Results

Program Results (g01nbce.r)