g01 Chapter Contents
g01 Chapter Introduction
NAG C Library Manual

1  Purpose

nag_moments_quad_form (g01nac) computes the cumulants and moments of quadratic forms in Normal variates.

2  Specification

 #include #include
 void nag_moments_quad_form (Nag_OrderType order, Nag_SelectMoments mom, Nag_IncludeMean mean, Integer n, const double a[], Integer pda, const double emu[], const double sigma[], Integer pdsig, Integer l, double rkum[], double rmom[], 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$, nag_moments_quad_form (g01nac) computes up to the first $12$ moments and cumulants of the quadratic form $Q={x}^{\mathrm{T}}Ax$. The $s$th moment (about the origin) is defined as
 $EQs,$
where $E$ denotes expectation. The $s$th moment of $Q$ can also be found as the coefficient of ${t}^{s}/s!$ in the expansion of $E\left({e}^{Qt}\right)$. The $s$th cumulant is defined as the coefficient of ${t}^{s}/s!$ in the expansion of $\mathrm{log}\left(E\left({e}^{Qt}\right)\right)$.
The function is based on the function CUM written by Magnus and Pesaran (1993a) and based on the theory given by Magnus (1978), Magnus (1979) and Magnus (1986).

4  References

Magnus J R (1978) The moments of products of quadratic forms in Normal variables Statist. Neerlandica 32 201–210
Magnus J R (1979) The expectation of products of quadratic forms in Normal variables: the practice Statist. Neerlandica 33 131–136
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 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:     momNag_SelectMomentsInput
On entry: indicates if moments are computed in addition to cumulants.
${\mathbf{mom}}=\mathrm{Nag_CumulantsOnly}$
Only cumulants are computed.
${\mathbf{mom}}=\mathrm{Nag_ComputeMoments}$
Moments are computed in addition to cumulants.
Constraint: ${\mathbf{mom}}=\mathrm{Nag_CumulantsOnly}$ or $\mathrm{Nag_ComputeMoments}$.
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:     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.
8:     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.
9:     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}}$.
10:   lIntegerInput
On entry: the required number of cumulants, and moments if specified.
Constraint: $1\le {\mathbf{l}}\le 12$.
11:   rkum[l]doubleOutput
On exit: the l cumulants of the quadratic form.
12:   rmom[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array rmom must be at least
• ${\mathbf{l}}$ when ${\mathbf{mom}}=\mathrm{Nag_ComputeMoments}$;
• $1$ otherwise.
On exit: if ${\mathbf{mom}}=\mathrm{Nag_ComputeMoments}$, the l moments of the quadratic form.
13:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l}}\le 12$.
On entry, ${\mathbf{l}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{l}}\ge 1$.
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{pdsig}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdsig}}>0$.
NE_INT_2
On entry, ${\mathbf{pda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pda}}\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_POS_DEF
On entry, sigma is not positive definite.

7  Accuracy

In a range of tests the accuracy was found to be a modest multiple of machine precision. See Magnus and Pesaran (1993b).

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 moments of the quadratic form
 $Q=∑t=2nytyt-1$
are computed using nag_moments_quad_form (g01nac). The matrix $A$ is given by:
 $Ai+1,i = 12, i=1,2,…n-1; Ai,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 and cumulants printed.

9.1  Program Text

Program Text (g01nace.c)

9.2  Program Data

Program Data (g01nace.d)

9.3  Program Results

Program Results (g01nace.r)