LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
SamplesFromDMMAP.m
1% x = SamplesFromDMMAP(D, K, prec)
2%
3% Generates random samples from a discrete marked
4% Markovian arrival process.
5%
6% Parameters
7% ----------
8% D : list of matrices of shape(M,M), length(N)
9% The D0...DN matrices of the DMMAP
10% K : integer
11% The number of samples to generate.
12% prec : double, optional
13% Numerical precision to check if the input DMMAP is
14% valid. The default value is 1e-14.
15%
16% Returns
17% -------
18% x : matrix, shape(K,2)
19% The random samples. Each row consists of two
20% columns: the (discrete) inter-arrival time and the
21% type of the arrival.
22
23function x = SamplesFromDMMAP(D,k,initial)
24
25 global BuToolsCheckInput;
26 if isempty(BuToolsCheckInput)
27 BuToolsCheckInput = true;
28 end
29
30 if BuToolsCheckInput && ~CheckDMMAPRepresentation(D)
31 error('SamplesFromDMMAP: input isn''t a valid DMMAP representation!');
32 end
33
34 N = size(D{1},1);
35
36 if ~exist('initial','var') || isempty(initial)
37 % draw initial state according to the stationary distribution
38 stst = MarginalDistributionFromDMMAP(D);
39 cummInitial = cumsum(stst);
40 r = rand();
41 state = 1;
42 while cummInitial(state)<=r
43 state=state+1;
44 end
45 else
46 state = initial;
47 end
48
49 % auxilary variables
50 sojourn = 1./(1-diag(D{1}));
51 logp = log(diag(D{1}));
52 nextpr = diag(sojourn)*D{1};
53 nextpr = nextpr - diag(diag(nextpr));
54 for i=2:length(D)
55 nextpr=[nextpr,diag(sojourn)*D{i}];
56 end
57 nextpr = cumsum(nextpr,2);
58
59 if length(D)>2
60 x = zeros(k,2);
61 else
62 x = zeros(k,1);
63 end
64 for n=1:k
65 time = 0;
66
67 % play state transitions
68 while state<=N
69 time = time + 1 + floor(log(rand()) / logp(state));
70 r = rand();
71 nstate = 1;
72 while nextpr(state,nstate)<=r
73 nstate = nstate+1;
74 end
75 state = nstate;
76 end
77 if length(D)>2
78 x(n,1) = time;
79 x(n,2) = ceil(state/N)-1;
80 else
81 x(n) = time;
82 end
83 state = rem(state-1,N)+1;
84 end
85end