algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
9
namespace
algotradelib
{
10
namespace
math
{
11
19
// The only header that names Eigen in a public signature; a second backend is a sibling header.
20
27
using
Matrix
= Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
28
30
using
Vector
= Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
31
38
using
SeriesView
= Eigen::Ref<const Vector>;
39
41
using
MutableSeriesView
= Eigen::Ref<Vector>;
42
44
using
MatrixView
= Eigen::Ref<const Matrix>;
45
47
using
Index
= Eigen::Index;
48
55
inline
Vector
unobserved
(std::size_t count) {
56
return
Vector::Constant(
static_cast<
Index
>
(count),
missing
());
57
}
58
66
inline
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
82
struct
Observed
{
84
Vector
values_
;
86
Vector
present_
;
87
};
88
95
inline
Observed
observed_parts
(
SeriesView
values) {
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
algotradelib::math
The numeric vocabulary: Scalar, missing(), and the matrix and vector types.
Definition
eigen.hpp:10
algotradelib::math::observed_parts
Observed observed_parts(SeriesView values)
Splits a series into zero-filled values and a presence mask.
Definition
eigen.hpp:95
algotradelib::math::Scalar
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition
scalar.hpp:18
algotradelib::math::MutableSeriesView
Eigen::Ref< Vector > MutableSeriesView
A writable reference to one series, such as a Vector or a column of a Matrix.
Definition
eigen.hpp:41
algotradelib::math::Matrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
Definition
eigen.hpp:27
algotradelib::math::Index
Eigen::Index Index
The signed index type of rows and columns.
Definition
eigen.hpp:47
algotradelib::math::unobserved
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition
eigen.hpp:55
algotradelib::math::MatrixView
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Definition
eigen.hpp:44
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::math::SeriesView
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition
eigen.hpp:38
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::math::Observed
A series split into zero-filled values and a presence mask, for sums that skip gaps.
Definition
eigen.hpp:82
algotradelib::math::Observed::values_
Vector values_
The input, with every unobserved entry replaced by 0.
Definition
eigen.hpp:84
algotradelib::math::Observed::present_
Vector present_
1 where the input is observed and 0 where it is not.
Definition
eigen.hpp:86
algotradelib
src
math
eigen.hpp
Generated by
1.18.0