algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
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
9namespace algotradelib {
10namespace analytics {
11
16// Kept apart from conversion.hpp so indicator headers do not include the trading types.
17
19using math::Scalar;
20
25using math::missing;
26
32using math::observed;
33
34namespace 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`.
41inline 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).
48inline 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.
73inline 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
The linear algebra types of the math module: Matrix, Vector, SeriesView, MutableSeriesView,...
Returns, rolling statistics, indicators and performance metrics over numeric series.
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
bool observed(Scalar value)
Tests whether a value is an observation.
Definition scalar.hpp:36
Eigen::Index Index
The signed index type of rows and columns.
Definition eigen.hpp:65
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition eigen.hpp:48
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition scalar.hpp:28
The library: broker-neutral types (core), and the modules below.