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_init (g05xa)

Purpose

nag_rand_bb_init (g05xa) initializes the Brownian bridge generator nag_rand_bb (g05xb). It must be called before any calls to nag_rand_bb (g05xb).

Syntax

[rcomm, ifail] = g05xa(t0, tend, times, 'ntimes', ntimes)
[rcomm, ifail] = nag_rand_bb_init(t0, tend, times, 'ntimes', ntimes)

Description

Brownian Bridge Algorithm

Details on the Brownian bridge algorithm and the Brownian bridge process (sometimes also called a non-free Wiener process) can be found in Section [Brownian Bridge] in the G05 Chapter Introduction. We briefly recall some notation and definitions.
Fix two times t0 < Tt0<T and let (ti)1iN (ti) 1iN  be any set of time points satisfying t0 < t1 < t2 <⋯< tN < Tt0<t1<t2<⋯<tN<T. Let (X ti )1iN (Xti) 1iN  denote a dd-dimensional Wiener sample path at these time points, and let CC be any dd by dd matrix such that CCTCCT is the desired covariance structure for the Wiener process. Each point XtiXti of the sample path is constructed according to the Brownian bridge interpolation algorithm (see Glasserman (2004) or Section [Brownian Bridge] in the G05 Chapter Introduction). We always start at some fixed point Xt0 = xd Xt0 = xd . If we set XT = x + C sqrt(Tt0) Z XT =x+ C T-t0 Z  where ZZ is any dd-dimensional standard Normal random variable, then XX will behave like a normal (free) Wiener process. However if we fix the terminal value XT = wd XT = wd , then XX will behave like a non-free Wiener process.

Implementation

Given the start and end points of the process, the order in which successive interpolation times tjtj are chosen is called the bridge construction order. The construction order is given by the array times. Further information on construction orders is given in Section [Brownian Bridge Algorithm] in the G05 Chapter Introduction. For clarity we consider here the common scenario where the Brownian bridge algorithm is used with quasi-random points. If pseudorandom numbers are used instead, these details can be ignored.
Suppose we require PP Wiener sample paths each of dimension dd. The main input to the Brownian bridge algorithm is then an array of quasi-random points Z1,Z2 ,…, ZPZ1,Z2,…,ZP where each point Zp = (Z1p,Z2p,,ZDp) Zp = (Z1p,Z2p,,ZDp)  has dimension D = d(N + 1)D=d(N+1) or D = dND=dN respectively, depending on whether a free or non-free Wiener process is required. When nag_rand_bb (g05xb) is called, the ppth sample path for 1pP1pP is constructed as follows: if a non-free Wiener process is required set XTXT equal to the terminal value ww, otherwise construct XTXT as
XT = Xt_0 + C sqrt(T − t0)
[ Z1p ⋮ Zdp ]
XT = Xt0 + C T-t0 [ Z1p Zdp ]
where CC is the matrix described in Section [Brownian Bridge Algorithm]. The array times holds the remaining time points t1 , t2 ,… tN t1 , t2 ,… tN  in the order in which the bridge is to be constructed. For each j = 1 ,…, Nj=1,…,N set r = times(j)r=timesj, find
q = max { t0, times(i) : 1i < j , times(i) < r }
q = max { t0, timesi : 1i<j , timesi < r }
and
s = min {T, times(i) : 1i < j , times(i) > r }
s = min {T, timesi : 1i<j , timesi > r }
and construct the point XrXr as
Xr = ( Xq (s − r) + Xs (r − q) )/( s − q ) + C sqrt( ( (s − r) (r − q) )/( (s − q) ) )
[ Zjd − ad + 1p ⋮ Zjd − ad + dp ]
Xr = Xq (s-r) + Xs (r-q) s-q + C (s-r) (r-q) (s-q) [ Zjd-ad+1p Zjd-ad+dp ]
where a = 0a=0 or a = 1a=1 respectively depending on whether a free or non-free Wiener process is required. Note that in our discussion jj is indexed from 11, and so XrXr is interpolated between the nearest (in time) Wiener points which have already been constructed. The function nag_rand_bb_make_bridge_order (g05xe) can be used to initialize the times array for several predefined bridge construction orders.

References

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

Parameters

Compulsory Input Parameters

1:     t0 – double scalar
The starting value t0t0 of the time interval.
2:     tend – double scalar
The end value TT of the time interval.
Constraint: tend > t0tend>t0.
3:     times(ntimes) – double array
ntimes, the dimension of the array, must satisfy the constraint ntimes1ntimes1.
The points in the time interval (t0,T)(t0,T) at which the Wiener process is to be constructed. The order in which points are listed in times determines the bridge construction order. The function nag_rand_bb_make_bridge_order (g05xe) can be used to create predefined bridge construction orders from a set of input times.
Constraints:
  • t0 < times(i) < tendt0<timesi<tend, for i = 1,2,,ntimesi=1,2,,ntimes;
  • times(i) times(j)timesi timesj, for i,j = 1,2,ntimesi,j=1,2,ntimes and ijij.

Optional Input Parameters

1:     ntimes – int64int32nag_int scalar
Default: The dimension of the array times.
The length of times, denoted by NN in Section [Brownian Bridge Algorithm].
Constraint: ntimes1ntimes1.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     rcomm(12 × (ntimes + 1)12×(ntimes+1)) – double array
Communication array, used to store information between calls to nag_rand_bb (g05xb). This array must not be directly modified.
2:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
Constraint: tend > t0tend>t0.
  ifail = 2ifail=2
Constraint: ntimes1ntimes1.
  ifail = 3ifail=3
Constraint: t0 < times(i) < tendt0<times(i)<tend for all ii.
  ifail = 4ifail=4
