Finance - Maple Programming Help

Home : Support : Online Help : Mathematics : Finance : Stochastic Processes : Finance/SamplePath

Finance

 SamplePath
 generate sample path(s) for a stochastic process

 Calling Sequence SamplePath(expression, t = timeinterval, opts) SamplePath(pathgenerator, opts) SamplePath(process, timegrid, opts) SamplePath(process, timeinterval, opts) SamplePath(transform, pathgenerator, opts) SamplePath(d, transform, pathgenerator, opts)

Parameters

 expression - algebraic expression; expression whose sample path is to be generated t - name; time variable for use in expression timeinterval - range; time interval pathgenerator - path generator data structure; path generator process - one- or multi-dimensional stochastic process, or list or vector of one-dimensional stochastic processes timegrid - list, Vector or time grid data structure; time grid transform - procedure; path function d - positive integer; dimension of the new process opts - (optional) equation(s) of the form option = value where option is one of replications or timesteps; specify options for the SamplePath command

Options

 • replications = posint -- This option specifies the number of replications of the sample path. By default, only one replication of the sample path is generated.
 • timesteps = posint -- This option specifies the number of time steps. This option is ignored if explicit time grid is specified. By default, only one time step is used.

Description

 • The SamplePath command generates sample paths for a given stochastic process. This command can handle one- as well as multi-dimensional processes.
 • In the one-dimensional case, the value returned by SamplePath is a matrix of size $m×n$, where $m$ is the requested number of replications of the sample path (see the replications option) and $n$ is the number of points in the time grid (the number of time steps plus one). Each row of this matrix is a single realization of the sample path of the stochastic process of interest. In the multi-dimensional case, the value returned by SamplePath is a matrix of size $m×d×n$, where $m$ and $n$ are the same as in the one-dimensional case, and $d$ is the dimension of the stochastic process to be simulated, which is either given explicitly or deduced from the dimension of the underlying stochastic process.
 • The SamplePath(expression, t = timeinterval, opts) calling sequence attempts to extract all the stochastic variables involved in expression and generate the corresponding path generator and path function using the time grid or the specified number of time steps. In this case, only a one-dimensional path can be generated. The parameter timeinterval must be of type range ${T}_{0}..{T}_{1}$, where ${T}_{0}$ and ${T}_{1}$ are non-negative constants such that ${T}_{0}<{T}_{1}$.
 Note that if $0<{T}_{0}$, the value at ${T}_{0}$ will be simulated using a single step of the default discretization method and hence can suffer from a significant discretization bias. Increasing the number of time steps will refine the grid between ${T}_{0}$ and ${T}_{1}$, but will have no effect on the value at ${T}_{0}$. To reduce the bias, use a time interval of the form $0..{T}_{1}$. All stochastic variables involved in expression should be of the form $X\left(t\right)$. If $X$ is multi-dimensional stochastic, then the individual components of $X$ can be accessed using the notation ${X\left(t\right)}_{i}$.
 • The SamplePath(pathgenerator, opts) calling sequence generates the specified number of replications of the sample path using the path generator pathgenerator. For more details, see Finance[PathGenerator].
 • The SamplePath(transform, pathgenerator, opts) calling sequence generates sample paths for a certain one-dimensional stochastic process given a path generator pathgenerator and a procedure transform that converts paths generated by pathgenerator to paths of the stochastic process of interest. Note that pathgenerator can be multi-dimensional.
 • To generate a single realization of the sample path of interest, SamplePath generates a sample path for the base stochastic process using the path generator pathgenerator and stores these values as an Array, for example $A$. If pathgenerator is one-dimensional, $A$ is a one-dimensional array of size $n$. If pathgenerator is multi-dimensional, $A$ is a two-dimensional array of size $k×n$, where $k$ is the dimension of pathgenerator. Then SamplePath evaluates $f\left(B,A\right)$, where $B$ is a preallocated one-dimensional floating-point array of the same size as $A$. The values stored in $B$ are used as the new replication of the sample path and stored as a row in the resulting matrix. This process will be repeated for the specified number of replications.
 • The SamplePath(d, transform, pathgenerator, opts) calling sequence uses the same method, but with a multi-dimensional process. Therefore, $A$ is now of size $d×n$. Note the difference between the SamplePath(transform, pathgenerator, opts) and SamplePath(1, transform, pathgenerator, opts) calling sequences. The first returns an array of size $m×n$, while the other returns an array of size $m×1×n$.
 • The SamplePath command uses the Maple random number generator (see RandomTools[MersenneTwister]) to generate the intermediate random numbers required by the corresponding algorithm. As a consequence, all values generated by SamplePath are deterministic, meaning the same values are generated every Maple session. Use randomize or RandomTools[MersenneTwister][SetState] to randomize the state of the internal random number generator.

