LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
trace_var.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_VAR_H
6
#define LINE_API_TRACE_TRACE_VAR_H
7
8
/**
9
* @file
10
* @ingroup api_trace
11
* Sample variance of a trace.
12
*
13
* Templated port of matlab/lib/kpctoolbox/trace/trace_var.m (which is
14
* MATLAB's var, denominator n-1), cross-checked against
15
* jar/src/main/java/jline/api/trace/Trace_var.java.
16
*
17
* DIVERGENCE, MATLAB vs JAR: the JAR computes E[X^2] - E[X]^2, i.e. the
18
* POPULATION variance with denominator n, while MATLAB's var uses the
19
* unbiased denominator n-1. The two differ by the factor n/(n-1), which is
20
* not negligible on the short traces used in tests and propagates into
21
* trace_scv, trace_idi and trace_idc. MATLAB is the reference, so `unbiased`
22
* defaults to true; pass false to reproduce the JAR.
23
*
24
* ARITHMETIC: sums and one division, exact in Rational.
25
*/
26
27
#include <vector>
28
29
#include "
line/api/trace/trace_mean.h
"
30
#include "
line/api/trace/trace_types.h
"
31
#include "
line/num/number.h
"
32
#include "
line/util/error.h
"
33
34
namespace
line
{
35
namespace
trace
{
36
37
/**
38
* @brief Sample variance of a trace.
39
*
40
* @param S the trace
41
* @param unbiased true for the n-1 denominator (MATLAB var), false for the
42
* n denominator (the JAR, and the lag-0 autocovariance)
43
*/
44
template
<
class
T>
45
T
trace_var
(
const
std::vector<T>& S,
bool
unbiased =
true
) {
46
detail::require_nonempty(S,
"trace_var"
);
47
const
long
n =
static_cast<
long
>
(S.size());
48
if
(unbiased && n < 2)
throw
InputError
(
"trace_var: the unbiased variance needs n >= 2"
);
49
const
T mu =
trace_mean
(S);
50
T s2 =
num_traits<T>::from_int
(0);
51
for
(
const
T& v : S) {
52
const
T d = v - mu;
53
s2 += d * d;
54
}
55
return
s2 /
num_traits<T>::from_int
(unbiased ? n - 1 : n);
56
}
57
58
}
// namespace trace
59
}
// namespace line
60
61
#endif
// LINE_API_TRACE_TRACE_VAR_H
line::InputError::InputError
InputError(const std::string &what)
Definition
error.h:39
error.h
The exception types the port throws.
line::trace
Definition
autocov.h:38
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::trace_var
T trace_var(const std::vector< T > &S, bool unbiased=true)
Sample variance of a trace.
Definition
trace_var.h:45
line
Definition
aoi_dist2ph.h:52
number.h
Number-type abstraction for the templated API port.
line::num_traits
Definition
number.h:111
trace_mean.h
Sample mean of a trace.
trace_types.h
Shared declarations for the empirical trace statistics domain.
include
line
api
trace
trace_var.h
Generated by
1.18.0