LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mtrace_sigma.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_SIGMA_H
6#define LINE_API_TRACE_MTRACE_SIGMA_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * One-step class transition frequencies of a marked trace,
12 *
13 * sigma(i,j) = #{t : A_t = i, A_{t+1} = j} / (N-1).
14 *
15 * Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_sigma.m, cross-checked
16 * against jar/src/main/java/jline/api/trace/Mtrace_sigma.java (identical).
17 *
18 * Note that this is the JOINT frequency of the pair, not the conditional
19 * transition probability: the whole matrix sums to 1, its rows do not.
20 *
21 * ARITHMETIC: counts over N-1, exact in Rational.
22 */
23
24#include <cstddef>
25#include <vector>
26
28#include "line/num/number.h"
29#include "line/util/error.h"
30#include "line/util/matrix.h"
31
32namespace line {
33namespace trace {
34
35/**
36 * @brief One-step class transition frequencies of a marked trace, sigma(i,j)
37 * = #{t : A_t = i, A_{t+1} = j} / (N-1).
38 *
39 * @param L class labels; @return (C x C) matrix over the labels of unique(L).
40 */
41template <class T>
42Matrix<T> mtrace_sigma(const std::vector<int>& L) {
43 if (L.size() < 2) throw InputError("mtrace_sigma: at least two events are required");
44 const std::vector<int> marks = detail::unique_labels(L);
45 const std::size_t C = marks.size();
46 Matrix<T> sigma(C, C, num_traits<T>::from_int(0));
47 const T den = num_traits<T>::from_int(static_cast<long>(L.size() - 1));
48 for (std::size_t i = 0; i < C; ++i) {
49 for (std::size_t j = 0; j < C; ++j) {
50 long count = 0;
51 for (std::size_t t = 0; t + 1 < L.size(); ++t)
52 if (L[t] == marks[i] && L[t + 1] == marks[j]) ++count;
53 sigma(i, j) = num_traits<T>::from_int(count) / den;
54 }
55 }
56 return sigma;
57}
58
59} // namespace trace
60} // namespace line
61
62#endif // LINE_API_TRACE_MTRACE_SIGMA_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_sigma(const std::vector< int > &L)
One-step class transition frequencies of a marked trace, sigma(i,j) = #{t : A_t = i,...
Number-type abstraction for the templated API port.
Shared declarations for the empirical trace statistics domain.