Constraint: all elements of times must be unique.

Accuracy

Not applicable.

Further Comments

The efficient implementation of a Brownian bridge algorithm requires the use of a workspace array called the working stack. Since previously computed points will be used to interpolate new points, they should be kept close to the hardware processing units so that the data can be accessed quickly. Ideally the whole stack should be held in hardware cache. Different bridge construction orders may require different amounts of working stack. Indeed, a naive bridge algorithm may require a stack of size N/4 N 4  or even N/2 N 2 , which could be very inefficient when NN is large. nag_rand_bb_init (g05xa) performs a detailed analysis of the bridge construction order specified by times. Heuristics are used to find an execution strategy which requires a small working stack, while still constructing the bridge in the order required.

Example

function nag_rand_bb_init_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,a,term,c] = get_bridge_gen_data();

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

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

% 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,a,term,c] = get_bridge_gen_data();
  % Set the basic parameters for a free Wiener process
  npaths = int64(2);
  d = 3;
  a = int64(0);
  start = zeros(d, 1);
  term  = zeros(d, 1);

  % As a=0, term need not be initialized

  % 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, 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)]);

  % Generate the pseudorandom points from N(0,1)
  [state, z, ifail] = nag_rand_dist_normal(int64(idim*npaths), 0, 1, state);

  z = reshape(z, idim, npaths);
function [state] = initialize_prng(genid, subid, seed)
  % Initialize the generator to a repeatable sequence
  [state, ifail] = nag_rand_init_repeat(genid, subid, seed)
 

state =

                   61
                 4826
                    6
                    0
                    0
            816500395
                    0
            305647340
                    0
            460678824
                    0
            582503647
                    0
            529178745
                    0
            547734508
                    0
                    0
                    0
              1403580
                    0
               810728
                    0
               527612
                    0
                    0
                    0
              1370589
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                 -209
                    0
               -22853
                    0
                 4826
                    1
                    1
                 4826


ifail =

                    0

Weiner Path 1, 11 time steps, 3 dimensions
              1          2          3
  1      1.6020     0.5611     1.6975
  2      1.2767     0.3972    -1.7199
  3     -0.1895    -0.8812    -5.1908
  4     -2.8083    -4.4484    -6.7697
  5     -5.6251    -6.0375    -3.2551
  6     -6.5404    -6.2009    -5.5638
  7     -4.6398    -4.9675    -7.4454
  8     -5.3501    -4.8563    -9.9002
  9     -7.1683    -7.2638    -9.7825
 10     -1.9440    -7.0725   -10.7577
 11     -4.9941    -3.5442   -10.1561

Weiner Path 2, 11 time steps, 3 dimensions
              1          2          3
  1      2.6097     6.2430     0.0316
  2      3.5442     4.2836     2.5742
  3      1.3068     6.1511     4.5362
  4      2.7487     8.6021     2.6880
  5      3.4584     6.1778    -0.6274
  6      0.5965     8.3014     0.5933
  7     -3.2701     5.4787     1.0727
  8     -4.7527     7.0988     0.9120
  9     -4.9375     7.9486     0.7657
 10     -7.1302     7.3180     0.2706
 11     -0.6289     9.8866    -2.2762


function g05xa_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,a,term,c] = get_bridge_gen_data();

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

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

% 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,a,term,c] = get_bridge_gen_data();
  % Set the basic parameters for a free Wiener process
  npaths = int64(2);
  d = 3;
  a = int64(0);
  start = zeros(d, 1);
  term  = zeros(d, 1);

  % As a=0, term need not be initialized

  % 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, 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)]);

  % Generate the pseudorandom points from N(0,1)
  [state, z, ifail] = g05sk(int64(idim*npaths), 0, 1, state);

  z = reshape(z, idim, npaths);
function [state] = initialize_prng(genid, subid, seed)
  % Initialize the generator to a repeatable sequence
  [state, ifail] = g05kf(genid, subid, seed)
 

state =

                   61
                 4826
                    6
                    0
                    0
            816500395
                    0
            305647340
                    0
            460678824
                    0
            582503647
                    0
            529178745
                    0
            547734508
                    0
                    0
                    0
              1403580
                    0
               810728
                    0
               527612
                    0
                    0
                    0
              1370589
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                 -209
                    0
               -22853
                    0
                 4826
                    1
                    1
                 4826


ifail =

                    0

Weiner Path 1, 11 time steps, 3 dimensions
              1          2          3
  1      1.6020     0.5611     1.6975
  2      1.2767     0.3972    -1.7199
  3     -0.1895    -0.8812    -5.1908
  4     -2.8083    -4.4484    -6.7697
  5     -5.6251    -6.0375    -3.2551
  6     -6.5404    -6.2009    -5.5638
  7     -4.6398    -4.9675    -7.4454
  8     -5.3501    -4.8563    -9.9002
  9     -7.1683    -7.2638    -9.7825
 10     -1.9440    -7.0725   -10.7577
 11     -4.9941    -3.5442   -10.1561

Weiner Path 2, 11 time steps, 3 dimensions
              1          2          3
  1      2.6097     6.2430     0.0316
  2      3.5442     4.2836     2.5742
  3      1.3068     6.1511     4.5362
  4      2.7487     8.6021     2.6880
  5      3.4584     6.1778    -0.6274
  6      0.5965     8.3014     0.5933
  7     -3.2701     5.4787     1.0727
  8     -4.7527     7.0988     0.9120
  9     -4.9375     7.9486     0.7657
 10     -7.1302     7.3180     0.2706
 11     -0.6289     9.8866    -2.2762



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–2013