5#ifndef LINE_API_MAM_MMAP_STATS_H
6#define LINE_API_MAM_MMAP_STATS_H
52Matrix<T> mmap_embedded_class(
const Mmap<T>& mm, std::size_t c) {
53 Matrix<T> negD0 = mm.D0;
54 for (std::size_t i = 0; i < negD0.rows(); ++i)
55 for (std::size_t j = 0; j < negD0.cols(); ++j) negD0(i, j) = -negD0(i, j);
64 std::vector<Matrix<T>> Pc;
66 for (std::size_t c = 0; c < mm.
classes(); ++c) Pc.push_back(detail::mmap_embedded_class(mm, c));
73 std::vector<Map<T>> maps;
75 for (std::size_t c = 0; c < mm.
classes(); ++c) {
77 for (std::size_t i = 0; i < A.
rows(); ++i)
78 for (std::size_t j = 0; j < A.
cols(); ++j) A(i, j) += mm.
D1(i, j) - mm.
Dc[c](i, j);
92 const std::size_t n = mm.
order();
93 const std::size_t C = mm.
classes();
95 for (std::size_t c = 0; c < C; ++c) {
97 for (std::size_t i = 0; i < n; ++i)
98 for (std::size_t j = 0; j < n; ++j)
99 B(i, j) = -mm.
D0(i, j) - mm.
D1(i, j) + mm.
Dc[c](i, j);
102 for (std::size_t i = 0; i + 1 < n; ++i)
103 for (std::size_t j = 0; j < n; ++j)
109 const std::vector<T> x =
solve(A, b);
110 for (std::size_t j = 0; j < n; ++j) pie(c, j) = x[j];
118 const std::size_t n = mm.
order();
123 for (std::size_t i = 0; i < n; ++i)
124 for (std::size_t j = 0; j < n; ++j) {
125 out.
D0(i, j) = mm.
D0(j, i) * piq[j] / piq[i];
126 out.
D1(i, j) = mm.
D1(j, i) * piq[j] / piq[i];
129 for (std::size_t c = 0; c < mm.
classes(); ++c) {
131 for (std::size_t i = 0; i < n; ++i)
132 for (std::size_t j = 0; j < n; ++j) R(i, j) = mm.
Dc[c](j, i) * piq[j] / piq[i];
141 const std::size_t C = mm.
classes();
142 const std::vector<T> alpha =
map_pie(mm.
map());
145 for (std::size_t i = 0; i < C; ++i) {
146 const std::vector<T> start =
vecmul(alpha, E[i]);
147 for (std::size_t j = 0; j < C; ++j) {
148 const std::vector<T> v =
vecmul(start, E[j]);
150 for (std::size_t k = 0; k < v.size(); ++k) acc += v[k];
160 const std::size_t C = mm.
classes();
161 const std::vector<T> alpha =
map_pie(mm.
map());
163 std::vector<std::vector<std::vector<T>>> sigma(
165 for (std::size_t i = 0; i < C; ++i) {
166 const std::vector<T> starti =
vecmul(alpha, E[i]);
167 for (std::size_t j = 0; j < C; ++j) {
168 const std::vector<T> startj =
vecmul(starti, E[j]);
169 for (std::size_t h = 0; h < C; ++h) {
170 const std::vector<T> v =
vecmul(startj, E[h]);
172 for (std::size_t k = 0; k < v.size(); ++k) acc += v[k];
173 sigma[i][j][h] = acc;
183 const std::size_t n = mm.
order();
184 const std::size_t C = mm.
classes();
187 for (std::size_t c = 0; c < C; ++c) {
189 for (std::size_t i = 0; i < n; ++i)
190 for (std::size_t j = 0; j < n; ++j) acc += theta[i] * mm.
Dc[c](i, j);
202 for (std::size_t c = 0; c < m.size(); ++c) idc[c] = v[c] / m[c];
211 for (std::size_t c = 0; c < lam.size(); ++c) total += lam[c];
223 const std::size_t C = mm.
classes();
226 for (std::size_t i = 0; i < C; ++i) S(i, i) = mV[i];
228 for (std::size_t i = 0; i < C; ++i)
229 for (std::size_t j = 0; j < C; ++j) {
230 if (i == j)
continue;
235 for (std::size_t r = 0; r < A.
rows(); ++r)
236 for (std::size_t s = 0; s < A.
cols(); ++s) A(r, s) += mm.
Dc[j](r, s);
238 for (std::size_t r = 0; r < B.
rows(); ++r)
239 for (std::size_t s = 0; s < B.
cols(); ++s) B(r, s) -= A(r, s);
240 pooled.
Dc.push_back(A);
241 pooled.
Dc.push_back(B);
243 S(i, j) = half * (pV[0] - mV[i] - mV[j]);
254 const std::size_t C = mm.
classes();
258 for (std::size_t i = 0; i < negD0.
rows(); ++i)
259 for (std::size_t j = 0; j < negD0.
cols(); ++j) negD0(i, j) = -negD0(i, j);
263 for (std::size_t i = 0; i < C; ++i) {
264 const std::vector<T> v =
vecmul(pie, E[i]);
265 for (std::size_t r = 0; r < v.size(); ++r) TG[i] += v[r];
269 for (std::size_t i = 0; i < C; ++i) {
270 std::vector<T> start =
vecmul(pie, E[i]);
271 for (std::size_t r = 0; r < start.size(); ++r) start[r] = start[r] / TG[i];
272 for (std::size_t j = 0; j < C; ++j) {
274 const std::vector<T> den =
vecmul(start, E[j]);
276 for (std::size_t r = 0; r < num.size(); ++r)
sn += num[r];
277 for (std::size_t r = 0; r < den.size(); ++r) sd += den[r];
278 MC(i, j) = fk *
sn / sd;
292 const std::size_t C = mm.
classes();
296 for (std::size_t i = 0; i < negD0.
rows(); ++i)
297 for (std::size_t j = 0; j < negD0.
cols(); ++j) negD0(i, j) = -negD0(i, j);
300 for (std::size_t a = 0; a < C; ++a) {
301 const std::vector<T> start =
vecmul(pie, E[a]);
305 for (std::size_t r = 0; r < start.size(); ++r) pa += start[r];
307 for (std::size_t h = 0; h < orders.size(); ++h) {
308 const std::vector<T> v =
vecmul(start,
matpow(M, orders[h]));
310 for (std::size_t r = 0; r < v.size(); ++r) acc += v[r];
326 if (n == 0)
throw InputError(
"mmap_sum: n must be positive");
327 const std::size_t ns = mm.
order();
328 const std::size_t C = mm.
classes();
329 const std::size_t N = ns * n;
335 for (
unsigned b = 0; b < n; ++b) {
336 const std::size_t off = b * ns;
337 for (std::size_t i = 0; i < ns; ++i)
338 for (std::size_t j = 0; j < ns; ++j) out.
D0(off + i, off + j) = mm.
D0(i, j);
340 for (std::size_t i = 0; i < ns; ++i)
341 for (std::size_t j = 0; j < ns; ++j) out.
D0(off + i, off + ns + j) = mm.
D1(i, j);
343 for (std::size_t i = 0; i < ns; ++i)
344 for (std::size_t j = 0; j < ns; ++j) {
345 out.
D1(off + i, j) = mm.
D1(i, j);
346 for (std::size_t c = 0; c < C; ++c) out.
Dc[c](off + i, j) = mm.
Dc[c](i, j);
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
Per-class variance of the counting process of a marked MAP.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
std::vector< T > mmap_lambda(const Mmap< T > &m)
Alias kept for parity with the MATLAB name.
std::vector< Matrix< T > > mmap_embedded(const Mmap< T > &mm)
Embedded per-class kernels E_c = (-D0)^-1 D1^(c).
std::vector< std::vector< std::vector< T > > > mmap_sigma2(const Mmap< T > &mm)
sigma2(i,j,h) = pie E_i E_j E_h 1, indexed as sigma2[i][j][h].
Matrix< T > mmap_count_mcov(const Mmap< T > &mm, const T &t)
Covariance matrix of the per-class counts over a window of length t.
Matrix< T > mmap_forward_moment(const Mmap< T > &mm, const std::vector< unsigned > &orders, bool normalize)
Forward moments: MOMENTS(a,h) is the order-orders[h] moment of the interval ENDING with a class-a arr...
Mmap< T > mmap_timereverse(const Mmap< T > &mm)
Time-reversed MMAP, D^-1 M' D with D = diag(map_prob) applied to every matrix.
std::vector< T > map_prob(const Map< T > &m)
Stationary distribution of the phase process, pi (D0 + D1) = 0.
std::vector< T > mmap_count_idc(const Mmap< T > &mm, const T &t)
Per-class index of dispersion of counts over a window of length t.
std::vector< T > mmap_count_mean(const Mmap< T > &mm, const T &t)
Per-class mean of the counting process over a window of length t.
std::vector< T > mmap_idc(const Mmap< T > &mm)
Asymptotic per-class index of dispersion, evaluated at t = 1e6 / sum_c lambda_c.
Matrix< T > mmap_sigma(const Mmap< T > &mm)
sigma(i,j) = pie E_i E_j 1, the probability that two consecutive marks are (i,j).
std::vector< Map< T > > mmap_maps(const Mmap< T > &mm)
The C MAPs seen by each class, MAP_c = (D0 + D1 - D1^(c), D1^(c)).
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
Matrix< T > mmap_pie(const Mmap< T > &mm)
Stationary phase distribution seen just after a class-c arrival, one row per class.
Mmap< T > mmap_sum(const Mmap< T > &mm, unsigned n)
MMAP of the sum of n independent copies, a block bidiagonal concatenation.
Matrix< T > mmap_cross_moment(const Mmap< T > &mm, unsigned k)
Cross moments of order k: MC(i,j) is E[T^k] of the interval that FOLLOWS a class-i arrival,...
std::vector< T > mmap_count_var(const Mmap< T > &mm, const T &t)
Per-class variance of the counting process of a marked MAP.
T num_factorial(unsigned n)
Factorial as a value of T.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Matrix< T > matpow(const Matrix< T > &A, unsigned k)
Integer matrix power, by repeated squaring.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Number-type abstraction for the templated API port.
A MAP as the pair of matrices (D0, D1).
An MMAP: the underlying MAP plus the per-class arrival matrices.
std::size_t classes() const
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
std::size_t order() const