LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
mtrace_cov.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_COV_H
6
#define LINE_API_TRACE_MTRACE_COV_H
7
8
/**
9
* @file
10
* @ingroup api_trace
11
* Class-pair covariance matrices of a marked trace.
12
*
13
* For every ordered pair (c1,c2) the two masked series
14
*
15
* X0(i) = T_i if A_i = c1 else 0,
16
* X1(i) = T_{i+1} if A_{i+1} = c2 else 0, i = 1..N-1,
17
*
18
* are formed and their 2x2 sample covariance matrix (denominator N-2, i.e.
19
* MATLAB's cov on N-1 observations) is returned.
20
*
21
* Templated port of matlab/lib/m3a/m3a/mtrace/mtrace_cov.m, cross-checked
22
* against jar/src/main/java/jline/api/trace/Mtrace_cov.java (identical,
23
* including the unbiased denominator and the indexing of classes by the raw
24
* label 1..max(A) rather than by position in unique(A)).
25
*
26
* ARITHMETIC: sample second moments, exact in Rational.
27
*/
28
29
#include <cstddef>
30
#include <vector>
31
32
#include "
line/api/trace/trace_types.h
"
33
#include "
line/num/number.h
"
34
#include "
line/util/error.h
"
35
#include "
line/util/matrix.h
"
36
37
namespace
line
{
38
namespace
trace
{
39
40
/**
41
* @brief Class-pair covariance matrices of a marked trace.
42
*
43
* @param Tv inter-arrival times
44
* @param A class labels, positive
45
* @return cov[c1-1][c2-1], each a 2x2 matrix
46
*/
47
template
<
class
T>
48
std::vector<std::vector<Matrix<T>>>
mtrace_cov
(
const
std::vector<T>& Tv,
49
const
std::vector<int>& A) {
50
detail::require_marked(Tv, A,
"mtrace_cov"
);
51
const
std::size_t N = A.size();
52
if
(N < 3)
throw
InputError
(
"mtrace_cov: at least three events are required"
);
53
int
C = 0;
54
for
(std::size_t k = 0; k < N; ++k)
55
if
(A[k] > C) C = A[k];
56
if
(C <= 0)
throw
InputError
(
"mtrace_cov: class labels must be positive"
);
57
58
const
T zero =
num_traits<T>::from_int
(0);
59
const
T den =
num_traits<T>::from_int
(
static_cast<
long
>
(N - 2));
60
std::vector<std::vector<Matrix<T>>> COV(
61
static_cast<
std::size_t
>
(C),
62
std::vector<
Matrix<T>
>(
static_cast<
std::size_t
>
(C),
Matrix<T>
(2, 2, zero)));
63
64
std::vector<T> x(N - 1), y(N - 1);
65
for
(
int
c1 = 1; c1 <= C; ++c1) {
66
for
(
int
c2 = 1; c2 <= C; ++c2) {
67
for
(std::size_t i = 0; i + 1 < N; ++i) {
68
x[i] = (A[i] == c1) ? Tv[i] : zero;
69
y[i] = (A[i + 1] == c2) ? Tv[i + 1] : zero;
70
}
71
T mx = zero, my = zero;
72
for
(std::size_t i = 0; i + 1 < N; ++i) {
73
mx += x[i];
74
my += y[i];
75
}
76
const
T n1 =
num_traits<T>::from_int
(
static_cast<
long
>
(N - 1));
77
mx /= n1;
78
my /= n1;
79
T sxx = zero, sxy = zero, syy = zero;
80
for
(std::size_t i = 0; i + 1 < N; ++i) {
81
const
T dx = x[i] - mx, dy = y[i] - my;
82
sxx += dx * dx;
83
sxy += dx * dy;
84
syy += dy * dy;
85
}
86
Matrix<T>
c(2, 2, zero);
87
c(0, 0) = sxx / den;
88
c(0, 1) = sxy / den;
89
c(1, 0) = c(0, 1);
90
c(1, 1) = syy / den;
91
COV[
static_cast<
std::size_t
>
(c1 - 1)][
static_cast<
std::size_t
>
(c2 - 1)] = c;
92
}
93
}
94
return
COV;
95
}
96
97
}
// namespace trace
98
}
// namespace line
99
100
#endif
// LINE_API_TRACE_MTRACE_COV_H
line::InputError::InputError
InputError(const std::string &what)
Definition
error.h:39
line::Matrix
Definition
matrix.h:56
error.h
The exception types the port throws.
matrix.h
Dense matrix and non-owning view.
line::trace
Definition
autocov.h:38
line::trace::mtrace_cov
std::vector< std::vector< Matrix< T > > > mtrace_cov(const std::vector< T > &Tv, const std::vector< int > &A)
Class-pair covariance matrices of a marked trace.
Definition
mtrace_cov.h:48
line
Definition
aoi_dist2ph.h:52
number.h
Number-type abstraction for the templated API port.
line::num_traits
Definition
number.h:111
trace_types.h
Shared declarations for the empirical trace statistics domain.
include
line
api
trace
mtrace_cov.h
Generated by
1.18.0