algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::math Namespace Reference

The numeric vocabulary: Scalar, missing(), and the matrix and vector types. More...

Classes

struct  Observed
 A series split into zero-filled values and a presence mask, for sums that skip gaps. More...

Typedefs

using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
 A dense, column-major matrix of Scalar.
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>
 A dense column vector of Scalar.
using SeriesView = Eigen::Ref<const Vector>
 A read-only reference to one series.
using MutableSeriesView = Eigen::Ref<Vector>
 A writable reference to one series, such as a Vector or a column of a Matrix.
using MatrixView = Eigen::Ref<const Matrix>
 A read-only reference to a whole matrix.
using Index = Eigen::Index
 The signed index type of rows and columns.
using Llt = Eigen::LLT<Matrix>
 Cholesky decomposition of a symmetric positive-definite matrix. Check info().
using Ldlt = Eigen::LDLT<Matrix>
 Pivoting Cholesky (LDLT) decomposition of a symmetric, possibly semi-definite matrix.
using HouseholderQr = Eigen::HouseholderQR<Matrix>
 Householder QR decomposition: the fastest QR, with no rank information.
using ColPivQr = Eigen::ColPivHouseholderQR<Matrix>
 Column-pivoting Householder QR: a least-squares solution and a rank.
using FullPivQr = Eigen::FullPivHouseholderQR<Matrix>
 Full-pivoting Householder QR: slower than ColPivQr, with the most reliable rank.
using FullPivLu = Eigen::FullPivLU<Matrix>
 LU decomposition with full pivoting, for a square system with no known structure.
using Svd = Eigen::BDCSVD<Matrix>
 Divide-and-conquer SVD: minimum-norm least-squares solutions and singular values.
using SelfAdjointEigen = Eigen::SelfAdjointEigenSolver<Matrix>
 Eigenvalues (ascending) and eigenvectors of a symmetric matrix. Check info().
using Scalar = double
 The floating-point type the analytics and math layers compute in (double).

Functions

Vector unobserved (std::size_t count)
 Makes a vector filled with missing().
Matrix unobserved (std::size_t rows, std::size_t columns)
 Makes a matrix filled with missing().
Observed observed_parts (SeriesView values)
 Splits a series into zero-filled values and a presence mask.
Vector observed_or (SeriesView values, Scalar filler)
 Replaces every unobserved entry of a series.
Vector both_present (const Vector &left, const Vector &right)
 Combines two presence masks: 1 only where both series are observed.
Svd thin_svd (const Matrix &a)
 Computes a thin SVD, ready to solve().
Scalar missing ()
 Returns the value that stands for "no observation" (a quiet NaN).
bool observed (Scalar value)
 Tests whether a value is an observation.
Scalar undefined ()
 Returns the value that stands for "the data was there, but the statistic does not exist".
bool is_undefined (Scalar value)
 Tests whether a value is the undefined() marker.

Variables

constexpr unsigned int LOWER = Eigen::Lower
 Selects the lower triangle, as in matrix.selfadjointView<math::LOWER>().
constexpr unsigned int UPPER = Eigen::Upper
 Selects the upper triangle.
constexpr unsigned int UNIT_LOWER = Eigen::UnitLower
 Selects the lower triangle with an implicit unit diagonal.
constexpr unsigned int UNIT_UPPER = Eigen::UnitUpper
 Selects the upper triangle with an implicit unit diagonal.
constexpr auto SUCCESS = Eigen::Success
 The value a decomposition's info() returns on success.

Detailed Description

The numeric vocabulary: Scalar, missing(), and the matrix and vector types.

Typedef Documentation

◆ ColPivQr

using algotradelib::math::ColPivQr = Eigen::ColPivHouseholderQR<Matrix>

Column-pivoting Householder QR: a least-squares solution and a rank.

A good default for a regression on market data, where nearly collinear columns are common; check rank() against the number of columns.

Definition at line 176 of file eigen.hpp.

