20namespace metric_detail {
24 std::size_t count_{0};
29 bool constant_{
false};
42 const Scalar count = parts.present_.sum();
43 result.count_ =
static_cast<std::size_t
>(count);
44 if (result.count_ == 0) {
47 result.mean_ = parts.values_.sum() / count;
48 result.squared_ = (parts.values_ - result.mean_ * parts.present_).squaredNorm();
49 if (result.count_ >= 2) {
50 const Scalar deviation = std::sqrt(result.squared_ /
static_cast<Scalar>(result.count_ - 1));
51 result.constant_ = constant_detail::rounded_constant(
52 deviation, result.mean_, result.count_, parts.values_, parts.present_, 0, parts.values_.size()
87 std::size_t observations = 0;
88 for (
math::Index row = 0; row < levels.size(); ++row) {
93 if (peak_row < 0 || peak < levels[row]) {
100 const Scalar decline = (peak - levels[row]) / peak;
127 const auto summary = metric_detail::summarize(period_returns);
128 if (summary.count_ < 2) {
131 const Scalar deviation = std::sqrt(summary.squared_ /
static_cast<Scalar>(summary.count_ - 1));
132 if (deviation ==
Scalar{0} || summary.constant_) {
137 return (summary.mean_ - risk_free) / deviation;
152 std::size_t count = 0;
153 std::size_t below = 0;
156 for (
math::Index row = 0; row < period_returns.size(); ++row) {
157 const Scalar value = period_returns[row];
163 if (value < target) {
165 const Scalar gap = target - value;
166 shortfall += gap * gap;
181 const Scalar downside = std::sqrt(shortfall /
static_cast<Scalar>(count));
182 if (downside ==
Scalar{0}) {
185 return (sum /
static_cast<Scalar>(count) - target) / downside;
199 if (!(periods_per_year >
Scalar{0})) {
200 throw std::invalid_argument(
"periods per year must be positive");
202 return observed(ratio) ? ratio * std::sqrt(periods_per_year) : ratio;
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
bool observed(Scalar value)
Tests whether a value is an observation; see math::observed.
Scalar sortino(math::SeriesView period_returns, Scalar target=Scalar{0})
Computes the per-period Sortino ratio, (mean(returns) - target) / downside_deviation.
Scalar annualize_ratio(Scalar ratio, Scalar periods_per_year)
Annualizes a per-period ratio by multiplying it by sqrt(periods_per_year).
MaxDrawdown max_drawdown(math::SeriesView levels)
Finds the worst peak-to-trough decline of a level series.
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
Scalar sharpe(math::SeriesView period_returns, Scalar risk_free=Scalar{0})
Computes the per-period Sharpe ratio, (mean(returns) - risk_free) / stdev(returns).
Observed observed_parts(SeriesView values)
Splits a series into zero-filled values and a presence mask.
double Scalar
The floating-point type the analytics and math layers compute in (double).
Eigen::Index Index
The signed index type of rows and columns.
Scalar undefined()
Returns the value that stands for "the data was there, but the statistic does not exist".
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
The library: broker-neutral types (core), and the modules below.
Re-exports math::Scalar, math::missing and math::observed into analytics.
The worst peak-to-trough decline of a level series, and where it happened.
math::Index peak_row_
The row of the peak the decline fell from, or -1 if there is none.
math::Index trough_row_
The row of the low the decline reached, or -1 if there is none.
Scalar magnitude_
The decline as a non-negative fraction of the peak (0.2 is 20% below it).