LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
map_acfc.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_ACFC_H
6#define LINE_API_MAM_MAP_ACFC_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * Autocorrelation of the counting process of a MAP at a given timescale.
12 *
13 * Templated port of matlab/lib/kpctoolbox/map/map_acfc.m. With Q = D0 + D1, pi
14 * the stationary phase vector, u the slot length and tmp = (e pi - Q)^-1,
15 *
16 * rho(k) = PRE exp(Q (k-1) u) POST / Var[N(u)],
17 * PRE = pi D1 (I - exp(Q u)),
18 * POST = (I - exp(Q u)) tmp^2 D1 e,
19 *
20 * i.e. the lag-k covariance of the numbers of arrivals in consecutive windows
21 * of length u, normalized by their common variance (map_varcount). This is the
22 * counting-process autocorrelation, not the inter-arrival autocorrelation
23 * map_acf, and the two are different functions of the same MAP: a renewal
24 * process has zero inter-arrival autocorrelation at every lag but a nonzero
25 * count autocorrelation unless it is Poisson.
26 *
27 * ARITHMETIC: transcendental, three matrix exponentials per lag.
28 *
29 * The JAR has no counterpart of this function.
30 */
31
32#include <cstddef>
33#include <vector>
34
38#include "line/num/number.h"
39#include "line/util/error.h"
40#include "line/util/expm.h"
41#include "line/util/linalg.h"
42#include "line/util/matrix.h"
43
44namespace line {
45namespace mam {
46
47/**
48 * @brief Autocorrelation of the counting process of a MAP at a given
49 * timescale.
50 *
51 * @param m the MAP (D0, D1)
52 * @param kset lags, each >= 1
53 * @param u length of the counting window (the timescale)
54 * @return rho(k) for each lag, in the order of kset
55 */
56template <class T>
57std::vector<T> map_acfc(const Map<T>& m, const std::vector<unsigned>& kset, const T& u) {
59 "map_acfc requires transcendental arithmetic");
60 const std::size_t n = m.order();
61 if (!(u > num_traits<T>::from_int(0))) throw InputError("map_acfc: the timescale must be positive");
62 const Matrix<T> Q = map_infgen(m);
63 const std::vector<T> piq = map_prob(m);
64 const Matrix<T> tmp = detail::map_count_deviation(Q, piq);
65 const Matrix<T> tmp2 = matmul(tmp, tmp);
66 const std::vector<T> e = ones<T>(n);
67
68 const Matrix<T> Eu = expm(Q, u);
69 const std::vector<T> piD1 = vecmul(piq, m.D1);
70 const std::vector<T> piD1Eu = vecmul(piD1, Eu);
71 std::vector<T> pre(n);
72 for (std::size_t i = 0; i < n; ++i) pre[i] = piD1[i] - piD1Eu[i]; // pi D1 (I - e^{Qu})
73
74 const std::vector<T> tail = mulvec(tmp2, mulvec(m.D1, e)); // tmp^2 D1 e
75 const std::vector<T> Eutail = mulvec(Eu, tail);
76 std::vector<T> post(n);
77 for (std::size_t i = 0; i < n; ++i) post[i] = tail[i] - Eutail[i]; // (I - e^{Qu}) tmp^2 D1 e
78
79 const std::vector<T> uv(1, u);
80 const T vart = map_varcount(m, uv)[0];
81 if (vart == num_traits<T>::from_int(0))
82 throw NumericError("map_acfc: the counts have zero variance at this timescale");
83
84 std::vector<T> out;
85 out.reserve(kset.size());
86 for (std::size_t j = 0; j < kset.size(); ++j) {
87 if (kset[j] < 1u) throw InputError("map_acfc: lags must be at least 1");
88 const T lagtime = num_traits<T>::from_int(static_cast<long>(kset[j] - 1u)) * u;
89 std::vector<T> v = pre;
90 if (!(lagtime == num_traits<T>::from_int(0))) v = vecmul(pre, expm(Q, lagtime));
92 for (std::size_t i = 0; i < n; ++i) s += v[i] * post[i];
93 out.push_back(s / vart);
94 }
95 return out;
96}
97
98} // namespace mam
99} // namespace line
100
101#endif // LINE_API_MAM_MAP_ACFC_H
InputError(const std::string &what)
Definition error.h:39
NumericError(const std::string &what)
Definition error.h:45
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...
Variance of the counts of a MAP over windows of length t, in the spelling of matlab/lib/kpctoolbox/ma...
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_acfc(const Map< T > &m, const std::vector< unsigned > &kset, const T &u)
Autocorrelation of the counting process of a MAP at a given timescale.
Definition map_acfc.h:57
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