5#ifndef LINE_API_CACHE_LRUM_MAP_LEVELSTATS_H
6#define LINE_API_CACHE_LRUM_MAP_LEVELSTATS_H
89 const std::vector<T>& Tv) {
91 "cache_lrum_map_levelstats requires transcendental arithmetic");
92 const std::size_t d = D0.
rows();
93 const std::size_t h = Tv.size();
94 if (d == 0 || D0.
cols() != d || D1.
rows() != d || D1.
cols() != d)
95 throw InputError(
"cache_lrum_map_levelstats: D0 and D1 must be square and of equal size");
96 if (h == 0)
throw InputError(
"cache_lrum_map_levelstats: no characteristic times");
98 for (std::size_t l = 0; l < h; ++l)
100 throw InputError(
"cache_lrum_map_levelstats: characteristic times must be positive");
103 for (std::size_t i = 0; i < d; ++i)
104 for (std::size_t j = 0; j < d; ++j) negD0(i, j) = -D0(i, j);
108 std::vector<Matrix<T>> E(h), N(h), A(h);
109 for (std::size_t l = 0; l < h; ++l) {
110 E[l] =
expm(D0, Tv[l]);
112 for (std::size_t i = 0; i < d; ++i)
113 for (std::size_t j = 0; j < d; ++j) IE(i, j) = I(i, j) - E[l](i, j);
121 std::vector<Matrix<T>> R(h);
122 for (std::size_t li = h; li-- > 0;) {
126 lhs = (h == 1) ? A0 : A[li - 1];
127 for (std::size_t i = 0; i < d; ++i)
128 for (std::size_t j = 0; j < d; ++j) inner(i, j) = I(i, j) - A[li](i, j);
130 lhs = (li == 0) ? A0 : A[li - 1];
132 for (std::size_t i = 0; i < d; ++i)
133 for (std::size_t j = 0; j < d; ++j) inner(i, j) = I(i, j) - RE(i, j);
142 const std::vector<T> pM =
vecmul(pi0, M);
144 for (std::size_t i = 0; i < d; ++i) {
145 const T e =
num_abs(T(pM[i] - pi0[i]));
146 if (e > res) res = e;
150 "cache_lrum_map_levelstats: R_1 exp(D0 T_1) has no unit Perron root, so the "
151 "level-0 balance equation has no stochastic solution");
154 std::vector<std::vector<T>> pih(h);
155 pih[0] =
vecmul(pi0, R[0]);
156 for (std::size_t l = 1; l < h; ++l) pih[l] =
vecmul(pih[l - 1], R[l]);
158 std::vector<T> holding(h + 1, zero);
160 const std::vector<T> v =
vecmul(pi0, N0);
161 for (std::size_t i = 0; i < d; ++i) holding[0] += v[i];
163 for (std::size_t l = 0; l < h; ++l) {
164 const std::vector<T> v =
vecmul(pih[l], N[l]);
165 for (std::size_t i = 0; i < d; ++i) holding[1 + l] += v[i];
168 for (std::size_t l = 0; l <= h; ++l) denom += holding[l];
169 if (denom == zero)
throw NumericError(
"cache_lrum_map_levelstats: zero total holding time");
172 out.
prob.resize(h + 1);
173 for (std::size_t l = 0; l <= h; ++l) out.
prob[l] = holding[l] / denom;
174 out.
occ.assign(out.
prob.begin() + 1, out.
prob.end());
182 for (std::size_t l = 0; l < h; ++l) {
183 const std::vector<T> v =
vecmul(
vecmul(pih[l], N[l]), D1);
185 for (std::size_t i = 0; i < d; ++i) s += v[i];
186 out.
hitfrac[l] = s / denom / lam;
NumericError(const std::string &what)
Steady-state distribution of a continuous-time Markov chain.
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
The exception types the port throws.
Matrix exponential by scaling and squaring with a diagonal Pade approximant.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
CacheLrumMapLevelStats< T > cache_lrum_map_levelstats(const Matrix< T > &D0, const Matrix< T > &D1, const std::vector< T > &Tv)
Level statistics of one item's embedded (list, phase) chain in the LRU(m)-MAP TTL approximation.
T map_lambda(const Map< T > &m)
Stationary arrival rate, lambda = pi D1 e.
std::vector< T > dtmc_solve(const Matrix< T > &P)
Stationary distribution of a stochastic matrix P.
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 > eye(std::size_t n)
Identity of order n.
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Number-type abstraction for the templated API port.
Per-item level statistics, MATLAB's [prob, occ, hitfrac].
std::vector< T > hitfrac
(h) fraction of the item's requests hitting in list l
std::vector< T > occ
(h) occupancy of lists 1..h, prob(2:end)
std::vector< T > prob
(h+1) time-stationary probability of level 0..h
A MAP as the pair of matrices (D0, D1).