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_inc (g05xd)

## Purpose

nag_rand_bb_inc (g05xd) computes scaled increments of sample paths of a free or non-free Wiener process, where the sample paths are constructed by a Brownian bridge algorithm. The initialization function nag_rand_bb_inc_init (g05xc) must be called prior to the first call to nag_rand_bb_inc (g05xd).

## Syntax

[z, b, ifail] = g05xd(npaths, diff, z, c, rcomm, 'rcord', rcord, 'd', d, 'a', a)
[z, b, ifail] = nag_rand_bb_inc(npaths, diff, 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 (g05xc). 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.
Fix two times t0 < T${t}_{0}, let (ti)1in ${\left({t}_{i}\right)}_{1\le i\le \mathit{n}}$ be any set of time points satisfying t0 < t1 < t2 < < tn < T${t}_{0}<{t}_{1}<{t}_{2}<\cdots <{t}_{\mathit{n}}, and let Xt0${X}_{{t}_{0}}$, (X ti )1in ${\left({X}_{{t}_{i}}\right)}_{1\le i\le \mathit{n}}$, XT${X}_{T}$ denote a d$d$-dimensional Wiener sample path at these time points.
The Brownian bridge increments generator uses the Brownian bridge algorithm to construct sample paths for the (free or non-free) Wiener process X$X$, and then uses this to compute the scaled Wiener increments
 ( Xt1 − Xt0 )/( t1 − t0 ) , ( Xt2 − Xt1 )/( t2 − t1 ) , … , ( Xtn − Xtn − 1 )/( tn − tn − 1 ) , ( XT − Xtn )/( T − tn ) $Xt1 - Xt0 t1 - t0 , Xt2 - Xt1 t2 - t1 ,…, Xtn - Xtn-1 tn - tn-1 , XT - Xtn T - tn$
The example program in Section [Example] shows how these increments can be used to compute a numerical solution to a stochastic differential equation (SDE) driven by a (free or non-free) Wiener process.

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

### Compulsory Input Parameters

1:     npaths – int64int32nag_int scalar
The number of Wiener sample paths.
Constraint: npaths1${\mathbf{npaths}}\ge 1$.
2:     diff(d) – double array
d, the dimension of the array, must satisfy the constraint d1${\mathbf{d}}\ge 1$.
The difference between the terminal value and starting value of the Wiener process. If a = 0${\mathbf{a}}=0$, diff is ignored.
3:     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$.
4:     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.
5:     rcomm(12 × (ntimes + 1)$12×\left(\mathit{ntimes}+1\right)$) – double array
Communication array as returned by the last call to nag_rand_bb_inc_init (g05xc) or nag_rand_bb_inc (g05xd). 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 array diff 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 and diff is ignored.
If a = 1${\mathbf{a}}=1$, a non-free Wiener process is created where diff is the difference between the terminal value and the starting value of the process.
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 scaled Wiener increments.
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 increment ((Xp,ikXp,i1k))/((titi1))$\frac{\left({X}_{p,i}^{k}-{X}_{p,i-1}^{k}\right)}{\left({t}_{i}-{t}_{i-1}\right)}$ is stored at B(p,k + (i1) × d)$B\left(p,k+\left(i-1\right)×{\mathbf{d}}\right)$.
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_inc_example
% We wish to solve the stochastic differential equation (SDE)
%   dSt = r*St*dt + sigma*St*dXt
% where X is a one dimensional Wiener process.
% This means we have
%     a = 0
%     d = 1
%     c = 1
% We now set the other parameters of the SDE and the Euler-Maruyama scheme

% Initial value of the process
s0 = 1;
r = 0.05;
sigma = 0.12;

% Number of paths to simulate
npaths = int64(3);

% The time interval [t0,T] on which to solve the SDE
t0   = 0;
tend = 1;

% The time steps in the discretization of [t0,T]
ntimesteps = 20;
t = t0 + (1:ntimesteps)*(tend-t0)/(ntimesteps+1);

% Make the bridge construction order
bgord = int64(3);
move = zeros(0, 1, 'int64');
[times, ifail] = nag_rand_bb_make_bridge_order(t0, tend, t, move, 'bgord', bgord);

% Generate the Z values
d = 1;
a = int64(0);
[z] = get_z(npaths, d, a, ntimesteps);

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

% Get the scaled increments of the Wiener process
dif = zeros(d, 1);
c   = ones(d, 1);
[z, b, ifail] = nag_rand_bb_inc(npaths, dif, z, c, rcomm, 'a', a);

% Do the Euler-Maruyama time stepping
st = zeros(ntimesteps+1, npaths);

% Do first time step
st(1,:) = s0 + (t(1)-t0)*(r*s0+sigma*s0*b(1, :));
for i=2:ntimesteps
st(i,:) = st(i-1,:) + (t(i)-t(i-1))*(r*st(i-1,:)+sigma*st(i-1,:).*b(i,:));
end
% Do last time step
st(ntimesteps+1,:) = st(ntimesteps,:) + (tend-t(ntimesteps))*...
(r*st(ntimesteps,:)+sigma*st(ntimesteps,:).*b(ntimesteps+1,:));

% Compute the analytic solution:
%    ST = S0*exp( (r-sigma^2/2)T + sigma WT )
analytic = s0*exp((r-0.5*sigma*sigma)*tend+sigma*sqrt(tend-t0)*z(1,:));

% Display the results
fprintf('\nEuler-Maruyama solution for Geometric Brownian motion\n');
fprintf('        Path 1     Path 2     Path 3\n');
for i = 1:ntimesteps+1
fprintf('%2d  %10.4f %10.4f %10.4f\n', i, st(i, :));
end
fprintf('\nAnalytic solution at final time step\n');
fprintf('        Path 1     Path 2     Path 3\n');
fprintf('    %10.4f %10.4f %10.4f\n', analytic);

function [z] = get_z(npaths, d, a, ntimes)
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);

