hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_bb (g05xb)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 Brownian Bridge in the G05 Chapter Introduction and Description in nag_rand_bb_init (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, 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.
2:     startd – double array
The starting value of the Wiener process.
3:     termd – double array
The terminal value at which the non-free Wiener process should end. If a=0, term is ignored.
4:     zldz: – double array
The first dimension, ldz, of the array z must satisfy
  • if rcord=1, ldzd×n+1-a;
  • if rcord=2, ldznpaths.
The second dimension of the array z must be at least npaths if rcord=1 and at least d×n+1-a if rcord=2.
The Normal random numbers used to construct the sample paths.
If rcord=1 and quasi-random numbers are used, the d×n+1-a, where n=nint rcomm2-dimensional quasi-random points should be stored in successive columns of z.
If rcord=2 and quasi-random numbers are used, the d×n+1-a, where n=nint rcomm2-dimensional quasi-random points should be stored in successive rows of z.
5:     cldc: – double array
The first dimension of the array c must be at least d.
The second dimension of the array c must be at least d.
The lower triangular Cholesky factorization C such that CCT gives the covariance matrix of the Wiener process. Elements of C above the diagonal are not referenced.
6:     rcomm* – 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_init (g05xa) or nag_rand_bb (g05xb).
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
Default: 1
The order in which Normal random numbers are stored in z and in which the generated values are returned in b.
Constraint: rcord=1 or 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.
3:     a int64int32nag_int scalar
Default: 1
If a=0, a free Wiener process is created beginning at start and term is ignored.
If a=1, a non-free Wiener process is created beginning at start and ending at term.
Constraint: a=0 or 1.

Output Parameters

1:     zldz: – double array
The first dimension, ldz, of the array z will be
  • if rcord=1, ldz=d×n+1-a;
  • if rcord=2, ldz=npaths.
The second dimension of the array z will be npaths if rcord=1 and at least d×n+1-a if rcord=2.
The Normal random numbers premultiplied by C.
2:     bldb: – double array
The first dimension, ldb, of the array b will be
  • if rcord=1, ldb=d×n+1;
  • if rcord=2, ldb=npaths.
The second dimension of the array b will be npaths if rcord=1 and at least d×n+1 if rcord=2.
The values of the Wiener sample paths.
Let Xp,ik denote the kth dimension of the ith point of the pth sample path where 1kd, 1in+1 and 1pnpaths.
If rcord=1, the point Xp,ik will be stored at bk+i-1×d,p.
If rcord=2, the point Xp,ik will be stored at bp,k+i-1×d.
The starting value start is never stored, whereas the terminal value is always stored.
3:     ifail int64int32nag_int scalar
ifail=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
On entry, rcomm was not initialized or has been corrupted.
   ifail=2
Constraint: npaths1.
   ifail=3
On entry, rcord=_ was an illegal value.
   ifail=4
Constraint: d1.
   ifail=5
Constraint: a=0​ or ​1.
   ifail=6
Constraint: ldzd×ntimes+1-a.
Constraint: ldznpaths.
   ifail=7
ldc is too small.
   ifail=8
Constraint: ldbd×ntimes+1.
Constraint: ldbnpaths.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

None.

Example

This example calls nag_rand_bb (g05xb), nag_rand_bb_init (g05xa) and nag_rand_bb_make_bridge_order (g05xe) to generate two sample paths of a three-dimensional non-free Wiener process. The process starts at zero and each sample path terminates at the point 1.0,0.5,0.0. Quasi-random numbers are used to construct the sample paths.
See Example in nag_rand_bb_init (g05xa) and nag_rand_bb_make_bridge_order (g05xe) for additional examples.
function g05xb_example


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

% 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;
  n = 10;
  ntimes = int64(n);

  % We want to generate the Wiener process at these time points
  intime = 1:n + t0;
  tend = t0 + n + 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];

  % Cholesky factorize the covariance matrix c
  [c, info] = f07fd('l', c);

function [z] = get_z(npaths, d, ntimes)
  % Non-free Wiener process (the default)
  a = int64(1); 
  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);
g05xb example results

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


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015