2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Support integer distributions.

This commit is contained in:
Nick
2019-09-24 10:11:58 -04:00
parent 2bb436b821
commit 72a66b109a
2 changed files with 61 additions and 1 deletions

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 29 KiB

View File

@@ -15,7 +15,6 @@ class empirical_cumulative_distribution_function {
public:
empirical_cumulative_distribution_function(RandomAccessContainer && v)
{
static_assert(!std::is_integral_v<Real>, "Integer data not implemented.");
m_v = std::move(v);
if (!std::is_sorted(m_v.begin(), m_v.end())) {
std::sort(m_v.begin(), m_v.end());
@@ -23,6 +22,17 @@ public:
}
auto operator()(Real x) const {
if constexpr (std::is_integral_v<Real>) {
if (x < m_v[0]) {
return double(0);
}
if (x >= m_v[m_v.size()-1]) {
return double(1);
}
auto it = std::upper_bound(m_v.begin(), m_v.end(), x);
return static_cast<double>(std::distance(m_v.begin(), it))/static_cast<double>(m_v.size());
}
else {
if (x < m_v[0]) {
return Real(0);
}
@@ -31,9 +41,11 @@ public:
}
auto it = std::upper_bound(m_v.begin(), m_v.end(), x);
return static_cast<Real>(std::distance(m_v.begin(), it))/static_cast<Real>(m_v.size());
}
}
private:
RandomAccessContainer m_v;
};