LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mtrace_cross_moment.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_CROSS_MOMENT_H
6#define LINE_API_TRACE_MTRACE_CROSS_MOMENT_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Class-pair cross moments of a marked trace: the k-th moment of the interval
12 * that separates an event of class i from the next event, of class j,
13 *
14 * MC(i,j) = mean{ T_t^k : A_{t-1} = i, A_t = j }.
15 *
16 * Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_cross_moment.m,
17 * cross-checked against
18 * jar/src/main/java/jline/api/trace/Mtrace_cross_moment.java. The two agree
19 * on every observed pair; they differ only in what they report for a pair
20 * that never occurs, where MATLAB returns 0/0 = NaN and the JAR sets NaN
21 * explicitly. Rational arithmetic has no NaN, so this port reports the
22 * observed counts alongside the moments and leaves the unobserved entries at
23 * zero: count(i,j) == 0 marks them.
24 *
25 * ARITHMETIC: sums of integer powers and a division, exact in Rational.
26 */
27
28#include <cstddef>
29#include <vector>
30
32#include "line/num/number.h"
33#include "line/util/error.h"
34#include "line/util/matrix.h"
35
36namespace line {
37namespace trace {
38
39/** Return value of mtrace_cross_moment. */
40template <class T>
42 Matrix<T> mc; ///< (C x C) moments; entry undefined where count = 0
43 Matrix<long> count; ///< (C x C) number of observed transitions
44};
45
46/**
47 * @brief Class-pair cross moments of a marked trace: the k-th moment of the
48 * interval that separates an event of class i from the next event, of
49 * class j, MC(i,j) = mean{ T_t^k : A_{t-1} = i, A_t = j }.
50 *
51 * @param Tv inter-arrival times
52 * @param L class labels
53 * @param k moment order
54 */
55template <class T>
57 const std::vector<int>& L, unsigned k) {
58 detail::require_marked(Tv, L, "mtrace_cross_moment");
59 const std::vector<int> marks = detail::unique_labels(L);
60 const std::size_t C = marks.size();
62 out.mc = Matrix<T>(C, C, num_traits<T>::from_int(0));
63 out.count = Matrix<long>(C, C, 0);
64
65 std::vector<std::size_t> idx(L.size());
66 for (std::size_t t = 0; t < L.size(); ++t) {
67 std::size_t c = 0;
68 while (c < C && marks[c] != L[t]) ++c;
69 idx[t] = c;
70 }
71 for (std::size_t t = 1; t < Tv.size(); ++t) {
72 const std::size_t i = idx[t - 1], j = idx[t];
73 out.mc(i, j) += num_pow_int(Tv[t], k);
74 out.count(i, j) += 1;
75 }
76 for (std::size_t i = 0; i < C; ++i)
77 for (std::size_t j = 0; j < C; ++j)
78 if (out.count(i, j) > 0) out.mc(i, j) /= num_traits<T>::from_int(out.count(i, j));
79 return out;
80}
81
82} // namespace trace
83} // namespace line
84
85#endif // LINE_API_TRACE_MTRACE_CROSS_MOMENT_H
The exception types the port throws.
Dense matrix and non-owning view.
MtraceCrossMomentResult< T > mtrace_cross_moment(const std::vector< T > &Tv, const std::vector< int > &L, unsigned k)
Class-pair cross moments of a marked trace: the k-th moment of the interval that separates an event o...
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:192
Number-type abstraction for the templated API port.
Return value of mtrace_cross_moment.
Matrix< T > mc
(C x C) moments; entry undefined where count = 0
Matrix< long > count
(C x C) number of observed transitions
Shared declarations for the empirical trace statistics domain.