LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
SamplesFromMMAP.m
1% x = SamplesFromMMAP(D, K, prec)
2%
3% Generates random samples from a marked Markovian
4% arrival process.
5%
6% Parameters
7% ----------
8% D : list of matrices of shape(M,M), length(N)
9% The D0...DN matrices of the MMAP
10% K : integer
11% The number of samples to generate.
12% prec : double, optional
13% Numerical precision to check if the input MMAP 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 inter-arrival time and the type of the
21% arrival.
22
23function x = SamplesFromMMAP(D,k,initial)
24
25 global BuToolsCheckInput;
26 if isempty(BuToolsCheckInput)
27 BuToolsCheckInput = true;
28 end
29
30 if BuToolsCheckInput && ~CheckMMAPRepresentation(D)
31 error('SamplesFromMMAP: input isn''t a valid MMAP 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 = MarginalDistributionFromMMAP(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./diag(D{1});
51 nextpr = diag(sojourn)*D{1};
52 nextpr = nextpr - diag(diag(nextpr));
53 for i=2:length(D)
54 nextpr=[nextpr,diag(sojourn)*D{i}];
55 end
56 nextpr = cumsum(nextpr,2);
57
58 if length(D)>2
59 x = zeros(k,2);
60 else
61 x = zeros(k,1);
62 end
63 for n=1:k
64 time = 0;
65
66 % play state transitions
67 while state<=N
68 time = time - log(rand()) * sojourn(state);
69 r = rand();
70 nstate = 1;
71 while nextpr(state,nstate)<=r
72 nstate = nstate+1;
73 end
74 state = nstate;
75 end
76 if length(D)>2
77 x(n,1) = time;
78 x(n,2) = ceil(state/N)-1;
79 else
80 x(n) = time;
81 end
82 state = rem(state-1,N)+1;
83 end
84end