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 indicator headers 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 <=
43
std::abs(mean) *
static_cast<
Scalar
>
(count) *
Scalar
{8} * std::numeric_limits<Scalar>::epsilon();
44
}
45
46
// Whether every observed row of `values` in `[first, first + length)` holds the same value;
47
// `present` is the presence mask `math::Observed` gives (`1` observed, `0` not).
48
inline
bool
all_equal(
49
const
math::Vector
& values,
50
const
math::Vector
& present,
51
math::Index
first,
52
math::Index
length
53
) {
54
bool
seen =
false
;
55
Scalar
reference{0};
56
for
(
math::Index
row = first; row < first + length; ++row) {
57
if
(present[row] ==
Scalar
{0}) {
58
continue
;
59
}
60
if
(!seen) {
61
reference = values[row];
62
seen =
true
;
63
}
else
if
(values[row] != reference) {
64
return
false
;
65
}
66
}
67
return
true
;
68
}
69
70
// Whether a window whose deviation about `mean` came out rounding-small holds equal observations,
71
// checked value by value. Each caller keeps its own exact-zero test beside it: this one never
72
// reads a zero as equality by itself, since a positive dispersion can underflow to a zero deviation.
73
inline
bool
rounded_constant(
74
Scalar
deviation,
75
Scalar
mean,
76
std::size_t count,
77
const
math::Vector
& values,
78
const
math::Vector
& present,
79
math::Index
first,
80
math::Index
length
81
) {
82
return
within_rounding(deviation, mean, count) && all_equal(values, present, first, length);
83
}
84
85
}
// namespace constant_detail
86
87
}
// namespace analytics
88
}
// namespace algotradelib
matrix.hpp
The linear algebra types of the math module: Matrix, Vector, SeriesView, MutableSeriesView,...
algotradelib::analytics
Returns, rolling statistics, indicators and performance metrics over numeric series.
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:36
algotradelib::math::Index
Eigen::Index Index
The signed index type of rows and columns.
Definition
eigen.hpp:65
algotradelib::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition
eigen.hpp:48
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