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

# NAG Toolbox: nag_rand_field_2d_user_setup (g05zq)

## Purpose

nag_rand_field_2d_user_setup (g05zq) performs the setup required in order to simulate stationary Gaussian random fields in two dimensions, for a user-defined variogram, using the circulant embedding method. Specifically, the eigenvalues of the extended covariance matrix (or embedding matrix) are calculated, and their square roots output, for use by nag_rand_field_2d_generate (g05zs), which simulates the random field.

## Syntax

[lam, xx, yy, m, approx, rho, icount, eig, user, ifail] = g05zq(ns, xmin, xmax, ymin, ymax, maxm, var, cov2, even, 'pad', pad, 'icorr', icorr, 'user', user)
[lam, xx, yy, m, approx, rho, icount, eig, user, ifail] = nag_rand_field_2d_user_setup(ns, xmin, xmax, ymin, ymax, maxm, var, cov2, even, 'pad', pad, 'icorr', icorr, 'user', user)

## Description

A two-dimensional random field $Z\left(\mathbf{x}\right)$ in ${ℝ}^{2}$ is a function which is random at every point $\mathbf{x}\in {ℝ}^{2}$, so $Z\left(\mathbf{x}\right)$ is a random variable for each $\mathbf{x}$. The random field has a mean function $\mu \left(\mathbf{x}\right)=𝔼\left[Z\left(\mathbf{x}\right)\right]$ and a symmetric positive semidefinite covariance function $C\left(\mathbf{x},\mathbf{y}\right)=𝔼\left[\left(Z\left(\mathbf{x}\right)-\mu \left(\mathbf{x}\right)\right)\left(Z\left(\mathbf{y}\right)-\mu \left(\mathbf{y}\right)\right)\right]$. $Z\left(\mathbf{x}\right)$ is a Gaussian random field if for any choice of $n\in ℕ$ and ${\mathbf{x}}_{1},\dots ,{\mathbf{x}}_{n}\in {ℝ}^{2}$, the random vector ${\left[Z\left({\mathbf{x}}_{1}\right),\dots ,Z\left({\mathbf{x}}_{n}\right)\right]}^{\mathrm{T}}$ follows a multivariate Normal distribution, which would have a mean vector $\stackrel{~}{\mathbf{\mu }}$ with entries ${\stackrel{~}{\mu }}_{i}=\mu \left({\mathbf{x}}_{i}\right)$ and a covariance matrix $\stackrel{~}{C}$ with entries ${\stackrel{~}{C}}_{ij}=C\left({\mathbf{x}}_{i},{\mathbf{x}}_{j}\right)$. A Gaussian random field $Z\left(\mathbf{x}\right)$ is stationary if $\mu \left(\mathbf{x}\right)$ is constant for all $\mathbf{x}\in {ℝ}^{2}$ and $C\left(\mathbf{x},\mathbf{y}\right)=C\left(\mathbf{x}+\mathbf{a},\mathbf{y}+\mathbf{a}\right)$ for all $\mathbf{x},\mathbf{y},\mathbf{a}\in {ℝ}^{2}$ and hence we can express the covariance function $C\left(\mathbf{x},\mathbf{y}\right)$ as a function $\gamma$ of one variable: $C\left(\mathbf{x},\mathbf{y}\right)=\gamma \left(\mathbf{x}-\mathbf{y}\right)$. $\gamma$ is known as a variogram (or more correctly, a semivariogram) and includes the multiplicative factor ${\sigma }^{2}$ representing the variance such that $\gamma \left(0\right)={\sigma }^{2}$.
The functions nag_rand_field_2d_user_setup (g05zq) and nag_rand_field_2d_generate (g05zs) are used to simulate a two-dimensional stationary Gaussian random field, with mean function zero and variogram $\gamma \left(\mathbf{x}\right)$, over a domain $\left[{x}_{\mathrm{min}},{x}_{\mathrm{max}}\right]×\left[{y}_{\mathrm{min}},{y}_{\mathrm{max}}\right]$, using an equally spaced set of ${N}_{1}×{N}_{2}$ points; ${N}_{1}$ points in the $x$-direction and ${N}_{2}$ points in the $y$-direction. The problem reduces to sampling a Normal random vector $\mathbf{X}$ of size ${N}_{1}×{N}_{2}$, with mean vector zero and a symmetric covariance matrix $A$, which is an ${N}_{2}$ by ${N}_{2}$ block Toeplitz matrix with Toeplitz blocks of size ${N}_{1}$ by ${N}_{1}$. Since $A$ is in general expensive to factorize, a technique known as the circulant embedding method is used. $A$ is embedded into a larger, symmetric matrix $B$, which is an ${M}_{2}$ by ${M}_{2}$ block circulant matrix with circulant blocks of size ${M}_{1}$ by ${M}_{1}$, where ${M}_{1}\ge 2\left({N}_{1}-1\right)$ and ${M}_{2}\ge 2\left({N}_{2}-1\right)$. $B$ can now be factorized as $B=W\Lambda {W}^{*}={R}^{*}R$, where $W$ is the two-dimensional Fourier matrix (${W}^{*}$ is the complex conjugate of $W$), $\Lambda$ is the diagonal matrix containing the eigenvalues of $B$ and $R={\Lambda }^{\frac{1}{2}}{W}^{*}$. $B$ is known as the embedding matrix. The eigenvalues can be calculated by performing a discrete Fourier transform of the first row (or column) of $B$ and multiplying by ${M}_{1}×{M}_{2}$, and so only the first row (or column) of $B$ is needed – the whole matrix does not need to be formed.
The symmetry of $A$ as a block matrix, and the symmetry of each block of $A$, depends on whether the variogram $\gamma$ is even or not. $\gamma$ is even in its first coordinate if $\gamma \left({\left[{-x}_{1},{x}_{2}\right]}^{\mathrm{T}}\right)=\gamma \left({\left[{x}_{1},{x}_{2}\right]}^{\mathrm{T}}\right)$, even in its second coordinate if $\gamma \left({\left[{x}_{1},{-x}_{2}\right]}^{\mathrm{T}}\right)=\gamma \left({\left[{x}_{1},{x}_{2}\right]}^{\mathrm{T}}\right)$, and even if it is even in both coordinates (in two dimensions it is impossible for $\gamma$ to be even in one coordinate and uneven in the other). If $\gamma$ is even then $A$ is a symmetric block matrix and has symmetric blocks; if $\gamma$ is uneven then $A$ is not a symmetric block matrix and has non-symmetric blocks. In the uneven case, ${M}_{1}$ and ${M}_{2}$ are set to be odd in order to guarantee symmetry in $B$.
As long as all of the values of $\Lambda$ are non-negative (i.e., $B$ is positive semidefinite), $B$ is a covariance matrix for a random vector $\mathbf{Y}$ which has ${M}_{2}$ blocks of size ${M}_{1}$. Two samples of $\mathbf{Y}$ can now be simulated from the real and imaginary parts of ${R}^{*}\left(\mathbf{U}+i\mathbf{V}\right)$, where $\mathbf{U}$ and $\mathbf{V}$ have elements from the standard Normal distribution. Since ${R}^{*}\left(\mathbf{U}+i\mathbf{V}\right)=W{\Lambda }^{\frac{1}{2}}\left(\mathbf{U}+i\mathbf{V}\right)$, this calculation can be done using a discrete Fourier transform of the vector ${\Lambda }^{\frac{1}{2}}\left(\mathbf{U}+i\mathbf{V}\right)$. Two samples of the random vector $\mathbf{X}$ can now be recovered by taking the first ${N}_{1}$ elements of the first ${N}_{2}$ blocks of each sample of $\mathbf{Y}$ – because the original covariance matrix $A$ is embedded in $B$, $\mathbf{X}$ will have the correct distribution.
If $B$ is not positive semidefinite, larger embedding matrices $B$ can be tried; however if the size of the matrix would have to be larger than maxm, an approximation procedure is used. We write $\Lambda ={\Lambda }_{+}+{\Lambda }_{-}$, where ${\Lambda }_{+}$ and ${\Lambda }_{-}$ contain the non-negative and negative eigenvalues of $B$ respectively. Then $B$ is replaced by $\rho {B}_{+}$ where ${B}_{+}=W{\Lambda }_{+}{W}^{*}$ and $\rho \in \left(0,1\right]$ is a scaling factor. The error $\epsilon$ in approximating the distribution of the random field is given by
 $ε= 1-ρ 2 trace⁡Λ + ρ2 trace⁡Λ- M .$