Examples

 > $\mathrm{with}\left(\mathrm{Finance}\right):$
 > $X≔\mathrm{WienerProcess}\left(\right):$
 > $W≔\mathrm{WienerProcess}\left(⟨⟨1.0,0.5⟩|⟨0.5,1.0⟩⟩\right):$

Create 20 replications of the sample path of $X$ on the interval $0..1$ using $100$ time steps.

 > $A≔\mathrm{SamplePath}\left(X\left(t\right),t=0..1,\mathrm{timesteps}=100,\mathrm{replications}=20\right)$
  (1)

Here is a single realization of the sample path.

 > ${A}_{1}$
 $\left[\begin{array}{ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc}{0.}& {-0.0479341933636560}& {0.0304831766096739}& {-0.0195654808735083}& {-0.0686705707668586}& {-0.0448877354743149}& {-0.0597117995227733}& {-0.0667870713913599}& {0.0397963580520930}& {-0.105148853029448}& {-0.0479708892965665}& {-0.0218140578984559}& {-0.100893888585543}& {0.0140572284636797}& {0.0427091250604421}& {-0.0763599603940362}& {-0.0772598666935107}& {-0.0986045638585020}& {-0.100345031537427}& {-0.109285711411873}& {-0.129208412735232}& {-0.0734470367274836}& {-0.108906790268056}& {-0.120147198054830}& {-0.124027954889153}& {-0.139774674176570}& {-0.112857123800552}& {-0.0412751646322613}& {-0.0926848920010449}& {-0.144355796471781}& {-0.0814867521289317}& {-0.0151408064417854}& {-0.0135865370034655}& {0.0689736695416643}& {0.102596815099606}& {0.0710806720255644}& {0.0376770566032572}& {-0.0197167435219005}& {-0.0649895992994265}& {-0.0799454645899813}& {-0.237778734898867}& {-0.319235246826053}& {-0.398903454308261}& {-0.387199978484117}& {-0.276257966271294}& {-0.338824956475097}& {-0.288680728874785}& {-0.307265303838278}& {-0.363513134778663}& {-0.446193768660670}& {-0.355268240197160}& {-0.454330626879113}& {-0.566154085267234}& {-0.731811810337136}& {-0.479593124375999}& {-0.368635137156733}& {-0.285315810032091}& {-0.354444630943346}& {-0.298435300339297}& {-0.275810494539844}& {-0.154608023445884}& {-0.261295844541879}& {-0.312915887226655}& {-0.303054548953834}& {-0.241615443357456}& {-0.137141162441418}& {-0.189306241738097}& {-0.176862720766993}& {-0.136675434349325}& {-0.0956141970101758}& {-0.0983399888386455}& {-0.172970974505231}& {-0.234135125123993}& {-0.381289887341551}& {-0.352563234358239}& {-0.260738224872286}& {-0.386247591757898}& {-0.401600388792696}& {-0.288315387864818}& {-0.279780352637731}& {-0.340396970972017}& {-0.121587871804732}& {-0.113134227132579}& {0.0277415702464636}& {0.0515935002267239}& {-0.0262427703054549}& {-0.0210246018930152}& {-0.0878551107749035}& {-0.0731588370407880}& {-0.0549408524513844}& {0.192573478688965}& {0.246364468931367}& {0.343405994985747}& {0.0849207290141026}& {0.0147631556225880}& {-0.153463517770626}& {-0.171620988832669}& {-0.239049950923769}& {-0.294012476760913}& {-0.223726267622059}& {-0.261781173761270}\end{array}\right]$ (2)

