1function [T,A,LAST,FIRST,STS] = mmap_sample(
MMAP, nSamples, pi, seed)
2% Generates samples from a Marked MAP.
4% -
MMAP: representation of the Marked MAP
5% - nSamples: number of random samples to collect
6% - pi: (optional) initial probability. If not provided, steady state
7% probabilities are used.
8% - seed: (optional) seed for the random number generator
10% default: stationary inialization (as in single-class MAP)
17% check
if single-
class
19%
MMAP with K=1 has three matrices
20if (length(
MMAP) == 2 || length(
MMAP) == 3)
22 [T,LAST,FIRST]=map_sample({
MMAP{1},
MMAP{2}}, nSamples, pi, seed);
24 [T,LAST,FIRST]=map_sample({
MMAP{1},
MMAP{2}}, nSamples, pi);
30%
if provided, initialize RNG with the given seed
32 s = RandStream(
'mt19937ar',
'seed', seed);
33 RandStream.setGlobalStream(s);
42% single state -> exponential
44 T=exprnd(map_mean(
MMAP),nSamples,1);
49 A=randp(crate,nSamples,1);
55% rates of the exponential distributions in each state
58% row s contains the discrete probability distribution
for the transitions
59% from state s (the first M are the embedded transitions, the following
60% M*C are the transitions that mark an arrival)
61targets=zeros(M,M+M*C);
65 targets(i,j) = D0(i,j) / rates(i);
70 targets(i,c*M+j) =
MMAP{2+c}(i,j) / rates(i);
75% pick a random initial state according to pi
76initState=find(rand()<cumsum(pi),1);
86batchCounter=zeros(1,M);
95STS = zeros(nSamples,1);
98 % regenerate exponential batch
if needed
99 if (h == 1 || batchCounter(s) > batchSize(s))
100 batchSize(s)=min(100,nSamples-h+1);
101 timeBatch{s}=exprnd(1/rates(s),batchSize(s),1);
102 tranBatch{s}=randp(targets(s,:),batchSize(s),1);
105 % pick from batch random values
106 time = timeBatch{s}(batchCounter(s));
107 transition = tranBatch{s}(batchCounter(s));
108 % increase batch counter usage
109 batchCounter(s)=batchCounter(s)+1;
111 % should be equivalent to but much faster than idivide
112 % type=idivide(int32(transition-1),int32(M),
'floor');
113 type=int32(floor((transition-1)/M));
115 % no event, internal transition
117 sNext=mod(transition,M);
121 %fprintf(
'No event at time %0.2f, transition %d->%d\n', ...
130 sNext = mod(transition,M);
134 %fprintf(
'Event %2d at time %.2f, transition %d->%d, class %d\n', ...
135 % h, T(h), s, sNext, A(h));
136 % reset time accumulator