G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01NAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G01NAF computes the cumulants and moments of quadratic forms in Normal variates.

## 2  Specification

 SUBROUTINE G01NAF ( MOM, MEAN, N, A, LDA, EMU, SIGMA, LDSIG, L, RKUM, RMOM, WK, IFAIL)
 INTEGER N, LDA, LDSIG, L, IFAIL REAL (KIND=nag_wp) A(LDA,N), EMU(*), SIGMA(LDSIG,N), RKUM(L), RMOM(*), WK(3*N*(N+1)/2+N) CHARACTER(1) MOM, MEAN

## 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$, G01NAF 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 routine is based on the routine 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  Parameters

1:     MOM – CHARACTER(1)Input
On entry: indicates if moments are computed in addition to cumulants.
${\mathbf{MOM}}=\text{'C'}$
Only cumulants are computed.
${\mathbf{MOM}}=\text{'M'}$
Moments are computed in addition to cumulants.
Constraint: ${\mathbf{MOM}}=\text{'C'}$ or $\text{'M'}$.
2:     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:     N – INTEGERInput
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{N}}>1$.
4:     A(LDA,N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
5:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which G01NAF is called.
Constraint: ${\mathbf{LDA}}\ge {\mathbf{N}}$.
6:     EMU($*$) – REAL (KIND=nag_wp) arrayInput
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.
7:     SIGMA(LDSIG,N) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
8:     LDSIG – INTEGERInput
On entry: the first dimension of the array SIGMA as declared in the (sub)program from which G01NAF is called.
Constraint: ${\mathbf{LDSIG}}\ge {\mathbf{N}}$.
9:     L – INTEGERInput
On entry: the required number of cumulants, and moments if specified.
Constraint: $1\le {\mathbf{L}}\le 12$.
10:   RKUM(L) – REAL (KIND=nag_wp) arrayOutput
On exit: the L cumulants of the quadratic form.
11:   RMOM($*$) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array RMOM must be at least ${\mathbf{L}}$ if ${\mathbf{MOM}}=\text{'M'}$, and at least $1$ otherwise.
On exit: if ${\mathbf{MOM}}=\text{'M'}$, the L moments of the quadratic form.
12:   WK($3×{\mathbf{N}}×\left({\mathbf{N}}+1\right)/2+{\mathbf{N}}$) – REAL (KIND=nag_wp) arrayWorkspace
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 1$, or ${\mathbf{L}}<1$, or ${\mathbf{L}}>12$, or ${\mathbf{LDA}}<{\mathbf{N}}$, or ${\mathbf{LDSIG}}<{\mathbf{N}}$, or ${\mathbf{MOM}}\ne \text{'C'}$ or $\text{'M'}$, or ${\mathbf{MEAN}}\ne \text{'M'}$ or $\text{'Z'}$.
${\mathbf{IFAIL}}=2$
 On entry, the matrix $\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 G01NAF. 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 (g01nafe.f90)

### 9.2  Program Data

Program Data (g01nafe.d)

### 9.3  Program Results

Program Results (g01nafe.r)