Back to Documentations

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

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

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using acf_v = AutoCorrVisitor<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 calculates the autocorrelation of given column. The result is a vector of auto correlations with lags of 0 up to max_lag. result[0] is always 1 which is lag of 0. Lags are the indices of the result vector.

  explicit
  AutoCorrVisitor (size_type max_lag);

  max_lag: Lag periods requested
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 AutoCorrVisitor;

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using acf_v = AutoCorrVisitor<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 calculates the autocorrelation 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
#include <DataFrame/DataFrameStatsVisitors.h>

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

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using pacf_v = PartialAutoCorrVisitor<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 calculates the partial autocorrelation of given column. In time series analysis, the partial autocorrelation function (PACF) gives the partial correlation of a stationary time series with its own lagged values, regressed the values of the time series at all shorter lags. It contrasts with the autocorrelation function, which does not control for other lags.
This calculation requires intensive matrix arithmetic, and it is time consuming. For that reason, the max lag is limited to up to 375 periods. It is recommended to enable multithreading for higher lag periods and bigger time-series.

  explicit
  PartialAutoCorrVisitor (size_type max_lag);

  max_lag: Lag periods requested
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;

    StlVecType<unsigned long>  idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466,
                                       123467, 123468, 123469, 123470, 123471, 123472, 123473 };
    StlVecType<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 };
    StlVecType<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 };
    StlVecType<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 { 15 };
    auto                    fut = df.single_act_visit_async<double>("col_1", auto_corr);
    const auto              &result = fut.get().get_result();

    assert(result.size() == 15);
    assert(result[0] == 1.0);
    assert(fabs(result[1] - 0.562001) < 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() == 15);
    assert(result2[0] == 1.0);
    assert(fabs(result2[1] - 0.903754) < 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() == 15);
    assert(result3[0] == 1.0);
    assert(fabs(result3[1] - 0.903754) < 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("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;
    }
}

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

static void test_PartialAutoCorrVisitor()  {

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

    StrDataFrame    df;

    try  {
        df.read("IBM.csv", io_format::csv2);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
    }

    PartialAutoCorrVisitor<double, std::string> pacf { 50 };

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

    assert(pacf.get_result().size() == 50);
    assert(std::fabs(pacf.get_result()[0] - 1.0) < 0.000001);
    assert(std::fabs(pacf.get_result()[1] - 0.999915) < 0.000001);
    assert(std::fabs(pacf.get_result()[10] - 0.094446) < 0.000001);
    assert(std::fabs(pacf.get_result()[30] - 0.004907) < 0.000001);
    assert(std::fabs(pacf.get_result()[48] - 0.004338) < 0.000001);
    assert(std::fabs(pacf.get_result()[49] - 0.045952) < 0.000001);
}

C++ DataFrame