LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
dtmc_simulate.m
1%{ @file dtmc_simulate.m
2 % @brief Simulates a trajectory of a discrete-time Markov chain
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Simulates a trajectory of a discrete-time Markov chain
9 %
10 % @details
11 % Generates a sample path of the Markov chain for n time steps.
12 %
13 % @par Syntax:
14 % @code
15 % sts = dtmc_simulate(P, pi0, n)
16 % @endcode
17 %
18 % @par Parameters:
19 % <table>
20 % <tr><th>Name<th>Description
21 % <tr><td>P<td>Stochastic transition matrix
22 % <tr><td>pi0<td>Initial probability distribution vector
23 % <tr><td>n<td>Number of steps to simulate
24 % </table>
25 %
26 % @par Returns:
27 % <table>
28 % <tr><th>Name<th>Description
29 % <tr><td>sts<td>Vector of state indices visited in the simulation
30 % </table>
31%}
32function [sts]=dtmc_simulate(P, pi0, n)
33
34rnd = rand;
35cpi0 = cumsum(pi0);
36if isempty(intersect(find( rnd - cpi0 > 0), find(pi0>0)))
37 st = min(find(pi0));
38else
39 st = min(intersect(1 + find( rnd - cpi0 > 0),find(pi0>0)));
40end
41
42F = cumsum(P,2);
43for i=1:n
44 sts(i) = st; soujt(i)=1;
45 if F(st,end)==0 || P(st,st)==1
46 return;
47 end
48 rnd = rand;
49 if isempty(intersect(find( rnd - F(st,:) > 0),find(P(st,:)>0)))
50 st = min(find(P(st,:)>0));
51 else
52 st = min(intersect(1 + find( rnd - F(st,:) > 0),find(P(st,:)>0)));
53 end
54end
55end