Back to Documentations

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

template<typename T, typename I = unsigned long>
struct  DynamicTimeWarpVisitor

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

template<typename T, typename I = unsigned 
using dtw_v = DynamicTimeWarpVisitor<T, I>;
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.

Dynamic Time Warping (DTW) is a powerful algorithm used in time series analysis to measure the similarity between two temporal sequences. Unlike traditional distance metrics like Euclidean distance, DTW can handle sequences of different lengths. It can align sequences that may be out of sync, making it particularly useful in fields such as speech recognition, gesture analysis, and finance.
This functor optionally normalizes the columns before calculating the distance. The complexity of the algorithm is O(nXm) and currently it is not multithreaded.
explicit
DynamicTimeWarpVisitor(
    normalization_type norm_type = normalization_type::none,
    distance_func &&f = [](const value_type &x, const value_type &y) -> double  {
                            return (std::fabs(x - y));
                        })
        
get_results() Returns the numeric distance
T: Column data type
I: Index type
static void test_DynamicTimeWarpVisitor()  {

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

    StrDataFrame    df;

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

    dtw_v<double, std::string>  dtw;

    df.single_act_visit<double, double>("IBM_Open", "IBM_Close", dtw);
    assert((std::fabs(dtw.get_result() - 2682.91) < 0.01));

    df.single_act_visit<double, double>("IBM_High", "IBM_Low", dtw);
    assert((std::fabs(dtw.get_result() - 6255.33) < 0.01));

    df.single_act_visit<double, double>("IBM_Open", "IBM_Low", dtw);
    assert((std::fabs(dtw.get_result() - 3649.94) < 0.01));

    df.single_act_visit<double, double>("IBM_Close", "IBM_Low", dtw);
    assert((std::fabs(dtw.get_result() - 3851.43) < 0.01));

    df.single_act_visit<double, double>("IBM_Open", "IBM_High", dtw);
    assert((std::fabs(dtw.get_result() - 3833.26) < 0.01));

    df.single_act_visit<double, double>("IBM_Close", "IBM_High", dtw);
    assert((std::fabs(dtw.get_result() - 3737.44) < 0.01));

    DynamicTimeWarpVisitor<double, std::string> dtw2 (normalization_type::z_score);

    df.single_act_visit<double, double>("IBM_Open", "IBM_Close", dtw2);
    assert((std::fabs(dtw2.get_result() - 70.4392) < 0.0001));

    df.single_act_visit<double, double>("IBM_High", "IBM_Low", dtw2);
    assert((std::fabs(dtw2.get_result() - 90.0254) < 0.0001));

    df.single_act_visit<double, double>("IBM_Open", "IBM_Low", dtw2);
    assert((std::fabs(dtw2.get_result() - 77.116) < 0.0001));

    df.single_act_visit<double, double>("IBM_Close", "IBM_Low", dtw2);
    assert((std::fabs(dtw2.get_result() - 76.7581) < 0.0001));

    df.single_act_visit<double, double>("IBM_Open", "IBM_High", dtw2);
    assert((std::fabs(dtw2.get_result() - 77.2208) < 0.0001));

    df.single_act_visit<double, double>("IBM_Close", "IBM_High", dtw2);
    assert((std::fabs(dtw2.get_result() - 77.2344) < 0.0001));
}

C++ DataFrame