3
$\begingroup$

The Dynamic Time Warping algorithm is implemented in version 11 as WarpingDistance and WarpingCorrespondence. It allows some flexibility in terms of the distance function used -- I'm wondering if it can take a custom distance function in the same way that Nearest can take a user-defined distance. When I try it:

s1 = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6};
s2 = {1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7}; 
f[x1_, x2_] := (x1 - x2)^2;
{n, m} = WarpingCorrespondence[s1, s2, DistanceFunction -> f];

the error is:

WarpingCorrespondence::invdist: The distance function f is not supported.

(I am actually interested in a non-Euclidean distance function, but thought I would keep it simple for the question). Am I somehow misunderstanding how to specify the DistanceFunction, or is there perhaps some workaround?

$\endgroup$
1
  • $\begingroup$ It support some non-Euclidean distance function as its Details and Options.I don't sure your have seem it. $\endgroup$
    – yode
    Commented Apr 17, 2017 at 21:48

1 Answer 1

3
$\begingroup$

As the documentation in Details and Options indicating,any custom DistanceFunction is not allowed:

WarpingCorrespondence accepts a DistanceFunction->d option with settings

We can know Total[Abs[u-v]]/Total[Abs[u+v]] is equivalent to the BrayCurtisDistance totally as its documentation,but regard this example:

WarpingCorrespondence[s1, s2, 
 DistanceFunction -> BrayCurtisDistance]
(* {{1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10, 10, 10, 11, 12, 12, 
  12, 12, 13, 13, 13}, {1, 1, 1, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
  11, 12, 13, 14, 15, 16, 17}} *)
WarpingCorrespondence[s1, s2, 
 DistanceFunction -> 
  Function[{u, v}, Total[Abs[u - v]]/Total[Abs[u + v]]]]
(*Get nothing*)

When we use GeneralUtilities`PrintDefinitions to see the interior code,we can find all trick in the Signal`TimeWarpingDump`iTimeWarping.A important code constrain this behavior

If[
            !MatchQ[distance,
                Alternatives[Automatic,
                    BrayCurtisDistance, CanberraDistance, EuclideanDistance, SquaredEuclideanDistance,
                    NormalizedSquaredEuclideanDistance, BinaryDistance, ManhattanDistance, ChessboardDistance,
                    CosineDistance, CorrelationDistance, MatchingDissimilarity, "DerivativeDistance"
                ]
            ],
            Message[Signal`TimeWarpingDump`h::invdist, distance];
            Return[$Failed]
        ]

We can consider get rid of this limitation to implement our custom distance function.


A workaround

Since it just accept a specified list,we can make use of this feature to implement our custom distance function like this

Block[{BrayCurtisDistance},BrayCurtisDistance[x1_, x2_] := (x1 - x2)^2; 
 WarpingCorrespondence[s1, s2, DistanceFunction -> BrayCurtisDistance]]

{{1,2,3,4,5,6,7,8,8,8,9,10,10,10,11,12,12,12,12,13,13,13},{1,1,1,1,1,2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}}

Of course,you can use other valid function in that list,but I have not test any side effect.use it carefully,please.

$\endgroup$
3
  • $\begingroup$ I am always in awe of people who can get inside Mathematica functions and bend them to your will! $\endgroup$
    – bill s
    Commented Apr 17, 2017 at 23:21
  • $\begingroup$ Hi yode -- I don't think this succeeds in redefining the distance function. For the given s1 and s2, Euclidean distance gives different {n,m} values than Cosine distance. But if I try to redefine BrayCurtis to these two, the answer is always the same. I think the changed definition is not being transferred to the internal routine. $\endgroup$
    – bill s
    Commented Apr 18, 2017 at 0:20
  • $\begingroup$ Yes,actually I have realized that before your comment.But considering Block[{CosineDistance},CosineDistance[u_,v_]:=Total[Abs[u-v]]/Total[Abs[u+v]]; CosineDistance[{a,b,c},{x,y,z}]] work,maybe I miss something. $\endgroup$
    – yode
    Commented Apr 18, 2017 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.