1function [
MMAP, EXACT] = mmap2k_fit(M1, M2, M3, GAMMA,
P, F, B)
2% [
MMAP, EXACT] = mmap2k_fit(M1, M2, M3, GAMMA,
P, F, B)
3% Closed-
form fit of an
MMAP(2,K): a marked MAP of second order with K classes.
5% The fit factorizes into two independent inverse problems:
6% 1.
the underlying MAP(2) from (M1, M2, M3, GAMMA), by
the acyclic canonical
7% inverse of amap2_fit_gamma. Order two loses nothing: every MAP(2)
is
8% equivalent to one of
the two acyclic canonical forms;
9% 2.
the marking, which
is LINEAR in
the class characteristics. Writing
the
10% marking as fractions of
the aggregate D1,
11%
form 1: D1{c} = D1 .* [q1c 0; q2c q3c]
12%
form 2: D1{c} = D1 .* [0 q1c; q2c q3c]
13%
the map (q1c,q2c,q3c) -> (p_c, p_c F_c, p_c B_c)
is linear, with a 3x3
14% matrix depending only on (h1,h2,r1,r2) and IDENTICAL
for every
class. Its
15% inverse
is therefore pre-computed once below and applied per
class, so
the
16% cost does not grow with K and no quadratic program
is needed.
18% The inverse was derived symbolically in SageMath, see
19% sage/proofs/mmap2k_marking_inverse.py. It
is singular exactly on
the six
20% degenerate loci r1 in {0,1}, r2 in {0,1}, h1-h2+h2*r1 = 0 and, per
form,
21% h1*r2-h2 = 0 or h1*r1*r2-h1*r1+h1-h2 = 0, which are
the branches
22% mamap2m_fit_fb_multiclass handles. There, and whenever
the closed
form leaves
23%
the unit box,
this function falls back to that quadratic program.
26% M1,M2,M3: raw moments of
the inter-arrival times
27% GAMMA: autocorrelation decay rate
28%
P:
class probabilities (sum to one)
29% F: first-order forward moments (sum_c
P(c) F(c) = M1)
30% B: first-order backward moments (sum_c
P(c) B(c) = M1)
34% EXACT:
true when
the closed
form matched
P, F and B exactly
39P =
P(:); F = F(:); B = B(:);
41if length(F) ~= K || length(B) ~= K
42 error(
'mmap2k_fit: P, F and B must have the same length');
45[~, AMAPS] = amap2_fit_gamma(M1, M2, M3, GAMMA);
47BEST = []; BESTERR = Inf;
48for j = 1:length(AMAPS)
51 if size(D0,1) ~= 2 || D0(2,1) ~= 0
69 [q(1,c), q(2,c), q(3,c), good] = marking_inverse(
form, h1, h2, r1, r2,
P(c), F(c), B(c), degentol);
78 % feasible when
the fractions lie in
the unit box and close per phase
79 viol = max(0, max(max(-q))) + max(0, max(max(q-1))) + max(abs(sum(q,2) - 1));
82 BEST = {D0, D1,
form, q};
86if ~isempty(BEST) && BESTERR <= feastol
87 D0 = BEST{1}; D1 = BEST{2};
form = BEST{3}; q = min(max(BEST{4},0),1);
93 MMAP{2+c} = D1 .* [q(1,c) 0; q(2,c) q(3,c)];
95 MMAP{2+c} = D1 .* [0 q(1,c); q(2,c) q(3,c)];
102% degenerate underlying
form, or characteristics outside
the feasible set
103MMAP = mamap2m_fit_gamma_fb(M1, M2, M3, GAMMA,
P, F, B);
108function [q1, q2, q3, good] = marking_inverse(
form, h1, h2, r1, r2, p, Fc, Bc, degentol)
109% Pre-computed inverse of
the per-
class linear
map, see
the header.
110q1 = 0; q2 = 0; q3 = 0;
113 d1 = (r2-1)*(r1-1)*(h1*r2-h2);
115 d3 = r1*r2*(h1+h2*r1-h2);
116 if min([abs(d1) abs(d2) abs(d3) abs(h1+h2*r1-h2) abs(h1*r2-h2)]) < degentol
120 q1 = p * W * ((h1*r2 - h1 - h2) + Bc) / d1;
121 q2 = p * W * ((h1^2*(r2-1) + h1*h2*r1*(r2-1) - h2^2*r1)/((h1+h2*r1-h2)*(h1*r2-h2)) ...
122 - Fc/(h1+h2*r1-h2) + Bc/(h1*r2-h2)) / d2;
123 q3 = p * W * ((h1 + h2*r1) - Fc) / d3;
125 U = h1*r1*r2 - h1*r1 + h1 - h2;
126 d1 = (r2-1)*(r1-1)*U;
127 d2 = (r2-1)*(h1+h2*r1-h2);
128 if min([abs(d1) abs(d2) abs(r2) abs(U) abs(h1+h2*r1-h2)]) < degentol
131 V = r1*r2 - r1 - r2 + 2;
132 q1 = p * V * ((h1*r1*r2 - h1*r1 - h2) + Bc) / d1;
133 q2 = p * V * (h2 - Fc) / d2;
134 q3 = p * V * ((h1^2 + h1*h2*r1*r2 - h2^2)/((h1+h2*r1-h2)*U) ...
135 - Fc/(h1+h2*r1-h2) - Bc/U) / r2;