LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
mmap2k_fit.m
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.
4%
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.
17%
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.
24%
25% Input:
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)
31%
32% Output:
33% MMAP: the fitted MMAP as {D0, D1, D11, ..., D1K}
34% EXACT: true when the closed form matched P, F and B exactly
35
36degentol = 1e-8;
37feastol = 1e-8;
38
39P = P(:); F = F(:); B = B(:);
40K = length(P);
41if length(F) ~= K || length(B) ~= K
42 error('mmap2k_fit: P, F and B must have the same length');
43end
44
45[~, AMAPS] = amap2_fit_gamma(M1, M2, M3, GAMMA);
46
47BEST = []; BESTERR = Inf;
48for j = 1:length(AMAPS)
49 D0 = AMAPS{j}{1};
50 D1 = AMAPS{j}{2};
51 if size(D0,1) ~= 2 || D0(2,1) ~= 0
52 continue
53 end
54 if D1(1,2) == 0
55 form = 1;
56 elseif D1(1,1) == 0
57 form = 2;
58 else
59 continue
60 end
61 h1 = -1/D0(1,1);
62 h2 = -1/D0(2,2);
63 r1 = D0(1,2) * h1;
64 r2 = D1(2,2) * h2;
65
66 q = zeros(3,K);
67 ok = true;
68 for c = 1:K
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);
70 if ~good
71 ok = false;
72 break
73 end
74 end
75 if ~ok
76 continue
77 end
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));
80 if viol < BESTERR
81 BESTERR = viol;
82 BEST = {D0, D1, form, q};
83 end
84end
85
86if ~isempty(BEST) && BESTERR <= feastol
87 D0 = BEST{1}; D1 = BEST{2}; form = BEST{3}; q = min(max(BEST{4},0),1);
88 MMAP = cell(1,2+K);
89 MMAP{1} = D0;
90 MMAP{2} = D1;
91 for c = 1:K
92 if form == 1
93 MMAP{2+c} = D1 .* [q(1,c) 0; q(2,c) q(3,c)];
94 else
95 MMAP{2+c} = D1 .* [0 q(1,c); q(2,c) q(3,c)];
96 end
97 end
98 EXACT = true;
99 return
100end
101
102% degenerate underlying form, or characteristics outside the feasible set
103MMAP = mamap2m_fit_gamma_fb(M1, M2, M3, GAMMA, P, F, B);
104EXACT = false;
105
106end
107
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;
111good = false;
112if form == 1
113 d1 = (r2-1)*(r1-1)*(h1*r2-h2);
114 d2 = (r2-1)*r1;
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
117 return
118 end
119 W = r1*r2 - r2 + 1;
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;
124else
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
129 return
130 end
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;
136end
137good = true;
138end
Definition Station.m:307
Definition Station.m:265