Back to Documentations

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

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

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

template<typename A, typename T,
         typename I = unsigned long,
         std::size_t A = 0>
using bias_v = BiasVisitor<A, 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 visitor calculates rate of change between the source and a moving average. AV is the type of mean/average visitor

This works with both scalar and multidimensional (i.e. vectors or arrays) datasets.

get_result() returns the vector of change rates of a scalar column. In case of a multidimensional column, it returns a vector of vectors. Each inner vector is the length of data dimension. In case of a multidimensional column, the change rates are done per dimension

    explicit
    BiasVisitor(AV avg, size_t roll_period = 26)
        
A: Mean visitor type.
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_BiasVisitor()  {

    using StrDataFrame = StdDataFrame<std::string>;

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

    StrDataFrame    df;

    try  {
        df.read("SHORT_IBM.csv", io_format::csv2);
        df.remove_data_by_loc<double, long>({ 0, 1500 });

        using avg1 = MeanVisitor<double, std::string>;

        avg1                                avg1_v;
        bias_v<avg1, double, std::string>   bias1 (avg1_v);

        df.single_act_visit<double>("IBM_Close", bias1);

        assert(bias1.get_result().size() == 221);
        assert(std::isnan(bias1.get_result()[0]));
        assert(std::isnan(bias1.get_result()[24]));
        assert(std::abs(bias1.get_result()[25] - 0.0309) < 0.0001);
        assert(std::abs(bias1.get_result()[30] - 0.0477) < 0.0001);
        assert(std::abs(bias1.get_result()[35] - 0.0907) < 0.0001);
        assert(std::abs(bias1.get_result()[220] - -0.0698) < 0.0001);
        assert(std::abs(bias1.get_result()[215] - -0.049) < 0.0001);
        assert(std::abs(bias1.get_result()[210] - 0.0242) < 0.0001);

        using s_avg1 = StableMeanVisitor<double, std::string>;

        s_avg1                              s_avg1_v;
        bias_v<s_avg1, double, std::string> s_bias1 (s_avg1_v);

        df.single_act_visit<double>("IBM_Close", s_bias1);

        assert(s_bias1.get_result().size() == 221);
        assert(std::isnan(s_bias1.get_result()[0]));
        assert(std::isnan(s_bias1.get_result()[24]));
        assert(std::abs(s_bias1.get_result()[25] - 0.0309) < 0.0001);
        assert(std::abs(s_bias1.get_result()[30] - 0.0477) < 0.0001);
        assert(std::abs(s_bias1.get_result()[35] - 0.0907) < 0.0001);
        assert(std::abs(s_bias1.get_result()[220] - -0.0698) < 0.0001);
        assert(std::abs(s_bias1.get_result()[215] - -0.049) < 0.0001);
        assert(std::abs(s_bias1.get_result()[210] - 0.0242) < 0.0001);

        using avg2 = WeightedMeanVisitor<double, std::string>;

        avg2                                avg2_v;
        bias_v<avg2, double, std::string>   bias2 (avg2_v);

        df.single_act_visit<double>("IBM_Close", bias2);

        assert(bias2.get_result().size() == 221);
        assert(std::isnan(bias2.get_result()[0]));
        assert(std::isnan(bias2.get_result()[24]));
        assert(std::abs(bias2.get_result()[25] - 0.0224) < 0.0001);
        assert(std::abs(bias2.get_result()[30] - 0.0381) < 0.0001);
        assert(std::abs(bias2.get_result()[35] - 0.068) < 0.0001);
        assert(std::abs(bias2.get_result()[220] - -0.0532) < 0.0001);
        assert(std::abs(bias2.get_result()[215] - -0.0496) < 0.0001);
        assert(std::abs(bias2.get_result()[210] - 0.0168) < 0.0001);

        using avg3 = GeometricMeanVisitor<double, std::string>;

        avg3                                avg3_v;
        bias_v<avg3, double, std::string>   bias3 (avg3_v);

        df.single_act_visit<double>("IBM_Close", bias3);

        assert(bias3.get_result().size() == 221);
        assert(std::isnan(bias3.get_result()[0]));
        assert(std::isnan(bias3.get_result()[24]));
        assert(std::abs(bias3.get_result()[25] - 0.0311) < 0.0001);
        assert(std::abs(bias3.get_result()[30] - 0.0479) < 0.0001);
        assert(std::abs(bias3.get_result()[35] - 0.0919) < 0.0001);
        assert(std::abs(bias3.get_result()[220] - -0.0685) < 0.0001);
        assert(std::abs(bias3.get_result()[215] - -0.0485) < 0.0001);
        assert(std::abs(bias3.get_result()[210] - 0.0245) < 0.0001);

        using avg4 = HarmonicMeanVisitor<double, std::string>;

        avg4                                avg4_v;
        bias_v<avg4, double, std::string>   bias4 (avg4_v);

        df.single_act_visit<double>("IBM_Close", bias4);

        assert(bias4.get_result().size() == 221);
        assert(std::isnan(bias4.get_result()[0]));
        assert(std::isnan(bias4.get_result()[24]));
        assert(std::abs(bias4.get_result()[25] - 0.0313) < 0.0001);
        assert(std::abs(bias4.get_result()[30] - 0.0481) < 0.0001);
        assert(std::abs(bias4.get_result()[35] - 0.093) < 0.0001);
        assert(std::abs(bias4.get_result()[220] - -0.0672) < 0.0001);
        assert(std::abs(bias4.get_result()[215] - -0.048) < 0.0001);
        assert(std::abs(bias4.get_result()[210] - 0.0248) < 0.0001);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
        ::exit(-1);
    }

    // Now multidimensional data
    //
    RandGenParams<double>   p;

    p.seed = 123;
    p.min_value = 1;
    p.max_value = 10.0;

    constexpr std::size_t   dim { 3 };

    using ary_col_t = std::array<double, dim>;
    using vec_col_t = std::vector<double>;

    // Generate and load 3 random columns
    //
    auto    rand_vec =
        gen_uniform_real_dist<double>(df.get_index().size() * dim, p);

    std::vector<ary_col_t>   array_col(df.get_index().size());
    std::vector<vec_col_t>   vector_col(df.get_index().size());

    for (std::size_t i { 0 }, j { 0 }; j < rand_vec.size(); ++i)  {
        vector_col[i].resize(dim);
        for (std::size_t d { 0 }; d < dim; ++d)
            array_col[i][d] = vector_col[i][d] = rand_vec[j++];
    }
    df.load_column<ary_col_t>("array_col", std::move(array_col));
    df.load_column<vec_col_t>("vector_col", std::move(vector_col));

    MeanVisitor<ary_col_t, std::string>             ary_mean_v;
    MeanVisitor<vec_col_t, std::string>             vec_mean_v;
    GeometricMeanVisitor<ary_col_t, std::string>    ary_geom_v;
    GeometricMeanVisitor<vec_col_t, std::string>    vec_geom_v;

    const auto  &ary_mean_res = df.single_act_visit<ary_col_t>("array_col", ary_mean_v).get_result();
    const auto  &vec_mean_res = df.single_act_visit<vec_col_t>("vector_col", vec_mean_v).get_result();
    const auto  &ary_geom_res = df.single_act_visit<ary_col_t>("array_col", ary_geom_v).get_result();
    const auto  &vec_geom_res = df.single_act_visit<vec_col_t>("vector_col", vec_geom_v).get_result();

    assert(ary_mean_res.size() == 3);
    assert(vec_mean_res.size() == 3);
    assert(std::abs(ary_mean_res[0] - 5.47712) < 0.00001);
    assert(std::abs(ary_mean_res[2] - 5.67667) < 0.00001);
    assert(std::abs(vec_mean_res[0] - 5.47712) < 0.00001);
    assert(std::abs(vec_mean_res[2] - 5.67667) < 0.00001);

    assert(ary_geom_res.size() == 3);
    assert(vec_geom_res.size() == 3);
    assert(std::abs(ary_geom_res[0] - 4.80128) < 0.00001);
    assert(std::abs(ary_geom_res[2] - 4.97216) < 0.00001);
    assert(std::abs(vec_geom_res[0] - 4.80128) < 0.00001);
    assert(std::abs(vec_geom_res[2] - 4.97216) < 0.00001);

    using ary_mean_t = MeanVisitor<ary_col_t, std::string>;
    using vec_mean_t = MeanVisitor<vec_col_t, std::string>;

    bias_v<ary_mean_t, ary_col_t, std::string>   ary_bias_v { ary_mean_v, 26 };
    bias_v<vec_mean_t, vec_col_t, std::string>   vec_bias_v { vec_mean_v, 26 };

    df.single_act_visit<ary_col_t>("array_col", ary_bias_v);
    df.single_act_visit<vec_col_t>("vector_col", vec_bias_v);

    assert(ary_bias_v.get_result().size() == 221);
    for (std::size_t i { 0 }; const auto &vec : ary_bias_v.get_result())  {
        if (i++ < 25)  assert(vec.empty());
        else  assert(vec.size() == dim);
    }
    assert(std::fabs(ary_bias_v.get_result()[25][0] - 0.182259) < 0.000001);
    assert(std::fabs(ary_bias_v.get_result()[35][1] - -0.461714) < 0.000001);
    assert(std::fabs(ary_bias_v.get_result()[36][2] - -0.184178) < 0.000001);
    assert(std::fabs(ary_bias_v.get_result()[220][1] - -0.845398) < 0.000001);

    assert(vec_bias_v.get_result().size() == 221);
    for (std::size_t i { 0 }; const auto &vec : vec_bias_v.get_result())  {
        if (i++ < 25)  assert(vec.empty());
        else  assert(vec.size() == dim);
    }
    assert(std::fabs(vec_bias_v.get_result()[25][0] - 0.182259) < 0.000001);
    assert(std::fabs(vec_bias_v.get_result()[35][1] - -0.461714) < 0.000001);
    assert(std::fabs(vec_bias_v.get_result()[36][2] - -0.184178) < 0.000001);
    assert(std::fabs(vec_bias_v.get_result()[220][1] - -0.845398) < 0.000001);
}

C++ DataFrame