# NAG FL Interfaceg05xdf (bb_​inc)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g05xdf 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 routine g05xcf must be called prior to the first call to g05xdf.

## 2Specification

Fortran Interface
 Subroutine g05xdf ( d, a, diff, 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) :: diff(d), c(ldc,*), rcomm(*) Real (Kind=nag_wp), Intent (Inout) :: z(ldz,*), b(ldb,*)
#include <nag.h>
 void g05xdf_ (const Integer *npaths, const Integer *rcord, const Integer *d, const Integer *a, const double diff[], double z[], const Integer *ldz, const double c[], const Integer *ldc, double b[], const Integer *ldb, const double rcomm[], Integer *ifail)
The routine may be called by the names g05xdf or nagf_rand_bb_inc.

## 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 g05xcf. 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$
Glasserman P (2004) Monte Carlo Methods in Financial Engineering Springer

## 5Arguments

Note: the following variable is used in the parameter descriptions: $N={\mathbf{ntimes}}$, the length of the array times passed to the initialization routine g05xcf.
1: $\mathbf{npaths}$Integer Input
On entry: the number of Wiener sample paths.
Constraint: ${\mathbf{npaths}}\ge 1$.
2: $\mathbf{rcord}$Integer Input
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}$Integer Input
On entry: the dimension of each Wiener sample path.
Constraint: ${\mathbf{d}}\ge 1$.
4: $\mathbf{a}$Integer Input
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)$Real (Kind=nag_wp) array Input
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({\mathbf{ldz}},*\right)$Real (Kind=nag_wp) array Input/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 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{ldz}$Integer Input
On entry: the first dimension of the array z as declared in the (sub)program from which g05xdf is called.
Constraints:
• if ${\mathbf{rcord}}=1$, ${\mathbf{ldz}}\ge {\mathbf{d}}×\left(N+1-{\mathbf{a}}\right)$;
• if ${\mathbf{rcord}}=2$, ${\mathbf{ldz}}\ge {\mathbf{npaths}}$.
8: $\mathbf{c}\left({\mathbf{ldc}},*\right)$Real (Kind=nag_wp) array Input
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.
9: $\mathbf{ldc}$Integer Input
On entry: the first dimension of the array c as declared in the (sub)program from which g05xdf is called.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{d}}$.
10: $\mathbf{b}\left({\mathbf{ldb}},*\right)$Real (Kind=nag_wp) array Output
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 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{ldb}$Integer Input
On entry: the first dimension of the array b as declared in the (sub)program from which g05xdf is called.
Constraints:
• if ${\mathbf{rcord}}=1$, ${\mathbf{ldb}}\ge {\mathbf{d}}×\left(N+1\right)$;
• if ${\mathbf{rcord}}=2$, ${\mathbf{ldb}}\ge {\mathbf{npaths}}$.
12: $\mathbf{rcomm}\left(*\right)$Real (Kind=nag_wp) array Communication 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 g05xcf or g05xdf.
On entry: communication array as returned by the last call to g05xcf or g05xdf. This array must not be directly modified.
13: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

g05xdf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g05xdf 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

The scaled Wiener increments produced by this routine 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 = f(t,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 g05xdf 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.1Program Text

Program Text (g05xdfe.f90)

None.

### 10.3Program Results

Program Results (g05xdfe.r)