LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
trace_acf.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_TRACE_ACF_H
6
#define LINE_API_TRACE_TRACE_ACF_H
7
8
/**
9
* @file
10
* @ingroup api_trace
11
* Autocorrelation coefficients of a trace at the requested lags.
12
*
13
* rho(k) = acv(k) / acv(0)
14
*
15
* Templated port of matlab/lib/kpctoolbox/trace/trace_acf.m, cross-checked
16
* against `jar/src/main/java/jline/api/trace/Trace_var.java#trace_acf`.
17
*
18
* DIVERGENCE INSIDE MATLAB: trace_acf.m has two branches. When the Signal
19
* Processing Toolbox is present it uses xcorr(...,'coeff'), whose estimator
20
* divides every lag by the same n (the biased estimator); otherwise it falls
21
* back to autocov, which divides lag p by n-p. The two therefore differ by
22
* the factor n/(n-p) at lag p -- the same trace gives different acf values
23
* depending on which toolboxes are installed. Measured on the trace
24
* 1,2,3,4,5: MATLAB with the toolbox returns 0.4 and -0.1 at lags 1 and 2,
25
* while the JAR and the MATLAB fallback return 1/2 and -1/6. The JAR ports
26
* the fallback branch, and so does this header -- it is the branch the two
27
* codebases share, and the one whose lag-0 value is the sample variance.
28
*
29
* OUT-OF-RANGE LAGS: MATLAB clamps them to n-2 and then deletes the clamped
30
* entries, the JAR filters out every lag outside (0, n-2]. Both silently
31
* return fewer values than lags requested; this port follows the JAR filter
32
* and documents it rather than throwing, so that trace_summary and
33
* trace_gamma keep working on short traces.
34
*
35
* ARITHMETIC: a ratio of autocovariances, exact in Rational.
36
*/
37
38
#include <cstddef>
39
#include <vector>
40
41
#include "
line/api/trace/autocov.h
"
42
#include "
line/api/trace/trace_types.h
"
43
#include "
line/num/number.h
"
44
#include "
line/util/error.h
"
45
46
namespace
line
{
47
namespace
trace
{
48
49
/**
50
* @brief Autocorrelation coefficients of a trace at the requested lags.
51
*
52
* @param S the trace
53
* @param lags the lags to evaluate; entries outside (0, n-2] are dropped
54
* @return one coefficient per surviving lag, in the order given
55
*/
56
template
<
class
T>
57
std::vector<T>
trace_acf
(
const
std::vector<T>& S,
const
std::vector<int>& lags) {
58
detail::require_nonempty(S,
"trace_acf"
);
59
const
long
n =
static_cast<
long
>
(S.size());
60
std::vector<int> kept;
61
for
(
int
l : lags)
62
if
(l > 0 && l <= n - 2) kept.push_back(l);
63
if
(kept.empty())
return
std::vector<T>();
64
65
const
std::vector<T> acv =
autocov
(S);
66
if
(acv[0] ==
num_traits<T>::from_int
(0))
67
throw
NumericError
(
"trace_acf: the trace is constant, the acf is undefined"
);
68
std::vector<T> rho;
69
rho.reserve(kept.size());
70
for
(
int
l : kept) rho.push_back(acv[
static_cast<
std::size_t
>
(l)] / acv[0]);
71
return
rho;
72
}
73
74
/** Lag-1 autocorrelation, the MATLAB and JAR default. */
75
template
<
class
T>
76
std::vector<T>
trace_acf
(
const
std::vector<T>& S) {
77
return
trace_acf
(S, std::vector<int>(1, 1));
78
}
79
80
}
// namespace trace
81
}
// namespace line
82
83
#endif
// LINE_API_TRACE_TRACE_ACF_H
autocov.h
Sample autocovariance sequence of a trace, lags 0 .
line::NumericError::NumericError
NumericError(const std::string &what)
Definition
error.h:45
error.h
The exception types the port throws.
line::trace
Definition
autocov.h:38
line::trace::autocov
std::vector< T > autocov(const std::vector< T > &S)
Sample autocovariance sequence of a trace, lags 0 .
Definition
autocov.h:46
line::trace::trace_acf
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
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
trace_acf.h
Generated by
1.18.0