LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
m3pp_interleave_fitc.m
1function [fit,m3pps] = m3pp_interleave_fitc(av, btv, binfv, ...
2 acc, gtcc, t, tinf, ...
3 mapping, reorder)
4% Fits k second-order M3PP[m_j] and interleaves them into a
5% M3PP[m] of order k+1, with m = \sum_j=1^k m_j.
6%
7% INPUT
8% av: vector of length k with the per-process rate
9% btv: vector of length k with the per-process IDC(t)
10% binfv: vector of length k with the per-process IDC(inf)
11% acc: cell-array of length k; j-th element is a vector of length m_j
12% with the per-class rates in the j-th M3PP
13% gtcc: cell-array of length k; j-th element is a vector of length m_j
14% with the per-class variance + marginal_covariance in the j-th
15% M3PP
16% t: finite time scale
17% tinf: near-infinite time scale
18% mapping: binary m x k matrix mapping classes to processes
19% reorder: (optional, default = 1), enables reordering
20%
21% OUTPUT
22% fit: result of the interleaving, M3PP[m] of order k+1
23% m3pps: cell-array with the fitted and interleaved second-order
24% M3PP[m_j]
25
26if nargin < 9
27 reorder = 1;
28end
29
30% get number of partitions
31k = length(av);
32
33% number of classes for each partition
34mv = zeros(k,1);
35for j = 1:k
36 mv(j) = length(acc{j});
37end
38
39% total number of classes
40m = sum(mv);
41
42% check consistency
43for j = 1:k
44 if abs(av(j)-sum(acc{j})) > 1e-8
45 error('Inconsistent per-class rates in the %-th partition', j);
46 end
47end
48
49% vector of lower bounds for the upper off-diagonal elements
50uv = zeros(k,1);
51% vector of upper bounds for the upper off-diagonal elements
52dv = zeros(k,1);
53
54% compute bounds for the upper off-diagonal elment of each mmpp(2)
55for j = 1:k
56 if btv(j) >= binfv(j)
57 d = compute_d(btv(j), binfv(j)*(1+1e-4), t);
58 else
59 d = compute_d(btv(j), binfv(j), t);
60 end
61 z = (binfv(j)-1)*d^3*av(j);
62 u = d*z/(2*av(j)^2*d^2+z);
63 uv(j) = u;
64 dv(j) = d;
65end
66
67% find feasible values for off diagonal elements
68if ~reorder
69 [r,tran] = compute_feasible_interleave(uv,dv);
70 order = 1:k;
71else
72 [r,tran,order] = compute_feasible_interleave_reorder(uv,dv);
73end
74
75% compute position of each class
76cpos = zeros(m,1);
77for c = 1:m
78 % process modeling this class
79 j = find(mapping(c,:) == 1);
80 % position of the process
81 jpos = find(order == j);
82 % compute class position
83 cpos(c) = 0;
84 for hpos = 1:(jpos-1)
85 h = order(hpos);
86 cpos(c) = cpos(c) + sum(mapping(:,h));
87 end
88 cpos(c) = cpos(c) + sum(mapping(1:c,j));
89end
90
91% fit m3pp[m_j] processes
92m3pps = cell(k,1);
93for o = 1:k
94 % get process in o-th position
95 j = order(o);
96 % fit underlying mmpp(2)
97 %tran
98 if ~tran(j)
99 r1 = r(1,j);
100 r2 = r(2,j);
101 else
102 r1 = r(2,j);
103 r2 = r(1,j);
104 end
105 d = r1 + r2;
106 z = (binfv(j)-1)*d^3*av(j);
107 delta = sqrt(z/(2*r1*r2));
108 l2 = av(j) - r2/d * delta;
109 l1 = l2 + delta;
110 if tran(j)
111 tmp = r1;
112 r1 = r2;
113 r2 = tmp;
114 tmp = l1;
115 l1 = l2;
116 l2 = tmp;
117 end
118 mmpp = cell(1,2);
119 mmpp{1}(1,2) = r1;
120 mmpp{1}(2,1) = r2;
121 mmpp{2}(1,1) = l1;
122 mmpp{2}(2,2) = l2;
123 drates = -sum(mmpp{1}+mmpp{2},2);
124 for h = 1:2
125 mmpp{1}(h,h) = drates(h);
126 end
127 % get vectors of per-class characteristics
128 acj = acc{j};
129 gtcj = gtcc{j};
130 % fit m3pp(2,mj)
131 m3pps{o} = m3pp2m_fitc_approx_ag_multiclass(mmpp, acj, gtcj, t);
132end
133
134% total number of classes
135m = 0;
136for j = 1:k
137 m = m + length(acc{j});
138end
139
140% compute interleaving
141fit_unordered = m3pp2m_interleave(m3pps);
142
143% re-order output classes
144fit = cell(1,2+m);
145fit{1} = fit_unordered{1};
146fit{2} = fit_unordered{2};
147for i = 1:m
148 fit{2+i} = fit_unordered{2+cpos(i)};
149end
150
151% check feasibility and normalize
152if ~mmap_isfeasible(fit, 1e-10)
153 warning('Fitted MMAP infeasibile at tolerance 1e-10\n');
154end
155fit = mmap_normalize(fit);
156
157% find mapping of classes to the m3pps
158J = zeros(m,1);
159O = zeros(m,1);
160for i = 1:m
161 j = find(mapping(i,:) == 1);
162 o = sum(mapping(1:i,j));
163 J(i) = j;
164 O(i) = o;
165end
166
167% total rate
168a = sum(av);
169fa = map_count_mean(fit,1);
170
171% per class rates
172ac = zeros(m,1);
173for i = 1:m
174 ac(i) = acc{J(i)}(O(i));
175end
176fac = mmap_count_mean(fit,1);
177
178% compare characteristics
179fprintf('Rate: input = %f, %f\n', a, fa);
180for i = 1:m
181 fprintf('Class %d, rate: input = %f, output = %f\n', i, ac(i), fac(i));
182end
183fbtv = zeros(k,1);
184for j = 1:k
185 m3pp2 = cell(1,4);
186 m3pp2{1} = fit{1};
187 m3pp2{2} = fit{2};
188 m3pp2{3} = zeros(k+1,k+1);
189 for i = 1:m
190 if mapping(i,j) == 1
191 m3pp2{3} = m3pp2{3} + fit{2+i};
192 end
193 end
194 m3pp2{4} = m3pp2{2}-m3pp2{3};
195 fvj = mmap_count_var(m3pp2,t);
196 fbtv(j) = fvj(1)/(av(j)*t);
197end
198for j = 1:k
199 fprintf('Partition %d, IDC(%.2f): input = %f, output = %f\n', j, t, btv(j), fbtv(j));
200end
201fbinfv = zeros(k,1);
202fbinfv_limit = zeros(k,1);
203for j = 1:k
204 m3pp2 = cell(1,4);
205 m3pp2{1} = fit{1};
206 m3pp2{2} = fit{2};
207 m3pp2{3} = zeros(k+1,k+1);
208 for i = 1:m
209 if mapping(i,j) == 1
210 m3pp2{3} = m3pp2{3} + fit{2+i};
211 end
212 end
213 m3pp2{4} = m3pp2{2}-m3pp2{3};
214 fvj = mmap_count_var(m3pp2,tinf);
215 fbinfv(j) = fvj(1)/(av(j)*tinf);
216 fvj = mmap_count_var(m3pp2, 1e6);
217 fbinfv_limit(j) = fvj(1)/(av(j)*1e6);
218end
219for j = 1:k
220 fprintf('Partition %d, IDC(inf = %.2f): input = %f, output = %f, output_limit = %f\n', j, tinf, binfv(j), fbinfv(j), fbinfv_limit(j));
221end
222fvtc = mmap_count_var(fit,t);
223for j = 1:k
224 n = 1;
225 for i = 1:m
226 if mapping(i,j) == 1
227 m3pp3 = cell(1,5);
228 m3pp3{1} = fit{1};
229 m3pp3{2} = fit{2};
230 m3pp3{3} = fit{2+i};
231 m3pp3{4} = zeros(k+1,k+1);
232 for h = 1:m
233 if h ~= i && mapping(h,j) == 1
234 m3pp3{4} = m3pp3{4} + fit{2+h};
235 end
236 end
237 m3pp3{5} = m3pp3{2} - m3pp3{3} - m3pp3{4};
238 covji = mmap_count_mcov(m3pp3, t);
239 fprintf('gamma(%d): ', i);
240 fprintf('input = %f, output = %f\n', gtcc{j}(n), fvtc(i) + covji(1,2));
241 n = n + 1;
242 end
243 end
244end
245
246 % solve non-linear equation to fit underlying mmpp(2)
247 function d = compute_d(bt1,binf,t1)
248 if ~(binf>bt1 && bt1>1)
249 error('No solution, infeasible IDC(t): IDC(%.2f) = %.3f, IDC(inf) = %.3f\n',...
250 t1, bt1, binf);
251 end
252 % d = r1 + r2
253 g = (binf-1)/(binf-bt1);
254 % according to WolframAlpha
255 % the solution can be written as (ProductLog[-c e^(-c)]+c)/t1
256 z = -g*exp(-g);
257 w = fsolve(@(w) z-w*exp(w), 1, optimset('Display','none',...
258 'MaxIter',10000,...
259 'MaxFunEvals',10000,...
260 'TolFun', 1.0e-12,...
261 'TolX',1.0e-12));
262 d = (w + g)/t1;
263 end
264
265end