Three choices for $\rho$ are available, and are determined by the input argument icorr:
• setting ${\mathbf{icorr}}=0$ sets
 $ρ= trace⁡Λ trace⁡Λ+ ,$
• setting ${\mathbf{icorr}}=1$ sets
 $ρ= trace⁡Λ trace⁡Λ+ ,$
• setting ${\mathbf{icorr}}=2$ sets $\rho =1$.
nag_rand_field_2d_user_setup (g05zq) finds a suitable positive semidefinite embedding matrix $B$ and outputs its sizes in the vector m and the square roots of its eigenvalues in lam. If approximation is used, information regarding the accuracy of the approximation is output. Note that only the first row (or column) of $B$ is actually formed and stored.

## References

Dietrich C R and Newsam G N (1997) Fast and exact simulation of stationary Gaussian processes through circulant embedding of the covariance matrix SIAM J. Sci. Comput. 18 1088–1107
Schlather M (1999) Introduction to positive definite functions and to unconditional simulation of random fields Technical Report ST 99–10 Lancaster University
Wood A T A and Chan G (1994) Simulation of stationary Gaussian processes in ${\left[0,1\right]}^{d}$ Journal of Computational and Graphical Statistics 3(4) 409–432

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{ns}\left(2\right)$int64int32nag_int array
The number of sample points to use in each direction, with ${\mathbf{ns}}\left(1\right)$ sample points in the $x$-direction, ${N}_{1}$ and ${\mathbf{ns}}\left(2\right)$ sample points in the $y$-direction, ${N}_{2}$. The total number of sample points on the grid is therefore ${\mathbf{ns}}\left(1\right)×{\mathbf{ns}}\left(2\right)$.
Constraints:
• ${\mathbf{ns}}\left(1\right)\ge 1$;
• ${\mathbf{ns}}\left(2\right)\ge 1$.
2:     $\mathrm{xmin}$ – double scalar
The lower bound for the $x$-coordinate, for the region in which the random field is to be simulated.
Constraint: ${\mathbf{xmin}}<{\mathbf{xmax}}$.
3:     $\mathrm{xmax}$ – double scalar
The upper bound for the $x$-coordinate, for the region in which the random field is to be simulated.
Constraint: ${\mathbf{xmin}}<{\mathbf{xmax}}$.
4:     $\mathrm{ymin}$ – double scalar
The lower bound for the $y$-coordinate, for the region in which the random field is to be simulated.
Constraint: ${\mathbf{ymin}}<{\mathbf{ymax}}$.
5:     $\mathrm{ymax}$ – double scalar
The upper bound for the $y$-coordinate, for the region in which the random field is to be simulated.
Constraint: ${\mathbf{ymin}}<{\mathbf{ymax}}$.
6:     $\mathrm{maxm}\left(2\right)$int64int32nag_int array
Determines the maximum size of the circulant matrix to use – a maximum of ${\mathbf{maxm}}\left(1\right)$ elements in the $x$-direction, and a maximum of ${\mathbf{maxm}}\left(2\right)$ elements in the $y$-direction. The maximum size of the circulant matrix is thus ${\mathbf{maxm}}\left(1\right)$$×$${\mathbf{maxm}}\left(2\right)$.
Constraints:
• if ${\mathbf{even}}=1$, ${\mathbf{maxm}}\left(i\right)\ge {2}^{k}$, where $k$ is the smallest integer satisfying ${2}^{k}\ge 2\left({\mathbf{ns}}\left(i\right)-1\right)$, for $i=1,2$ ;
• if ${\mathbf{even}}=0$, ${\mathbf{maxm}}\left(i\right)\ge {3}^{k}$, where $k$ is the smallest integer satisfying ${3}^{k}\ge 2\left({\mathbf{ns}}\left(i\right)-1\right)$, for $i=1,2$ .
7:     $\mathrm{var}$ – double scalar
The multiplicative factor ${\sigma }^{2}$ of the variogram $\gamma \left(\mathbf{x}\right)$.
Constraint: ${\mathbf{var}}\ge 0.0$.
8:     $\mathrm{cov2}$ – function handle or string containing name of m-file
cov2 must evaluate the variogram $\gamma \left(\mathbf{x}\right)$ for all $\mathbf{x}$ if ${\mathbf{even}}=0$, and for all $\mathbf{x}$ with non-negative entries if ${\mathbf{even}}=1$. The value returned in gamma is multiplied internally by var.
[gamma, user] = cov2(x, y, user)

