G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG05XDF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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.

## 2  Specification

 SUBROUTINE G05XDF ( NPATHS, RCORD, D, A, DIFF, Z, LDZ, C, LDC, B, LDB, RCOMM, IFAIL)
 INTEGER NPATHS, RCORD, D, A, LDZ, LDC, LDB, IFAIL REAL (KIND=nag_wp) DIFF(D), Z(LDZ,*), C(LDC,*), B(LDB,*), RCOMM(*)

## 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 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$
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  Parameters

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:     $\mathrm{NPATHS}$ – INTEGERInput
On entry: the number of Wiener sample paths.
Constraint: ${\mathbf{NPATHS}}\ge 1$.
2:     $\mathrm{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:     $\mathrm{D}$ – INTEGERInput
On entry: the dimension of each Wiener sample path.
Constraint: ${\mathbf{D}}\ge 1$.
4:     $\mathrm{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:     $\mathrm{DIFF}\left({\mathbf{D}}\right)$ – REAL (KIND=nag_wp) arrayInput
On entry: the difference between the terminal value and starting value of the Wiener process. If ${\mathbf{A}}=0$, DIFF is ignored.
6:     $\mathrm{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 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:     $\mathrm{LDZ}$ – INTEGERInput
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:     $\mathrm{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.
9:     $\mathrm{LDC}$ – INTEGERInput
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:   $\mathrm{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 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:   $\mathrm{LDB}$ – INTEGERInput
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:   $\mathrm{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 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:   $\mathrm{IFAIL}$ – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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 parameter, 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

Not applicable.

## 8  Parallelism 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.

## 10  Example

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 = 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 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.1  Program Text

Program Text (g05xdfe.f90)

None.

### 10.3  Program Results

Program Results (g05xdfe.r)