Here is an example involving a two-dimensional process.

 > $B≔\mathrm{SamplePath}\left({W\left(t\right)}_{1}+{W\left(t\right)}_{2},t=0..1,\mathrm{timesteps}=100,\mathrm{replications}=20\right)$
  (3)
 > ${B}_{1}$
 $\left[\begin{array}{ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc}{0.}& {-0.0446476095933288}& {-0.147923427675440}& {-0.263004388774307}& {-0.343656197700827}& {0.00302364489783069}& {-0.0616008779746269}& {-0.288375288199879}& {-0.697241828209834}& {-0.526521810313209}& {-0.337019943653995}& {-0.204904623778670}& {-0.108816885111216}& {-0.204633414028342}& {-0.215451451494870}& {-0.201381498586102}& {-0.544715135442531}& {-0.511651701359765}& {-0.591298067986934}& {-0.598915389977074}& {-0.543702050237471}& {-0.563541690177637}& {-0.555225833186224}& {-0.649830031451228}& {-0.715952454434735}& {-0.911407814342725}& {-0.981955933227156}& {-1.02649924692099}& {-1.29020345744018}& {-1.18826201783084}& {-1.07344397726087}& {-1.02142095191339}& {-0.979494320087719}& {-0.933598508122424}& {-1.15131662832551}& {-1.18844836259691}& {-1.04231634265651}& {-0.694848221318310}& {-0.706307620706273}& {-0.730309607561081}& {-1.02276701370709}& {-1.02949263925387}& {-1.20275528578372}& {-1.05425599499636}& {-1.24101733884272}& {-1.05295552653113}& {-0.737305087370064}& {-0.891050265744144}& {-1.09142019944558}& {-0.920729237059212}& {-1.14244394845497}& {-1.18251233547386}& {-1.27370034859969}& {-1.41358903105429}& {-1.58425310765601}& {-1.78789041203833}& {-1.77272918607169}& {-1.94096199306346}& {-1.94237118096188}& {-1.74455278571435}& {-1.77469865842910}& {-1.54732630291377}& {-1.63517939400490}& {-1.21678580655447}& {-0.845918610174121}& {-0.633004208232345}& {-0.591495777319722}& {-0.373719056162875}& {-0.619418563011077}& {-0.773199897701747}& {-0.961638515828715}& {-0.956239579605641}& {-1.01737483374367}& {-1.04501114796537}& {-1.15032959849386}& {-1.12590669969349}& {-0.905672481755369}& {-1.30621136867085}& {-1.55510357465659}& {-1.38591235362190}& {-1.27433331031152}& {-1.38137569224361}& {-1.05820625576738}& {-1.39307606964198}& {-1.24187114498614}& {-1.15426322298845}& {-1.22737462806078}& {-1.34806853652357}& {-1.39425964726129}& {-1.34490263454750}& {-1.28579010299582}& {-1.20078978989264}& {-1.01309026198000}& {-1.31708709688007}& {-1.55704296953705}& {-1.77308497526639}& {-1.88391263120827}& {-1.95589208457071}& {-2.10509350086576}& {-2.27846946991868}& {-2.25676929166191}\end{array}\right]$ (4)

Create path generators for $X$ and $W$ on the interval $0..5$ and use them to generate sample paths for $X$ and $W$.

 > $P≔\mathrm{PathGenerator}\left(X,\mathrm{TimeGrid}\left(5,100\right)\right):$
 > $A≔\mathrm{SamplePath}\left(P,\mathrm{replications}=20\right)$
  (5)
 > $M≔\mathrm{PathGenerator}\left(W,\mathrm{TimeGrid}\left(5,100\right)\right):$
 > $B≔\mathrm{SamplePath}\left(M,\mathrm{replications}=20\right)$
  (6)

