DynamicTimeWarping Structure 
Namespace: Accord.Statistics.Kernels
[SerializableAttribute] public struct DynamicTimeWarping : IKernel, IKernel<double[]>, IKernel<double[][]>, ICloneable, IDistance, IDistance<double[]>, IDistance<double[], double[]>, IDistance<double[][]>, IDistance<double[][], double[][]>
The DynamicTimeWarping type exposes the following members.
Name  Description  

DynamicTimeWarping(Int32) 
Constructs a new Dynamic Time Warping kernel.
 
DynamicTimeWarping(Double, Int32) 
Constructs a new Dynamic Time Warping kernel.
 
DynamicTimeWarping(Int32, Double) 
Constructs a new Dynamic Time Warping kernel.
 
DynamicTimeWarping(Int32, Double, Int32) 
Constructs a new Dynamic Time Warping kernel.

Name  Description  

Alpha 
Gets or sets the hypersphere ratio. Default is 1.
 
Degree 
Gets or sets the polynomial degree for this kernel. Default is 1.
 
Length 
Gets or sets the length for the feature vectors
contained in each sequence used by the kernel.

Name  Description  

Clone 
Creates a new object that is a copy of the current instance.
 
Distance(Double, Double) 
Computes the squared distance in feature space
between two points given in input space.
 
Distance(Double, Double) 
Computes the squared distance in feature space
between two points given in input space.
 
Equals  Indicates whether this instance and a specified object are equal. (Inherited from ValueType.)  
Function(Double, Double) 
Dynamic Time Warping kernel function.
 
Function(Double, Double) 
Dynamic Time Warping kernel function.
 
GetHashCode  Returns the hash code for this instance. (Inherited from ValueType.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
ToString  Returns the fully qualified type name of this instance. (Inherited from ValueType.) 
Name  Description  

Distance 
Computes the kernel distance for a kernel function even if it doesn't
implement the IDistance interface. Can be used to check
the proper implementation of the distance function.
(Defined by Tools.)  
HasMethod 
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)  
ToT  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)  
ToT  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by Matrix.) 
The Dynamic Time Warping Sequence Kernel is a sequence kernel, accepting vector sequences of variable size as input. Despite the sequences being variable in size, the vectors contained in such sequences should have its size fixed and should be informed at the construction of this kernel.
The conversion of the DTW global distance to a dot product uses a combination of a technique known as spherical normalization and the polynomial kernel. The degree of the polynomial kernel and the alpha for the spherical normalization should be given at the construction of the kernel. For more information, please see the referenced papers shown below.
The use of a cache is highly advisable when using this kernel.
The following example demonstrates how to create and learn a Support Vector Machine (SVM) to recognize sequences of univariate observations using the Dynamic Time Warping kernel.
// Suppose you have sequences of univariate observations, // and that those sequences could be of arbitrary length. // In this example, we have sequences binary numbers: double[][] inputs = { // Class 1 new double[] { 0,1,1,0 }, new double[] { 0,0,1,0 }, new double[] { 0,1,1,1,0 }, new double[] { 0,1,0 }, // Class +1 new double[] { 1,0,0,1 }, new double[] { 1,1,0,1 }, new double[] { 1,0,0,0,1 }, new double[] { 1,0,1 }, new double[] { 1,0,0,0,1,1 } }; int[] outputs = { 0, 0, 0, 0, // First four sequences are of class 0 1, 1, 1, 1, 1 // Last five sequences are of class 1 }; // Create the Sequential Minimal Optimization learning algorithm var smo = new SequentialMinimalOptimization<DynamicTimeWarping>() { Complexity = 1.5, // Set the parameters of the kernel Kernel = new DynamicTimeWarping(alpha: 1, degree: 1) }; // And use it to learn a machine! var svm = smo.Learn(inputs, outputs); // Now we can compute predicted values bool[] predicted = svm.Decide(inputs); // And check how far we are from the expected values double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.0
Now, instead of having univariate observations, the following example demonstrates how to create and learn a sequences of multivariate (or ndimensional) observations.
// Suppose you have sequences of multivariate observations, and that // those sequences could be of arbitrary length. On the other hand, // each observation have a fixed, delimited number of dimensions. // In this example, we have sequences of 3dimensional observations. // Each sequence can have an arbitrary length, but each observation // will always have length 3: double[][][] sequences = { new double[][] // first sequence { new double[] { 1, 1, 1 }, // first observation of the first sequence new double[] { 1, 2, 1 }, // second observation of the first sequence new double[] { 1, 4, 2 }, // third observation of the first sequence new double[] { 2, 2, 2 }, // fourth observation of the first sequence }, new double[][] // second sequence (note that this sequence has a different length) { new double[] { 1, 1, 1 }, // first observation of the second sequence new double[] { 1, 5, 6 }, // second observation of the second sequence new double[] { 2, 7, 1 }, // third observation of the second sequence }, new double[][] // third sequence { new double[] { 8, 2, 1 }, // first observation of the third sequence }, new double[][] // fourth sequence { new double[] { 8, 2, 5 }, // first observation of the fourth sequence new double[] { 1, 5, 4 }, // second observation of the fourth sequence } }; // Now, we will also have different class labels associated which each // sequence. We will assign 1 to sequences whose observations start // with { 1, 1, 1 } and +1 to those that do not: int[] outputs = { 0, 0, // First two sequences are of class 0 (those start with {1,1,1}) 1, 1, // Last two sequences are of class 1 (don't start with {1,1,1}) }; // Now we can create the Sequential Minimal Optimization learning algorithm var smo = new SequentialMinimalOptimization<DynamicTimeWarping, double[][]>() { Complexity = 1.5, // Set the parameters of the kernel Kernel = new DynamicTimeWarping(alpha: 1, degree: 1) }; // And use it to learn a machine! var svm = smo.Learn(sequences, outputs); // Now we can compute predicted values bool[] predicted = svm.Decide(sequences); // And check how far we are from the expected values double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.0 // At this point, we should have obtained an useful machine. Let's // see if it can understand a few examples it hasn't seem before: double[][] a = { new double[] { 1, 1, 1 }, new double[] { 7, 2, 5 }, new double[] { 2, 5, 1 }, }; double[][] b = { new double[] { 7, 5, 2 }, new double[] { 4, 2, 5 }, new double[] { 3, 1, 2 }, }; // Following the aforementioned logic, sequence (a) should be // classified as 1, and sequence (b) should be classified as +1. bool resultA = svm.Decide(a); // false bool resultB = svm.Decide(b); // true