Input Parameters

1:     $\mathrm{x}$ – double scalar
The coordinate $x$ at which the variogram $\gamma \left(\mathbf{x}\right)$ is to be evaluated.
2:     $\mathrm{y}$ – double scalar
The coordinate $y$ at which the variogram $\gamma \left(\mathbf{x}\right)$ is to be evaluated.
3:     $\mathrm{user}$ – Any MATLAB object
cov2 is called from nag_rand_field_2d_user_setup (g05zq) with the object supplied to nag_rand_field_2d_user_setup (g05zq).

Output Parameters

1:     $\mathrm{gamma}$ – double scalar
The value of the variogram $\gamma \left(\mathbf{x}\right)$.
2:     $\mathrm{user}$ – Any MATLAB object
9:     $\mathrm{even}$int64int32nag_int scalar
Indicates whether the covariance function supplied is even or uneven.
${\mathbf{even}}=0$
The covariance function is uneven.
${\mathbf{even}}=1$
The covariance function is even.
Constraint: ${\mathbf{even}}=0$ or $1$.

### Optional Input Parameters

1:     $\mathrm{pad}$int64int32nag_int scalar
Default: ${\mathbf{pad}}=1$
Determines whether the embedding matrix is padded with zeros, or padded with values of the variogram. The choice of padding may affect how big the embedding matrix must be in order to be positive semidefinite.
${\mathbf{pad}}=0$
The embedding matrix is padded with zeros.
${\mathbf{pad}}=1$
The embedding matrix is padded with values of the variogram.
Constraint: ${\mathbf{pad}}=0$ or $1$.
2:     $\mathrm{icorr}$int64int32nag_int scalar
Default: ${\mathbf{icorr}}=0$
Determines which approximation to implement if required, as described in Description.
Constraint: ${\mathbf{icorr}}=0$, $1$ or $2$.
3:     $\mathrm{user}$ – Any MATLAB object
user is not used by nag_rand_field_2d_user_setup (g05zq), but is passed to cov2. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

