LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mtrace_sigma2.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_SIGMA2_H
6#define LINE_API_TRACE_MTRACE_SIGMA2_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Two-step class transition frequencies of a marked trace,
12 *
13 * sigma(i,j,h) = #{t : A_t = i, A_{t+1} = j, A_{t+2} = h} / (N-2).
14 *
15 * Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_sigma2.m, cross-checked
16 * against jar/src/main/java/jline/api/trace/Mtrace_sigma2.java (identical).
17 *
18 * The three-index array is returned flattened into a C x C^2 matrix, row i
19 * and column j*C + h, so that it needs no tensor type; summing it gives 1 and
20 * summing over h reproduces mtrace_sigma up to the different denominator.
21 *
22 * ARITHMETIC: counts over N-2, exact in Rational.
23 */
24
25#include <cstddef>
26#include <vector>
27
29#include "line/num/number.h"
30#include "line/util/error.h"
31#include "line/util/matrix.h"
32
33namespace line {
34namespace trace {
35
36/**
37 * @brief Two-step class transition frequencies of a marked trace,
38 * sigma(i,j,h) = #{t : A_t = i, A_{t+1} = j, A_{t+2} = h} / (N-2).
39 *
40 * @param L class labels; @return (C x C*C) matrix, entry (i, j*C+h).
41 */
42template <class T>
43Matrix<T> mtrace_sigma2(const std::vector<int>& L) {
44 if (L.size() < 3) throw InputError("mtrace_sigma2: at least three events are required");
45 const std::vector<int> marks = detail::unique_labels(L);
46 const std::size_t C = marks.size();
47 Matrix<T> sigma(C, C * C, num_traits<T>::from_int(0));
48 const T den = num_traits<T>::from_int(static_cast<long>(L.size() - 2));
49 for (std::size_t i = 0; i < C; ++i) {
50 for (std::size_t j = 0; j < C; ++j) {
51 for (std::size_t h = 0; h < C; ++h) {
52 long count = 0;
53 for (std::size_t t = 0; t + 2 < L.size(); ++t)
54 if (L[t] == marks[i] && L[t + 1] == marks[j] && L[t + 2] == marks[h]) ++count;
55 sigma(i, j * C + h) = num_traits<T>::from_int(count) / den;
56 }
57 }
58 }
59 return sigma;
60}
61
62} // namespace trace
63} // namespace line
64
65#endif // LINE_API_TRACE_MTRACE_SIGMA2_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Dense matrix and non-owning view.
Matrix< T > mtrace_sigma2(const std::vector< int > &L)
Two-step class transition frequencies of a marked trace, sigma(i,j,h) = #{t : A_t = i,...
Number-type abstraction for the templated API port.
Shared declarations for the empirical trace statistics domain.