# simulate

Simulate Markov chain state walks

## Description

## Examples

### Simulate Random Walk Through Markov Chain

Consider this theoretical, right-stochastic transition matrix of a stochastic process.

$$P=\left[\begin{array}{ccccccc}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\end{array}\right].$$

Create the Markov chain that is characterized by the transition matrix *P*.

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);

Plot a directed graph of the Markov chain. Indicate the probability of transition by using edge colors.

```
figure;
graphplot(mc,'ColorEdges',true);
```

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

```
rng(1); % For reproducibility
numSteps = 20;
X = simulate(mc,numSteps)
```

`X = `*21×1*
3
7
1
3
6
1
3
7
2
5
⋮

`X`

is a 21-by-1 matrix. Rows correspond to steps in the random walk. Because `X(1)`

is `3`

, the random walk begins at state 3.

Visualize the random walk.

figure; simplot(mc,X);

### Specify Starting States for Multiple Simulations

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

rng('default'); % For reproducibility mc = mcmix(4,'Zeros',8);

`mc`

is a `dtmc`

object.

Plot a digraph of the Markov chain.

figure; graphplot(mc);

State `4`

is 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);
```

`X`

is 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.

For each time, plot the proportions states that are visited over all simulations.

figure; simplot(mc,X)

## Input Arguments

`mc`

— Discrete-time Markov chain

`dtmc`

object

Discrete-time Markov chain with `NumStates`

states and transition matrix `P`

, specified as a `dtmc`

object. `P`

must be fully specified (no `NaN`

entries).

`numSteps`

— Number of discrete time steps

positive integer

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

**Data Types: **`double`

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

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 enclose*
`Name`

*in quotes.*

**Example: **`'X0',[1 0 2]`

specifies simulating three times, the
first simulation starts in state 1 and the final two simulations start in state 3.
No simulations start in state 2.

`X0`

— Initial states of simulations

vector of nonnegative integers

Initial states of simulations, specified as the comma-separated pair consisting of `'X0'`

and a vector of nonnegative integers of `NumStates`

length. `X0`

provides counts for the number of simulations to begin in each state. The total number of simulations (`numSims`

) is `sum(X0)`

.

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

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

specifies 10 simulations
starting in state 1, 10 simulations starting in state 2, no simulations
starting in state 3, and 5 simulations starting in state 4.
`simulate`

conducts ```
sum(X0) =
25
```

simulations.

**Data Types: **`double`

## Output Arguments

`X`

— Indices of states

numeric matrix of positive integers

Indices of states visited during the simulations, returned as a `(1 + numSteps)`

-by-`numSims`

numeric matrix of positive integers. The first row contains the initial states. Columns, in order, are all simulations beginning in the first state, then all simulations beginning in the second state, and so on.

## Tips

To start

simulations from state`n`

, use:`k`

X0 = zeros(1,NumStates); X0(

*k*) =*n*;To visualize the data created by

`simulate`

, use`simplot`

.

## Version History

**Introduced in R2017b**

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)