SamplePath - Maple Help

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)$
 ${A}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {-0.0479341933636560}& {0.0304831766096739}& {-0.0195654808735083}& {-0.0686705707668586}& {-0.0448877354743149}& {-0.0597117995227733}& {-0.0667870713913599}& {0.0397963580520930}& {-0.105148853029448}& {\dots }\\ {0.}& {-0.117750941636095}& {-0.116934785825332}& {-0.250517496888092}& {-0.331583754083276}& {-0.302578534529765}& {-0.347587013424151}& {-0.298237955550490}& {-0.207473104084077}& {-0.461396253839055}& {\dots }\\ {0.}& {-0.0317098436112035}& {-0.190224613284634}& {-0.395680736838448}& {-0.337571977942103}& {-0.279925886966956}& {-0.318728187342427}& {-0.224029424610407}& {-0.208978284729931}& {-0.118658897120611}& {\dots }\\ {0.}& {-0.181384149447982}& {-0.0844915548194531}& {-0.0999352270370572}& {-0.144858524030824}& {-0.0822805569425558}& {-0.0545780879543684}& {-0.0357954701681366}& {0.00337984482394410}& {0.0732547101501118}& {\dots }\\ {0.}& {0.0303490684701116}& {-0.0206163022715641}& {-0.0160532005643673}& {-0.210988017157376}& {-0.0650101305655686}& {-0.293626970946465}& {-0.409647678749793}& {-0.578509061826582}& {-0.348672428409742}& {\dots }\\ {0.}& {0.150216384950731}& {0.231469604441781}& {0.100091192412553}& {0.156798016302198}& {0.197851735117630}& {0.194564454725022}& {0.0570978498818944}& {0.0617540480980156}& {0.0410574097447373}& {\dots }\\ {0.}& {-0.0964812645296412}& {-0.224339571931091}& {-0.345252896500850}& {-0.248884869331294}& {-0.167112771086766}& {0.0251591669974852}& {0.192627115555397}& {0.257158332469419}& {0.173978954529674}& {\dots }\\ {0.}& {0.192984038318604}& {0.321304595652381}& {0.159538183692487}& {0.302891582213812}& {0.219883254957474}& {0.210725794991766}& {0.480166123441422}& {0.410498687546562}& {0.547323329416928}& {\dots }\\ {0.}& {0.0548035316249174}& {-0.130803135012091}& {-0.243190616864745}& {-0.158189147096027}& {-0.227215674275220}& {-0.169485399401195}& {-0.341977104547747}& {-0.445780971558515}& {-0.421356785948741}& {\dots }\\ {0.}& {-0.168057660811367}& {-0.257509908924896}& {-0.352460896462996}& {-0.306144503874347}& {-0.187187681299347}& {-0.208327023549474}& {-0.274977511695447}& {-0.209802523679318}& {-0.244623091695730}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{20 × 101 Array}}\end{array}$ (1)

Here is a single realization of the sample path.

 > $A\left[1\right]$
 $\left[{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}{,}{\dots }{,}{\text{⋯ 1 Array entries not shown}}\right]$ (2)

