Finance
SamplePath
generate sample path(s) for a stochastic process
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
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)
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
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.
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 T0..T1, where T0 and T1 are non-negative constants such that T0<T1.
Note that if 0<T0, the value at T0 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 T0 and T1, but will have no effect on the value at T0. To reduce the bias, use a time interval of the form 0..T1. All stochastic variables involved in expression should be of the form X⁡t. If X is multi-dimensional stochastic, then the individual components of X can be accessed using the notation X⁡ti.
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⁡B,A, 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.
with⁡Finance:
X≔WienerProcess⁡:
W≔WienerProcess⁡1.0,0.5|0.5,1.0:
Create 20 replications of the sample path of X on the interval 0..1 using 100 time steps.
A≔SamplePath⁡X⁡t,t=0..1,timesteps=100,replications=20
Here is a single realization of the sample path.
A1
Here is an example involving a two-dimensional process.
B≔SamplePath⁡W⁡t1+W⁡t2,t=0..1,timesteps=100,replications=20
B1
Create path generators for X and W on the interval 0..5 and use them to generate sample paths for X and W.
P≔PathGenerator⁡X,TimeGrid⁡5,100:
A≔SamplePath⁡P,replications=20
M≔PathGenerator⁡W,TimeGrid⁡5,100:
B≔SamplePath⁡M,replications=20
Here are realizations of the sample paths.
Use the above path generator to generate sample paths for the process ⅇA⁡t.
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 ≔ procB,Alocald,i;d ≔ rtable_dims⁡A;forifromlhs⁡dtorhs⁡ddoB[i] ≔ exp⁡A[i]end doend proc
SamplePath⁡f,P,replications=20
Simulate the one-dimensional process W⁡t1+W⁡t2 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:
SamplePath⁡f2,M,replications=20
This is the same example using the last calling sequence. Note the difference between procedures f2 and 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:
SamplePath⁡1,F2,M,replications=20
Simulate the three-dimensional process W⁡t1,W⁡t2,W⁡t1+W⁡t2 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:
SamplePath⁡3,F,M,replications=20
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≔RandomTools:-GetState⁡:
Use the same state of the random number generator to compare the results.
RandomTools:-SetState⁡state=S:
A1≔SamplePath⁡X⁡t2,t=0..10,timesteps=10
This is the same example as above but using a Maple procedure.
f := proc(B, A) local i; for i from 1 to 11 do B[i] := A[i]^2; end do; end proc:
A2≔SamplePath⁡f,X,0..10,timesteps=10
This is the same example using an explicitly defined time grid.
T≔TimeGrid⁡0,1,2,3,4,5,6,7,8,9,10
T ≔ moduleend module
This is a multi-dimensional path that depends on one stochastic factor.
SamplePath⁡−X⁡t,X⁡t,t=0..10,timesteps=10
This is a multivariate stochastic process.
Σ≔1.0,0.5|0.5,1.0
Σ≔1.00.50.51.0
W≔WienerProcess⁡Σ
W≔_W1
SamplePath⁡W⁡t,t=0..3,timesteps=10,replications=104
S≔SamplePath⁡W⁡t1,W⁡t2,W⁡t1+W⁡t2,t=0..3,timesteps=9,replications=104
Here is the first replication of the sample path.
map⁡evalf5,S1
0.0.31717−0.16490−1.1906−1.4647−1.5699−1.6450−1.9399−1.5251−1.33820.0.060433−0.35577−1.0683−0.69408−0.12185−0.22109−0.98585−1.2004−1.18800.0.37760−0.52067−2.2589−2.1587−1.6918−1.8661−2.9258−2.7255−2.5262
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≔PoissonProcess⁡0.3
J≔_P
S≔SamplePath⁡5⁢J⁡t−t+W⁡t1,t=0..3,timesteps=100,replications=10
PathPlot⁡S,timegrid=0..3,color=red..blue,thickness=3
Here are some expressions involving stochastic variables.
Y≔WienerProcess⁡
Y≔_W2
Z≔t↦exp⁡0.05⋅t+0.3⋅Y⁡t
Z≔t↦ⅇ0.05⋅t+0.3⋅Y⁡t
U≔t↦maximize⁡Z⁡u,u=0..t
S≔SamplePath⁡Z⁡t,U⁡t,t=0..3,timesteps=100,replications=5
V≔t↦int⁡Z⁡u,u=0..tt
V≔t↦∫0tZ⁡uⅆut
S≔SamplePath⁡Z⁡t,V⁡t,t=0..3,timesteps=100,replications=5
Here is the use of a path function.
testproc := proc(Y, X) local i; for i from 1 to 11 do Y[i] := exp(X[i]); end do; end proc:
S≔SamplePath⁡testproc,Y,0..3,timesteps=10,replications=10
PathPlot⁡S,timegrid=0..3,thickness=3,color=red..blue
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.
The Finance[SamplePath] command was introduced in Maple 15.
For more information on Maple 15 changes, see Updates in Maple 15.
See Also
Finance[BrownianMotion]
Finance[CEVProcess]
Finance[DeterministicProcess]
Finance[Diffusion]
Finance[Drift]
Finance[ExpectedValue]
Finance[GaussianShortRateProcess]
Finance[GeometricBrownianMotion]
Finance[HestonProcess]
Finance[OrnsteinUhlenbeckProcess]
Finance[PathGenerator]
Finance[PathPlot]
Finance[SampleValues]
Finance[SquareRootDiffusion]
Finance[StochasticProcesses]
Finance[TimeGrid]
Finance[ValueAtRisk]
Finance[WienerProcess]
RandomTools[MersenneTwister]
Download Help Document