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

# Syntax

C#
```public static void g01na(
string mom,
string mean,
int n,
double[,] a,
double[] emu,
double[,] sigma,
int l,
double[] rkum,
double[] rmom,
out int ifail
)```
Visual Basic
```Public Shared Sub g01na ( _
mom As String, _
mean As String, _
n As Integer, _
a As Double(,), _
emu As Double(), _
sigma As Double(,), _
l As Integer, _
rkum As Double(), _
rmom As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g01na(
String^ mom,
String^ mean,
int n,
array<double,2>^ a,
array<double>^ emu,
array<double,2>^ sigma,
int l,
array<double>^ rkum,
array<double>^ rmom,
[OutAttribute] int% ifail
)```
F#
```static member g01na :
mom : string *
mean : string *
n : int *
a : float[,] *
emu : float[] *
sigma : float[,] *
l : int *
rkum : float[] *
rmom : float[] *
ifail : int byref -> unit
```

#### Parameters

mom
Type: System..::..String
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"}$.
mean
Type: System..::..String
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"}$.
n
Type: System..::..Int32
On entry: $n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
a
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: the $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
emu
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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.
sigma
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: the $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
l
Type: System..::..Int32
On entry: the required number of cumulants, and moments if specified.
Constraint: $1\le {\mathbf{l}}\le 12$.
rkum
Type: array<System..::..Double>[]()[][]
An array of size [l]
On exit: the l cumulants of the quadratic form.
rmom
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

Let $x$ have an $n$-dimensional multivariate Normal distribution with mean $\mu$ and variance-covariance matrix $\Sigma$. Then for a symmetric matrix $A$, g01na 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 method is based on the method CUM written by Magnus and Pesaran (1993a) and based on the theory given by Magnus (1978)Magnus (1979) and Magnus (1986).

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDA, LDSIG) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 1$, or ${\mathbf{l}}<1$, or ${\mathbf{l}}>12$, 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.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

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

None.

None.

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

Example program (C#): g01nae.cs

Example program data: g01nae.d

Example program results: g01nae.r