20#include <ql/math/statistics/histogram.hpp>
21#include <ql/math/statistics/incrementalstatistics.hpp>
22#include <ql/math/comparison.hpp>
37 Real quantile(
const std::vector<Real>& samples,
Real prob) {
38 Size nsample = samples.size();
39 QL_REQUIRE(prob >= 0.0 && prob <= 1.0,
40 "Probability has to be in [0,1].");
41 QL_REQUIRE(nsample > 0,
"The sample size has to be positive." );
47 const Real a = 1. / 3, b = 2*a / (nsample+a);
49 return *std::min_element(samples.begin(), samples.end());
51 return *std::max_element(samples.begin(), samples.end());
54 Size index =
static_cast<Size>(std::floor((nsample+a)*prob+a));
55 std::vector<Real> sorted(index+1);
56 std::partial_sort_copy(samples.begin(), samples.end(),
57 sorted.begin(), sorted.end());
60 Real weight = nsample*prob + a - index;
61 return (1-weight) * sorted[index-1] + weight * sorted[index];
84 #if defined(QL_EXTRA_SAFETY_CHECKS)
92 #if defined(QL_EXTRA_SAFETY_CHECKS)
100 QL_REQUIRE(!
data_.empty(),
"no data given");
110 std::ceil(std::log(
static_cast<Real>(
data_.size()))
111 /std::log(2.0) + 1));
117 Real h = 2.0 * (r2-r1) * std::pow(
static_cast<Real>(
data_.size()), -1.0/3.0);
118 bins_ =
static_cast<Size>(std::ceil((max-min)/h));
125 Real h = 3.5 * std::sqrt(variance)
126 * std::pow(
static_cast<Real>(
data_.size()), -1.0/3.0);
127 bins_ =
static_cast<Size>(std::ceil((max-min)/h));
131 QL_FAIL(
"a bin-partition algorithm is required");
133 QL_FAIL(
"unknown bin-partition algorithm");
161 bool processed =
false;
std::vector< Real > breaks_
std::vector< Real > frequency_
std::vector< Size > counts_
Algorithm algorithm() const
Real frequency(Size i) const
Size counts(Size i) const
const std::vector< Real > & breaks() const
std::vector< Real > data_
Statistics tool based on incremental accumulation.
void addSequence(DataIterator begin, DataIterator end)
adds a sequence of data to the set, with default weight
template class providing a null value for a given type.
std::size_t Size
size of a container
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)