LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
autocov.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_AUTOCOV_H
6
#define LINE_API_TRACE_AUTOCOV_H
7
8
/**
9
* @file
10
* @ingroup api_trace
11
* Sample autocovariance sequence of a trace, lags 0 .. n-2.
12
*
13
* Templated port of matlab/lib/kpctoolbox/contrib/autocov.m, cross-checked
14
* against the private autocov() of
15
* jar/src/main/java/jline/api/trace/Trace_var.java (identical formula; the
16
* MATLAB implementation evaluates it by FFT, which is the same quantity up to
17
* rounding but is not usable in exact arithmetic, so this port evaluates the
18
* defining sum directly).
19
*
20
* acv(p) = 1/(n-1-p) sum_{i=1}^{n-p} (S_i - Sbar)(S_{i+p} - Sbar)
21
*
22
* The denominator is the one autocov.m actually applies,
23
* `real(acv(1:M-1)) ./ (M-(1:M-1))'`: its k-th entry is lag k-1, divided by
24
* M-k. The lag-0 term is therefore MATLAB's var (denominator n-1), as the
25
* header of autocov.m states, and the last lag n-2 is divided by 1. (The
26
* formula in that header reads 1/(N-p) with p 1-based, which is the same
27
* thing.) This port and the JAR divided lag p by n-p until 2026-11-06
28
* (BUG-113), which made the lag-0 term the population variance and every
29
* trace_acf, trace_gamma and trace_summary value differ from MATLAB's.
30
*
31
* ARITHMETIC: sums, products and divisions, exact in Rational.
32
*/
33
34
#include <cstddef>
35
#include <vector>
36
37
#include "
line/api/trace/trace_mean.h
"
38
#include "
line/api/trace/trace_types.h
"
39
#include "
line/num/number.h
"
40
#include "
line/util/error.h
"
41
42
namespace
line
{
43
namespace
trace
{
44
45
/**
46
* @brief Sample autocovariance sequence of a trace, lags 0 .. n-2.
47
*
48
* @return acv[0..n-2], acv[p] the lag-p sample autocovariance.
49
*/
50
template
<
class
T>
51
std::vector<T>
autocov
(
const
std::vector<T>& S) {
52
detail::require_nonempty(S,
"autocov"
);
53
const
std::size_t n = S.size();
54
if
(n < 2)
throw
InputError
(
"autocov: the trace must have at least two samples"
);
55
const
T mu =
trace_mean
(S);
56
std::vector<T> X(n);
57
for
(std::size_t i = 0; i < n; ++i) X[i] = S[i] - mu;
58
59
std::vector<T> acv(n - 1,
num_traits<T>::from_int
(0));
60
for
(std::size_t p = 0; p + 1 < n; ++p) {
61
T s =
num_traits<T>::from_int
(0);
62
for
(std::size_t i = 0; i + p < n; ++i) s += X[i] * X[i + p];
63
acv[p] = s /
num_traits<T>::from_int
(
static_cast<
long
>
(n - 1 - p));
64
}
65
return
acv;
66
}
67
68
}
// namespace trace
69
}
// namespace line
70
71
#endif
// LINE_API_TRACE_AUTOCOV_H
line::InputError::InputError
InputError(const std::string &what)
Definition
error.h:105
error.h
The exception types the port throws.
line::trace
Definition
autocov.h:43
line::trace::trace_mean
T trace_mean(const std::vector< T > &S)
(1/n) sum_i S(i).
Definition
trace_mean.h:31
line::trace::autocov
std::vector< T > autocov(const std::vector< T > &S)
Sample autocovariance sequence of a trace, lags 0 .
Definition
autocov.h:51
line
Conservation laws of a layered queueing network, enumerated from its structure.
Definition
aoi_dist2ph.h:51
number.h
Number-type abstraction for the templated API port.
line::num_traits
Definition
number.h:123
trace_mean.h
Sample mean of a trace.
trace_types.h
Shared declarations for the empirical trace statistics domain.
include
line
api
trace
autocov.h
Generated by
1.18.0