◆ FullPivLu

using algotradelib::math::FullPivLu = Eigen::FullPivLU<Matrix>

LU decomposition with full pivoting, for a square system with no known structure.

Definition at line 182 of file eigen.hpp.

◆ FullPivQr

using algotradelib::math::FullPivQr = Eigen::FullPivHouseholderQR<Matrix>

Full-pivoting Householder QR: slower than ColPivQr, with the most reliable rank.

Definition at line 179 of file eigen.hpp.

◆ HouseholderQr

using algotradelib::math::HouseholderQr = Eigen::HouseholderQR<Matrix>

Householder QR decomposition: the fastest QR, with no rank information.

Definition at line 168 of file eigen.hpp.

◆ Index

using algotradelib::math::Index = Eigen::Index

The signed index type of rows and columns.

Definition at line 65 of file eigen.hpp.

◆ Ldlt

using algotradelib::math::Ldlt = Eigen::LDLT<Matrix>

Pivoting Cholesky (LDLT) decomposition of a symmetric, possibly semi-definite matrix.

Definition at line 165 of file eigen.hpp.

◆ Llt

using algotradelib::math::Llt = Eigen::LLT<Matrix>

Cholesky decomposition of a symmetric positive-definite matrix. Check info().

Definition at line 162 of file eigen.hpp.

◆ Matrix

using algotradelib::math::Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>

A dense, column-major matrix of Scalar.

Frames put time on the rows and one series per column, so a column binds to SeriesView without a copy.

Definition at line 45 of file eigen.hpp.

◆ MatrixView

using algotradelib::math::MatrixView = Eigen::Ref<const Matrix>

A read-only reference to a whole matrix.

Definition at line 62 of file eigen.hpp.

◆ MutableSeriesView

A writable reference to one series, such as a Vector or a column of a Matrix.

Definition at line 59 of file eigen.hpp.

◆ Scalar

The floating-point type the analytics and math layers compute in (double).

Prices, quantities and money in orders and broker data stay Decimal64; convert between the two with the functions in analytics/conversion.hpp.

Definition at line 18 of file scalar.hpp.

◆ SelfAdjointEigen

using algotradelib::math::SelfAdjointEigen = Eigen::SelfAdjointEigenSolver<Matrix>

Eigenvalues (ascending) and eigenvectors of a symmetric matrix. Check info().

Definition at line 211 of file eigen.hpp.

◆ SeriesView

using algotradelib::math::SeriesView = Eigen::Ref<const Vector>

A read-only reference to one series.

Binds without copying to a Vector or a column of a Matrix, and to an expression (which is evaluated into a temporary). Functions over one series take this type.

Definition at line 56 of file eigen.hpp.

◆ Svd

using algotradelib::math::Svd = Eigen::BDCSVD<Matrix>

Divide-and-conquer SVD: minimum-norm least-squares solutions and singular values.

Construct it with thin_svd(), which requests the thin factors solve() needs on every supported Eigen release.

Definition at line 193 of file eigen.hpp.

◆ Vector

using algotradelib::math::Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>

A dense column vector of Scalar.

Definition at line 48 of file eigen.hpp.

Function Documentation

◆ both_present()

Vector algotradelib::math::both_present ( const Vector & left,
const Vector & right )
inline

Combines two presence masks: 1 only where both series are observed.

Use it to restrict a co-moment (covariance, correlation) and both series' deviations to the same rows; otherwise a correlation can fall outside [-1, 1].

Parameters
leftThe first series' Observed::present_.
rightThe second series' Observed::present_, of the same length.
Returns
The elementwise product of the two masks.

Definition at line 144 of file eigen.hpp.

Referenced by both_present().

◆ is_undefined()

bool algotradelib::math::is_undefined ( Scalar value)
inline

Tests whether a value is the undefined() marker.

Parameters
valueThe value to test.
Returns
true only for undefined() (including a negated copy); false for missing(), any other NaN and every number.
See also
undefined() for when the marker survives.

