LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mmap_count_var.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_MMAP_COUNT_VAR_H
6#define LINE_API_MAM_MMAP_COUNT_VAR_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * Per-class variance of the counting process of a marked MAP.
12 *
13 * Templated port of matlab/lib/m3a/m3a/mmap/mmap_count_var.m. It is the He and
14 * Neuts variance of map_count_var applied class by class: for class k, with
15 * D = D0 + D1 the generator of the phase process (the AGGREGATE one, not the
16 * class-k one), theta its stationary vector and tmp = (e theta - D)^-1,
17 *
18 * Var[N_k(t)] = (lambda_k - 2 lambda_k^2 + 2 theta Dk tmp Dk e) t
19 * - 2 theta Dk tmp (I - exp(D t)) tmp Dk e,
20 *
21 * with lambda_k = theta Dk e. The phase process is shared across classes, so
22 * the exponential is computed once and reused for every class.
23 *
24 * ARITHMETIC: transcendental, as map_count_var.
25 *
26 * This function was previously out of reach of the port for want of expm only;
27 * the other MMAP counting descriptors (mmap_count_mean, mmap_count_lambda) are
28 * rational and are already in mmap_lambda.h.
29 */
30
31#include <cstddef>
32#include <vector>
33
37#include "line/num/number.h"
38#include "line/util/error.h"
39#include "line/util/expm.h"
40#include "line/util/linalg.h"
41#include "line/util/matrix.h"
42
43namespace line {
44namespace mam {
45
46/**
47 * @brief Per-class variance of the counting process of a marked MAP.
48 *
49 * @param mm the marked MAP
50 * @param t window length
51 * @return Var[N_k(t)] for each class k
52 */
53template <class T>
54std::vector<T> mmap_count_var(const Mmap<T>& mm, const T& t) {
56 "mmap_count_var requires transcendental arithmetic");
57 if (t < num_traits<T>::from_int(0)) throw InputError("mmap_count_var: negative window length");
58 const std::size_t n = mm.order();
59 const std::size_t K = mm.classes();
60 if (K == 0) throw InputError("mmap_count_var: the MMAP has no classes");
61
62 const Map<T> base = mm.map();
63 const Matrix<T> D = map_infgen(base);
64 const std::vector<T> theta = map_prob(base);
65 const Matrix<T> tmp = detail::map_count_deviation(D, theta);
66 const std::vector<T> e = ones<T>(n);
67 const Matrix<T> E = expm(D, t);
68 const T two = num_traits<T>::from_int(2);
69
70 std::vector<T> out;
71 out.reserve(K);
72 for (std::size_t k = 0; k < K; ++k) {
73 const Matrix<T>& Dk = mm.Dc[k];
74 const std::vector<T> thetaDk = vecmul(theta, Dk);
75 T lam = num_traits<T>::from_int(0);
76 for (const T& v : thetaDk) lam += v;
77 const std::vector<T> c = vecmul(thetaDk, tmp); // theta Dk tmp
78 const std::vector<T> d = mulvec(tmp, mulvec(Dk, e)); // tmp Dk e
79 const std::vector<T> cDk = vecmul(c, Dk);
80 T cDke = num_traits<T>::from_int(0);
81 for (const T& v : cDk) cDke += v;
82
83 const std::vector<T> cE = vecmul(c, E);
84 T corr = num_traits<T>::from_int(0);
85 for (std::size_t i = 0; i < n; ++i) corr += (c[i] - cE[i]) * d[i];
86 out.push_back((lam - two * lam * lam + two * cDke) * t - two * corr);
87 }
88 return out;
89}
90
91} // namespace mam
92} // namespace line
93
94#endif // LINE_API_MAM_MMAP_COUNT_VAR_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.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
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_prob(const Map< T > &m)
Stationary distribution of the phase process, pi (D0 + D1) = 0.
Definition map_moment.h:73
std::vector< T > mmap_count_var(const Mmap< T > &mm, const T &t)
Per-class variance of the counting process of a marked MAP.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Definition linalg.h:50
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
An MMAP: the underlying MAP plus the per-class arrival matrices.
Definition mmap_lambda.h:45
Map< T > map() const
Definition mmap_lambda.h:52
std::size_t classes() const
Definition mmap_lambda.h:51
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
Definition mmap_lambda.h:48
std::size_t order() const
Definition mmap_lambda.h:50