I have a large number of time series data and I need to be able to compare the similarity of the curves to a reference curve. The reference curve in question is shown in red in the figure, and I'd like to compute a similarity metric for the blue curve that tells me that these things are highly similar.

example figure

The reason that I want these things to be similar is that they are both characterized by a largely monotonic rise, followed by a near plateau at some upper value, and then followed by a largely monotonic drop back down to the initial level. I have computed some similarity measures such as the Frechet distance and DTW distances, using the following snippets of code

sim_score = similaritymeasures.frechet_dist(Z, Zref)
sim_score2, _ = fastdtw(Z, Zref, dist=euclidean)

The problem is is that the scores computed using these techniques do not tell me that these curves are similar. For example, the fastdtw score is shown in the title of the figure, and this score is actually quite large when compared to other comparison scores for curves which I would judge by eye to be much more dissimilar. For example, the following curve has roughly half the fastdtw distance, despite it not having as similar of a shape.

second example

The issue seems to be that the temporal overlap is still being weighted much more heavily then more shape-related attributes. Now it's perhaps possible that a more nuanced understanding of what's possible with DTW would give me the outcome I need, perhaps using a more fully-featured DTW package like https://pypi.org/project/dtw-python/, but I'm unfamiliar with many of the options available in that package. Or maybe there's another method that would be better suited. Of course, I could brute force this by creating a piecewise continuous curve with a bunch of variable parameters describing a step-like shape, optimize them against the target curve, and take the best error match as the distance metric. If the optimization doesn't get trapped in a local minima, then this should work, but it's more involved and messier than I'd like to do if I can avoid it.

Would appreciate any other ideas you all might be able to suggest. Thanks in advance!

  • $\begingroup$ Have you tried plain old crosscorrelation? It looks like it should work well with your examples. $\endgroup$ – MBaz Apr 11 '20 at 17:27
  • $\begingroup$ Good point. I actually hadn't looked at that yet. The lags between my two signals are not known in advance, and the duration of each individual plateau region in the signals are not necessarily equal, but playing with that would be a good thing to consider. Thanks! $\endgroup$ – gammapoint Apr 11 '20 at 17:37
  • 1
    $\begingroup$ Cross correlation would give you a peak at a time equal to the delay between the signals, and the strength of the cross correlation relative to the strengths of each signals' autocorrelation should give you a measure of how good the fit is. $\endgroup$ – TimWescott Apr 11 '20 at 18:49
  • 1
    $\begingroup$ do you happen to have a mathematical model for what causes these signals, or what they'll be used for? A winning strategy is often finding the parameters of the model that most likely led to these signals, and then comparing the parameter sets. $\endgroup$ – Marcus Müller Apr 11 '20 at 20:01

While I still don't yet understand all the options in the dtw-python package, it seems as though the relative ordering of my two examples is now as expected using the code snippet

alignment = dtw.dtw(y, yref, keep_internals=True)

This allows me to create the following alignment figure using that package.

enter image description here

Additionally, the relative ordering of my two examples is now as I would hope. The more similar example has a distance metric of 6 whereas the more dissimilar example has a distance metric of ~11. I will have to explore the internals of this package more to see if I could be doing something even better, but this looks promising. Curious what the differences are in implementation that make this ordering so much different from the other two packages used.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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