g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_rand_bb_inc (g05xdc)

## 1  Purpose

nag_rand_bb_inc (g05xdc) 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 (g05xcc) must be called prior to the first call to nag_rand_bb_inc (g05xdc).

## 2  Specification

 #include #include
 void nag_rand_bb_inc (Nag_OrderType order, Integer npaths, Integer d, Integer a, const double diff[], double z[], Integer pdz, const double c[], Integer pdc, double b[], Integer pdb, const double rcomm[], NagError *fail)

## 3  Description

For details on the Brownian bridge algorithm and the bridge construction order see Section 2.6 in the g05 Chapter Introduction and Section 3 in nag_rand_bb_inc_init (g05xcc). 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 ${t}_{0}, let ${\left({t}_{i}\right)}_{1\le i\le N}$ be any set of time points satisfying ${t}_{0}<{t}_{1}<{t}_{2}<\cdots <{t}_{N}, and let ${X}_{{t}_{0}}$, ${\left({X}_{{t}_{i}}\right)}_{1\le i\le N}$, ${X}_{T}$ denote a $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$, 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$
The example program in Section 10 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.

## 4  References

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

## 5  Arguments

Note: the following variable is used in the parameter descriptions: $N={\mathbf{ntimes}}$, the length of the array times passed to the initialization function nag_rand_bb_inc_init (g05xcc).
1:    $\mathbf{order}$Nag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2:    $\mathbf{npaths}$IntegerInput
On entry: the number of Wiener sample paths.
Constraint: ${\mathbf{npaths}}\ge 1$.
3:    $\mathbf{d}$IntegerInput
On entry: the dimension of each Wiener sample path.
Constraint: ${\mathbf{d}}\ge 1$.
4:    $\mathbf{a}$IntegerInput
On entry: if ${\mathbf{a}}=0$, a free Wiener process is created and diff is ignored.
If ${\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.
Constraint: ${\mathbf{a}}=0$ or $1$.
5:    $\mathbf{diff}\left[{\mathbf{d}}\right]$const doubleInput
On entry: the difference between the terminal value and starting value of the Wiener process. If ${\mathbf{a}}=0$, diff is ignored.
6:    $\mathbf{z}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array z must be at least
• ${\mathbf{pdz}}×{\mathbf{npaths}}$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$;
• ${\mathbf{pdz}}×\left({\mathbf{d}}×\left(\mathit{N}+1-{\mathbf{a}}\right)\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$.
The $\left(i,j\right)$th element of the matrix $Z$ is stored in
• ${\mathbf{z}}\left[\left(j-1\right)×{\mathbf{pdz}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{z}}\left[\left(i-1\right)×{\mathbf{pdz}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the Normal random numbers used to construct the sample paths.
If quasi-random numbers are used, the ${\mathbf{d}}×\left(N+1-{\mathbf{a}}\right)$-dimensional quasi-random points should be stored in successive rows of $Z$.
On exit: the Normal random numbers premultiplied by c.
7:    $\mathbf{pdz}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array z.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdz}}\ge {\mathbf{d}}×\left(N+1-{\mathbf{a}}\right)$;
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdz}}\ge {\mathbf{npaths}}$.
8:    $\mathbf{c}\left[\mathit{dim}\right]$const doubleInput
Note: the dimension, dim, of the array c must be at least ${\mathbf{pdc}}×{\mathbf{d}}$.
The $\left(i,j\right)$th element of the matrix $C$ is stored in ${\mathbf{c}}\left[\left(j-1\right)×{\mathbf{pdc}}+i-1\right]$.
On entry: the lower triangular Cholesky factorization $C$ such that $C{C}^{\mathrm{T}}$ gives the covariance matrix of the Wiener process. Elements of $C$ above the diagonal are not referenced.
9:    $\mathbf{pdc}$IntegerInput
On entry: the stride separating matrix row elements in the array c.
Constraint: ${\mathbf{pdc}}\ge {\mathbf{d}}$.
10:  $\mathbf{b}\left[\mathit{dim}\right]$doubleOutput
Note: the dimension, dim, of the array b must be at least
• ${\mathbf{pdb}}×{\mathbf{npaths}}$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$;
• ${\mathbf{pdb}}×\left({\mathbf{d}}×\left(\mathit{N}+1\right)\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$.
The $\left(i,j\right)$th element of the matrix $B$ is stored in
• ${\mathbf{b}}\left[\left(j-1\right)×{\mathbf{pdb}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{b}}\left[\left(i-1\right)×{\mathbf{pdb}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On exit: the scaled Wiener increments.
Let ${X}_{p,i}^{k}$ denote the $k$th dimension of the $i$th point of the $p$th sample path where $1\le k\le {\mathbf{d}}$, $1\le i\le \mathit{N}+1$ and $1\le p\le {\mathbf{npaths}}$. The increment $\frac{\left({X}_{p,i}^{k}-{X}_{p,i-1}^{k}\right)}{\left({t}_{i}-{t}_{i-1}\right)}$ is stored at $B\left(p,k+\left(i-1\right)×{\mathbf{d}}\right)$.
11:  $\mathbf{pdb}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array b.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdb}}\ge {\mathbf{d}}×\left(N+1\right)$;
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdb}}\ge {\mathbf{npaths}}$.
12:  $\mathbf{rcomm}\left[\mathit{dim}\right]$const doubleCommunication Array
Note: the dimension, $\mathit{dim}$, 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 (g05xcc) or nag_rand_bb_inc (g05xdc).
On entry: communication array as returned by the last call to nag_rand_bb_inc_init (g05xcc) or nag_rand_bb_inc (g05xdc). This array MUST NOT be directly modified.
13:  $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.2.1.2 in the Essential Introduction for further information.
NE_ARRAY_SIZE
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{d}}×\left({\mathbf{ntimes}}+1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1\right)$.
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}\ge {\mathbf{npaths}}$.
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}\ge 〈\mathit{\text{value}}〉$.
On entry, ${\mathbf{pdz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{d}}×\left({\mathbf{ntimes}}+1-{\mathbf{a}}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdz}}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1-{\mathbf{a}}\right)$.
On entry, ${\mathbf{pdz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdz}}\ge {\mathbf{npaths}}$.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_ILLEGAL_COMM
On entry, rcomm was not initialized or has been corrupted.
NE_INT
On entry, ${\mathbf{a}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{a}}=0\text{​ or ​}1$.
On entry, ${\mathbf{d}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{d}}\ge 1$.
On entry, ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{npaths}}\ge 1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 3.6.6 in the Essential Introduction for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 3.6.5 in the Essential Introduction for further information.

