SamplePath - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Finance

  

SamplePath

  

generate sample path(s) for a stochastic process

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

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 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 Xt. If X is multi-dimensional stochastic, then the individual components of X can be accessed using the notation Xti.

• 

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 fB&comma;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.

Examples

withFinance&colon;

XWienerProcess&colon;

WWienerProcess1.0&comma;0.5|0.5&comma;1.0&colon;

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

ASamplePathXt&comma;t=0..1&comma;timesteps=100&comma;replications=20

Here is a single realization of the sample path.

A1

Here is an example involving a two-dimensional process.

BSamplePathWt1+Wt2&comma;t=0..1&comma;timesteps=100&comma;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.

PPathGeneratorX&comma;TimeGrid5&comma;100&colon;

ASamplePathP&comma;replications=20

MPathGeneratorW&comma;TimeGrid5&comma;100&colon;

BSamplePathM&comma;replications=20

Here are realizations of the sample paths.

A1

B1

Use the above path generator to generate sample paths for the process &ExponentialE;At.

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;

fprocB&comma;Alocald&comma;i&semi;drtable_dimsA&semi;forifromlhsdtorhsddoB&lsqb;i&rsqb;expA&lsqb;i&rsqb;end doend proc

(1)

SamplePathf&comma;P&comma;replications=20

Simulate the one-dimensional process Wt1+Wt2 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:

SamplePathf2&comma;M&comma;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:

SamplePath1&comma;F2&comma;M&comma;replications=20

Simulate the three-dimensional process Wt1&comma;Wt2&comma;Wt1+Wt2 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:

SamplePath3&comma;F&comma;M&comma;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.

SRandomTools:-GetState&colon;

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

RandomTools:-SetStatestate=S&colon;

A1SamplePathXt2&comma;t=0..10&comma;timesteps=10

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

RandomTools:-SetStatestate=S&colon;

f := proc(B, A) local i; for i from 1 to 11 do B[i] := A[i]^2; end do; end proc:

A2SamplePathf&comma;X&comma;0..10&comma;timesteps=10

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

TTimeGrid0&comma;1&comma;2&comma;3&comma;4&comma;5&comma;6&comma;7&comma;8&comma;9&comma;10

Tmoduleend module

(2)

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

SamplePathXt&comma;Xt&comma;t=0..10&comma;timesteps=10

This is a multivariate stochastic process.

Σ1.0&comma;0.5|0.5&comma;1.0

Σ1.00.50.51.0

(3)

WWienerProcessΣ

W_W1

(4)

SamplePathWt&comma;t=0..3&comma;timesteps=10&comma;replications=104

SSamplePathWt1&comma;Wt2&comma;Wt1+Wt2&comma;t=0..3&comma;timesteps=9&comma;replications=104

Here is the first replication of the sample path.

mapevalf5&comma;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

(5)

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.

JPoissonProcess0.3

J_P

(6)

SSamplePath5Jtt+Wt1&comma;t=0..3&comma;timesteps=100&comma;replications=10

PathPlotS&comma;timegrid=0..3&comma;color=red..blue&comma;thickness=3

Here are some expressions involving stochastic variables.

YWienerProcess

Y_W2

(7)

Ztexp0.05t+0.3Yt

Zt&ExponentialE;0.05t+0.3Yt

(8)

UtmaximizeZu&comma;u=0..t

UtmaximizeZu&comma;u=0..t

(9)

SSamplePathZt&comma;Ut&comma;t=0..3&comma;timesteps=100&comma;replications=5

VtintZu&comma;u=0..tt

Vt0tZu&DifferentialD;ut

(10)

SSamplePathZt&comma;Vt&comma;t=0..3&comma;timesteps=100&comma;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:

SSamplePathtestproc&comma;Y&comma;0..3&comma;timesteps=10&comma;replications=10

PathPlotS&comma;timegrid=0..3&comma;thickness=3&comma;color=red..blue

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.

• 

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]