26#include "ElementsKernel/Exception.h"
35 : m_x_sampling(x_sampling), m_y_sampling(y_sampling) {
36 if (x_sampling.
size() != y_sampling.
size()) {
42 auto iter = sampling.
begin();
43 while (iter != sampling.
end()) {
53 auto iter = sampling.
begin();
54 while (iter != sampling.
end()) {
55 xs.push_back((*iter).first);
56 ys.push_back((*iter).second);
65 auto iter_pdf = pdf_sampling.
cbegin();
67 while (iter_pdf != pdf_sampling.
cend()) {
69 cumul.push_back(total);
81 cumul.push_back(*iter / total);
90 if (ratio > 1. || ratio < 0.) {
91 throw Elements::Exception(
"Cumulative::findValue : ratio parameter must be in range [0,1]");
103 double begin_value = *iter_x;
104 double tray = *iter_y;
110 double end_value = *(--iter_x);
115 result = begin_value;
118 result = 0.5 * (begin_value + end_value);
130 if (rate > 1. || rate <= 0.) {
131 throw Elements::Exception(
"Cumulative::findMinInterval : rate parameter must be in range ]0,1]");
145 while (iter_2_x !=
m_x_sampling.
cend() && (*iter_2_y - *iter_1_y + first_correction) < rate) {
152 if ((*iter_2_x - *iter_1_x) <= (*max_x - *min_x)) {
158 first_correction = 0.;
166 if (rate > 1. || rate <= 0.) {
167 throw Elements::Exception(
"Cumulative::findCenteredInterval : rate parameter must be in range ]0,1]");
180 if ((*iter_y) < max_value) {
181 double tray = *iter_y;
192 double max_x = *iter_x;
197 double max_x = *iter_x;
210 return (*function)(x_value);
Class for build cumulative from PDF and extract feature out of it.
Cumulative(Cumulative &&other)=default
move constructor
TrayPosition
when looking for the position having a given value, one may encounter tray where the value is constan...
std::vector< double > m_x_sampling
std::pair< double, double > findCenteredInterval(double rate) const
return the horizontal interval starting where the Cumulative has value (1-ratio)/2 and ending where t...
std::vector< double > m_y_sampling
void normalize()
Normalize the Cumulative. After calling this function the last vertical value is 1....
double findValue(double ratio, TrayPosition position=TrayPosition::middle) const
Find the first horizontal sample which vertical value is bigger or equal to the ratio value....
std::pair< double, double > findMinInterval(double rate) const
Scan the horizontal axis looking for the smallest x-interval for which the vertical interval is at le...
static Cumulative fromPdf(std::vector< double > &x_sampling, std::vector< double > &pdf_sampling)
Factory from the sampling of a PDF. The Cumulative vertical samples are build as the sum of the the p...
double eval(double x_value) const
return the value of the cumulative at a given value of the horizontal axis. If the value do not match...
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
const_iterator begin() const
Returns a const iterator to the first pair of the dataset.
const_iterator end() const
Returns a const iterator to the one after last pair dataset.
ELEMENTS_API std::unique_ptr< Function > interpolate(const std::vector< double > &x, const std::vector< double > &y, InterpolationType type, bool extrapolate=false)