Here are realizations of the sample paths.

 > ${A}_{1}$
 $\left[\begin{array}{ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc}{0.}& {0.179089877591003}& {0.0841682869907233}& {0.0445396325630757}& {0.277582487242290}& {0.111255660069248}& {-0.138433995866310}& {-0.0341263760031961}& {-0.541695054435285}& {-0.853171565913784}& {-0.974159481865011}& {-1.04768491153744}& {-0.859950047154223}& {-0.913522368403430}& {-0.954184417907333}& {-0.519071250071515}& {-0.563951858290380}& {-0.400698989841174}& {-0.446478026422464}& {-0.327386073471089}& {-0.156702947423265}& {-0.138173479758539}& {-0.263702293902665}& {-0.418709473660026}& {-0.266650002947036}& {-0.200228598345017}& {-0.214343823808412}& {-0.0375122486466324}& {0.0409865488540140}& {0.377861827713081}& {0.597107660637591}& {0.455757094628624}& {0.618760442287351}& {0.631623893413706}& {0.582475610080751}& {0.466248831222143}& {0.689925417645035}& {0.870427207077037}& {1.10595892867562}& {1.29496913447595}& {1.21797994018953}& {1.60763075462912}& {1.62472912775154}& {1.45981101124726}& {1.48787969086614}& {1.55575371888499}& {1.59490746867051}& {1.75002813043260}& {1.68951083085843}& {1.64812121498828}& {2.12912571967895}& {2.06088721641186}& {2.33305709214899}& {2.55900328395835}& {1.98176891146744}& {1.96582967455996}& {2.55663150898460}& {2.49331510249644}& {2.51264817858826}& {2.63683048487408}& {2.43807208261899}& {2.43963728670200}& {2.53835401722610}& {2.35257875044343}& {2.48807848566208}& {2.35655208056866}& {2.46955934404504}& {2.52755314314159}& {2.44864681630052}& {2.49247729918723}& {2.69747509498832}& {2.32310302315864}& {2.45302519239857}& {2.27692581193719}& {2.23088460502326}& {2.07936087599135}& {1.99884306570257}& {2.00323163873970}& {1.74288771408887}& {1.43192605015146}& {1.31687360051414}& {1.29447827648299}& {1.55191941948409}& {1.42748185723014}& {1.28948120605928}& {1.51800977549844}& {1.78331495176649}& {1.94287193713097}& {1.70043969113962}& {1.22937688254845}& {0.977604433093408}& {0.901829000926684}& {1.20218885269869}& {1.30138654211794}& {1.11635465516390}& {0.882583867362423}& {0.704166316308675}& {0.894671691038036}& {1.06560422936571}& {1.03029774420078}& {1.06301930244219}\end{array}\right]$ (7)
 > ${B}_{1}$
  (8)

