1% [D0, D1, logli] = MAPFromTrace(trace, orders, maxIter, stopCond, initial, result)
3% Performs MAP fitting
using the EM algorithm (ErCHMM,
8% trace : column vector, length K
9% The samples of the trace
10% orders : list of int, length(N), or
int
11% The length of the list determines the number of
12% Erlang branches to use in the fitting method.
13% The entries of the list are the orders of the
14% Erlang distributions. If
this parameter
is a
15% single integer, all possible branch number - order
16% combinations are tested where the total number of
18% maxIter : int, optional
19% Maximum number of iterations. The
default value
is
21% stopCond : double, optional
22% The algorithm stops
if the relative improvement of
23% the log likelihood falls below stopCond. The
24%
default value
is 1e-7
25% initial : tuple of a vector and a matrix, shape(N,N), optional
26% The rate parameters of the Erlang distributions
27% and the branch transition probability matrix to be
28% used initially. If not given, a
default initial
29% guess
is determined and the algorithm starts from
31% result : {
"vecmat",
"matmat"}, optional
32% The result can be returned two ways. If
"matmat" is
33% selected, the result
is returned in the classical
34% representation of MAPs, thus the D0 and D1 matrices.
35% If
"vecmat" is selected, the rate parameters of the
36% Erlang branches and the branch transition probability
37% matrix are returned. The
default value
is "matmat"
41% (D0, D1) : tuple of matrix, shape (M,M) and matrix, shape (M,M)
42% If the
"matmat" result format
is chosen, the function
43% returns the D0 and D1 matrices of the MAP
44% (lambda,
P) : tuple of vector, length N and matrix, shape (M,M)
45% If the
"vecmat" result format
is chosen, the function
46% returns the vector of the Erlang rate parameters of
47% the branches and the branch transition probability
50% The log-likelihood divided by the trace length
54% This procedure
is quite slow in the supported
55% mathematical frameworks. If the maximum speed
is
56% needed, please use the multi-core optimized c++
57% implementation called SPEM-FIT_.
63% .. [1] Okamura, Hiroyuki, and Tadashi Dohi. Faster
64% maximum likelihood estimation algorithms for
65% Markovian arrival processes. Quantitative
66% Evaluation of Systems, 2009. QEST
'09. Sixth
67% International Conference on the. IEEE, 2009.
69% .. [2] Horváth, Gábor, and Hiroyuki Okamura. A Fast EM
70% Algorithm for Fitting Marked Markovian Arrival
71% Processes with a New Special Structure. Computer
72% Performance Engineering. Springer Berlin
73% Heidelberg, 2013. 119-133.
75function [D0, D1, logli] = MAPFromTrace (trace, orders, maxIter, stopCond, initial, result)
77 if ~exist('maxIter
','var
') || isempty(maxIter)
80 if ~exist('stopCond
','var
') || isempty(stopCond)
83 if ~exist('initial
','var
')
86 if ~exist('result
','var
') || isempty(result)
89 global BuToolsVerbose;
91 function o = allorders (branches, sumorders)
96 for ii=1:sumorders-branches+1
97 x = allorders (branches-1, sumorders-ii);
100 % check if we have it already
115 function printOrders (ord)
117 fprintf('%g
',ord(oi));
130 allord = allorders (br, orders);
131 for ox=1:length(allord)
133 fprintf('Trying orders
');
134 printOrders(allord{ox});
137 [oX,oY,l] = MAPFromTrace (trace, allord{ox}, maxIter, stopCond, initial, result);
142 bestOrders = allord{ox};
150 fprintf('Best solution: logli=%g, orders=
', bestLogli);
151 printOrders(bestOrders);
157 function X = generatorFromErlangs (erll, erlo)
158 X = zeros (sum(erlo));
160 for ii=1:length(erll)
161 X(xx:xx+erlo(ii)-1, xx:xx+erlo(ii)-1) = erll(ii)*(diag(ones(1,erlo(ii)-1),1)-diag(ones(1,erlo(ii))));
169 % initial alpha and lambda is such that the mean is matched
171 alphav = ones(1,M) / M;
172 lambda = diag(orders) * (1:M)';
173 trm = sum(trace)/length(trace);
174 inim = sum(alphav ./ (1:M));
175 lambda = lambda * inim / trm;
176 P = ones(M,1)*alphav;
177 elseif length(initial)==2
180 alphav = DTMCSolve(
P);
182 error(
'Invalid initial branch probability and rate vectors!');
194 while abs((ologli-logli)/logli)>stopCond && steps<=maxIter
199 Q(i,:) = ((lambda(i)*trace).^(orders(i)-1) / factorial(orders(i)-1) * lambda(i)) .* exp(-lambda(i)*trace);
201 % forward likelihood vectors:
205 prev = prev*diag(Q(:,k))*
P;
206 scale = log2(sum(prev));
207 prev = prev * 2^-scale;
208 Ascale(k) = scprev + scale;
212 Av = [alphav; A(1:end-1,:)];
213 Ascalev = [0, Ascale(1:end-1)];
214 % backward likelihood vectors:
218 next = diag(Q(:,k))*
P*next;
219 scale = log2(sum(next));
220 next = next * 2^-scale;
221 Bscale(k) = scprev + scale;
225 Bv = [B(:,2:end), ones(M,1)];
226 Bscalev = [Bscale(2:end), 0];
229 logli = (log(llh) + Bscale(1) * log(2)) / K;
233 % Calculate
new estimates
for the parameters
237 AB(:,m) = AB(:,m)./nor;
242 lambda = (orders.*v1 ./ v2)
';
245 nor = illh*2.^(Ascalev+Bscalev-Bscale(1))
';
247 Avv(:,m) = Avv(:,m) .* nor;
251 P(m,:) = P(m,:) / sum(P(m,:));
255 if BuToolsVerbose && etime(clock(),t1)>2
256 fprintf('Num of iterations: %d, logli: %g\n
', steps, logli);
262 fprintf('Num of iterations: %d, logli: %g\n
', steps, logli);
263 fprintf('EM algorithm terminated. (orders=
');
265 fprintf('%g
',orders(i));
274 if strcmp(result,'vecmat
')
277 elseif strcmp(result,'matmat
')
278 D0 = generatorFromErlangs(lambda, orders);
279 D1 = zeros(size(D0));
280 indicesTo = [1 cumsum(orders(1:end-1))+1];
281 indicesFrom = cumsum(orders);
282 D1(indicesFrom,indicesTo) = diag(lambda)*P;