algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
numeric.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "
../math/matrix.hpp
"
4
5
#include <cmath>
6
#include <cstddef>
7
#include <limits>
8
9
namespace
algotradelib
{
10
namespace
analytics
{
11
16
// Kept apart from conversion.hpp so the metrics do not include the trading types.
17
19
using
math::Scalar
;
20
25
using
math::missing
;
26
32
using
math::observed
;
33
34
namespace
constant_detail {
35
36
// Whether a deviation computed about `mean` over `count` observations is small enough to be
37
// rounding alone. Equal observations that are not exact binary fractions have a computed mean an
38
// ulp or so away from them (the mean of {0.1, 0.1, 0.1} is not 0.1), so their deviation comes out a
39
// few ulps of the mean rather than zero. The bound is a generous relative one, `8 * count * epsilon`
40
// of the mean's magnitude, against a summation error of about `count * epsilon / 2`.
41
inline
bool
within_rounding(
Scalar
deviation,
Scalar
mean, std::size_t count) {
42
return
deviation <= std::abs(mean) * static_cast<Scalar>(count) *
Scalar
{8} * std::numeric_limits<Scalar>::epsilon();
43
}
44
45
// Whether every observed row of `values` in `[first, first + length)` holds the same value;
46
// `present` is the presence mask `math::Observed` gives (`1` observed, `0` not).
47
inline
bool
all_equal(
const
math::Vector
& values,
const
math::Vector
& present,
math::Index
first,
math::Index
length) {
48
bool
seen =
false
;
49
Scalar
reference{0};
50
for
(
math::Index
row = first; row < first + length; ++row) {
51
if
(present[row] ==
Scalar
{0}) {
52
continue
;
53
}
54
if
(!seen) {
55
reference = values[row];
56
seen =
true
;
57
}
else
if
(values[row] != reference) {
58
return
false
;
59
}
60
}
61
return
true
;
62
}
63
64
// Whether a window whose deviation about `mean` came out rounding-small holds equal observations,
65
// checked value by value. Each caller keeps its own exact-zero test beside it: this one never
66
// reads a zero as equality by itself, since a positive dispersion can underflow to a zero deviation.
67
inline
bool
rounded_constant(
68
Scalar
deviation,
Scalar
mean, std::size_t count,
const
math::Vector
& values,
const
math::Vector
& present,
69
math::Index
first,
math::Index
length
70
) {
71
return
within_rounding(deviation, mean, count) && all_equal(values, present, first, length);
72
}
73
74
}
// namespace constant_detail
75
76
}
// namespace analytics
77
}
// namespace algotradelib
matrix.hpp
The linear algebra types of the math module: Matrix, Vector, SeriesView, MutableSeriesView,...
algotradelib::analytics
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
Definition
conversion.hpp:14
algotradelib::math::Scalar
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition
scalar.hpp:18
algotradelib::math::observed
bool observed(Scalar value)
Tests whether a value is an observation.
Definition
scalar.hpp:38
algotradelib::math::Index
Eigen::Index Index
The signed index type of rows and columns.
Definition
eigen.hpp:47
algotradelib::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition
eigen.hpp:30
algotradelib::math::missing
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition
scalar.hpp:28
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib
src
analytics
numeric.hpp
Generated by
1.18.0