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_bb (g05xb)

## Purpose

nag_rand_bb (g05xb) uses a Brownian bridge algorithm to construct sample paths for a free or non-free Wiener process. The initialization function nag_rand_bb_init (g05xa) must be called prior to the first call to nag_rand_bb (g05xb).

## Syntax

[z, b, ifail] = g05xb(npaths, start, term, z, c, rcomm, 'rcord', rcord, 'd', d, 'a', a)
[z, b, ifail] = nag_rand_bb(npaths, start, term, z, c, rcomm, 'rcord', rcord, 'd', d, 'a', a)

## Description

For details on the Brownian bridge algorithm and the bridge construction order see Section [Brownian Bridge] in the G05 Chapter Introduction and Section [Description] in (g05xa). Recall that the terms Wiener process (or free Wiener process) and Brownian motion are often used interchangeably, while a non-free Wiener process (also known as a Brownian bridge process) refers to a process which is forced to terminate at a given point.

## References

Glasserman P (2004) Monte Carlo Methods in Financial Engineering Springer

## Parameters

Note: the following variable is used in the parameter descriptions: n = ntimes$\mathit{n}={\mathbf{ntimes}}$, the length of the array times passed to the initialization function nag_rand_bb_init (g05xa).

### Compulsory Input Parameters

1:     npaths – int64int32nag_int scalar
The number of Wiener sample paths to create.
Constraint: npaths1${\mathbf{npaths}}\ge 1$.
2:     start(d) – double array
d, the dimension of the array, must satisfy the constraint d1${\mathbf{d}}\ge 1$.
The starting value of the Wiener process.
3:     term(d) – double array
d, the dimension of the array, must satisfy the constraint d1${\mathbf{d}}\ge 1$.
The terminal value at which the non-free Wiener process should end. If a = 0${\mathbf{a}}=0$, term is ignored.
4:     z(ldz, : $:$) – double array
The first dimension, ldz, of the array z must satisfy
• if rcord = 1${\mathbf{rcord}}=1$, ldzd × (n + 1a)$\mathit{ldz}\ge {\mathbf{d}}×\left(\mathit{n}+1-{\mathbf{a}}\right)$;
• if rcord = 2${\mathbf{rcord}}=2$, ldznpaths$\mathit{ldz}\ge {\mathbf{npaths}}$.
The second dimension of the array must be at least npaths${\mathbf{npaths}}$ if rcord = 1${\mathbf{rcord}}=1$ and at least d × (n + 1a)${\mathbf{d}}×\left(\mathit{n}+1-{\mathbf{a}}\right)$ if rcord = 2${\mathbf{rcord}}=2$
The Normal random numbers used to construct the sample paths.
If quasi-random numbers are used, the d × (n + 1a)${\mathbf{d}}×\left(\mathit{n}+1-{\mathbf{a}}\right)$-dimensional quasi-random points should be stored in successive rows of Z$Z$.
5:     c(ldc, : $:$) – double array
The first dimension of the array c must be at least d${\mathbf{d}}$
The second dimension of the array must be at least d${\mathbf{d}}$
The lower triangular Cholesky factorization C$C$ such that CCT$C{C}^{\mathrm{T}}$ gives the covariance matrix of the Wiener process. Elements of c above the diagonal are not referenced.
6:     rcomm(12 × (ntimes + 1)$12×\left(\mathit{ntimes}+1\right)$) – double array
Communication array as returned by the last call to nag_rand_bb_init (g05xa) or nag_rand_bb (g05xb). This array must not be directly modified.

### Optional Input Parameters

1:     rcord – int64int32nag_int scalar
The order in which Normal random numbers are stored in z and in which the generated values are returned in b.
Default: 1$1$
Constraint: rcord = 1${\mathbf{rcord}}=1$ or 2$2$.
2:     d – int64int32nag_int scalar
Default: The dimension of the arrays term, start and the first dimension of the array c. (An error is raised if these dimensions are not equal.)
The dimension of each Wiener sample path.
Constraint: d1${\mathbf{d}}\ge 1$.
3:     a – int64int32nag_int scalar
If a = 0${\mathbf{a}}=0$, a free Wiener process is created beginning at start and term is ignored.
If a = 1${\mathbf{a}}=1$, a non-free Wiener process is created beginning at start and ending at term.
Default: 1$1$
Constraint: a = 0${\mathbf{a}}=0$ or 1$1$.