Here is an example involving a two-dimensional process.

 > $B≔\mathrm{SamplePath}\left(W\left(t\right)\left[1\right]+W\left(t\right)\left[2\right],t=0..1,\mathrm{timesteps}=100,\mathrm{replications}=20\right)$
 ${B}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {-0.0446476095933288}& {-0.147923427675440}& {-0.263004388774307}& {-0.343656197700827}& {0.00302364489783069}& {-0.0616008779746269}& {-0.288375288199879}& {-0.697241828209834}& {-0.526521810313209}& {\dots }\\ {0.}& {-0.0235242371046852}& {0.103951205485880}& {0.205722772840123}& {0.143600806285698}& {0.118719386724885}& {0.120080552367705}& {0.118890810283558}& {-0.0190542909279948}& {0.394669822912381}& {\dots }\\ {0.}& {0.0296664774116212}& {0.192928919875135}& {0.100489171638744}& {-0.0527650039100835}& {-0.127013661884435}& {-0.389509503935817}& {-0.359005436098644}& {-0.438719013305792}& {-0.536533448150867}& {\dots }\\ {0.}& {0.0105651668526184}& {-0.164704970532703}& {0.00292732813319752}& {0.297934129455047}& {0.314491345564396}& {0.199963259069491}& {0.436673974651707}& {0.533306512496797}& {0.769451869349253}& {\dots }\\ {0.}& {-0.134960317220148}& {-0.0877763812255615}& {0.221831065493857}& {0.344176971213703}& {0.174127149787734}& {-0.113247373574680}& {0.0161416771465625}& {0.222779017202539}& {0.226683935093410}& {\dots }\\ {0.}& {0.121847578487764}& {0.262816135151376}& {0.0280140715495834}& {0.113551657920345}& {0.180640898099765}& {0.108423593911370}& {0.447557612783902}& {0.334910384880317}& {0.314936795064095}& {\dots }\\ {0.}& {-0.0644622586829868}& {-0.188465662740034}& {-0.303939386717596}& {-0.103910642253973}& {-0.0768409168495495}& {-0.0980354337083416}& {-0.153690867854982}& {-0.138427441670583}& {-0.160423616775969}& {\dots }\\ {0.}& {0.238262783546133}& {0.0232411151449511}& {0.278382811551901}& {0.191018216888425}& {0.173304134698574}& {-0.0599709430325005}& {0.0697721513089630}& {-0.189236730974552}& {-0.329155926263107}& {\dots }\\ {0.}& {-0.108237707220779}& {-0.0594763884353466}& {-0.133243691713135}& {-0.125549181433162}& {0.125715327259173}& {0.269551947384731}& {0.382173471005493}& {0.273405741445042}& {0.377002478737866}& {\dots }\\ {0.}& {0.0618501333507068}& {-0.368169170370901}& {-0.326754712466196}& {-0.431095691400244}& {-0.664858845593429}& {-0.743187887739653}& {-0.745926975695828}& {-0.803727741907958}& {-0.600227497556346}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{20 × 101 Array}}\end{array}$ (3)
 > $B\left[1\right]$
 $\left[{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}{,}{\dots }{,}{\text{⋯ 1 Array entries not shown}}\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)$
 ${A}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {0.179089877591003}& {0.0841682869907233}& {0.0445396325630757}& {0.277582487242290}& {0.111255660069248}& {-0.138433995866310}& {-0.0341263760031961}& {-0.541695054435285}& {-0.853171565913784}& {\dots }\\ {0.}& {0.315924088587137}& {0.123815211189781}& {-0.250941617711976}& {-0.260361439321397}& {0.00204384122764739}& {0.0457211199817899}& {-0.0756838423760031}& {0.0459921353778949}& {0.0239876797533367}& {\dots }\\ {0.}& {0.185132073982801}& {0.128684914388447}& {0.272921010210915}& {0.0870452271813354}& {0.491781508711092}& {0.336035968126910}& {0.457012719268348}& {0.436281360401074}& {0.431468055802254}& {\dots }\\ {0.}& {0.216970922846184}& {0.124155592994525}& {-0.0608095307484787}& {-0.253670053390810}& {-0.0622399366200796}& {0.0104216256715259}& {-0.0437935415799468}& {-0.169182886585982}& {0.0154807089753594}& {\dots }\\ {0.}& {-0.144384539810101}& {0.274012716734281}& {-0.392738313426037}& {-0.392146007617872}& {-0.179911338162562}& {-0.217806196643904}& {-0.263828094388565}& {-0.286051367352810}& {-0.104406168830946}& {\dots }\\ {0.}& {-0.383223849165700}& {-0.0107390513807721}& {-0.355198594575376}& {-0.569643512542712}& {-0.904277626336340}& {-1.27478705134778}& {-1.14435002428411}& {-0.996151116472306}& {-1.12394274087875}& {\dots }\\ {0.}& {-0.265295668415084}& {-0.485511615951683}& {-0.777788994144459}& {-0.538519356303157}& {-0.724677960726530}& {-0.950171065126278}& {-0.860059785877894}& {-0.744330708668235}& {-1.12184842219602}& {\dots }\\ {0.}& {-0.0812290043116096}& {-0.0836237340186025}& {-0.242544761654690}& {-0.282614814518276}& {-0.458696404237724}& {-0.286889923441475}& {-0.291794524071756}& {-0.657271877211278}& {-0.635657453677228}& {\dots }\\ {0.}& {0.587838893647384}& {0.643350685184986}& {0.739434992798955}& {1.09085291589115}& {1.11778111787275}& {1.15077362706753}& {1.26493281279011}& {1.20727307158301}& {1.39992649020149}& {\dots }\\ {0.}& {0.0656986672318721}& {0.0533348062719081}& {0.0567279860871317}& {0.531895104871369}& {0.383873228098408}& {0.629065631389632}& {0.331956027445730}& {0.328269122152075}& {0.562406625991730}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{20 × 101 Array}}\end{array}$ (5)
 > $M≔\mathrm{PathGenerator}\left(W,\mathrm{TimeGrid}\left(5,100\right)\right):$
 > $B≔\mathrm{SamplePath}\left(M,\mathrm{replications}=20\right)$
 ${B}{≔}\begin{array}{c}\left[\begin{array}{cc}{0.}& {-1.04924877152758}\\ {0.196818376877418}& {-1.21768535967961}\\ {0.0968577035851758}& {-0.961349174300977}\\ {0.203559988730230}& {-0.908882640840844}\\ {-0.0763550809234677}& {-0.639452601384043}\\ {-0.0991780271091046}& {-0.527327514800969}\\ {-0.308033447109983}& {-0.636791679976937}\\ {-0.306203158155557}& {-0.485163774766738}\\ {-0.265782970860922}& {-0.818116014343444}\\ {-0.238433831967978}& {-0.637169775835448}\\ {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{slice of 20 × 2 × 101 Array}}\end{array}$ (6)

Here are realizations of the sample paths.

 > $A\left[1\right]$
 $\left[{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}{,}{\dots }{,}{\text{⋯ 1 Array entries not shown}}\right]$ (7)
 > $B\left[1\right]$
 $\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {0.196818376877418}& {0.0968577035851758}& {0.203559988730230}& {-0.0763550809234677}& {-0.0991780271091046}& {-0.308033447109983}& {-0.306203158155557}& {-0.265782970860922}& {-0.238433831967978}& {\dots }\\ {0.}& {0.407010649173528}& {0.0490645321385439}& {0.103429224931317}& {-0.456897055191860}& {-0.340557699273314}& {-0.538854294921920}& {-0.634918454532307}& {-0.771567038580644}& {-0.761344976930684}& {\dots }\end{array}\right]\\ \hfill {\text{2 × 101 Array}}\end{array}$ (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)$
 $\begin{array}{c}\left[\begin{array}{ccccccccccc}{1.}& {1.26970708069510}& {1.98051006788772}& {2.00554946641013}& {2.27162457037356}& {2.90848274165077}& {2.80563852165809}& {2.40765313219302}& {2.07867723906156}& {1.12179462337224}& {\dots }\\ {1.}& {1.44891148572607}& {1.39036450549517}& {1.53742952556397}& {2.05013759398132}& {2.02857340767009}& {1.37575992837092}& {1.61695437145040}& {1.64951698052306}& {1.53245606928608}& {\dots }\\ {1.}& {1.15759100240565}& {0.989282914613827}& {0.878406444523057}& {0.739099310591964}& {0.562111598770146}& {0.501401921191423}& {0.758717021610400}& {0.735753240656966}& {0.693771518708170}& {\dots }\\ {1.}& {1.74189598078067}& {1.27799095320887}& {1.94767559351059}& {1.71159203210620}& {1.79715114648123}& {1.15276179892619}& {1.41514500025986}& {1.39919656505608}& {1.18913197728380}& {\dots }\\ {1.}& {1.14497967005970}& {1.32241174717107}& {1.12086678633664}& {1.04768680902894}& {1.08055030721048}& {1.07366080002656}& {1.01230037506524}& {0.978102150340041}& {0.879207841346007}& {\dots }\\ {1.}& {0.822664890430624}& {0.972752731459613}& {1.58868161908574}& {1.68911445039012}& {1.09831406909265}& {1.67618602402161}& {1.02360043229725}& {1.42629950715897}& {1.18694787872504}& {\dots }\\ {1.}& {0.627705937834236}& {0.836516472713537}& {0.851523741752974}& {0.777403615503865}& {1.02057103436666}& {1.03220355728121}& {0.744082968246609}& {0.946556459065327}& {0.785773114761445}& {\dots }\\ {1.}& {0.795775790397301}& {0.893615296626921}& {0.830813096260986}& {0.945093271501350}& {1.05065430253429}& {0.926581469707109}& {0.925438009777388}& {1.06692226776526}& {0.890317127293294}& {\dots }\\ {1.}& {0.930653074504863}& {0.913174834407809}& {1.27459067200520}& {1.40661473951510}& {1.55425320534495}& {2.33746910915494}& {1.40685497998957}& {1.12695617844730}& {1.19814470478762}& {\dots }\\ {1.}& {0.695323182205202}& {0.624548382753467}& {0.687784145172546}& {0.544551607743297}& {0.638025536235492}& {0.795134712874281}& {0.736889488061441}& {0.661378355546983}& {0.908128159080318}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{20 × 101 Array}}\end{array}$ (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)[2]; 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)$
 $\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {-0.460677298205267}& {-1.07106879080060}& {-1.40461435536504}& {-1.29147295776424}& {-1.70081245239351}& {-1.33244188129603}& {-1.54618618008410}& {-1.79702382321104}& {-1.55685676664431}& {\dots }\\ {0.}& {0.422489601704026}& {-0.0163072694470068}& {-0.109844619259372}& {0.686572005544409}& {0.796574621142072}& {1.07718577309755}& {1.30785407831645}& {1.46493535109163}& {1.71924256906325}& {\dots }\\ {0.}& {0.301223279492628}& {-0.307496781361153}& {0.0171811911508298}& {0.106635469244275}& {0.0843620898422919}& {0.444290003100029}& {0.419169501247461}& {0.0308068260318245}& {0.411162401136727}& {\dots }\\ {0.}& {0.213283245900953}& {0.475433796470540}& {0.533068372537442}& {0.606497733539854}& {0.285220916545373}& {0.0698590535535195}& {-0.129455711791545}& {0.140743425698527}& {0.324844622177747}& {\dots }\\ {0.}& {0.115855203147051}& {0.262364587130187}& {-0.178548627242625}& {0.120736303581130}& {-0.679485157181190}& {-1.04590033506109}& {-0.978030437228430}& {-0.826803116091448}& {-1.00195494815506}& {\dots }\\ {0.}& {0.0131555173184250}& {0.278464322568558}& {0.224401306413526}& {0.612448028612098}& {0.688782533636485}& {1.02071642697900}& {1.10200212035742}& {1.21626612151018}& {1.85960124122864}& {\dots }\\ {0.}& {0.439786389195204}& {1.00209294700981}& {0.789476614385524}& {0.409287121672812}& {0.974813606244339}& {0.902041138615255}& {0.842034037621940}& {0.909985319531469}& {1.27165056873350}& {\dots }\\ {0.}& {0.364540474744646}& {0.163565866152006}& {0.758870598024379}& {0.802681853246884}& {0.542919168285149}& {-0.0891943176236484}& {0.382104173119622}& {0.391002901347407}& {1.17767649852341}& {\dots }\\ {0.}& {-0.0176590665165599}& {0.113252893462040}& {0.268886487598902}& {0.568828668973641}& {0.609383230811343}& {0.917297216778507}& {0.203501680108177}& {0.599991131522249}& {0.588884848681972}& {\dots }\\ {0.}& {0.206002210550620}& {0.492951419840478}& {0.487361058027667}& {0.109805463772951}& {-0.269943235625741}& {-0.168687320005330}& {-0.791616470514357}& {-1.24353216468825}& {-1.38679960295500}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{20 × 101 Array}}\end{array}$ (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)[2]; 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)$
 $\begin{array}{c}\left[\begin{array}{c}{0.}\\ {-0.135871178611748}\\ {-0.135742389585259}\\ {-0.430761374236704}\\ {-0.737491313053186}\\ {-0.324851373259067}\\ {-0.0243776218106232}\\ {0.445566290165327}\\ {1.07293066480225}\\ {1.10475536474528}\\ {⋮}\end{array}\right]\\ \hfill {\text{slice of 20 × 1 × 101 Array}}\end{array}$ (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)[2]; 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)$
 $\begin{array}{c}\left[\begin{array}{ccc}{0.}& {-0.274901832879569}& {-0.270752124485207}\\ {-0.00562575226381989}& {-0.114847368218895}& {-0.108687547973344}\\ {0.00126329911563663}& {-0.0247421880692085}& {0.156257917604740}\\ {0.187753041597412}& {0.0621024342019541}& {0.288829414205203}\\ {0.383605150300664}& {0.235443030727788}& {0.615240704493871}\\ {0.254741893952900}& {0.235361606227607}& {0.495593797084583}\\ {0.116267582267117}& {0.153220882936555}& {0.235649753661604}\\ {0.215064217959773}& {0.158358615414225}& {0.0432884856949052}\\ {0.268180069046813}& {0.145667569974028}& {-0.124100066159435}\\ {0.207089400886651}& {-0.264936487415694}& {-0.310868660252361}\\ {⋮}& {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{slice of 20 × 3 × 101 Array}}\end{array}$ (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)$
 ${\mathrm{A1}}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {1.59394858373315}& {0.356151368392595}& {1.69138534026626}& {5.40733187056488}& {5.84551584855219}& {9.69272011646318}& {4.26148452002018}& {0.00341610941081613}& {1.79957990320981}& {\dots }\end{array}\right]\\ \hfill {\text{1 × 11 Array}}\end{array}$ (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)$
 ${\mathrm{A2}}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {1.59394858373315}& {0.356151368392595}& {1.69138534026626}& {5.40733187056488}& {5.84551584855219}& {9.69272011646318}& {4.26148452002018}& {0.00341610941081613}& {1.79957990320981}& {\dots }\end{array}\right]\\ \hfill {\text{1 × 11 Array}}\end{array}$ (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)$
 $\begin{array}{c}\left[\begin{array}{cc}{-0.}& {0.493734241843489}\end{array}\right]\\ \hfill {\text{slice of 1 × 2 × 11 Array}}\end{array}$ (17)

