1#ifndef STAN_MATH_PRIM_FUN_QUANTILE_HPP
2#define STAN_MATH_PRIM_FUN_QUANTILE_HPP
30template <
typename T, require_vector_t<T>* =
nullptr,
31 require_vector_vt<std::is_arithmetic, T>* =
nullptr>
32inline double quantile(
const T& samples_vec,
const double p) {
36 const size_t n_sample = samples_vec.size();
47 return *std::min_element(x.data(), x.data() + n_sample);
49 return *std::max_element(x.data(), x.data() + n_sample);
51 double index = (n_sample - 1) * p;
52 size_t lo = std::floor(index);
53 size_t hi = std::ceil(index);
55 std::sort(x.data(), x.data() + n_sample, std::less<double>());
57 double h = index - lo;
58 return (1 - h) * x.coeff(lo) + h * x.coeff(hi);
78template <
typename T,
typename Tp, require_all_vector_t<T, Tp>* =
nullptr,
79 require_vector_vt<std::is_arithmetic, T>* =
nullptr,
80 require_std_vector_vt<std::is_arithmetic, Tp>* =
nullptr>
81inline std::vector<double>
quantile(
const T& samples_vec,
const Tp& ps) {
85 const size_t n_sample = samples_vec.size();
86 const size_t n_ps = ps.size();
87 if (n_ps == 0 || n_sample == 0) {
95 std::vector<double> ret(n_ps, 0.0);
97 std::sort(x.data(), x.data() + n_sample, std::less<double>());
98 Eigen::ArrayXd index = (n_sample - 1) * p;
100 for (
size_t i = 0; i < n_ps; ++i) {
103 }
else if (p[i] == 1.) {
104 ret[i] = x.coeff(n_sample - 1);
106 size_t lo = std::floor(index[i]);
107 size_t hi = std::ceil(index[i]);
109 double h = index[i] - lo;
111 ret[i] = (1 - h) * x.coeff(lo) + h * x.coeff(hi);
T as_array_or_scalar(T &&v)
Returns specified input value.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
double quantile(const T &samples_vec, const double p)
Return sample quantiles corresponding to the given probabilities.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...