I need to illustrate similarity measures of time series in a paper. Common illustrations are the euclidean distance and dynamic time warping.
We will start with the data for the time series. The two time series Q
and R
will
be stored in the file timeseries.dat
. The first column of the file is the time, the second is
time series Q
and the third is time series R
.
t q r v
1 14 -1
2 0 -5
3 0 1
4 0 5
5 0 5
6 -1 4
7 -2 3
8 -2 2
9 -1 0
10 0 -5
11 4 -9
12 9 -10
13 14 -11
14 15 -11
15 20 -10
16 24 -9
17 25 -8
18 29 -3
19 29 -2
20 29 3
21 27 4
22 23 9
23 22 10
24 17 11
25 15 12
26 13 12
27 12 12
28 11 12
29 11 11
30 12 11
31 16 10
32 16 9
33 17 8
34 18 7
35 19 7
36 19 6
37 17 7
38 12 8
39 8 9
40 4 14
41 2 15
42 0 15
43 -1 15
44 -5 14
45 -6 13
46 -10 9
47 -11 7
48 -17 3
49 -18 1
50 -22 -3
51 -23 -7
52 -28 -12
53 -28 -17
54 -29 -23
55 -30 -24
56 -34 -25
57 -35 -26
58 -40 -26
59 -41 -25
60 -41 -23
61 -42 -22
62 -43 -20
63 -44 -20
64 -45 -21
65 -46 -25
66 -47 -25
67 -48 -29
68 -48 -30
69 -48 -31
70 -48 -37
71 -48 -41
72 -48 -42
73 -48 -48
74 -48 -49
75 -48 -54
76 -48 -55
77 -48 -56
78 -48 -57
79 -48 -57
80 -49 -57
81 -50 -56
82 -51 -55
83 -52 -54
84 -53 -53
85 -54 -51
86 -54 -50
87 -54 -50
88 -54 -50
89 -54 -51
90 -53 -56
91 -52 -57
92 -51 -58
93 -51 -62
94 -51 -62
95 -50 -62
96 -49 -61
The time series data above can be plotted with the following tex file.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=time,
width=\textwidth,
height=\axisdefaultheight,
legend pos=outer north east]
\addplot[blue, mark=none] table[x=t, y=q] {timeseries.dat};
\addlegendentry{Q}
\addplot[red, mark=none] table[x=t, y=r] {timeseries.dat};
\addlegendentry{R}
\end{axis}
\end{tikzpicture}
\end{document}
The euclidean distance between two time series can be illustrated by lines between the two data points to a
given time. We will use Quiver plots frpm PGFPlots to draw those line. Quiver plots draw vectors from a
given starting point into a direction. The starting point is given by x
and y
and
the direction of the vector has to be given by u
and v
. The default for
u
and v
is zero. So we need only the direction of the vector for the
y
axis. Means a new column for our data that contains the difference of r
and
q
.
t q r v
1 14 -1 -15
2 0 -5 -5
3 0 1 1
4 0 5 5
5 0 5 5
6 -1 4 5
7 -2 3 5
8 -2 2 4
9 -1 0 1
10 0 -5 -5
11 4 -9 -13
12 9 -10 -19
13 14 -11 -25
14 15 -11 -26
15 20 -10 -30
16 24 -9 -33
17 25 -8 -33
18 29 -3 -32
19 29 -2 -31
20 29 3 -26
21 27 4 -23
22 23 9 -14
23 22 10 -12
24 17 11 -6
25 15 12 -3
26 13 12 -1
27 12 12 0
28 11 12 1
29 11 11 0
30 12 11 -1
31 16 10 -6
32 16 9 -7
33 17 8 -9
34 18 7 -11
35 19 7 -12
36 19 6 -13
37 17 7 -10
38 12 8 -4
39 8 9 1
40 4 14 10
41 2 15 13
42 0 15 15
43 -1 15 16
44 -5 14 19
45 -6 13 19
46 -10 9 19
47 -11 7 18
48 -17 3 20
49 -18 1 19
50 -22 -3 19
51 -23 -7 16
52 -28 -12 16
53 -28 -17 11
54 -29 -23 6
55 -30 -24 6
56 -34 -25 9
57 -35 -26 9
58 -40 -26 14
59 -41 -25 16
60 -41 -23 18
61 -42 -22 20
62 -43 -20 23
63 -44 -20 24
64 -45 -21 24
65 -46 -25 21
66 -47 -25 22
67 -48 -29 19
68 -48 -30 18
69 -48 -31 17
70 -48 -37 11
71 -48 -41 7
72 -48 -42 6
73 -48 -48 0
74 -48 -49 -1
75 -48 -54 -6
76 -48 -55 -7
77 -48 -56 -8
78 -48 -57 -9
79 -48 -57 -9
80 -49 -57 -8
81 -50 -56 -6
82 -51 -55 -4
83 -52 -54 -2
84 -53 -53 0
85 -54 -51 3
86 -54 -50 4
87 -54 -50 4
88 -54 -50 4
89 -54 -51 3
90 -53 -56 -3
91 -52 -57 -5
92 -51 -58 -7
93 -51 -62 -11
94 -51 -62 -11
95 -50 -62 -12
96 -49 -61 -12
We can plot that with the following tex file.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=time,
width=\textwidth,
height=\axisdefaultheight,
legend pos=outer north east]
\addplot[blue, mark=none] table[x=t, y=q] {timeseries.dat};
\addlegendentry{Q}
\addplot[red, mark=none] table[x=t, y=r] {timeseries.dat};
\addlegendentry{R}
\addplot[gray, quiver={v=\thisrow{v}}] table[x=t, y=q] {timeseries.dat};
\end{axis}
\end{tikzpicture}
\end{document}
Dynamic time warping is a bit more complicated. The time series data will be in a
timeseries.dat
again. But the path will be in a special file path.dat
. A path of
dynamic time warping comes normally as a list of index pairs. For example ((1, 1), (1, 2), (1, 3), (2, 4),
(3, 4), (4, 4)). Means that the first data point of Q will be matched to the first, second and third of R,
the second of Q to the fourth of R, the third of Q to the fourth of R and so on. The first column of the
path file will be the first item of a pair, the second column is the data of R to the second item in the
pair, the third column is the differnce of the second and the first item of a pair and the fourth column is
the difference of the data of R of the second item and the data of Q of the first item of the pair.
t q r v
1 14 -1
2 0 -5
3 0 1
4 0 5
5 0 5
6 -1 4
7 -2 3
8 -2 2
9 -1 0
10 0 -5
11 4 -9
12 9 -10
13 14 -11
14 15 -11
15 20 -10
16 24 -9
17 25 -8
18 29 -3
19 29 -2
20 29 3
21 27 4
22 23 9
23 22 10
24 17 11
25 15 12
26 13 12
27 12 12
28 11 12
29 11 11
30 12 11
31 16 10
32 16 9
33 17 8
34 18 7
35 19 7
36 19 6
37 17 7
38 12 8
39 8 9
40 4 14
41 2 15
42 0 15
43 -1 15
44 -5 14
45 -6 13
46 -10 9
47 -11 7
48 -17 3
49 -18 1
50 -22 -3
51 -23 -7
52 -28 -12
53 -28 -17
54 -29 -23
55 -30 -24
56 -34 -25
57 -35 -26
58 -40 -26
59 -41 -25
60 -41 -23
61 -42 -22
62 -43 -20
63 -44 -20
64 -45 -21
65 -46 -25
66 -47 -25
67 -48 -29
68 -48 -30
69 -48 -31
70 -48 -37
71 -48 -41
72 -48 -42
73 -48 -48
74 -48 -49
75 -48 -54
76 -48 -55
77 -48 -56
78 -48 -57
79 -48 -57
80 -49 -57
81 -50 -56
82 -51 -55
83 -52 -54
84 -53 -53
85 -54 -51
86 -54 -50
87 -54 -50
88 -54 -50
89 -54 -51
90 -53 -56
91 -52 -57
92 -51 -58
93 -51 -62
94 -51 -62
95 -50 -62
96 -49 -61
t r u v
1 14 0 -15
2 0 0 -5
3 0 0 1
4 0 0 5
5 0 0 5
5 0 1 4
5 0 2 3
5 0 3 2
6 -1 3 1
7 -2 3 -3
8 -2 3 -7
8 -2 4 -8
8 -2 5 -9
8 -2 6 -9
8 -2 7 -8
8 -2 8 -7
8 -2 9 -6
9 -1 9 -2
10 0 9 -2
11 4 9 -1
11 4 10 0
12 9 10 0
12 9 11 1
12 9 12 2
12 9 13 3
12 9 14 3
12 9 15 3
12 9 16 3
12 9 17 2
12 9 18 2
12 9 19 1
12 9 20 0
12 9 21 -1
12 9 22 -2
12 9 23 -2
12 9 24 -3
12 9 25 -2
12 9 26 -1
12 9 27 0
13 14 27 0
14 15 27 0
15 20 26 -5
16 24 25 -9
17 25 24 -10
18 29 23 -14
19 29 22 -14
20 29 21 -14
21 27 20 -12
22 23 19 -8
23 22 18 -7
24 17 17 -2
25 15 16 0
26 13 15 2
27 12 14 3
28 11 13 4
29 11 12 4
30 12 11 3
31 16 10 -1
32 16 9 -1
33 17 8 -2
34 18 7 -3
35 19 7 -4
36 19 7 -4
37 17 7 -3
38 12 7 1
39 8 7 1
39 8 8 -1
40 4 8 -1
41 2 7 1
42 0 7 1
43 -1 6 2
44 -5 6 2
45 -6 6 -1
46 -10 6 -2
47 -11 5 -1
48 -17 5 0
49 -18 4 1
50 -22 4 -1
51 -23 3 0
51 -23 4 -1
51 -23 5 -2
51 -23 6 -3
51 -23 7 -3
51 -23 8 -2
51 -23 9 0
51 -23 10 1
51 -23 11 3
51 -23 12 3
51 -23 13 2
52 -28 13 3
53 -28 13 3
54 -29 13 0
55 -30 13 0
56 -34 13 3
57 -35 13 -2
58 -40 13 -1
59 -41 12 0
60 -41 11 0
61 -42 11 0
62 -43 10 1
63 -44 9 2
64 -45 8 3
65 -46 8 -2
66 -47 7 -1
67 -48 6 0
68 -48 5 0
69 -48 4 0
70 -48 3 0
71 -48 2 0
72 -48 1 0
73 -48 0 0
74 -48 -1 0
75 -48 -2 0
76 -48 -3 0
77 -48 -4 0
78 -48 -5 0
79 -48 -6 0
80 -49 -6 0
81 -50 -7 1
82 -51 -8 2
83 -52 -8 -2
84 -53 -9 -1
85 -54 -10 0
86 -54 -11 0
87 -54 -12 0
88 -54 -13 0
89 -54 -14 0
89 -54 -13 -1
89 -54 -12 -2
89 -54 -11 -3
89 -54 -10 -3
89 -54 -9 -3
89 -54 -8 -2
89 -54 -7 -1
89 -54 -6 0
90 -53 -6 0
91 -52 -7 -1
92 -51 -7 0
93 -51 -8 0
94 -51 -9 0
94 -51 -8 1
94 -51 -7 1
94 -51 -6 1
94 -51 -5 0
94 -51 -4 -5
94 -51 -3 -6
94 -51 -2 -7
94 -51 -1 -11
94 -51 0 -11
95 -50 0 -12
96 -49 0 -12
The following tex file will plot the time series with dynamic time warping.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=time,
width=\textwidth,
height=\axisdefaultheight,
legend pos=outer north east]
\addplot[blue, mark=none] table[x=t, y=q] {timeseries.dat};
\addlegendentry{Q}
\addplot[red, mark=none] table[x=t, y=r] {timeseries.dat};
\addlegendentry{R}
\addplot[gray, quiver={u=\thisrow{u}, v=\thisrow{v}}] table[x=t, y=r] {path.dat};
\end{axis}
\end{tikzpicture}
\end{document}