LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
m3pp_interleave_fitc_trace.m
1function [fit,m3pps] = m3pp_interleave_fitc_trace(T, A, t, tinf, mapping)
2% Interleaves k M3PP to fit a multi-class trace with m classes.
3% INPUT
4% - T: inter-arrival time
5% - A: labels
6% - t: finite time scale
7% - tinf: near-infinite time scale
8% - mapping: m x k binary matrix mapping the m classes to k m3pp (optional,
9% by default k = m)
10
11
12% number of classes
13L = unique(A);
14m = length(L);
15
16% by default choose time scales as follows
17if nargin < 3
18 t = 10*mean(T);
19 tinf = max(10*t, (sum(T)-T(1)) / 100);
20end
21% compute counting process at resolution t1 and tinf
22Nt = mtrace_iat2counts(T, A, t);
23Ninf = mtrace_iat2counts(T, A, tinf);
24
25% by default, %%map one class per m3pp
26if nargin < 5
27 % mapping = eye(m);
28 delta = 0.75;
29 SIGMA = cov(Ninf);
30 pool = 1:m;
31 J=1;
32 variance = diag(SIGMA);
33 while length(pool)>0
34 k = pool(maxpos(variance(pool)));
35 mapping(k,J)=1;
36 pool(find(k == pool))=[];
37 for h=pool
38 if SIGMA(k,h)/sqrt(SIGMA(k,k)*SIGMA(h,h)) >= delta
39 mapping(h,J)=1;
40 pool(find(h == pool))=[];
41 end
42 end
43 J = J+1;
44 end
45
46end
47% check mapping consistency
48if size(mapping,1) ~= m
49 error('Number of classes does not match mapping.');
50end
51
52% number of m3pps
53k = size(mapping,2);
54
55% check mapping
56rowsum = sum(mapping,2);
57if max(rowsum) > 1 || min(rowsum) < 1
58 error('Invalid mapping');
59end
60
61fprintf('Fitting %d classes with %d M3PP(2,m_j) processes\n', m, k);
62
63% rate
64a = 1/mean(T);
65
66% per-class rates
67pc = zeros(m,1);
68for j = 1:m
69 pc(j) = sum(A==L(j))/length(A);
70end
71ac = pc * a;
72
73% filters
74f = cell(k,1);
75for j = 1:k
76 f{j} = mapping(:,j) == 1;
77end
78
79% total rates of each m3pp
80av = zeros(k,1);
81for j = 1:k
82 av(j) = sum(ac(f{j}));
83end
84
85% per-class rates within each m3pp
86acc = cell(k,1);
87for j = 1:k
88 acc{j} = ac(f{j});
89end
90
91% compute IDC(t) e IDC(inf) for each m3pp
92btv = zeros(k,1);
93binfv = zeros(k,1);
94for j = 1:k
95 % counting processes for the j-th m3pp
96 Ntj = zeros(size(Nt,1),1);
97 Ninfj = zeros(size(Ninf,1),1);
98 for i = 1:m
99 % sum classes fitted by this m3pp
100 if mapping(i,j)
101 Ntj = Ntj + Nt(:,i);
102 Ninfj = Ninfj + Ninf(:,i);
103 end
104 end
105 % IDC
106 btv(j) = var(Ntj)/(av(j)*t);
107 binfv(j) = var(Ninfj)/(av(j)*tinf);
108end
109
110% compute per-class variance plus marginal covariance within each m3pp
111gtc = cell(k,1);
112for j = 1:k
113 % number of classes fitted with this m3pp
114 mj = sum(mapping(:,j));
115 % filter classes
116 fj = mapping(:,j) == 1;
117 % per-class variance of this m3pp
118 vtj = var(Nt(:,fj))';
119 % per class variance + marginal covariance of this m3pp
120 stj = zeros(mj,1);
121 h = 1;
122 for i = 1:m
123 % if class i is mapped to the j-th m3pp
124 if mapping(i,j) == 1
125 covtji = cov(Nt(:,i), sum(Nt(:,fj),2)-Nt(:,i));
126 stj(h) = covtji(1,2);
127 h = h + 1;
128 end
129 end
130 % vector of per-class variance plus margina covariance
131 gtc{j} = vtj + stj;
132end
133
134[fit,m3pps] = m3pp_interleave_fitc(av, btv, binfv, ...
135 acc, gtc, t, tinf, mapping);
136
137end