Definition at line 69 of file scalar.hpp.

Referenced by is_undefined().

◆ missing()

Scalar algotradelib::math::missing ( )
inline

Returns the value that stands for "no observation" (a quiet NaN).

Used for an indicator's warm-up rows, a gap in a panel, or a field a broker did not report. Every reduction in the analytics layer skips it.

Returns
A quiet NaN.

Definition at line 28 of file scalar.hpp.

Referenced by missing(), unobserved(), and unobserved().

◆ observed()

◆ observed_or()

Vector algotradelib::math::observed_or ( SeriesView values,
Scalar filler )
inline

Replaces every unobserved entry of a series.

Parameters
valuesThe series; any NaN counts as unobserved.
fillerThe value to put in place of each unobserved entry.
Returns
A copy of values with each NaN replaced by filler (so it has no NaN unless filler is one).

Definition at line 130 of file eigen.hpp.

Referenced by observed_or(), algotradelib::analytics::rolling_max(), and algotradelib::analytics::rolling_min().

◆ observed_parts()

Observed algotradelib::math::observed_parts ( SeriesView values)
inline

◆ thin_svd()

Svd algotradelib::math::thin_svd ( const Matrix & a)
inline

Computes a thin SVD, ready to solve().

Parameters
aThe matrix to decompose.
Returns
The decomposition of a.

Definition at line 202 of file eigen.hpp.

Referenced by thin_svd().

◆ undefined()

Scalar algotradelib::math::undefined ( )
inline

Returns the value that stands for "the data was there, but the statistic does not exist".

Examples: a z-score over a window with no dispersion, a correlation against a constant, a Sortino ratio over a series that never fell below its target. It differs from missing(), which means there was no data. Both are quiet NaNs, so observed() is false for both and reductions skip both; only is_undefined() tells them apart.

Returns
A quiet NaN with a payload that is_undefined() recognizes.
Warning
Test is_undefined() on a value a function returned (or one copied from a matrix). Arithmetic on it, such as undefined() + 1.0, yields a NaN that may no longer be recognized.

Definition at line 51 of file scalar.hpp.

Referenced by algotradelib::analytics::beta(), algotradelib::analytics::max_drawdown(), algotradelib::analytics::rolling_corr(), undefined(), and algotradelib::analytics::zscore().

◆ unobserved() [1/2]

◆ unobserved() [2/2]

Matrix algotradelib::math::unobserved ( std::size_t rows,
std::size_t columns )
inline

Makes a matrix filled with missing().

Parameters
rowsThe number of rows.
columnsThe number of columns.
Returns
A rows by columns matrix of unobserved values.

Definition at line 84 of file eigen.hpp.

Variable Documentation

◆ LOWER

unsigned int algotradelib::math::LOWER = Eigen::Lower
inlineconstexpr

Selects the lower triangle, as in matrix.selfadjointView<math::LOWER>().

A self-adjoint view tells a decomposition to read one triangle and assume the other.

Definition at line 153 of file eigen.hpp.

◆ SUCCESS

auto algotradelib::math::SUCCESS = Eigen::Success
inlineconstexpr

The value a decomposition's info() returns on success.

Definition at line 214 of file eigen.hpp.

◆ UNIT_LOWER

unsigned int algotradelib::math::UNIT_LOWER = Eigen::UnitLower
inlineconstexpr

Selects the lower triangle with an implicit unit diagonal.

Definition at line 157 of file eigen.hpp.

◆ UNIT_UPPER

unsigned int algotradelib::math::UNIT_UPPER = Eigen::UnitUpper
inlineconstexpr

Selects the upper triangle with an implicit unit diagonal.

Definition at line 159 of file eigen.hpp.

◆ UPPER

unsigned int algotradelib::math::UPPER = Eigen::Upper
inlineconstexpr

Selects the upper triangle.

Definition at line 155 of file eigen.hpp.