### Output Parameters

1:     $\mathrm{lam}\left({\mathbf{maxm}}\left(1\right)×{\mathbf{maxm}}\left(2\right)\right)$ – double array
Contains the square roots of the eigenvalues of the embedding matrix.
2:     $\mathrm{xx}\left({\mathbf{ns}}\left(1\right)\right)$ – double array
The points of the $x$-coordinates at which values of the random field will be output.
3:     $\mathrm{yy}\left({\mathbf{ns}}\left(2\right)\right)$ – double array
The points of the $y$-coordinates at which values of the random field will be output.
4:     $\mathrm{m}\left(2\right)$int64int32nag_int array
${\mathbf{m}}\left(1\right)$ contains ${M}_{1}$, the size of the circulant blocks and ${\mathbf{m}}\left(2\right)$ contains ${M}_{2}$, the number of blocks, resulting in a final square matrix of size ${M}_{1}×{M}_{2}$.
5:     $\mathrm{approx}$int64int32nag_int scalar
Indicates whether approximation was used.
${\mathbf{approx}}=0$
No approximation was used.
${\mathbf{approx}}=1$
Approximation was used.
6:     $\mathrm{rho}$ – double scalar
Indicates the scaling of the covariance matrix. ${\mathbf{rho}}=1.0$ unless approximation was used with ${\mathbf{icorr}}=0$ or $1$.
7:     $\mathrm{icount}$int64int32nag_int scalar
Indicates the number of negative eigenvalues in the embedding matrix which have had to be set to zero.
8:     $\mathrm{eig}\left(3\right)$ – double array
Indicates information about the negative eigenvalues in the embedding matrix which have had to be set to zero. ${\mathbf{eig}}\left(1\right)$ contains the smallest eigenvalue, ${\mathbf{eig}}\left(2\right)$ contains the sum of the squares of the negative eigenvalues, and ${\mathbf{eig}}\left(3\right)$ contains the sum of the absolute values of the negative eigenvalues.
9:     $\mathrm{user}$ – Any MATLAB object
10:   $\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$
Constraint: ${\mathbf{ns}}\left(1\right)\ge 1$, ${\mathbf{ns}}\left(2\right)\ge 1$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{xmin}}<{\mathbf{xmax}}$.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{ymin}}<{\mathbf{ymax}}$.
${\mathbf{ifail}}=6$
Constraint: the minima for maxm are $\left[_,_\right]$.
Where, if ${\mathbf{even}}=1$, the minimum calculated value of ${\mathbf{maxm}}\left(\mathit{i}\right)$ is given by ${2}^{k}$, where $k$ is the smallest integer satisfying ${2}^{k}\ge 2\left({\mathbf{ns}}\left(\mathit{i}\right)-1\right)$, and if ${\mathbf{even}}=0$, the minimum calculated value of ${\mathbf{maxm}}\left(\mathit{i}\right)$ is given by ${3}^{k}$, where $k$ is the smallest integer satisfying ${3}^{k}\ge 2\left({\mathbf{ns}}\left(\mathit{i}\right)-1\right)$, for $\mathit{i}=1,2$.
${\mathbf{ifail}}=7$
Constraint: ${\mathbf{var}}\ge 0.0$.
${\mathbf{ifail}}=9$
Constraint: ${\mathbf{even}}=0$ or $1$.
${\mathbf{ifail}}=10$
Constraint: ${\mathbf{pad}}=0$ or $1$.
${\mathbf{ifail}}=11$
Constraint: ${\mathbf{icorr}}=0$, $1$ or $2$.
${\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

If on exit ${\mathbf{approx}}=1$, see the comments in Description regarding the quality of approximation; increase the values in maxm to attempt to avoid approximation.

None.

## Example

This example calls nag_rand_field_2d_user_setup (g05zq) to calculate the eigenvalues of the embedding matrix for $25$ sample points on a $5$ by $5$ grid of a two-dimensional random field characterized by the symmetric stable variogram:
 $γx = σ2 exp - x′ ν ,$
where ${x}^{\prime }=\left|\frac{x}{{\ell }_{1}}+\frac{y}{{\ell }_{2}}\right|$, and ${\ell }_{1}$, ${\ell }_{2}$ and $\nu$ are parameters.
It should be noted that the symmetric stable variogram is one of the pre-defined variograms available in nag_rand_field_2d_predef_setup (g05zr). It is used here purely for illustrative purposes.
```function g05zq_example

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

% Random Field variance
var = 0.5;
% Domain endpoints
xmin = -1;
xmax =  1;
ymin = -0.5;
ymax =  0.5;
% Number of sample points in x and y
ns = [int64(5), 5];
% Maximum dimensions of circulant matrix
maxm = [int64(81), 81];
% Scaling factor rho=1.
icorr = int64(2);

% Put covariance parameters (for cov2) in user
norm_p = int64(2);
l1 = 0.1;
l2 = 0.15;
nu = 1.2;
user = {norm_p; l1; l2; nu};
% cov2 is even
even = int64(1);

% Get square roots of the eigenvalues of the embedding matrix
[lam, xx, yy, m, approx, rho, icount, eig, user, ifail] = ...
g05zq( ...
ns, xmin, xmax, ymin, ymax, maxm, var, ...
@cov2, even, 'icorr', icorr, 'user', user);

fprintf('Size of embedding matrix = %d\n\n', m(1)*m(2));

% Display approximation information if approximation used
if approx == 1
fprintf('Approximation required\n\n');
fprintf('rho = %10.5f\n', rho);
fprintf('eig = %10.5f%10.5f%10.5f\n', eig(1:3));
fprintf('icount = %d\n', icount);
else
fprintf('Approximation not required\n\n');
end

% Display square roots of the eigenvalues of the embedding matrix
fprintf('Square roots of eigenvalues of embedding matrix:\n');
mm = m(1)*m(2);
mlam = reshape(lam(1:mm), m(1), m(2));
for i = 1:m(1)
fprintf('%8.4f',mlam(i,:));
fprintf('\n');
end

function [gam, user] = cov2(x, y, user)
norm_p = user{1};
l1     = user{2};
l2     = user{3};
nu     = user{4};

tl1 = abs(x)/l1;
tl2 = abs(y)/l2;

if norm_p == 1
rnorm = tl1 +  tl2;
else
rnorm = sqrt(tl1^2+tl2^2);
end

gam = exp(-(rnorm^nu));
```
```g05zq example results

Size of embedding matrix = 64

Approximation not required

Square roots of eigenvalues of embedding matrix:
0.8966  0.8234  0.6810  0.5757  0.5391  0.5757  0.6810  0.8234
0.8940  0.8217  0.6804  0.5756  0.5391  0.5756  0.6804  0.8217
0.8877  0.8175  0.6792  0.5754  0.5391  0.5754  0.6792  0.8175
0.8813  0.8133  0.6780  0.5751  0.5390  0.5751  0.6780  0.8133
0.8787  0.8116  0.6774  0.5750  0.5390  0.5750  0.6774  0.8116
0.8813  0.8133  0.6780  0.5751  0.5390  0.5751  0.6780  0.8133
0.8877  0.8175  0.6792  0.5754  0.5391  0.5754  0.6792  0.8175
0.8940  0.8217  0.6804  0.5756  0.5391  0.5756  0.6804  0.8217
```