LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mtrace_summary.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_SUMMARY_H
6#define LINE_API_TRACE_MTRACE_SUMMARY_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Descriptor set of a marked trace: the first five raw moments of the
12 * inter-arrival times, the autocorrelation function, the first two forward
13 * and backward moments, the first two class-pair cross moments, the class
14 * probabilities and the one-step class transition frequencies.
15 *
16 * This is exactly the input a marked MAP fitting procedure consumes.
17 *
18 * Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_summary.m, cross-checked
19 * against jar/src/main/java/jline/api/trace/Mtrace_summary.java. The two
20 * assemble the same list; their entries differ wherever the underlying
21 * function does (see mtrace_moment.h for the normalization defect that
22 * affects F1, F2 and, in the JAR, B1 and B2 as well, and
23 * mtrace_backward_moment.h for the JAR's different backward statistic).
24 *
25 * The acf lag set is 1..100 in both references; it is a parameter here
26 * because trace_acf drops the lags a short trace cannot support.
27 *
28 * ARITHMETIC: all components are sample moments and frequencies, exact in
29 * Rational.
30 */
31
32#include <cstddef>
33#include <vector>
34
42#include "line/num/number.h"
43#include "line/util/error.h"
44#include "line/util/matrix.h"
45
46namespace line {
47namespace trace {
48
49/** Return value of mtrace_summary. */
50template <class T>
52 std::vector<T> M; ///< raw moments of order 1..5
53 std::vector<T> acf; ///< autocorrelation at the requested lags
54 Matrix<T> F1, F2; ///< forward moments of order 1 and 2
55 Matrix<T> B1, B2; ///< backward moments of order 1 and 2
56 Matrix<T> C1, C2; ///< class-pair cross moments of order 1 and 2
57 std::vector<T> Pc; ///< class probabilities
58 Matrix<T> Pab; ///< one-step class transition frequencies
59};
60
61/**
62 * @brief Descriptor set of a marked trace: the first five raw moments of the
63 * inter-arrival times, the autocorrelation function, the first two
64 * forward and backward moments, the first two class-pair cross
65 * moments, the class probabilities and the one-step class transition
66 * frequencies.
67 *
68 * @param Tv inter-arrival times
69 * @param A class labels
70 * @param max_lag largest acf lag (100 in both references)
71 */
72template <class T>
73MtraceSummary<T> mtrace_summary(const std::vector<T>& Tv, const std::vector<int>& A,
74 int max_lag = 100) {
75 detail::require_marked(Tv, A, "mtrace_summary");
77 for (unsigned k = 1; k <= 5; ++k) {
79 for (std::size_t i = 0; i < Tv.size(); ++i) s += num_pow_int(Tv[i], k);
80 out.M.push_back(s / num_traits<T>::from_int(static_cast<long>(Tv.size())));
81 }
82 std::vector<int> lags;
83 for (int l = 1; l <= max_lag; ++l) lags.push_back(l);
84 out.acf = trace_acf(Tv, lags);
85
86 const std::vector<unsigned> o1(1, 1u), o2(1, 2u);
87 out.F1 = mtrace_forward_moment(Tv, A, o1);
88 out.F2 = mtrace_forward_moment(Tv, A, o2);
89 out.B1 = mtrace_backward_moment(Tv, A, o1);
90 out.B2 = mtrace_backward_moment(Tv, A, o2);
91 out.C1 = mtrace_cross_moment(Tv, A, 1u).mc;
92 out.C2 = mtrace_cross_moment(Tv, A, 2u).mc;
93 out.Pc = mtrace_pc<T>(A);
94 out.Pab = mtrace_sigma<T>(A);
95 return out;
96}
97
98} // namespace trace
99} // namespace line
100
101#endif // LINE_API_TRACE_MTRACE_SUMMARY_H
The exception types the port throws.
Dense matrix and non-owning view.
Backward moments of a marked trace: the moments of the inter-arrival time that PRECEDES an event of e...
Class-pair cross moments of a marked trace: the k-th moment of the interval that separates an event o...
Forward moments of a marked trace: the moments of the inter-arrival time that FOLLOWS an event of eac...
Class probabilities of a marked trace, p_c = count_c / N.
One-step class transition frequencies of a marked trace,.
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...
Matrix< T > mtrace_forward_moment(const std::vector< T > &Tv, const std::vector< int > &A, const std::vector< unsigned > &orders, bool norm=true)
Forward moments of a marked trace: the moments of the inter-arrival time that FOLLOWS an event of eac...
MtraceSummary< T > mtrace_summary(const std::vector< T > &Tv, const std::vector< int > &A, int max_lag=100)
Descriptor set of a marked trace: the first five raw moments of the inter-arrival times,...
Matrix< T > mtrace_backward_moment(const std::vector< T > &Tv, const std::vector< int > &A, const std::vector< unsigned > &orders, bool norm=true)
Backward moments of a marked trace: the moments of the inter-arrival time that PRECEDES an event of e...
std::vector< T > mtrace_pc(const std::vector< int > &A)
Class probabilities of a marked trace, p_c = count_c / N.
Definition mtrace_pc.h:42
Matrix< T > mtrace_sigma(const std::vector< int > &L)
One-step class transition frequencies of a marked trace, sigma(i,j) = #{t : A_t = i,...
std::vector< T > trace_acf(const std::vector< T > &S, const std::vector< int > &lags)
Autocorrelation coefficients of a trace at the requested lags.
Definition trace_acf.h:57
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_summary.
Matrix< T > Pab
one-step class transition frequencies
std::vector< T > Pc
class probabilities
std::vector< T > M
raw moments of order 1..5
Matrix< T > C2
class-pair cross moments of order 1 and 2
Matrix< T > B2
backward moments of order 1 and 2
std::vector< T > acf
autocorrelation at the requested lags
Matrix< T > F2
forward moments of order 1 and 2
Autocorrelation coefficients of a trace at the requested lags.
Shared declarations for the empirical trace statistics domain.