Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

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

## Syntax

[rkum, rmom, ifail] = g01na(a, sigma, l, 'n', n, 'emu', emu)
[rkum, rmom, ifail] = nag_stat_moments_quad_form(a, sigma, l, 'n', n, 'emu', emu)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 23: mom and mean were removed from the interface; emu was made optional

## 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_stat_moments_quad_form (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 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).

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{a}\left(\mathit{lda},{\mathbf{n}}\right)$ – double array
lda, the first dimension of the array, must satisfy the constraint $\mathit{lda}\ge {\mathbf{n}}$.
The $n$ by $n$ symmetric matrix $A$. Only the lower triangle is referenced.
2:     $\mathrm{sigma}\left(\mathit{ldsig},{\mathbf{n}}\right)$ – double array
ldsig, the first dimension of the array, must satisfy the constraint $\mathit{ldsig}\ge {\mathbf{n}}$.
The $n$ by $n$ variance-covariance matrix $\Sigma$. Only the lower triangle is referenced.
Constraint: the matrix $\Sigma$ must be positive definite.
3:     $\mathrm{l}$int64int32nag_int scalar
The required number of cumulants, and moments if specified.
Constraint: $1\le {\mathbf{l}}\le 12$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the arrays a, sigma and the second dimension of the arrays a, sigma. (An error is raised if these dimensions are not equal.)
$n$, the dimension of the quadratic form.
Constraint: ${\mathbf{n}}>1$.
2:     $\mathrm{emu}\left(:\right)$ – double array
The dimension of the array emu must be at least ${\mathbf{n}}$ if $\mathit{mean}=\text{'M'}$, and at least $1$ otherwise
If supplied, emu must contain the $n$ elements of the vector $\mu$.

### Output Parameters

1:     $\mathrm{rkum}\left({\mathbf{l}}\right)$ – double array
The l cumulants of the quadratic form.
2:     $\mathrm{rmom}\left(:\right)$ – double array
The dimension of the array rmom will be ${\mathbf{l}}$ if $\mathit{mom}=\text{'M'}$ and $1$ otherwise
If $\mathit{mom}=\text{'M'}$, the l moments of the quadratic form.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 1$, or ${\mathbf{l}}<1$, or ${\mathbf{l}}>12$, or $\mathit{lda}<{\mathbf{n}}$, or $\mathit{ldsig}<{\mathbf{n}}$, or $\mathit{mom}\ne \text{'C'}$ or $\text{'M'}$, or $\mathit{mean}\ne \text{'M'}$ or $\text{'Z'}$.
${\mathbf{ifail}}=2$
 On entry, the matrix $\Sigma$ is not positive definite.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

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

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 nag_stat_moments_quad_form (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.
```function g01na_example

fprintf('g01na example results\n\n');

% Problem parameters
n    = 10;
l    = int64(4);
beta = 0.8;
con  = 1.0;

% Simple autoregression setup
a            = zeros(n,n);
a(2:n,1:n-1) = 0.5*eye(n-1);
emu = zeros(n,1);
for j=1:n
emu(j) = con*beta^j;
end
sigma = zeros(n,n);
sigma(1,1) = 1;
for j = 2:n
sigma(j,j) = sigma(j-1,j-1)*beta^2 + 1;
end
for i = 1:n
s = sigma(i,i);
for j = i+1:n
sigma(j,i) = s*beta^(j-i);
end
end

[rkum, rmom, ifail] = g01na( ...
a, sigma, l, 'emu', emu);

% Display results
fprintf(' n = %3d, beta = %6.3f, con = %6.3f\n\n', n, beta, con);
fprintf('    Cumulants       Moments\n\n');
ival = double([1:l]');
fprintf('%3d%12.4e    %12.4e\n',[ival rkum rmom]');

```
```g01na example results

n =  10, beta =  0.800, con =  1.000

Cumulants       Moments

1  1.7517e+01      1.7517e+01
2  3.5010e+02      6.5695e+02
3  1.6091e+04      3.9865e+04
4  1.1700e+06      3.4039e+06
```