Not applicable.

## 8  Parallelism and Performance

nag_rand_bb_inc (g05xdc) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_rand_bb_inc (g05xdc) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

None.

## 10  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
 $d Yt = ft,Yt dt + σt,Yt dXt$
on the interval $\left[{t}_{0},T\right]$ where ${\left({X}_{t}\right)}_{{t}_{0}\le t\le T}$ is a (free or non-free) Wiener process and $f$ and $\sigma$ are suitable functions. A numerical solution to this SDE can be obtained by the Euler–Maruyama method. For any discretization ${t}_{0}<{t}_{1}<{t}_{2}<\cdots <{t}_{N+1}=T$ of $\left[{t}_{0},T\right]$, set
 $Y ti+1 = Y ti + f ti,Yti ti+1 - ti + σ ti,Yti Xti+1 - Xti$
for $i=1,\dots ,N$ so that ${Y}_{{t}_{N+1}}$ is an approximation to ${Y}_{T}$. The scaled Wiener increments produced by nag_rand_bb_inc (g05xdc) can be used in the Euler–Maruyama scheme outlined above by writing
 $Yti+1 = Yti + ti+1 - ti f ti,Yti + σ ti,Yti Xti+1 - Xti ti+1 - ti .$
The following example program uses this method to solve the SDE for geometric Brownian motion
 $d St = rSt dt + σSt dXt$
where $X$ is a Wiener process, and compares the results against the analytic solution
 $ST = S0 exp r-σ2/2 T + σXT .$
Quasi-random variates are used to construct the Wiener increments.

### 10.1  Program Text

Program Text (g05xdce.c)

None.

### 10.3  Program Results

Program Results (g05xdce.r)