Euler-Maruyama solution for Geometric Brownian motion
Path 1     Path 2     Path 3
1      0.9668     1.0367     0.9992
2      0.9717     1.0254     1.0077
3      0.9954     1.0333     1.0098
4      0.9486     1.0226     0.9911
5      0.9270     1.0113     1.0630
6      0.8997     1.0127     1.0164
7      0.8955     1.0138     1.0771
8      0.8953     0.9953     1.0691
9      0.8489     1.0462     1.0484
10      0.8449     1.0592     1.0429
11      0.8158     1.0233     1.0625
12      0.7997     1.0384     1.0729
13      0.8025     1.0138     1.0725
14      0.8187     1.0499     1.0554
15      0.8270     1.0459     1.0529
16      0.7914     1.0294     1.0783
17      0.8076     1.0224     1.0943
18      0.8208     1.0359     1.0773
19      0.8190     1.0326     1.0857
20      0.8217     1.0326     1.1095
21      0.8084     0.9695     1.1389

Analytic solution at final time step
Path 1     Path 2     Path 3
0.8079     0.9685     1.1389

function g05xd_example
% We wish to solve the stochastic differential equation (SDE)
%   dSt = r*St*dt + sigma*St*dXt
% where X is a one dimensional Wiener process.
% This means we have
%     a = 0
%     d = 1
%     c = 1
% We now set the other parameters of the SDE and the Euler-Maruyama scheme

% Initial value of the process
s0 = 1;
r = 0.05;
sigma = 0.12;

% Number of paths to simulate
npaths = int64(3);

% The time interval [t0,T] on which to solve the SDE
t0   = 0;
tend = 1;

% The time steps in the discretization of [t0,T]
ntimesteps = 20;
t = t0 + (1:ntimesteps)*(tend-t0)/(ntimesteps+1);

% Make the bridge construction order
bgord = int64(3);
move = zeros(0, 1, 'int64');
[times, ifail] = g05xe(t0, tend, t, move, 'bgord', bgord);

% Generate the Z values
d = 1;
a = int64(0);
[z] = get_z(npaths, d, a, ntimesteps);

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

% Get the scaled increments of the Wiener process
dif = zeros(d, 1);
c   = ones(d, 1);
[z, b, ifail] = g05xd(npaths, dif, z, c, rcomm, 'a', a);

% Do the Euler-Maruyama time stepping
st = zeros(ntimesteps+1, npaths);

% Do first time step
st(1,:) = s0 + (t(1)-t0)*(r*s0+sigma*s0*b(1, :));
for i=2:ntimesteps
st(i,:) = st(i-1,:) + (t(i)-t(i-1))*(r*st(i-1,:)+sigma*st(i-1,:).*b(i,:));
end
% Do last time step
st(ntimesteps+1,:) = st(ntimesteps,:) + (tend-t(ntimesteps))*...
(r*st(ntimesteps,:)+sigma*st(ntimesteps,:).*b(ntimesteps+1,:));

% Compute the analytic solution:
%    ST = S0*exp( (r-sigma^2/2)T + sigma WT )
analytic = s0*exp((r-0.5*sigma*sigma)*tend+sigma*sqrt(tend-t0)*z(1,:));

% Display the results
fprintf('\nEuler-Maruyama solution for Geometric Brownian motion\n');
fprintf('        Path 1     Path 2     Path 3\n');
for i = 1:ntimesteps+1
fprintf('%2d  %10.4f %10.4f %10.4f\n', i, st(i, :));
end
fprintf('\nAnalytic solution at final time step\n');
fprintf('        Path 1     Path 2     Path 3\n');
fprintf('    %10.4f %10.4f %10.4f\n', analytic);

function [z] = get_z(npaths, d, a, ntimes)
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);

Euler-Maruyama solution for Geometric Brownian motion
Path 1     Path 2     Path 3
1      0.9668     1.0367     0.9992
2      0.9717     1.0254     1.0077
3      0.9954     1.0333     1.0098
4      0.9486     1.0226     0.9911
5      0.9270     1.0113     1.0630
6      0.8997     1.0127     1.0164
7      0.8955     1.0138     1.0771
8      0.8953     0.9953     1.0691
9      0.8489     1.0462     1.0484
10      0.8449     1.0592     1.0429
11      0.8158     1.0233     1.0625
12      0.7997     1.0384     1.0729
13      0.8025     1.0138     1.0725
14      0.8187     1.0499     1.0554
15      0.8270     1.0459     1.0529
16      0.7914     1.0294     1.0783
17      0.8076     1.0224     1.0943
18      0.8208     1.0359     1.0773
19      0.8190     1.0326     1.0857
20      0.8217     1.0326     1.1095
21      0.8084     0.9695     1.1389

Analytic solution at final time step
Path 1     Path 2     Path 3
0.8079     0.9685     1.1389