## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g01naf ( mom, mean, n, a, lda, emu, l, rkum, rmom, wk,
 Integer, Intent (In) :: n, lda, ldsig, l Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: a(lda,n), emu(*), sigma(ldsig,n) Real (Kind=nag_wp), Intent (Inout) :: rmom(*) Real (Kind=nag_wp), Intent (Out) :: rkum(l), wk(3*n*(n+1)/2+n) Character (1), Intent (In) :: mom, mean
#include <nag.h>
 void g01naf_ (const char *mom, const char *mean, const Integer *n, const double a[], const Integer *lda, const double emu[], const double sigma[], const Integer *ldsig, const Integer *l, double rkum[], double rmom[], double wk[], Integer *ifail, const Charlen length_mom, const Charlen length_mean)
The routine may be called by the names g01naf or nagf_stat_moments_quad_form.

## 3Description

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
 $E(Qs),$
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).

## 4References

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

## 5Arguments

1: $\mathbf{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: $\mathbf{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: $\mathbf{n}$Integer Input
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
4: $\mathbf{a}\left({\mathbf{lda}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n×n$ symmetric matrix $A$. Only the lower triangle is referenced.
5: $\mathbf{lda}$Integer Input
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: $\mathbf{emu}\left(*\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{sigma}\left({\mathbf{ldsig}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $n×n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
8: $\mathbf{ldsig}$Integer Input
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: $\mathbf{l}$Integer Input
On entry: the required number of cumulants, and moments if specified.
Constraint: $1\le {\mathbf{l}}\le 12$.
10: $\mathbf{rkum}\left({\mathbf{l}}\right)$Real (Kind=nag_wp) array Output
On exit: the l cumulants of the quadratic form.
11: $\mathbf{rmom}\left(*\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{wk}\left(3×{\mathbf{n}}×\left({\mathbf{n}}+1\right)/2+{\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
13: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\ge 1$.
On entry, ${\mathbf{l}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{l}}\le 12$.
On entry, ${\mathbf{lda}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldsig}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldsig}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{mean}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mean}}=\text{'Z'}$ or $\text{'M'}$.
On entry, ${\mathbf{mom}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{mom}}=\text{'C'}$ or $\text{'M'}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>1$.
${\mathbf{ifail}}=2$
On entry, sigma is not positive definite.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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

## 8Parallelism and Performance

g01naf 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 routine. 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 moments of the quadratic form
 $Q=∑t=2nytyt-1$
are computed using g01naf. The matrix $A$ is given by:
 $A(i+1,i) = 12, i=1,2,…n-1; A(i,j) = 0, otherwise.$
The value of $\Sigma$ can be computed using the relationships
 $var(yt)=β2var(yt-1)+1$
and
 $cov(ytyt+k)=β cov(ytyt+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.

### 10.1Program Text

Program Text (g01nafe.f90)

### 10.2Program Data

Program Data (g01nafe.d)

### 10.3Program Results

Program Results (g01nafe.r)