ldz ldc ldb

### Output Parameters

1:     z(ldz, : $:$) – double array
The first dimension, ldz, of the array z will be
• if rcord = 1${\mathbf{rcord}}=1$, ldzd × (n + 1a)$\mathit{ldz}\ge {\mathbf{d}}×\left(\mathit{n}+1-{\mathbf{a}}\right)$;
• if rcord = 2${\mathbf{rcord}}=2$, ldznpaths$\mathit{ldz}\ge {\mathbf{npaths}}$.
The second dimension of the array will be npaths${\mathbf{npaths}}$ if rcord = 1${\mathbf{rcord}}=1$ and at least d × (n + 1a)${\mathbf{d}}×\left(\mathit{n}+1-{\mathbf{a}}\right)$ if rcord = 2${\mathbf{rcord}}=2$
The Normal random numbers premultiplied by c.
2:     b(ldb, : $:$) – double array
The first dimension, ldb, of the array b will be
• if rcord = 1${\mathbf{rcord}}=1$, ldbd × (n + 1)$\mathit{ldb}\ge {\mathbf{d}}×\left(\mathit{n}+1\right)$;
• if rcord = 2${\mathbf{rcord}}=2$, ldbnpaths$\mathit{ldb}\ge {\mathbf{npaths}}$.
The second dimension of the array will be npaths${\mathbf{npaths}}$ if rcord = 1${\mathbf{rcord}}=1$ and at least d × (n + 1)${\mathbf{d}}×\left(\mathit{n}+1\right)$ if rcord = 2${\mathbf{rcord}}=2$
The values of the Wiener sample paths.
Let Xp,ik${X}_{p,i}^{k}$ denote the k$k$th dimension of the i$i$th point of the p$p$th sample path where 1kd$1\le k\le {\mathbf{d}}$, 1in + 1$1\le i\le \mathit{n}+1$ and 1pnpaths$1\le p\le {\mathbf{npaths}}$. The point Xp,ik${X}_{p,i}^{k}$ is stored at B(p,k + (i1) × d)$B\left(p,k+\left(i-1\right)×{\mathbf{d}}\right)$. The starting value start is never stored, whereas the terminal value is always stored.
3:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
On entry, rcomm was not initialized or has been corrupted. On entry, rcomm was not initialized or has been corrupted. On entry, rcomm was not initialized or has been corrupted.
ifail = 2${\mathbf{ifail}}=2$
Constraint: npaths1${\mathbf{npaths}}\ge 1$.
ifail = 3${\mathbf{ifail}}=3$
On entry, the value of rcord is invalid.
ifail = 4${\mathbf{ifail}}=4$
Constraint: d1${\mathbf{d}}\ge 1$.
ifail = 5${\mathbf{ifail}}=5$
Constraint: a = 0​ or ​1${\mathbf{a}}=0\text{​ or ​}1$.
ifail = 6${\mathbf{ifail}}=6$
Constraint: ldzd × (ntimes + 1a)$\mathit{ldz}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1-{\mathbf{a}}\right)$.
Constraint: ldznpaths$\mathit{ldz}\ge {\mathbf{npaths}}$.
ifail = 7${\mathbf{ifail}}=7$
ldc is too small.
ifail = 8${\mathbf{ifail}}=8$
Constraint: ldbd × (ntimes + 1)$\mathit{ldb}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1\right)$.
Constraint: ldbnpaths$\mathit{ldb}\ge {\mathbf{npaths}}$.
ifail = 999${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Not applicable.

None.

## Example

```function nag_rand_bb_example
% Get information required to set up the bridge
[bgord,t0,tend,ntimes,intime,nmove,move] = get_bridge_init_data();

% Make the bridge construction bgord
[times, ifail] = nag_rand_bb_make_bridge_order(t0, tend, intime, move, 'bgord', bgord);

% Initialize the Brownian bridge generator
[rcomm, ifail] = nag_rand_bb_init(t0, tend, times);

% Get additional information required by the bridge generator
[npaths, d, start, term, c] = get_bridge_gen_data();

% Generate the Z values
[z] = get_z(npaths, d, ntimes);

% Call the Brownian bridge generator routine
[z, b, ifail] = nag_rand_bb(npaths, start, term, z, c, rcomm);

% Display the results
for i = 1:npaths
fprintf('Weiner Path %d, %d time steps, %d dimensions\n', i, ntimes+1, d);
w = transpose(reshape(b(:,i), d, ntimes+1));
ifail = nag_file_print_matrix_real_gen('G', ' ', w, '');
fprintf('\n');
end

function [bgord,t0,tend,ntimes,intime,nmove,move] = get_bridge_init_data()
% Set the basic parameters for a Wiener process
t0 = 0;
ntimes = int64(10);

% We want to generate the Wiener process at these time points
intime = double(1:ntimes) + t0;
tend = t0 + double(ntimes) + 1;

nmove= int64(0);
move = zeros(nmove, 1, 'int64');
bgord = int64(3);
function [npaths,d,start,term,c] = get_bridge_gen_data();
% Set the basic parameters for a non-free Wiener process
npaths = int64(2);
d = 3;
start = zeros(d, 1);
term  = [1, 0.5, 0];

% We want the following covariance matrix
c = [6, 1, -0.2; 1, 5, 0.3; -0.2, 0.3, 4];

% nag_rand_bb works with the Cholesky factorization of the covariance matrix c
% so perform the decomposition
[c, info] = nag_lapack_dpotrf('l', c);
if info ~= 0
error('Specified covariance matrix is not positive definite: info=%d', info);
end
function [z] = get_z(npaths, d, ntimes)
a = int64(1); % Non-free Wiener process (the default)
idim = d*(ntimes+1-a);

% We now need to generate the input pseudorandom points

% First initialize the base pseudorandom number generator
state = initialize_prng(int64(6), int64(0), [int64(1023401)]);

% Scrambled quasi-random sequences preserve the good discrepancy
% properties of quasi-random sequences while counteracting the bias
% some applications experience when using quasi-random sequences.
% Initialize the scrambled quasi-random generator.
[iref, state] = initialize_scrambled_qrng(int64(1), int64(2), idim, state);

% Generate the quasi-random points from N(0,1)
xmean = zeros(idim, 1);
std   = ones(idim, 1);
[z, iref, ifail] = nag_rand_quasi_normal(xmean, std, npaths, iref);
z = z';
function [state] = initialize_prng(genid, subid, seed)
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);

function [iref, state] = initialize_scrambled_qrng(genid,stype,idim,state)
iskip = int64(0);
nsdigits = int64(32);
[iref, state, ifail] = nag_rand_quasi_init_scrambled(genid, stype, int64(idim), iskip, nsdigits, state);
```
```
Weiner Path 1, 11 time steps, 3 dimensions
1          2          3
1     -1.0602    -2.8701    -0.9415
2     -3.0575    -1.9502     0.2596
3     -6.8274    -2.4434     0.4597
4     -5.2855    -3.4475     0.0795
5     -8.1784    -5.2296    -0.0921
6     -4.6874    -5.0220     1.4862
7     -3.0959    -4.8623    -4.4076
8     -2.9605    -1.8936    -3.9539
9     -5.4685    -2.3856    -3.2031
10      0.1205    -5.0520    -1.0385
11      1.0000     0.5000     0.0000

Weiner Path 2, 11 time steps, 3 dimensions
1          2          3
1      0.6564     3.5142     1.5911
2     -2.3773     3.1618     3.0316
3      0.3020     6.8815     2.0875
4     -0.2169     4.6026     1.1982
5     -2.0684     4.1503     2.4758
6     -5.1075     3.7303     2.7563
7     -3.8497     3.6682     2.4827
8     -1.8292     4.4153     0.1916
9     -2.0649     0.6952    -2.1201
10      0.1962     1.7769    -5.7685
11      1.0000     0.5000     0.0000

```
```function g05xb_example
% Get information required to set up the bridge
[bgord,t0,tend,ntimes,intime,nmove,move] = get_bridge_init_data();

% Make the bridge construction bgord
[times, ifail] = g05xe(t0, tend, intime, move, 'bgord', bgord);

% Initialize the Brownian bridge generator
[rcomm, ifail] = g05xa(t0, tend, times);

% Get additional information required by the bridge generator
[npaths, d, start, term, c] = get_bridge_gen_data();

% Generate the Z values
[z] = get_z(npaths, d, ntimes);

% Call the Brownian bridge generator routine
[z, b, ifail] = g05xb(npaths, start, term, z, c, rcomm);

% Display the results
for i = 1:npaths
fprintf('Weiner Path %d, %d time steps, %d dimensions\n', i, ntimes+1, d);
w = transpose(reshape(b(:,i), d, ntimes+1));
ifail = x04ca('G', ' ', w, '');
fprintf('\n');
end

function [bgord,t0,tend,ntimes,intime,nmove,move] = get_bridge_init_data()
% Set the basic parameters for a Wiener process
t0 = 0;
ntimes = int64(10);

% We want to generate the Wiener process at these time points
intime = double(1:ntimes) + t0;
tend = t0 + double(ntimes) + 1;

nmove= int64(0);
move = zeros(nmove, 1, 'int64');
bgord = int64(3);
function [npaths,d,start,term,c] = get_bridge_gen_data();
% Set the basic parameters for a non-free Wiener process
npaths = int64(2);
d = 3;
start = zeros(d, 1);
term  = [1, 0.5, 0];

% We want the following covariance matrix
c = [6, 1, -0.2; 1, 5, 0.3; -0.2, 0.3, 4];

% g05xb works with the Cholesky factorization of the covariance matrix c
% so perform the decomposition
[c, info] = f07fd('l', c);
if info ~= 0
error('Specified covariance matrix is not positive definite: info=%d', info);
end
function [z] = get_z(npaths, d, ntimes)
a = int64(1); % Non-free Wiener process (the default)
idim = d*(ntimes+1-a);

% We now need to generate the input pseudorandom points

% First initialize the base pseudorandom number generator
state = initialize_prng(int64(6), int64(0), [int64(1023401)]);

% Scrambled quasi-random sequences preserve the good discrepancy
% properties of quasi-random sequences while counteracting the bias
% some applications experience when using quasi-random sequences.
% Initialize the scrambled quasi-random generator.
[iref, state] = initialize_scrambled_qrng(int64(1), int64(2), idim, state);

% Generate the quasi-random points from N(0,1)
xmean = zeros(idim, 1);
std   = ones(idim, 1);
[z, iref, ifail] = g05yj(xmean, std, npaths, iref);
z = z';
function [state] = initialize_prng(genid, subid, seed)
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);

function [iref, state] = initialize_scrambled_qrng(genid,stype,idim,state)
iskip = int64(0);
nsdigits = int64(32);
[iref, state, ifail] = g05yn(genid, stype, int64(idim), iskip, nsdigits, state);
```
```
Weiner Path 1, 11 time steps, 3 dimensions
1          2          3
1     -1.0602    -2.8701    -0.9415
2     -3.0575    -1.9502     0.2596
3     -6.8274    -2.4434     0.4597
4     -5.2855    -3.4475     0.0795
5     -8.1784    -5.2296    -0.0921
6     -4.6874    -5.0220     1.4862
7     -3.0959    -4.8623    -4.4076
8     -2.9605    -1.8936    -3.9539
9     -5.4685    -2.3856    -3.2031
10      0.1205    -5.0520    -1.0385
11      1.0000     0.5000     0.0000

Weiner Path 2, 11 time steps, 3 dimensions
1          2          3
1      0.6564     3.5142     1.5911
2     -2.3773     3.1618     3.0316
3      0.3020     6.8815     2.0875
4     -0.2169     4.6026     1.1982
5     -2.0684     4.1503     2.4758
6     -5.1075     3.7303     2.7563
7     -3.8497     3.6682     2.4827
8     -1.8292     4.4153     0.1916
9     -2.0649     0.6952    -2.1201
10      0.1962     1.7769    -5.7685
11      1.0000     0.5000     0.0000

```