← Back to Documentations

Signature Description
enum class quantile_policy : unsigned char  {
    lower_value = 1,  // Take the higher index
    higher_value = 2, // Take the lower index
    mid_point = 3,    // Average the two quantiles
    linear = 4,       // Linearly combine the two quantiles
};
This policy determines how to calculate quantiles when they fall between two values.
Linear calculates as:
X1 + (X2 - X1) * (1.0 - QT)

Signature Description Parameters
#include <DataFrame/DataFrameStatsVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct QuantileVisitor;

// -------------------------------------

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using qt_v = QuantileVisitor<T, I, A>;
This is a "single action visitor", meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This functor class finds the quantile specified by quantile and q_policy. Please see quantile_policy for more explanation.
    explicit
    QuantileVisitor(double quantile = 0.5,
                    quantile_policy q_policy = quantile_policy::mid_point,
                    bool skip_nan = false)
        
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
#include <DataFrame/DataFrameStatsVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct NQuantileVisitor;

// -------------------------------------

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using nqt_v = NQuantileVisitor<T, I, A>;
This is a "single action visitor", meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This does the same thing the as above QuantileVisitor, but for a vector of quantiles. If you need multiple quantiles at the same time, this is more efficient than repeatedly calling QuantileVisitor.
    explicit
    NQuantileVisitor(std::vector &&quantiles,
                     quantile_policy q_policy = quantile_policy::mid_point,
                     bool skip_nan = false)
        
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_quantile()  {

    std::cout << "\nTesting QuantileVisitor{ } ..." << std::endl;

    StlVecType<unsigned long>  idx = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
    StlVecType<double>         d1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
    MyDataFrame                df;

    df.load_data(std::move(idx), std::make_pair("col_1", d1));
    df.shuffle<double>({"col_1"}, false);

    QuantileVisitor<double, unsigned long, 128> v1 { 1, quantile_policy::mid_point };
    auto                                        result { df.single_act_visit<double>("col_1", v1).get_result() };

    assert(result == 40.0);

    QuantileVisitor<double, unsigned long, 128> v2 { 0.5, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v2).get_result();
    assert(result == 20.0);

    QuantileVisitor<double, unsigned long, 128> v3 { 0.5, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v3).get_result();
    assert(result == 20.0);

    QuantileVisitor<double, unsigned long, 128> v4 { 0.5, quantile_policy::higher_value };

    result = df.single_act_visit<double>("col_1", v4).get_result();
    assert(result == 20.0);

    QuantileVisitor<double, unsigned long, 128> v5 { 0.5, quantile_policy::lower_value };

    result = df.single_act_visit<double>("col_1", v5).get_result();
    assert(result == 20.0);

    QuantileVisitor<double, unsigned long, 128> v6 { 0.55, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v6).get_result();
    assert(result == 22.0);

    QuantileVisitor<double, unsigned long, 128> v7 { 0.55, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v7).get_result();
    assert(result == 22.0);

    QuantileVisitor<double, unsigned long, 128> v8 { 0.75, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v8).get_result();
    assert(result == 30.0);

    QuantileVisitor<double, unsigned long, 128> v9 { 0.75, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v9).get_result();
    assert(result == 30.0);

    QuantileVisitor<double, unsigned long, 128> v10 { 0, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v10).get_result();
    assert(result == 1.0);

    df.get_index().push_back(41);
    df.get_column<double>("col_1").push_back(41);

    QuantileVisitor<double, unsigned long, 128> v11 { 0.75, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v11).get_result();
    assert(result == 30.5);

    QuantileVisitor<double, unsigned long, 128> v12 { 0.75, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v12).get_result();
    assert(result == 30.75);

    QuantileVisitor<double, unsigned long, 128> v13 { 0.75, quantile_policy::lower_value };

    result = df.single_act_visit<double>("col_1", v13).get_result();
    assert(result == 30.0);

    QuantileVisitor<double, unsigned long, 128> v14 { 0.75, quantile_policy::higher_value };

    result = df.single_act_visit<double>("col_1", v14).get_result();
    assert(result == 31.0);

    QuantileVisitor<double, unsigned long, 128> v15 { 0.71, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v15).get_result();
    assert(result == 29.5);

    QuantileVisitor<double, unsigned long, 128> v16 { 0.71, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v16).get_result();
    assert(result == 29.11);

    QuantileVisitor<double, unsigned long, 128> v17 { 0.23, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v17).get_result();
    assert(result == 9.5);

    QuantileVisitor<double, unsigned long, 128> v18 { 0.2, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v18).get_result();
    assert(result == 8.5);

    QuantileVisitor<double, unsigned long, 128> v19 { 0.23, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v19).get_result();
    assert(result == 9.43);

    QuantileVisitor<double, unsigned long, 128> v20 { 0.23, quantile_policy::lower_value };

    result = df.single_act_visit<double>("col_1", v20).get_result();
    assert(result == 9.0);

    QuantileVisitor<double, unsigned long, 128> v21 { 0.23, quantile_policy::higher_value };

    result = df.single_act_visit<double>("col_1", v21).get_result();
    assert(result == 10.0);

    QuantileVisitor<double, unsigned long, 128> v22 { 1, quantile_policy::linear };

    result = df.single_act_visit<double>("col_1", v22).get_result();
    assert(result == 41.0);

    QuantileVisitor<double, unsigned long, 128> v23 { 0, quantile_policy::mid_point };

    result = df.single_act_visit<double>("col_1", v23).get_result();
    assert(result == 1.0);

    // N quantiles
    //
    NQuantileVisitor<double, unsigned long, 128>    nv { { 0.25, 0.75, 1.0, 0.0, 0.15, 0.5 }, quantile_policy::mid_point };
    const auto                                      nres { df.single_act_visit<double>("col_1", nv).get_result() };

    assert(nres.size() == 6);
    assert(nres[0] == 10.5);  // 25%
    assert(nres[1] == 30.5);  // 75%
    assert(nres[2] == 41.0);  // 100%
    assert(nres[3] == 1.0);   // 0%
    assert(nres[4] == 6.5);   // 15%
    assert(nres[5] == 20.5);    // 50%
}

C++ DataFrame