LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mtrace_count.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_TRACE_MTRACE_COUNT_H
6#define LINE_API_TRACE_MTRACE_COUNT_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Per-class count process of a marked trace on a fixed resolution: the number
12 * of events of each class in the successive windows of length `t` that start
13 * at the first arrival epoch.
14 *
15 * Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_count.m.
16 *
17 * REFERENCE DEFECT (MATLAB): the class test is `A == c` with c the LOOP INDEX
18 * 1..length(unique(A)), not the label unique(A)(c). Every trace whose labels
19 * are not exactly 1..C is therefore counted against the wrong classes, and
20 * labels outside that range are never counted at all -- with 0/1 labels, for
21 * instance, all the zeros are dropped and the class-1 column is reported as
22 * class 1 while column 2 stays empty. This port compares against the label,
23 * which is what the header comment of the function describes.
24 *
25 * DIVERGENCE, MATLAB vs JAR: Mtrace_count.java is not this function. It
26 * computes windowed count STATISTICS (mean, variance, index of dispersion and
27 * skewness of the count process, plus a multiscale sweep) over
28 * floor(total/window) windows, and its generateCountProcess increments every
29 * window from the previous index to the current one, so its counts are
30 * partial cumulative sums rather than per-window counts. It has no MATLAB
31 * counterpart and is not ported; the count-statistics wrapper it exists for
32 * is a composition of this function with trace_var / trace_skew.
33 *
34 * ARITHMETIC: comparisons of cumulative sums, exact in Rational.
35 */
36
37#include <cstddef>
38#include <vector>
39
41#include "line/num/number.h"
42#include "line/util/error.h"
43#include "line/util/matrix.h"
44
45namespace line {
46namespace trace {
47
48/** Return value of mtrace_count. */
49template <class T>
51 std::vector<int> labels; ///< the distinct labels, increasing
52 Matrix<long> counts; ///< (periods x C) events per window and class
53};
54
55/**
56 * @brief Per-class count process of a marked trace on a fixed resolution: the
57 * number of events of each class in the successive windows of length
58 * `t` that start at the first arrival epoch.
59 *
60 * @param Tv inter-arrival times
61 * @param A class labels
62 * @param t window length (the resolution)
63 */
64template <class T>
65MtraceCountResult<T> mtrace_count(const std::vector<T>& Tv, const std::vector<int>& A,
66 const T& t) {
67 detail::require_marked(Tv, A, "mtrace_count");
68 if (t <= num_traits<T>::from_int(0))
69 throw InputError("mtrace_count: the resolution must be positive");
70 const std::size_t n = Tv.size();
71
72 T span = num_traits<T>::from_int(0);
73 for (std::size_t i = 1; i < n; ++i) span += Tv[i];
74 long periods = 0;
75 while (num_traits<T>::from_int(periods) * t < span) ++periods; // ceil(span/t)
76
78 out.labels = detail::unique_labels(A);
79 const std::size_t C = out.labels.size();
80 out.counts = Matrix<long>(static_cast<std::size_t>(periods), C, 0);
81 const std::vector<T> cs = detail::cumsum0(Tv);
82
83 for (long i = 1; i <= periods; ++i) {
84 const T tstart = Tv[0] + num_traits<T>::from_int(i - 1) * t;
85 const T tend = Tv[0] + num_traits<T>::from_int(i) * t;
86 for (std::size_t k = 0; k < n; ++k) {
87 const T& epoch = cs[k + 1];
88 if (!(epoch > tstart) || !(epoch <= tend)) continue;
89 for (std::size_t c = 0; c < C; ++c)
90 if (A[k] == out.labels[c]) out.counts(static_cast<std::size_t>(i - 1), c) += 1;
91 }
92 }
93 return out;
94}
95
96} // namespace trace
97} // namespace line
98
99#endif // LINE_API_TRACE_MTRACE_COUNT_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Dense matrix and non-owning view.
MtraceCountResult< T > mtrace_count(const std::vector< T > &Tv, const std::vector< int > &A, const T &t)
Per-class count process of a marked trace on a fixed resolution: the number of events of each class i...
Number-type abstraction for the templated API port.
Return value of mtrace_count.
std::vector< int > labels
the distinct labels, increasing
Matrix< long > counts
(periods x C) events per window and class
Shared declarations for the empirical trace statistics domain.