# NAG Library Function Document

## 1Purpose

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

## 2Specification

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

## 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$, 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).
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{order}$Nag_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.3.1.3 in How to Use the NAG Library and its Documentation for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2:    $\mathbf{ratio_type}$Nag_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:    $\mathbf{mean}$Nag_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:    $\mathbf{n}$IntegerInput
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
5:    $\mathbf{a}\left[\mathit{dim}\right]$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:    $\mathbf{pda}$IntegerInput
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:    $\mathbf{b}\left[\mathit{dim}\right]$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:    $\mathbf{pdb}$IntegerInput
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:    $\mathbf{c}\left[\mathit{dim}\right]$const doubleInput
Note: the dimension, dim, of the array c must be at least ${\mathbf{n}}$ when ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$.
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:  $\mathbf{pdc}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array c.
Constraint: if ${\mathbf{ratio_type}}=\mathrm{Nag_QuadRatio}$, ${\mathbf{pdc}}\ge {\mathbf{n}}$
11:  $\mathbf{ela}\left[\mathit{dim}\right]$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 ela is not referenced.
12:  $\mathbf{emu}\left[\mathit{dim}\right]$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:  $\mathbf{sigma}\left[\mathit{dim}\right]$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:  $\mathbf{pdsig}$IntegerInput
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:  $\mathbf{l1}$IntegerInput
On entry: the first moment to be computed, ${l}_{1}$.
Constraint: $0<{\mathbf{l1}}\le {\mathbf{l2}}$.
16:  $\mathbf{l2}$IntegerInput
On entry: the last moment to be computed, ${l}_{2}$.
Constraint: ${\mathbf{l1}}\le {\mathbf{l2}}\le 12$.
17:  $\mathbf{lmax}$Integer *Output
On exit: the highest moment computed, ${l}_{\mathrm{MAX}}$. This will be ${l}_{2}$ on successful exit.
18:  $\mathbf{rmom}\left[{\mathbf{l2}}-{\mathbf{l1}}+1\right]$doubleOutput
On exit: the ${l}_{1}$ to ${l}_{\mathrm{MAX}}$ moments.
19:  $\mathbf{abserr}$double *Output
On exit: the estimated maximum absolute error in any computed moment.
20:  $\mathbf{eps}$doubleInput
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:  $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ACCURACY
The required accuracy has not been achieved in the integration. An estimate of the accuracy is returned in abserr.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_EIGENVALUES
The computation to compute the eigenvalues required in the calculation of moments has failed to converge: this is an unlikely error exit.
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}}$.
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_MOMENTS
Only $〈\mathit{\text{value}}〉$ moments exist, less than ${\mathbf{l1}}=〈\mathit{\text{value}}〉$, therefore none of the required moments can be computed.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
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 some of the required moments have been computed, the highest is given by lmax.

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

nag_moments_ratio_quad_forms (g01nbc) 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 function. 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 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.

### 10.1Program Text

Program Text (g01nbce.c)

### 10.2Program Data

Program Data (g01nbce.d)

### 10.3Program Results

Program Results (g01nbce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017