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
19// The only header that names Eigen in a public signature; a second backend is a sibling header.
20
27using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
28
30using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
31
38using SeriesView = Eigen::Ref<const Vector>;
39
41using MutableSeriesView = Eigen::Ref<Vector>;
42
44using MatrixView = Eigen::Ref<const Matrix>;
45
47using Index = Eigen::Index;
48
55inline Vector unobserved(std::size_t count) {
56 return Vector::Constant(static_cast<Index>(count), missing());
57}
58
66inline Matrix unobserved(std::size_t rows, std::size_t columns) {
67 return Matrix::Constant(static_cast<Index>(rows), static_cast<Index>(columns), missing());
68}
69
88
96 // `x == x` is false exactly for NaN, and vectorizes to a single compare.
97 const auto present = (values.array() == values.array());
98 Observed parts;
99 parts.values_ = present.select(values, Scalar{0});
100 parts.present_ = present.template cast<Scalar>();
101 return parts;
102}
103
104// The decompositions below have no caller in the library. They are commented out because
105// `thin_svd`, a non-template inline function, made every file that includes this header
106// instantiate Eigen's SVD (about two seconds of compile time each).
107// /**
108// * @brief Selects the lower triangle, as in `matrix.selfadjointView<math::LOWER>()`.
109// *
110// * A self-adjoint view tells a decomposition to read one triangle and assume the other.
111// */
112// inline constexpr unsigned int LOWER = Eigen::Lower;
113// /** @brief Selects the upper triangle. */
114// inline constexpr unsigned int UPPER = Eigen::Upper;
115// /** @brief Selects the lower triangle with an implicit unit diagonal. */
116// inline constexpr unsigned int UNIT_LOWER = Eigen::UnitLower;
117// /** @brief Selects the upper triangle with an implicit unit diagonal. */
118// inline constexpr unsigned int UNIT_UPPER = Eigen::UnitUpper;
119//
120// /** @brief Cholesky decomposition of a symmetric positive-definite matrix. Check `info()`. */
121// using Llt = Eigen::LLT<Matrix>;
122//
123// /** @brief Pivoting Cholesky (LDLT) decomposition of a symmetric, possibly semi-definite matrix. */
124// using Ldlt = Eigen::LDLT<Matrix>;
125//
126// /** @brief Householder QR decomposition: the fastest QR, with no rank information. */
127// using HouseholderQr = Eigen::HouseholderQR<Matrix>;
128//
129// /**
130// * @brief Column-pivoting Householder QR: a least-squares solution and a rank.
131// *
132// * A good default for a regression on market data, where nearly collinear columns are common;
133// * check `rank()` against the number of columns.
134// */
135// using ColPivQr = Eigen::ColPivHouseholderQR<Matrix>;
136//
137// /** @brief Full-pivoting Householder QR: slower than `ColPivQr`, with the most reliable rank. */
138// using FullPivQr = Eigen::FullPivHouseholderQR<Matrix>;
139//
140// /** @brief LU decomposition with full pivoting, for a square system with no known structure. */
141// using FullPivLu = Eigen::FullPivLU<Matrix>;
142//
143// /**
144// * @brief Divide-and-conquer SVD: minimum-norm least-squares solutions and singular values.
145// *
146// * Construct it with `thin_svd()`, which requests the thin factors `solve()` needs on every
147// * supported Eigen release.
148// */
149// #if EIGEN_VERSION_AT_LEAST(3, 4, 90)
150// using Svd = Eigen::BDCSVD<Matrix, Eigen::ComputeThinU | Eigen::ComputeThinV>;
151// #else
152// using Svd = Eigen::BDCSVD<Matrix>;
153// #endif
154//
155// /**
156// * @brief Computes a thin SVD, ready to `solve()`.
157// *
158// * @param a The matrix to decompose.
159// * @return The decomposition of `a`.
160// */
161// inline Svd thin_svd(const Matrix& a) {
162// #if EIGEN_VERSION_AT_LEAST(3, 4, 90)
163// return Svd(a);
164// #else
165// return Svd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
166// #endif
167// }
168//
169// /** @brief Eigenvalues (ascending) and eigenvectors of a symmetric matrix. Check `info()`. */
170// using SelfAdjointEigen = Eigen::SelfAdjointEigenSolver<Matrix>;
171//
172// /** @brief The value a decomposition's `info()` returns on success. */
173// inline constexpr auto SUCCESS = Eigen::Success;
174
175} // namespace math
176} // 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:95
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
Eigen::Ref< Vector > MutableSeriesView
A writable reference to one series, such as a Vector or a column of a Matrix.
Definition eigen.hpp:41
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
Definition eigen.hpp:27
Eigen::Index Index
The signed index type of rows and columns.
Definition eigen.hpp:47
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition eigen.hpp:55
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Definition eigen.hpp:44
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition eigen.hpp:30
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition scalar.hpp:28
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition eigen.hpp:38
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:82
Vector values_
The input, with every unobserved entry replaced by 0.
Definition eigen.hpp:84
Vector present_
1 where the input is observed and 0 where it is not.
Definition eigen.hpp:86