主要内容

simulate

Simulate Markov chain state walks

Description

example

X=模拟(MC,numSteps)returns dataX随机步行numStepsthrough sequences of states in the discrete-time Markov chainMC.

example

X=模拟(MC,numSteps,'X0',x0)可选地指定模拟的初始状态x0.

Examples

collapse all

考虑一个随机过程的理论,右式过渡矩阵。

P = [ 0 0 1 / 2 1 / 4 1 / 4 0 0 0 0 1 / 3 0 2 / 3 0 0 0 0 0 0 0 1 / 3 2 / 3 0 0 0 0 0 1 / 2 1 / 2 0 0 0 0 0 3 / 4 1 / 4 1 / 2 1 / 2 0 0 0 0 0 1 / 4 3 / 4 0 0 0 0 0 ] .

Create the Markov chain that is characterized by the transition matrixP.

P = [ 0 0 1/2 1/4 1/4 0 0 ; 0 0 1/3 0 2/3 0 0 ; 0 0 0 0 0 1/3 2/3; 0 0 0 0 0 1/2 1/2; 0 0 0 0 0 3/4 1/4; 1/2 1/2 0 0 0 0 0 ; 1/4 3/4 0 0 0 0 0 ]; mc = dtmc(P);

绘制马尔可夫链的有向图。通过使用边缘颜色来指示过渡的概率。

数字;graphplot(mc,'ColorEdges',true);

图包含一个轴对象。The axes object contains an object of type graphplot.

Simulate a 20-step random walk that starts from a random state.

RNG(1);%可再现性numSteps = 20; X = simulate(mc,numSteps)
X =21×13 7 1 3 6 1 3 7 2 5 ⋮

Xis a 21-by-1 matrix. Rows correspond to steps in the random walk. BecauseX(1)is3, the random walk begins at state 3.

Visualize the random walk.

数字;Simpleot(MC,X);

图包含一个轴对象。通过模拟达到的标题状态的轴对象包含类型图像的对象。

Create a four-state Markov chain from a randomly generated transition matrix containing eight infeasible transitions.

rng('默认');%可再现性MC= mcmix(4,“零”,8);

MCis adtmc目的。

绘制马尔可夫链的挖掘图。

数字;Graphplot(MC);

图包含一个轴对象。The axes object contains an object of type graphplot.

State4is an absorbing state.

Run three 10-step simulations for each state.

x0 = 3*ones(1,mc.NumStates); numSteps = 10; X = simulate(mc,numSteps,'X0',x0);

Xis an 11-by-12 matrix. Rows corresponds to steps in the random walk. Columns 1–3 are the simulations that start at state 1; column 4–6 are the simulations that start at state 2; columns 7–9 are the simulations that start at state 3; and columns 10–12 are the simulations that start at state 4.

每次,绘制在所有模拟中访问的比例状态。

数字;Simpleot(MC,X)

图包含一个轴对象。通过模拟达到的标题状态的轴对象包含类型图像的对象。

Input Arguments

collapse all

离散时间马尔可夫链数字states and transition matrixP, specified as adtmc目的。P必须完全指定(否NaNentries).

Number of discrete time steps in each simulation, specified as a positive integer.

Data Types:double

Name-Value Arguments

将可选的参数对Name1=Value1,...,NameN=ValueN, whereNameis the argument name and价值is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'X0',[1 0 2]specifies simulating three times, the first simulation starts in state 1 and the final two start in state 3.

模拟的初始状态,指定为逗号分隔对'X0'and a vector of nonnegative integers of数字长度。X0provides counts for the number of simulations to begin in each state. The total number of simulations (numsims) issum(X0).

The default is a single simulation beginning from a random initial state.

Example:'X0',[10 10 0 5]

Data Types:double

Output Arguments

collapse all

模拟期间访问的州指数,返回(1 + numsteps)-by-numsims正整数的数字矩阵。第一行包含初始状态。按顺序,列都是从第一个状态开始的模拟,然后是从第二个状态开始的所有模拟,依此类推。

Tips

  • To startnsimulations from statek使用:

    X0 = zeros(1,NumStates); X0(k) =n;

  • To visualize the data created bysimulate, 采用simplot.

Version History

Introduced in R2017b