Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameStatsVisitors.h> template<typename T, typename I = unsigned long, std::size_t A = 0> struct AutoCorrVisitor; |
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 calculates the auto correlation of given column. The result is a vector of auto correlations with lags of 0 up to length of column – 4. |
T: Column data type. T must be an arithmetic-enabled 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 FixedAutoCorrVisitor; |
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 calculates the auto correlation of given column. The calculation lag is fixed with the given lag parameter. See roll_policy for how the calculations are done FixedAutoCorrVisitor (size_type lag_period, roll_policy rp); lag_period: Period to offset the correlation calculations rp: How to roll over the input with the lagged period. See roll_policy |
T: Column data type. T must be an arithmetic-enabled type I: Index type. A: Memory alignment boundary for vectors. Default is system default alignment |
static void test_auto_correlation() { std::cout << "\nTesting Auto Correlation ..." << std::endl; std::vector<unsigned long> idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466, 123467, 123468, 123469, 123470, 123471, 123472, 123473 }; std::vector<double> d1 = { 15, 16, 15, 18, 19, 16, 21, 0.34, 1.56, 0.34, 2.3, 0.34, 19.0, 0.387, 0.123, 1.06, 0.65, 2.03, 0.4, 1.0, 0.007 }; std::vector<double> d2 = { 1.23, 1.22, 1.21, 1.20, 1.19, 1.185, 1.181, 1.19, 1.195, 1.189, 1.185, 1.18, 1.181, 1.186, 1.189, 1.19, 1.194, 1.198, 1.199, 1.197, 1.193 }; std::vector<int> i1 = { 22, 23, 24, 25, 99 }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("col_1", d1), std::make_pair("col_2", d2), std::make_pair("col_3", i1)); AutoCorrVisitor<double> auto_corr; auto fut = df.single_act_visit_async<double>("col_1", auto_corr); const auto &result = fut.get().get_result(); assert(result.size() == 17); assert(result[0] == 1.0); assert(fabs(result[1] - 0.562001) < 0.00001); assert(fabs(result[16] - -0.265228) < 0.00001); assert(fabs(result[6] - 0.388131) < 0.00001); assert(fabs(result[10] - 0.125514) < 0.00001); const auto &result2 = df.single_act_visit<double>("col_2", auto_corr).get_result(); assert(result2.size() == 17); assert(result2[0] == 1.0); assert(fabs(result2[1] - 0.903754) < 0.00001); assert(fabs(result2[16] - 0.183254) < 0.00001); assert(fabs(result2[6] - -0.263385) < 0.00001); assert(fabs(result2[10] - -0.712274) < 0.00001); const MyDataFrame df_c = df; const auto &result3 = df_c.single_act_visit<double>("col_2", auto_corr).get_result(); assert(result3.size() == 17); assert(result3[0] == 1.0); assert(fabs(result3[1] - 0.903754) < 0.00001); assert(fabs(result3[16] - 0.183254) < 0.00001); assert(fabs(result3[6] - -0.263385) < 0.00001); assert(fabs(result3[10] - -0.712274) < 0.00001); }
// ----------------------------------------------------------------------------- static void test_FixedAutoCorrVisitor() { std::cout << "\nTesting FixedAutoCorrVisitor{ } ..." << std::endl; StrDataFrame df; try { df.read("data/IBM.csv", io_format::csv2); FixedAutoCorrVisitor<double, std::string> fac { 31, roll_policy::blocks }; df.single_act_visit<double> ("IBM_Close", fac); assert(fac.get_result().size() == 162); assert(std::abs(fac.get_result()[0] - -0.5436) < 0.0001); assert(std::abs(fac.get_result()[12] - 0.1328) < 0.001); assert(std::abs(fac.get_result()[14] - -0.594) < 0.0001); assert(std::abs(fac.get_result()[161] - -0.1109) < 0.0001); assert(std::abs(fac.get_result()[160] - -0.231) < 0.0001); assert(std::abs(fac.get_result()[159] - 0.075) < 0.0001); FixedAutoCorrVisitor<double, std::string> fac2 { 31, roll_policy::continuous }; df.single_act_visit<double> ("IBM_Close", fac2); assert(fac2.get_result().size() == 5000); assert(std::abs(fac2.get_result()[0] - -0.5436) < 0.0001); assert(std::abs(fac2.get_result()[12] - -0.7213) < 0.001); assert(std::abs(fac2.get_result()[14] - -0.6657) < 0.0001); assert(std::abs(fac2.get_result()[4999] - 0.1446) < 0.0001); assert(std::abs(fac2.get_result()[4998] - 0.1809) < 0.0001); assert(std::abs(fac2.get_result()[4997] - 0.1732) < 0.0001); } catch (const DataFrameError &ex) { std::cout << ex.what() << std::endl; } }