1 // Copyright Nick Thompson 2019.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_DISTRIBUTIONS_EMPIRICAL_CUMULATIVE_DISTRIBUTION_FUNCTION_HPP
7 #define BOOST_MATH_DISTRIBUTIONS_EMPIRICAL_CUMULATIVE_DISTRIBUTION_FUNCTION_HPP
12 namespace boost { namespace math{
14 template<class RandomAccessContainer>
15 class empirical_cumulative_distribution_function {
16 using Real = typename RandomAccessContainer::value_type;
18 empirical_cumulative_distribution_function(RandomAccessContainer && v, bool sorted = false)
21 throw std::domain_error("At least one sample is required to compute an empirical CDF.");
25 std::sort(m_v.begin(), m_v.end());
29 auto operator()(Real x) const {
30 if constexpr (std::is_integral_v<Real>)
35 if (x >= m_v[m_v.size()-1]) {
38 auto it = std::upper_bound(m_v.begin(), m_v.end(), x);
39 return static_cast<double>(std::distance(m_v.begin(), it))/static_cast<double>(m_v.size());
46 if (x >= m_v[m_v.size()-1]) {
49 auto it = std::upper_bound(m_v.begin(), m_v.end(), x);
50 return static_cast<Real>(std::distance(m_v.begin(), it))/static_cast<Real>(m_v.size());
54 RandomAccessContainer&& return_data() {
55 return std::move(m_v);
59 RandomAccessContainer m_v;