LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
map_varcount.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_MAM_MAP_VARCOUNT_H
6#define LINE_API_MAM_MAP_VARCOUNT_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * Variance of the counts of a MAP over windows of length t, in the spelling of
12 * matlab/lib/kpctoolbox/map/map_varcount.m.
13 *
14 * With Q = D0 + D1, pi the stationary phase vector, lambda = pi D1 e and
15 * tmp = (e pi - Q)^-1,
16 *
17 * Var[N(t)] = (lambda - 2 lambda^2 + 2 pi D1 tmp D1 e) t
18 * - 2 pi D1 (I - exp(Q t)) tmp^2 D1 e.
19 *
20 * This is algebraically identical to map_count_var.m (see map_count_var.h):
21 * e pi and Q commute because Q e = 0 and pi Q = 0, so tmp commutes with
22 * exp(Q t) and tmp (I - exp) tmp = (I - exp) tmp^2. Both spellings are ported
23 * because both are called from the MATLAB tree -- map_acfc.m normalizes by
24 * map_varcount, not by map_count_var -- and their agreement is a useful check
25 * on the exponential.
26 *
27 * ARITHMETIC: transcendental, as map_count_var.
28 */
29
30#include <cstddef>
31#include <vector>
32
35#include "line/num/number.h"
36#include "line/util/error.h"
37#include "line/util/expm.h"
38#include "line/util/linalg.h"
39#include "line/util/matrix.h"
40
41namespace line {
42namespace mam {
43
44/**
45 * @brief Variance of the counts of a MAP over windows of length t, in the
46 * spelling of matlab/lib/kpctoolbox/map/map_varcount.m.
47 *
48 * @param m the MAP (D0, D1)
49 * @param tset window lengths
50 * @return Var[N(t)] for each window length, in the order of tset
51 */
52template <class T>
53std::vector<T> map_varcount(const Map<T>& m, const std::vector<T>& tset) {
55 "map_varcount requires transcendental arithmetic");
56 const std::size_t n = m.order();
57 const Matrix<T> Q = map_infgen(m);
58 const std::vector<T> piq = map_prob(m);
59 const Matrix<T> tmp = detail::map_count_deviation(Q, piq);
60 const Matrix<T> tmp2 = matmul(tmp, tmp);
61 const std::vector<T> e = ones<T>(n);
62 const std::vector<T> D1e = mulvec(m.D1, e);
63
64 const std::vector<T> piD1 = vecmul(piq, m.D1); // row vector pi D1
65 T lam = num_traits<T>::from_int(0);
66 for (const T& v : piD1) lam += v;
67
68 const std::vector<T> piD1tmp = vecmul(piD1, tmp);
69 T pre_corr = num_traits<T>::from_int(0);
70 for (std::size_t i = 0; i < n; ++i) pre_corr += piD1tmp[i] * D1e[i];
71 const T two = num_traits<T>::from_int(2);
72 const T pre = lam - two * lam * lam + two * pre_corr;
73
74 const std::vector<T> tail = mulvec(tmp2, D1e); // tmp^2 D1 e
75
76 std::vector<T> out;
77 out.reserve(tset.size());
78 for (std::size_t k = 0; k < tset.size(); ++k) {
79 if (tset[k] < num_traits<T>::from_int(0))
80 throw InputError("map_varcount: negative window length");
81 const Matrix<T> E = expm(Q, tset[k]);
82 const std::vector<T> piD1E = vecmul(piD1, E);
83 T post = num_traits<T>::from_int(0);
84 for (std::size_t i = 0; i < n; ++i) post += (piD1[i] - piD1E[i]) * tail[i];
85 out.push_back(pre * tset[k] - two * post);
86 }
87 return out;
88}
89
90} // namespace mam
91} // namespace line
92
93#endif // LINE_API_MAM_MAP_VARCOUNT_H
InputError(const std::string &what)
Definition error.h:39
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.
Variance of the counting process of a MAP at resolution t.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
Matrix< T > map_infgen(const Map< T > &m)
Generator of the underlying phase process, D0 + D1.
Definition map_moment.h:62
std::vector< T > map_varcount(const Map< T > &m, const std::vector< T > &tset)
Variance of the counts of a MAP over windows of length t, in the spelling of matlab/lib/kpctoolbox/ma...
std::vector< T > map_prob(const Map< T > &m)
Stationary distribution of the phase process, pi (D0 + D1) = 0.
Definition map_moment.h:73
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Definition linalg.h:50
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Definition linalg.h:36
std::vector< T > mulvec(const Matrix< T > &A, const std::vector< T > &v)
Matrix times column vector, A v.
Definition linalg.h:62
std::vector< T > ones(std::size_t n)
Column vector of ones, the ubiquitous e in MAP algebra.
Definition linalg.h:104
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Definition expm.h:141
Number-type abstraction for the templated API port.
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D1
Definition map_moment.h:55
std::size_t order() const
Definition map_moment.h:57