1% logli = LikelihoodFromTrace(trace, X, Y, prec)
3% Evaluates the log-likelihood of a trace with the given PH
4% distribution or MAP. The result
is divided by the length
7% If X
is a row vector, than (X,Y)
is interpreted as a PH
8% distribution, otherwise (X,Y)
is considered to be a MAP.
12% trace : column vector, length K
13% The samples of the trace
14% X : matrix, shape (1,M) or (M,M)
15% If X
is a row vector, it
is the initial probability
16% vector of the PH distribution. If X
is a square
17% matrix, it
is interpreted as the D0 matrix of a MAP
19% If X
is a row vector, Y
is the transient generator
20% of the PH distribution. If X
is a square matrix, Y
21%
is interpreted as the D1 matrix of a MAP
22% prec : double, optional
23% Numerical precision used by the randomization. The
24% default value
is 1e-14.
29% The log likelihood divided by the size of the trace
33% The procedure
is much faster with PH distributions.
35function logli = LikelihoodFromTrace (trace, X, Y, prec)
37 if ~exist(
'prec',
'var')
43 % We have a PH distribution. We can sort it to make the computation
48 lambda = max(abs(diag(A)));
49 P = A/lambda + eye(size(A));
51 eps = max(prec, 10^(log10(prec) + log10(lambda)));
61 while ~isempty(first) && k<maxIter
63 lpoi(first:end) = lpoi(first:end) + log(lambda) + trace(first:end) - log(k);
64 poi(first:end) = exp(lpoi(first:end));
65 spoi(first:end) = spoi(first:end) + poi(first:end);
66 fx(first:end) = fx(first:end) + poi(first:end) * (coeffv * a);
68 first = first + find(spoi(first:end)<1-eps,1,
'first') - 1;
70 logli = sum(log(fx))/length(trace);
77 % first we calculate matrix e^(D0*x(i))*D1
for each sample
78 [trace,ix] = sort(reshape(trace,[],1));
79 lambda = max(abs(diag(D0)));
80 P = D0/lambda + eye(size(D0));
81 eps = max(prec, 10^(log10(prec) + log10(lambda)));
87 fx = kron(poi,coeffv);
91 while ~isempty(first) && k<maxIter
93 lpoi(first:end) = lpoi(first:end) + log(lambda) + trace(first:end) - log(k);
94 poi(first:end) = exp(lpoi(first:end));
95 spoi(first:end) = spoi(first:end) + poi(first:end);
96 fx((first-1)*N+1:end,:) = fx((first-1)*N+1:end,:) + kron(poi(first:end),coeffv);
98 first = first + find(spoi(first:end)<1-eps,1,
'first') - 1;
100 alpha = DTMCSolve (inv(-D0)*D1);
105 l = l*fx((ixrev(i)-1)*N+1:ixrev(i)*N,:);
107 % sometimes we need to rescale the results to avoid
"nan"s
108 scale = ceil(log2(sum(l)));
120 logli = (log(sum(l))+sc*log(2)) / length(trace);