1% [alpha, A, logli] = PHFromTrace(trace, orders, maxIter, stopCond, initial, result)
3% Performs PH distribution fitting
using the EM algorithm
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 two vectors, optional
26% The initial values of the branch probabilities and
27% rate parameters
is given by this tuple. If not
28% given, a default initial guess
is determined and
29% the algorithm starts from there.
30% result : {
"vecmat",
"vecvec"}, optional
31% The result can be returned two ways. If
"vecmat" is
32% selected, the result
is returned in the classical
33% representation of phase-type distributions, thus the
34% initial vector and the generator matrix.
35% If
"vecvec" is selected, two vectors are returned,
36% one holds the branch probabilities, and the second
37% holds the rate parameters of the Erlang branches.
38% The
default value
is "vecmat"
42% (alpha, A) : tuple of matrix, shape (1,M) and matrix, shape (M,M)
43% If the
"vecmat" result format
is chosen, the function
44% returns the initial probability vector and the
45% generator matrix of the phase type distribution.
46% (pi, lambda) : tuple of vector, length N and vector, length N
47% If the
"vecvec" result format
is chosen, the function
48% returns the vector of branch probabilities and the
49% vector of branch rates in a tuple.
51% The log-likelihood divided by the trace length
55% This procedure
is quite fast in the supported
56% mathematical frameworks. If the maximum speed
is
57% needed, please use the multi-core optimized c++
58% implementation called SPEM-FIT_.
64% .. [1] Thummler, Axel, Peter Buchholz, and Miklós Telek.
65% A novel approach for fitting probability
66% distributions to real trace data with the EM
67% algorithm. Dependable Systems and Networks, 2005.
69function [alpha, A, logli] = PHFromTrace (trace, orders, maxIter, stopCond, initial, result)
71 if ~exist(
'maxIter',
'var') || isempty(maxIter)
74 if ~exist(
'stopCond',
'var') || isempty(stopCond)
77 if ~exist(
'initial',
'var')
80 if ~exist(
'result',
'var') || isempty(result)
83 global BuToolsVerbose;
85 function o = allorders (branches, sumorders)
90 for ii=1:sumorders-branches+1
91 x = allorders (branches-1, sumorders-ii);
94 % check
if we have it already
116 allord = allorders (br, orders);
117 for ox=1:length(allord)
118 [a,A,l] = PHFromTrace (trace, allord{ox}, maxIter, stopCond, initial, result);
123 bestOrders = allord{ox};
131 fprintf(
'Best solution: logli=%g, orders=', bestLogli);
132 for i=1:length(bestOrders)
133 fprintf(
'%g',bestOrders(i));
134 if i<length(bestOrders)
147 % initial alpha and lambda
is such that the mean
is matched
149 alphav = ones(1,M) / M;
150 lambda = diag(orders) * (1:M)
';
151 trm = sum(trace)/length(trace);
152 inim = sum(alphav ./ (1:M));
153 lambda = lambda * inim / trm;
154 elseif length(initial)==2
155 if length(initial{1})==M && length(initial{2})==M
156 alphav = reshape(initial{1},1,M);
157 lambda = reshape(initial{2},M,1);
159 error('The length of the initial branch probability and rate vectors
is not consistent with the length of the orders vector!
');
162 error('Invalid initial branch probability and rate vectors!
');
170 while abs((ologli-logli)/logli)>stopCond && steps<=maxIter
174 Q(i,:) = (alphav(i)*(lambda(i)*trace).^(orders(i)-1) / factorial(orders(i)-1) * lambda(i)) .* exp(-lambda(i)*trace);
176 logli = sum(log(sum(Q,1))) / K;
179 Q(i,:) = Q(i,:) ./ nor;
185 lambda = diag(orders)*v1 ./ v2;
187 if BuToolsVerbose && etime(clock(),t1)>2
188 fprintf(
'Num of iterations: %d, logli: %g\n', steps, logli);
194 fprintf(
'Num of iterations: %d, logli: %g\n', steps, logli);
195 fprintf(
'EM algorithm terminated. (orders=');
197 fprintf(
'%g',orders(i));
206 if strcmp(result,
'vecvec')
209 elseif strcmp(result,'vecmat')
215 alpha(ix) = alphav(i);
216 A(ix:ix+orders(i)-1, ix:ix+orders(i)-1) = lambda(i)*(diag(ones(1,orders(i)-1),1)-diag(ones(1,orders(i))));