2017-12-20 07:36:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-26 19:00:20 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/VarInt.h>
|
2020-08-15 06:47:34 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <Common/NaNUtils.h>
|
|
|
|
#include <Common/PODArray.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/sort.h>
|
|
|
|
#include <base/types.h>
|
2017-12-26 19:00:20 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
2020-11-13 11:28:18 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
2019-08-14 11:13:04 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
template <typename Value, typename Derived>
|
|
|
|
struct QuantileExactBase
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
/// The memory will be allocated to several elements at once, so that the state occupies 64 bytes.
|
2017-12-20 08:39:21 +00:00
|
|
|
static constexpr size_t bytes_in_arena = 64 - sizeof(PODArray<Value>);
|
2019-06-28 12:51:01 +00:00
|
|
|
using Array = PODArrayWithStackMemory<Value, bytes_in_arena>;
|
2017-12-20 07:36:30 +00:00
|
|
|
Array array;
|
|
|
|
|
|
|
|
void add(const Value & x)
|
|
|
|
{
|
2018-03-14 05:03:51 +00:00
|
|
|
/// We must skip NaNs as they are not compatible with comparison sorting.
|
|
|
|
if (!isNaN(x))
|
|
|
|
array.push_back(x);
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Weight>
|
2017-12-20 08:39:21 +00:00
|
|
|
void add(const Value &, const Weight &)
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
throw Exception("Method add with weight is not implemented for QuantileExact", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
void merge(const QuantileExactBase & rhs) { array.insert(rhs.array.begin(), rhs.array.end()); }
|
2017-12-20 07:36:30 +00:00
|
|
|
|
|
|
|
void serialize(WriteBuffer & buf) const
|
|
|
|
{
|
|
|
|
size_t size = array.size();
|
|
|
|
writeVarUInt(size, buf);
|
2018-09-02 03:00:04 +00:00
|
|
|
buf.write(reinterpret_cast<const char *>(array.data()), size * sizeof(array[0]));
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
readVarUInt(size, buf);
|
|
|
|
array.resize(size);
|
2018-09-02 03:00:04 +00:00
|
|
|
buf.read(reinterpret_cast<char *>(array.data()), size * sizeof(array[0]));
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
Value get(Float64 level)
|
|
|
|
{
|
|
|
|
auto derived = static_cast<Derived*>(this);
|
|
|
|
return derived->getImpl(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getMany(const Float64 * levels, const size_t * indices, size_t size, Value * result)
|
|
|
|
{
|
|
|
|
auto derived = static_cast<Derived*>(this);
|
|
|
|
return derived->getManyImpl(levels, indices, size, result);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Calculates quantile by collecting all values into array
|
|
|
|
* and applying n-th element (introselect) algorithm for the resulting array.
|
|
|
|
*
|
|
|
|
* It uses O(N) memory and it is very inefficient in case of high amount of identical values.
|
|
|
|
* But it is very CPU efficient for not large datasets.
|
|
|
|
*/
|
|
|
|
template <typename Value>
|
|
|
|
struct QuantileExact : QuantileExactBase<Value, QuantileExact<Value>>
|
|
|
|
{
|
|
|
|
using QuantileExactBase<Value, QuantileExact<Value>>::array;
|
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// Get the value of the `level` quantile. The level must be between 0 and 1.
|
2020-08-24 11:54:04 +00:00
|
|
|
Value getImpl(Float64 level)
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
2020-08-15 06:47:34 +00:00
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin(), array.begin() + n, array.end()); /// NOTE: You can think of the radix-select algorithm.
|
2017-12-20 07:36:30 +00:00
|
|
|
return array[n];
|
|
|
|
}
|
|
|
|
|
2018-08-13 08:33:51 +00:00
|
|
|
return std::numeric_limits<Value>::quiet_NaN();
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the `size` values of `levels` quantiles. Write `size` results starting with `result` address.
|
|
|
|
/// indices - an array of index levels such that the corresponding elements will go in ascending order.
|
2020-08-24 11:54:04 +00:00
|
|
|
void getManyImpl(const Float64 * levels, const size_t * indices, size_t size, Value * result)
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
size_t prev_n = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto level = levels[indices[i]];
|
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin() + prev_n, array.begin() + n, array.end());
|
2017-12-20 07:36:30 +00:00
|
|
|
result[indices[i]] = array[n];
|
|
|
|
prev_n = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
result[i] = Value();
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 19:12:31 +00:00
|
|
|
};
|
|
|
|
|
2019-08-14 11:18:46 +00:00
|
|
|
/// QuantileExactExclusive is equivalent to Excel PERCENTILE.EXC, R-6, SAS-4, SciPy-(0,0)
|
2019-08-13 19:12:31 +00:00
|
|
|
template <typename Value>
|
2021-04-04 09:33:06 +00:00
|
|
|
/// There are no virtual-like functions. So we don't inherit from QuantileExactBase.
|
2019-08-13 19:12:31 +00:00
|
|
|
struct QuantileExactExclusive : public QuantileExact<Value>
|
|
|
|
{
|
|
|
|
using QuantileExact<Value>::array;
|
2019-08-14 11:13:04 +00:00
|
|
|
|
|
|
|
/// Get the value of the `level` quantile. The level must be between 0 and 1 excluding bounds.
|
2019-08-19 16:03:25 +00:00
|
|
|
Float64 getFloat(Float64 level)
|
2019-08-13 19:12:31 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
2019-08-14 11:18:46 +00:00
|
|
|
if (level == 0. || level == 1.)
|
|
|
|
throw Exception("QuantileExactExclusive cannot interpolate for the percentiles 1 and 0", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
2019-08-13 19:12:31 +00:00
|
|
|
Float64 h = level * (array.size() + 1);
|
|
|
|
auto n = static_cast<size_t>(h);
|
|
|
|
|
|
|
|
if (n >= array.size())
|
2020-08-19 11:52:17 +00:00
|
|
|
return static_cast<Float64>(array[array.size() - 1]);
|
2019-08-13 19:12:31 +00:00
|
|
|
else if (n < 1)
|
2020-08-19 11:52:17 +00:00
|
|
|
return static_cast<Float64>(array[0]);
|
2019-08-13 19:12:31 +00:00
|
|
|
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin(), array.begin() + n - 1, array.end());
|
|
|
|
auto nth_elem = std::min_element(array.begin() + n, array.end());
|
2019-08-13 19:12:31 +00:00
|
|
|
|
2020-11-13 11:28:18 +00:00
|
|
|
return static_cast<Float64>(array[n - 1]) + (h - n) * static_cast<Float64>(*nth_elem - array[n - 1]);
|
2019-08-13 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::numeric_limits<Float64>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:03:25 +00:00
|
|
|
void getManyFloat(const Float64 * levels, const size_t * indices, size_t size, Float64 * result)
|
2019-08-13 19:12:31 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
size_t prev_n = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto level = levels[indices[i]];
|
2019-08-14 11:13:04 +00:00
|
|
|
if (level == 0. || level == 1.)
|
|
|
|
throw Exception("QuantileExactExclusive cannot interpolate for the percentiles 1 and 0", ErrorCodes::BAD_ARGUMENTS);
|
2019-08-13 19:12:31 +00:00
|
|
|
|
|
|
|
Float64 h = level * (array.size() + 1);
|
|
|
|
auto n = static_cast<size_t>(h);
|
|
|
|
|
|
|
|
if (n >= array.size())
|
2020-08-19 11:52:17 +00:00
|
|
|
result[indices[i]] = static_cast<Float64>(array[array.size() - 1]);
|
2019-08-13 19:12:31 +00:00
|
|
|
else if (n < 1)
|
2020-08-19 11:52:17 +00:00
|
|
|
result[indices[i]] = static_cast<Float64>(array[0]);
|
2019-08-13 19:12:31 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin() + prev_n, array.begin() + n - 1, array.end());
|
|
|
|
auto nth_elem = std::min_element(array.begin() + n, array.end());
|
|
|
|
|
|
|
|
result[indices[i]] = static_cast<Float64>(array[n - 1]) + (h - n) * static_cast<Float64>(*nth_elem - array[n - 1]);
|
2019-08-14 11:13:04 +00:00
|
|
|
prev_n = n - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
result[i] = std::numeric_limits<Float64>::quiet_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-23 18:30:04 +00:00
|
|
|
/// QuantileExactInclusive is equivalent to Excel PERCENTILE and PERCENTILE.INC, R-7, SciPy-(1,1)
|
2019-08-14 11:13:04 +00:00
|
|
|
template <typename Value>
|
2021-04-04 09:33:06 +00:00
|
|
|
/// There are no virtual-like functions. So we don't inherit from QuantileExactBase.
|
2019-08-14 11:13:04 +00:00
|
|
|
struct QuantileExactInclusive : public QuantileExact<Value>
|
|
|
|
{
|
|
|
|
using QuantileExact<Value>::array;
|
|
|
|
|
|
|
|
/// Get the value of the `level` quantile. The level must be between 0 and 1 including bounds.
|
2019-08-19 16:03:25 +00:00
|
|
|
Float64 getFloat(Float64 level)
|
2019-08-14 11:13:04 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
Float64 h = level * (array.size() - 1) + 1;
|
|
|
|
auto n = static_cast<size_t>(h);
|
|
|
|
|
|
|
|
if (n >= array.size())
|
2020-08-19 11:52:17 +00:00
|
|
|
return static_cast<Float64>(array[array.size() - 1]);
|
2019-08-14 11:13:04 +00:00
|
|
|
else if (n < 1)
|
2020-08-19 11:52:17 +00:00
|
|
|
return static_cast<Float64>(array[0]);
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin(), array.begin() + n - 1, array.end());
|
|
|
|
auto nth_elem = std::min_element(array.begin() + n, array.end());
|
|
|
|
|
|
|
|
return static_cast<Float64>(array[n - 1]) + (h - n) * static_cast<Float64>(*nth_elem - array[n - 1]);
|
2019-08-14 11:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::numeric_limits<Float64>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:03:25 +00:00
|
|
|
void getManyFloat(const Float64 * levels, const size_t * indices, size_t size, Float64 * result)
|
2019-08-14 11:13:04 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
size_t prev_n = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto level = levels[indices[i]];
|
|
|
|
|
|
|
|
Float64 h = level * (array.size() - 1) + 1;
|
|
|
|
auto n = static_cast<size_t>(h);
|
|
|
|
|
|
|
|
if (n >= array.size())
|
2020-08-19 11:52:17 +00:00
|
|
|
result[indices[i]] = static_cast<Float64>(array[array.size() - 1]);
|
2019-08-14 11:13:04 +00:00
|
|
|
else if (n < 1)
|
2020-08-19 11:52:17 +00:00
|
|
|
result[indices[i]] = static_cast<Float64>(array[0]);
|
2019-08-14 11:13:04 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-13 11:28:18 +00:00
|
|
|
nth_element(array.begin() + prev_n, array.begin() + n - 1, array.end());
|
|
|
|
auto nth_elem = std::min_element(array.begin() + n, array.end());
|
|
|
|
|
2021-09-20 18:21:40 +00:00
|
|
|
result[indices[i]] = static_cast<Float64>(array[n - 1]) + (h - n) * (static_cast<Float64>(*nth_elem) - array[n - 1]);
|
2019-08-14 11:13:04 +00:00
|
|
|
prev_n = n - 1;
|
2019-08-13 19:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
result[i] = std::numeric_limits<Float64>::quiet_NaN();
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 07:36:30 +00:00
|
|
|
};
|
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// QuantileExactLow returns the low median of given data.
|
|
|
|
// Implementation is as per "medium_low" function from python:
|
|
|
|
// https://docs.python.org/3/library/statistics.html#statistics.median_low
|
|
|
|
template <typename Value>
|
2020-08-24 11:54:04 +00:00
|
|
|
struct QuantileExactLow : public QuantileExactBase<Value, QuantileExactLow<Value>>
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
2020-08-24 11:54:04 +00:00
|
|
|
using QuantileExactBase<Value, QuantileExactLow<Value>>::array;
|
2020-08-15 06:47:34 +00:00
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
Value getImpl(Float64 level)
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
// sort inputs in ascending order
|
|
|
|
std::sort(array.begin(), array.end());
|
2021-05-08 14:05:58 +00:00
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// if level is 0.5 then compute the "low" median of the sorted array
|
|
|
|
// by the method of rounding.
|
2020-08-16 20:36:46 +00:00
|
|
|
if (level == 0.5)
|
|
|
|
{
|
2020-08-15 06:47:34 +00:00
|
|
|
auto s = array.size();
|
|
|
|
if (s % 2 == 1)
|
|
|
|
{
|
2020-08-16 20:36:46 +00:00
|
|
|
return array[static_cast<size_t>(floor(s / 2))];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-16 20:36:46 +00:00
|
|
|
return array[static_cast<size_t>((floor(s / 2)) - 1)];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 14:05:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// else quantile is the nth index of the sorted array obtained by multiplying
|
|
|
|
// level and size of array. Example if level = 0.1 and size of array is 10,
|
|
|
|
// then return array[1].
|
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
|
|
|
return array[n];
|
|
|
|
}
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
return std::numeric_limits<Value>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
void getManyImpl(const Float64 * levels, const size_t * indices, size_t size, Value * result)
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
// sort inputs in ascending order
|
|
|
|
std::sort(array.begin(), array.end());
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto level = levels[indices[i]];
|
2021-05-08 14:05:58 +00:00
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// if level is 0.5 then compute the "low" median of the sorted array
|
|
|
|
// by the method of rounding.
|
2020-08-16 20:36:46 +00:00
|
|
|
if (level == 0.5)
|
|
|
|
{
|
2020-08-15 06:47:34 +00:00
|
|
|
auto s = array.size();
|
|
|
|
if (s % 2 == 1)
|
|
|
|
{
|
2020-08-16 20:36:46 +00:00
|
|
|
result[indices[i]] = array[static_cast<size_t>(floor(s / 2))];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-16 20:36:46 +00:00
|
|
|
result[indices[i]] = array[static_cast<size_t>(floor((s / 2) - 1))];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 14:05:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// else quantile is the nth index of the sorted array obtained by multiplying
|
|
|
|
// level and size of array. Example if level = 0.1 and size of array is 10.
|
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
|
|
|
result[indices[i]] = array[n];
|
|
|
|
}
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
result[i] = Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// QuantileExactLow returns the high median of given data.
|
|
|
|
// Implementation is as per "medium_high function from python:
|
|
|
|
// https://docs.python.org/3/library/statistics.html#statistics.median_high
|
|
|
|
template <typename Value>
|
2020-08-24 11:54:04 +00:00
|
|
|
struct QuantileExactHigh : public QuantileExactBase<Value, QuantileExactHigh<Value>>
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
2020-08-24 11:54:04 +00:00
|
|
|
using QuantileExactBase<Value, QuantileExactHigh<Value>>::array;
|
2020-08-15 06:47:34 +00:00
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
Value getImpl(Float64 level)
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
// sort inputs in ascending order
|
|
|
|
std::sort(array.begin(), array.end());
|
2021-05-08 14:05:58 +00:00
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// if level is 0.5 then compute the "high" median of the sorted array
|
|
|
|
// by the method of rounding.
|
2020-08-16 20:36:46 +00:00
|
|
|
if (level == 0.5)
|
|
|
|
{
|
2020-08-15 06:47:34 +00:00
|
|
|
auto s = array.size();
|
2020-08-16 20:36:46 +00:00
|
|
|
return array[static_cast<size_t>(floor(s / 2))];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
2021-05-08 14:05:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// else quantile is the nth index of the sorted array obtained by multiplying
|
|
|
|
// level and size of array. Example if level = 0.1 and size of array is 10.
|
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
|
|
|
return array[n];
|
|
|
|
}
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
return std::numeric_limits<Value>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:54:04 +00:00
|
|
|
void getManyImpl(const Float64 * levels, const size_t * indices, size_t size, Value * result)
|
2020-08-15 06:47:34 +00:00
|
|
|
{
|
|
|
|
if (!array.empty())
|
|
|
|
{
|
|
|
|
// sort inputs in ascending order
|
|
|
|
std::sort(array.begin(), array.end());
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto level = levels[indices[i]];
|
2021-05-08 14:05:58 +00:00
|
|
|
|
2020-08-15 06:47:34 +00:00
|
|
|
// if level is 0.5 then compute the "high" median of the sorted array
|
|
|
|
// by the method of rounding.
|
2020-08-16 20:36:46 +00:00
|
|
|
if (level == 0.5)
|
|
|
|
{
|
2020-08-15 06:47:34 +00:00
|
|
|
auto s = array.size();
|
2020-08-16 20:36:46 +00:00
|
|
|
result[indices[i]] = array[static_cast<size_t>(floor(s / 2))];
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
2021-05-08 14:05:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// else quantile is the nth index of the sorted array obtained by multiplying
|
|
|
|
// level and size of array. Example if level = 0.1 and size of array is 10.
|
|
|
|
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
|
|
|
|
result[indices[i]] = array[n];
|
|
|
|
}
|
2020-08-15 06:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
result[i] = Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|