algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
eigen.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "./scalar.hpp"
4
5#include <Eigen/Dense>
6
7#include <cstddef>
8
9namespace algotradelib {
10namespace math {
11
37// The only header that names Eigen in a public signature; a second backend is a sibling header.
38
45using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
46
48using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
49
56using SeriesView = Eigen::Ref<const Vector>;
57
59using MutableSeriesView = Eigen::Ref<Vector>;
60
62using MatrixView = Eigen::Ref<const Matrix>;
63
65using Index = Eigen::Index;
66
73inline Vector unobserved(std::size_t count) {
74 return Vector::Constant(static_cast<Index>(count), missing());
75}
76
84inline Matrix unobserved(std::size_t rows, std::size_t columns) {
85 return Matrix::Constant(static_cast<Index>(rows), static_cast<Index>(columns), missing());
86}
87
106
114 // `x == x` is false exactly for NaN, and vectorizes to a single compare.
115 const auto present = (values.array() == values.array());
116 Observed parts;
117 parts.values_ = present.select(values, Scalar{0});
118 parts.present_ = present.template cast<Scalar>();
119 return parts;
120}
121
130inline Vector observed_or(SeriesView values, Scalar filler) {
131 return (values.array() == values.array()).select(values, filler);
132}
133
144inline Vector both_present(const Vector& left, const Vector& right) {
145 return left.cwiseProduct(right);
146}
147
153inline constexpr unsigned int LOWER = Eigen::Lower;
155inline constexpr unsigned int UPPER = Eigen::Upper;
157inline constexpr unsigned int UNIT_LOWER = Eigen::UnitLower;
159inline constexpr unsigned int UNIT_UPPER = Eigen::UnitUpper;
160
162using Llt = Eigen::LLT<Matrix>;
163
165using Ldlt = Eigen::LDLT<Matrix>;
166
168using HouseholderQr = Eigen::HouseholderQR<Matrix>;
169
176using ColPivQr = Eigen::ColPivHouseholderQR<Matrix>;
177
179using FullPivQr = Eigen::FullPivHouseholderQR<Matrix>;
180
182using FullPivLu = Eigen::FullPivLU<Matrix>;
183
190#if EIGEN_VERSION_AT_LEAST(3, 4, 90)
191using Svd = Eigen::BDCSVD<Matrix, Eigen::ComputeThinU | Eigen::ComputeThinV>;
192#else
193using Svd = Eigen::BDCSVD<Matrix>;
194#endif
195
202inline Svd thin_svd(const Matrix& a) {
203#if EIGEN_VERSION_AT_LEAST(3, 4, 90)
204 return Svd(a);
205#else
206 return Svd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
207#endif
208}
209
211using SelfAdjointEigen = Eigen::SelfAdjointEigenSolver<Matrix>;
212
214inline constexpr auto SUCCESS = Eigen::Success;
215
216} // namespace math
217} // namespace algotradelib
The numeric vocabulary: Scalar, missing(), and the matrix and vector types.
Definition eigen.hpp:10
Observed observed_parts(SeriesView values)
Splits a series into zero-filled values and a presence mask.
Definition eigen.hpp:113
Eigen::BDCSVD< Matrix > Svd
Divide-and-conquer SVD: minimum-norm least-squares solutions and singular values.
Definition eigen.hpp:193
Eigen::HouseholderQR< Matrix > HouseholderQr
Householder QR decomposition: the fastest QR, with no rank information.
Definition eigen.hpp:168
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
Vector both_present(const Vector &left, const Vector &right)
Combines two presence masks: 1 only where both series are observed.
Definition eigen.hpp:144
Eigen::SelfAdjointEigenSolver< Matrix > SelfAdjointEigen
Eigenvalues (ascending) and eigenvectors of a symmetric matrix. Check info().
Definition eigen.hpp:211
Eigen::Ref< Vector > MutableSeriesView
A writable reference to one series, such as a Vector or a column of a Matrix.
Definition eigen.hpp:59
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
Definition eigen.hpp:45
Eigen::Index Index
The signed index type of rows and columns.
Definition eigen.hpp:65
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition eigen.hpp:73
Svd thin_svd(const Matrix &a)
Computes a thin SVD, ready to solve().
Definition eigen.hpp:202
Vector observed_or(SeriesView values, Scalar filler)
Replaces every unobserved entry of a series.
Definition eigen.hpp:130
constexpr unsigned int LOWER
Selects the lower triangle, as in matrix.selfadjointView<math::LOWER>().
Definition eigen.hpp:153
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Definition eigen.hpp:62
Eigen::LDLT< Matrix > Ldlt
Pivoting Cholesky (LDLT) decomposition of a symmetric, possibly semi-definite matrix.
Definition eigen.hpp:165
constexpr unsigned int UNIT_UPPER
Selects the upper triangle with an implicit unit diagonal.
Definition eigen.hpp:159
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition eigen.hpp:48
constexpr unsigned int UNIT_LOWER
Selects the lower triangle with an implicit unit diagonal.
Definition eigen.hpp:157
Eigen::FullPivLU< Matrix > FullPivLu
LU decomposition with full pivoting, for a square system with no known structure.
Definition eigen.hpp:182
Eigen::LLT< Matrix > Llt
Cholesky decomposition of a symmetric positive-definite matrix. Check info().
Definition eigen.hpp:162
constexpr auto SUCCESS
The value a decomposition's info() returns on success.
Definition eigen.hpp:214
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition scalar.hpp:28
Eigen::ColPivHouseholderQR< Matrix > ColPivQr
Column-pivoting Householder QR: a least-squares solution and a rank.
Definition eigen.hpp:176
constexpr unsigned int UPPER
Selects the upper triangle.
Definition eigen.hpp:155
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition eigen.hpp:56
Eigen::FullPivHouseholderQR< Matrix > FullPivQr
Full-pivoting Householder QR: slower than ColPivQr, with the most reliable rank.
Definition eigen.hpp:179
The library: broker-neutral types (core), and the modules below.
A series split into zero-filled values and a presence mask, for sums that skip gaps.
Definition eigen.hpp:100
Vector values_
The input, with every unobserved entry replaced by 0.
Definition eigen.hpp:102
Vector present_
1 where the input is observed and 0 where it is not.
Definition eigen.hpp:104