Use the above path generator to generate sample paths for the process ${ⅇ}^{A\left(t\right)}$.

 > f := proc(B, A) local d, i; d := rtable_dims(A); for i from lhs(d) to rhs(d) do B[i] := exp(A[i]) end do; end proc;
 ${f}{:=}{\mathbf{proc}}\left({B}{,}{A}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{d}{,}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{d}{:=}{\mathrm{rtable_dims}}{}\left({A}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{from}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{lhs}}{}\left({d}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{rhs}}{}\left({d}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{B}{[}{i}{]}{:=}{\mathrm{exp}}{}\left({A}{[}{i}{]}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (9)
 > $\mathrm{SamplePath}\left(f,P,\mathrm{replications}=20\right)$
  (10)

Simulate the one-dimensional process ${W\left(t\right)}_{1}+{W\left(t\right)}_{2}$ where $W$ is the previously defined two-dimensional Wiener process.

 > f2 := proc(B, A) local d, i; d := rtable_dims(A); for i from lhs(d) to rhs(d) do B[i] := A[1, i]+A[2, i] end do; end proc:
 > $\mathrm{SamplePath}\left(\mathrm{f2},M,\mathrm{replications}=20\right)$
  (11)

This is the same example using the last calling sequence. Note the difference between procedures $\mathrm{f2}$ and $\mathrm{F2}$.

 > F2 := proc(B, A) local d, i; d := rtable_dims(A); for i from lhs(d) to rhs(d) do B[1, i] := A[1, i] + A[2, i]; end do; end proc:
 > $\mathrm{SamplePath}\left(1,\mathrm{F2},M,\mathrm{replications}=20\right)$
  (12)

Simulate the three-dimensional process $⟨{W\left(t\right)}_{1},{W\left(t\right)}_{2},{W\left(t\right)}_{1}+{W\left(t\right)}_{2}⟩$ where $W$ is the same two-dimensional Wiener process.

 > F := proc(B, A) local d, i; d := rtable_dims(A); for i from lhs(d) to rhs(d) do B[1, i] := A[1, i]; B[2, i] := A[2, i]; B[3, i] := A[1, i] + A[2, i]; end do; end proc:
 > $\mathrm{SamplePath}\left(3,F,M,\mathrm{replications}=20\right)$
  (13)

Simulate an expression involving the stochastic variable just defined. Call SamplePath using two different forms: algebraic and procedural. To check for consistency, save the state of the Maple random number generator and restore this state before the second simulation.

 > $S≔\mathrm{RandomTools}:-\mathrm{GetState}\left(\right):$

Use the same state of the random number generator to compare the results.

 > $\mathrm{RandomTools}:-\mathrm{SetState}\left(\mathrm{state}=S\right):$
 > $\mathrm{A1}≔\mathrm{SamplePath}\left({X\left(t\right)}^{2},t=0..10,\mathrm{timesteps}=10\right)$
  (14)

This is the same example as above but using a Maple procedure.

 > $\mathrm{RandomTools}:-\mathrm{SetState}\left(\mathrm{state}=S\right):$
 > f := proc(B, A) local i; for i from 1 to 11 do B[i] := A[i]^2; end do; end proc:
 > $\mathrm{A2}≔\mathrm{SamplePath}\left(f,X,0..10,\mathrm{timesteps}=10\right)$
  (15)

This is the same example using an explicitly defined time grid.

 > $T≔\mathrm{TimeGrid}\left(\left[0,1,2,3,4,5,6,7,8,9,10\right]\right)$
 ${T}{:=}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (16)

This is a multi-dimensional path that depends on one stochastic factor.

 > $\mathrm{SamplePath}\left(⟨-X\left(t\right),X\left(t\right)⟩,t=0..10,\mathrm{timesteps}=10\right)$
  (17)

This is a multivariate stochastic process.

 > $\mathrm{Σ}≔⟨⟨1.0,0.5⟩|⟨0.5,1.0⟩⟩$
 ${\mathrm{\Sigma }}{≔}\left[\begin{array}{cc}{1.0}& {0.5}\\ {0.5}& {1.0}\end{array}\right]$ (18)
 > $W≔\mathrm{WienerProcess}\left(\mathrm{Σ}\right)$
 ${W}{≔}{\mathrm{_W1}}$ (19)
 > $\mathrm{SamplePath}\left(W\left(t\right),t=0..3,\mathrm{timesteps}=10,\mathrm{replications}={10}^{4}\right)$
  (20)
 > $S≔\mathrm{SamplePath}\left(⟨{W\left(t\right)}_{1},{W\left(t\right)}_{2},{W\left(t\right)}_{1}+{W\left(t\right)}_{2}⟩,t=0..3,\mathrm{timesteps}=9,\mathrm{replications}={10}^{4}\right)$
  (21)

Here is the first replication of the sample path.

 > $\mathrm{map}\left({\mathrm{evalf}}_{5},{S}_{1}\right)$
 $\left[\begin{array}{cccccccccc}{0.}& {0.31717}& {-0.16490}& {-1.1906}& {-1.4647}& {-1.5699}& {-1.6450}& {-1.9399}& {-1.5251}& {-1.3382}\\ {0.}& {0.060433}& {-0.35577}& {-1.0683}& {-0.69408}& {-0.12185}& {-0.22109}& {-0.98585}& {-1.2004}& {-1.1880}\\ {0.}& {0.37760}& {-0.52067}& {-2.2589}& {-2.1587}& {-1.6918}& {-1.8661}& {-2.9258}& {-2.7255}& {-2.5262}\end{array}\right]$ (22)

Here is an example of a stochastic process that depends on two factors: one is the previously defined Wiener process; the second is a Poisson process.

 > $J≔\mathrm{PoissonProcess}\left(0.3\right)$
 ${J}{≔}{\mathrm{_P}}$ (23)
 > $S≔\mathrm{SamplePath}\left(5\left(J\left(t\right)-t\right)+{W\left(t\right)}_{1},t=0..3,\mathrm{timesteps}=100,\mathrm{replications}=10\right)$
  (24)
 > $\mathrm{PathPlot}\left(S,\mathrm{timegrid}=0..3,\mathrm{color}=\mathrm{red}..\mathrm{blue},\mathrm{thickness}=3\right)$ Here are some expressions involving stochastic variables.

 > $Y≔\mathrm{WienerProcess}\left(\right)$
 ${Y}{≔}{\mathrm{_W2}}$ (25)
 > $Z≔t→{ⅇ}^{0.05t+0.3Y\left(t\right)}$
 ${Z}{≔}{t}{↦}{{ⅇ}}^{{0.05}{}{t}{+}{0.3}{}{Y}{}\left({t}\right)}$ (26)
 > $U≔t→\mathrm{maximize}\left(Z\left(u\right),u=0..t\right)$
 ${U}{≔}{t}{↦}{\mathrm{maximize}}{}\left({Z}{}\left({u}\right){,}{u}{=}{0}{..}{t}\right)$ (27)
 > $S≔\mathrm{SamplePath}\left(⟨Z\left(t\right),U\left(t\right)⟩,t=0..3,\mathrm{timesteps}=100,\mathrm{replications}=5\right)$
  (28)
 > $V≔t→\frac{1\left({∫}_{0}^{t}Z\left(u\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}ⅆu\right)}{t}$
 ${V}{≔}{t}{↦}\frac{{{\int }}_{{0}}^{{t}}{Z}{}\left({u}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{u}}{{t}}$ (29)
 > $S≔\mathrm{SamplePath}\left(⟨Z\left(t\right),V\left(t\right)⟩,t=0..3,\mathrm{timesteps}=100,\mathrm{replications}=5\right)$
  (30)

Here is the use of a path function.

 > testproc := proc(Y, X) for i from 1 to 11 do Y[i] := exp(X[i]); end do; end proc:
 Warning, i is implicitly declared local to procedure testproc
 > $S≔\mathrm{SamplePath}\left(\mathrm{testproc},Y,0..3,\mathrm{timesteps}=10,\mathrm{replications}=10\right)$
  (31)
 > $\mathrm{PathPlot}\left(S,\mathrm{timegrid}=0..3,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}..\mathrm{blue}\right)$ References

 Glasserman, P., Monte Carlo Methods in Financial Engineering. New York: Springer-Verlag, 2004.
 Hull, J., Options, Futures, and Other Derivatives, 5th. edition. Upper Saddle River, New Jersey: Prentice Hall, 2003.
 Kloeden, P., and Platen, E., Numerical Solution of Stochastic Differential Equations, New York: Springer-Verlag, 1999.

Compatibility

 • The Finance[SamplePath] command was introduced in Maple 15.