PDF version (NAG web site
, 64-bit version, 64-bit version)
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,
dif,
z,
c,
rcomm, 'rcord',
rcord, 'd',
d, 'a',
a)
[
z,
b,
ifail] = nag_rand_bb_inc(
npaths,
dif,
z,
c,
rcomm, 'rcord',
rcord, 'd',
d, 'a',
a)
Description
For details on the Brownian bridge algorithm and the bridge construction order see
Brownian Bridge in the G05 Chapter Introduction and
Description in
nag_rand_bb_inc_init (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 , let be any set of time points satisfying , and let , , denote a -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
, and then uses this to compute the
scaled Wiener increments
The example program in
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:
, the length of the array
times passed to the initialization function
nag_rand_bb_inc_init (g05xc).
Compulsory Input Parameters
- 1:
– int64int32nag_int scalar
-
The number of Wiener sample paths.
Constraint:
.
- 2:
– double array
-
The difference between the terminal value and starting value of the Wiener process. If
,
dif is ignored.
- 3:
– double array
-
The first dimension,
, of the array
z must satisfy
- if , ;
- if , .
The second dimension of the array
z must be at least
if
and at least
if
.
The Normal random numbers used to construct the sample paths.
If quasi-random numbers are used, the -dimensional quasi-random points should be stored in successive rows of .
- 4:
– double array
-
The first dimension of the array
c must be at least
.
The second dimension of the array
c must be at least
.
The lower triangular Cholesky factorization such that gives the covariance matrix of the Wiener process. Elements of above the diagonal are not referenced.
- 5:
– double array
-
Note: the dimension of this array is dictated by the requirements of associated functions that must have been previously called. This array
must be the same array passed as argument
rcomm in the previous call to
nag_rand_bb_inc_init (g05xc) or
nag_rand_bb_inc (g05xd).
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:
– int64int32nag_int scalar
Default:
The order in which Normal random numbers are stored in
z and in which the generated values are returned in
b.
Constraint:
or .
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
dif 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:
.
- 3:
– int64int32nag_int scalar
Default:
If
, a free Wiener process is created and
dif is ignored.
If
, a non-free Wiener process is created where
dif is the difference between the terminal value and the starting value of the process.
Constraint:
or .
Output Parameters
- 1:
– double array
-
The first dimension,
, of the array
z will be
- if , ;
- if , .
The second dimension of the array
z will be
if
and at least
if
.
The Normal random numbers premultiplied by
c.
- 2:
– double array
-
The first dimension,
, of the array
b will be
- if , ;
- if , .
The second dimension of the array
b will be
if
and at least
if
.
The scaled Wiener increments.
Let denote the th dimension of the th point of the th sample path where , and . The increment is stored at .
- 3:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry,
rcomm was not initialized or has been corrupted.
-
-
Constraint: .
-
-
On entry, was an illegal value.
-
-
Constraint: .
-
-
Constraint: .
-
-
Constraint: .
Constraint: .
-
-
ldc is too small.
-
-
Constraint: .
Constraint: .
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
Not applicable.
Further Comments
None.
Example
The scaled Wiener increments produced by this function can be used to compute numerical solutions to stochastic differential equations (SDEs) driven by (free or non-free) Wiener processes. Consider an SDE of the form
on the interval
where
is a (free or non-free) Wiener process and
and
are suitable functions. A numerical solution to this SDE can be obtained by the Euler–Maruyama method. For any discretization
of
, set
for
so that
is an approximation to
. The scaled Wiener increments produced by
nag_rand_bb_inc (g05xd) can be used in the
Euler–Maruyama scheme outlined above by writing
The following example program uses this method to solve the SDE for geometric Brownian motion
where
is a Wiener process, and compares the results against the analytic solution
Quasi-random variates are used to construct the Wiener increments.
Open in the MATLAB editor:
g05xd_example
function g05xd_example
fprintf('g05xd example results\n\n');
s0 = 1;
r = 0.05;
sigma = 0.12;
npaths = int64(3);
t0 = 0;
tend = 1;
tn = 20;
t = t0 + (1:tn)*(tend-t0)/(tn+1);
bgord = int64(3);
move = zeros(0, 1, 'int64');
[times, ifail] = g05xe( ...
t0, tend, t, move, 'bgord', bgord);
d = 1;
a = int64(0);
[z] = get_z(npaths, d, a, tn);
[rcomm, ifail] = g05xc( ...
t0, tend, times);
dif = zeros(d, 1);
c = ones(d, 1);
[z, b, ifail] = g05xd( ...
npaths, dif, z, c, rcomm, 'a', a);
st = zeros(tn+1, npaths);
st(1,:) = s0 + (t(1)-t0)*(r*s0+sigma*s0*b(1, :));
v = st(1,:);
for i=2:tn
v = v + (t(i)-t(i-1))*(r*v+sigma*v.*b(i,:));
st(i,:) = v;
end
st(tn+1,:) = v + (tend-t(tn))*(r*v+sigma*v.*b(tn+1,:));
analytic = s0*exp((r-0.5*sigma^2)*tend+sigma*sqrt(tend-t0)*z(1,:));
fprintf('\nEuler-Maruyama solution for Geometric Brownian motion\n');
fprintf(' Path 1 Path 2 Path 3\n');
for i = 1:tn+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);
state = initialize_prng(int64(6), int64(0), [int64(1023401)]);
[iref, state] = initialize_scrambled_qrng(int64(1), int64(2), ...
idim, state);
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)
[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);
g05xd example results
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
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015