This is a multivariate stochastic process.

 > $\mathrm{\Sigma }≔⟨⟨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{\Sigma }\right)$
 ${W}{≔}{\mathrm{_W1}}$ (19)
 > $\mathrm{SamplePath}\left(W\left(t\right),t=0..3,\mathrm{timesteps}=10,\mathrm{replications}={10}^{4}\right)$
 $\begin{array}{c}\left[\begin{array}{cc}{0.}& {-1.85071516683032}\\ {0.439618584673188}& {-1.84682104512202}\\ {0.619495932441729}& {-2.35921926830536}\\ {1.91378391704713}& {-1.70950317626836}\\ {1.95699585376611}& {-1.35887536968829}\\ {1.87472269596972}& {-1.85628634129647}\\ {1.89822776213380}& {-1.80395587336555}\\ {2.45937012246483}& {-2.03885025220964}\\ {3.23781836430910}& {-2.52542870457822}\\ {3.54471189198704}& {-2.02688171986905}\\ {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{slice of 10000 × 2 × 11 Array}}\end{array}$ (20)
 > $S≔\mathrm{SamplePath}\left(⟨W\left(t\right)\left[1\right],W\left(t\right)\left[2\right],W\left(t\right)\left[1\right]+W\left(t\right)\left[2\right]⟩,t=0..3,\mathrm{timesteps}=9,\mathrm{replications}={10}^{4}\right)$
 ${S}{≔}\begin{array}{c}\left[\begin{array}{ccc}{0.}& {0.}& {0.}\\ {0.317169452751662}& {0.930314402076179}& {0.536380802893293}\\ {-0.164902406343093}& {0.351280503656691}& {1.60475616612873}\\ {-1.19056799981386}& {-0.182766603265477}& {0.924763997271092}\\ {-1.46467206235550}& {-0.326038588764484}& {-1.14444223763232}\\ {-1.56994079649986}& {-0.800971677331594}& {-0.815313663640158}\\ {-1.64504083812092}& {-0.377873496248400}& {-2.13263519635932}\\ {-1.93992495932433}& {-0.682736386853286}& {-3.47933464277819}\\ {-1.52510316767736}& {-1.41347269227275}& {-3.12986566230380}\\ {-1.33823754287311}& {-1.68499741737922}& {-3.11865620090146}\\ {⋮}& {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{slice of 10000 × 3 × 10 Array}}\end{array}$ (21)

Here is the first replication of the sample path.

 > $\mathrm{map}\left(\mathrm{evalf}\left[5\right],S\left[1\right]\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)\left[1\right],t=0..3,\mathrm{timesteps}=100,\mathrm{replications}=10\right)$
 ${S}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{0.}& {-0.341663132944776}& {-0.366606204439224}& {-0.662335465717544}& {-0.830594669980847}& {-0.618099152704310}& {-0.763446702776234}& {-1.04489936959030}& {-1.29953012433772}& {-1.26712440282559}& {\dots }\\ {0.}& {0.0341827208827795}& {-0.150006646063332}& {-0.0290335424668767}& {-0.631261688318766}& {-0.801656438506991}& {-0.904453626961884}& {-0.887245327551293}& {-0.677363190645626}& {-0.739486435890634}& {\dots }\\ {0.}& {0.0692081371718972}& {0.225543522842141}& {0.209058502512968}& {-0.0679754019829200}& {-0.173838446006218}& {-0.373744341815974}& {-0.679025695652852}& {-0.542994039335261}& {-0.774366423022671}& {\dots }\\ {0.}& {-0.136492993915993}& {-0.344173067173445}& {-0.284913017391817}& {-0.479409043261791}& {-0.896897109561952}& {-1.15163059886553}& {-1.57316990246362}& {-2.00184081944580}& {-2.21778219258942}& {\dots }\\ {0.}& {-0.0930973684931795}& {-0.138297183963465}& {-0.338364439736943}& {-0.479772512914268}& {-0.368719778623500}& {-0.552579925561552}& {-1.01450691161173}& {-1.29851407390259}& {3.44838143122746}& {\dots }\\ {0.}& {-0.269327947965667}& {-0.570116953370354}& {-0.689053065765268}& {-0.782108592377720}& {-0.501331130324550}& {-0.636683772974263}& {-0.616460941059246}& {-0.903095237274550}& {-0.863664223311882}& {\dots }\\ {0.}& {-0.0622500159220127}& {-0.150739843949849}& {-0.253181618845354}& {-0.0745345369796406}& {-0.141353920638261}& {-0.434818688442304}& {-0.630792205722745}& {-0.304305313751321}& {-0.325261202666159}& {\dots }\\ {0.}& {-0.208829217866858}& {-0.305322317621251}& {-0.353966097785680}& {-0.563029299524445}& {-0.632801565051464}& {-0.826010873687321}& {-0.988651131310563}& {-1.22192262495770}& {-1.35126693583338}& {\dots }\\ {0.}& {0.195798483351455}& {0.0683305263460480}& {-0.141917439554735}& {-0.208758754410481}& {-0.375678897136545}& {-0.220320365494615}& {-0.490927207714335}& {-0.973275584202822}& {-1.31779127311298}& {\dots }\\ {0.}& {-0.0586852066544176}& {-0.411760065934499}& {-0.454152687560330}& {-0.613875851132363}& {-0.923824330325783}& {-1.09792658409922}& {-1.35379885439922}& {-1.82128616985753}& {-1.84122953162053}& {\dots }\end{array}\right]\\ \hfill {\text{10 × 101 Array}}\end{array}$ (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↦\mathrm{exp}\left(0.05\cdot t+0.3\cdot Y\left(t\right)\right)$
 ${Z}{≔}{t}{↦}{{ⅇ}}^{{0.05}{\cdot }{t}{+}{0.3}{\cdot }{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)$
 ${S}{≔}\begin{array}{c}\end{array}$