# NAG Library Routine Document

## 1Purpose

g05xbf uses a Brownian bridge algorithm to construct sample paths for a free or non-free Wiener process. The initialization routine g05xaf must be called prior to the first call to g05xbf.

## 2Specification

Fortran Interface
 Subroutine g05xbf ( d, a, term, z, ldz, c, ldc, b, ldb,
 Integer, Intent (In) :: npaths, rcord, d, a, ldz, ldc, ldb Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: start(d), term(d), c(ldc,*), rcomm(*) Real (Kind=nag_wp), Intent (Inout) :: z(ldz,*), b(ldb,*)
#include nagmk26.h
 void g05xbf_ (const Integer *npaths, const Integer *rcord, const Integer *d, const double start[], const Integer *a, const double term[], double z[], const Integer *ldz, const double c[], const Integer *ldc, double b[], const Integer *ldb, const double rcomm[], Integer *ifail)

## 3Description

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 g05xaf. 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.

## 4References

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

## 5Arguments

Note: the following variable is used in the parameter descriptions: $\mathit{N}={\mathbf{ntimes}}$, the length of the array times passed to the initialization routine g05xaf.
1:     $\mathbf{npaths}$ – IntegerInput
On entry: the number of Wiener sample paths to create.
Constraint: ${\mathbf{npaths}}\ge 1$.
2:     $\mathbf{rcord}$ – IntegerInput
On entry: the order in which Normal random numbers are stored in z and in which the generated values are returned in b.
Constraint: ${\mathbf{rcord}}=1$ or $2$.
3:     $\mathbf{d}$ – IntegerInput
On entry: the dimension of each Wiener sample path.
Constraint: ${\mathbf{d}}\ge 1$.
4:     $\mathbf{start}\left({\mathbf{d}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the starting value of the Wiener process.
5:     $\mathbf{a}$ – IntegerInput
On entry: if ${\mathbf{a}}=0$, a free Wiener process is created beginning at start and term is ignored.
If ${\mathbf{a}}=1$, a non-free Wiener process is created beginning at start and ending at term.
Constraint: ${\mathbf{a}}=0$ or $1$.
6:     $\mathbf{term}\left({\mathbf{d}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the terminal value at which the non-free Wiener process should end. If ${\mathbf{a}}=0$, term is ignored.
7:     $\mathbf{z}\left({\mathbf{ldz}},*\right)$ – Real (Kind=nag_wp) arrayInput/Output
Note: the second dimension of the array z must be at least ${\mathbf{npaths}}$ if ${\mathbf{rcord}}=1$ and at least ${\mathbf{d}}×\left(\mathit{N}+1-{\mathbf{a}}\right)$ if ${\mathbf{rcord}}=2$.
On entry: the Normal random numbers used to construct the sample paths.
If ${\mathbf{rcord}}=1$ and quasi-random numbers are used, the ${\mathbf{d}}×\left(\mathit{N}+1-{\mathbf{a}}\right)$, where $\mathit{N}=\mathrm{nint}$ ${\mathbf{rcomm}}\left(2\right)$-dimensional quasi-random points should be stored in successive columns of z.
If ${\mathbf{rcord}}=2$ and quasi-random numbers are used, the ${\mathbf{d}}×\left(\mathit{N}+1-{\mathbf{a}}\right)$, where $\mathit{N}=\mathrm{nint}$ ${\mathbf{rcomm}}\left(2\right)$-dimensional quasi-random points should be stored in successive rows of z.
On exit: the Normal random numbers premultiplied by $C$.
8:     $\mathbf{ldz}$ – IntegerInput
On entry: the first dimension of the array z as declared in the (sub)program from which g05xbf is called.
Constraints:
• if ${\mathbf{rcord}}=1$, ${\mathbf{ldz}}\ge {\mathbf{d}}×\left(\mathit{N}+1-{\mathbf{a}}\right)$;
• if ${\mathbf{rcord}}=2$, ${\mathbf{ldz}}\ge {\mathbf{npaths}}$.
9:     $\mathbf{c}\left({\mathbf{ldc}},*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the second dimension of the array c must be at least ${\mathbf{d}}$.
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.
10:   $\mathbf{ldc}$ – IntegerInput
On entry: the first dimension of the array c as declared in the (sub)program from which g05xbf is called.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{d}}$.
11:   $\mathbf{b}\left({\mathbf{ldb}},*\right)$ – Real (Kind=nag_wp) arrayOutput
Note: the second dimension of the array b must be at least ${\mathbf{npaths}}$ if ${\mathbf{rcord}}=1$ and at least ${\mathbf{d}}×\left(\mathit{N}+1\right)$ if ${\mathbf{rcord}}=2$.
On exit: the values of the Wiener sample paths.
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}}$.
If ${\mathbf{rcord}}=1$, the point ${X}_{p,i}^{k}$ will be stored at ${\mathbf{b}}\left(k+\left(i-1\right)×{\mathbf{d}},p\right)$.
If ${\mathbf{rcord}}=2$, the point ${X}_{p,i}^{k}$ will be stored at ${\mathbf{b}}\left(p,k+\left(i-1\right)×{\mathbf{d}}\right)$.
The starting value start is never stored, whereas the terminal value is always stored.
12:   $\mathbf{ldb}$ – IntegerInput
On entry: the first dimension of the array b as declared in the (sub)program from which g05xbf is called.
Constraints:
• if ${\mathbf{rcord}}=1$, ${\mathbf{ldb}}\ge {\mathbf{d}}×\left(\mathit{N}+1\right)$;
• if ${\mathbf{rcord}}=2$, ${\mathbf{ldb}}\ge {\mathbf{npaths}}$.
13:   $\mathbf{rcomm}\left(*\right)$ – Real (Kind=nag_wp) arrayCommunication 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 g05xaf or g05xbf.
On entry: communication array as returned by the last call to g05xaf or g05xbf. This array must not be directly modified.
14:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, rcomm was not initialized or has been corrupted.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{npaths}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{rcord}}=〈\mathit{\text{value}}〉$ was an illegal value.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{d}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{d}}\ge 1$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{a}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{a}}=0\text{​ or ​}1$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{ldz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{d}}×\left({\mathbf{ntimes}}+1-{\mathbf{a}}\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldz}}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1-{\mathbf{a}}\right)$.
On entry, ${\mathbf{ldz}}=〈\mathit{\text{value}}〉$ and ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldz}}\ge {\mathbf{npaths}}$.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge 〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=8$
On entry, ${\mathbf{ldb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{d}}×\left({\mathbf{ntimes}}+1\right)=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldb}}\ge {\mathbf{d}}×\left({\mathbf{ntimes}}+1\right)$.
On entry, ${\mathbf{ldb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{npaths}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldb}}\ge {\mathbf{npaths}}$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8Parallelism and Performance

g05xbf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05xbf 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 routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

None.

## 10Example

This example calls g05xbf, g05xaf and g05xef 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 $\left(1.0,0.5,0.0\right)$. Quasi-random numbers are used to construct the sample paths.
See Section 10 in g05xaf and g05xef for additional examples.

### 10.1Program Text

Program Text (g05xbfe.f90)

None.

### 10.3Program Results

Program Results (g05xbfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017