.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_warping/plot_01_dtw_basics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_warping_plot_01_dtw_basics.py: Dynamic time warping and global constraints =========================================== DTW (Sakoe and Chiba, 1978) aligns two series by a monotone warping path that minimizes the accumulated local cost. Global constraints -- the Sakoe--Chiba band or the Itakura parallelogram -- limit how far the path may leave the diagonal: they speed up the computation and prevent pathological alignments. Lower bounds (Kim et al., 2001; Keogh, 2002) prune nearest-neighbour searches. .. GENERATED FROM PYTHON SOURCE LINES 12-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import viz from mvtsa import warping as wp from mvtsa.datasets import random_time_warp rng = np.random.default_rng(0) t = np.linspace(0, 1, 120) x = ( np.exp(-(((t - 0.3) / 0.06) ** 2)) - 0.7 * np.exp(-(((t - 0.65) / 0.08) ** 2)) + 0.03 * rng.normal(size=120) ) y = random_time_warp(x, strength=0.25, random_state=1) + 0.03 * rng.normal( size=120 ) .. GENERATED FROM PYTHON SOURCE LINES 32-34 Alignment and cost matrix ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: Python res = wp.dtw_path(x, y) print( f"Euclidean {np.linalg.norm(x - y) ** 2:.2f} | DTW {res.distance:.2f} " f"| path length {len(res.path)}" ) viz.plot_alignment(x, y, res.path, every=3) viz.plot_cost_matrix(x, y, res.path, cost=res.cost_matrix) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_001.png :alt: plot 01 dtw basics :srcset: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_002.png :alt: plot 01 dtw basics :srcset: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Euclidean 1.65 | DTW 0.09 | path length 147
.. GENERATED FROM PYTHON SOURCE LINES 43-45 Global constraints ------------------ .. GENERATED FROM PYTHON SOURCE LINES 45-57 .. code-block:: Python for window in (None, 0.1, 0.05, ("itakura", 2.0)): r = wp.dtw_path(x, y, window=window) print(f"window {window!s:16s}: DTW {r.distance:.3f}") r = wp.dtw_path(x, y, window=0.1) viz.plot_cost_matrix( x, y, r.path, cost=r.cost_matrix, window_mask=wp.window_mask(len(x), len(y), 0.1), ) .. image-sg:: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_003.png :alt: plot 01 dtw basics :srcset: /auto_examples/05_warping/images/sphx_glr_plot_01_dtw_basics_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none window None : DTW 0.088 window 0.1 : DTW 0.088 window 0.05 : DTW 0.094 window ('itakura', 2.0): DTW 0.101
.. GENERATED FROM PYTHON SOURCE LINES 58-60 Lower bounds ------------ .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Python print( f"LB_Kim {wp.lb_kim(x, y):.3f} <= LB_Keogh {wp.lb_keogh(x, y, radius=12):.3f} " f"<= DTW(window=12) {wp.dtw(x, y, window=12):.3f}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none LB_Kim 0.002 <= LB_Keogh 0.006 <= DTW(window=12) 0.088 .. GENERATED FROM PYTHON SOURCE LINES 65-67 Step patterns and penalties --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python for step in ("symmetric1", "symmetric2", "asymmetric"): print(f"{step:11s}: {wp.dtw(x, y, step=step):.3f}") for pen in (0.0, 0.01, 0.05): r = wp.dtw_path(x, y, penalty=pen) print(f"penalty {pen}: path length {len(r.path)}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none symmetric1 : 0.088 symmetric2 : 0.135 asymmetric : 0.052 penalty 0.0: path length 147 penalty 0.01: path length 128 penalty 0.05: path length 124 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.940 seconds) .. _sphx_glr_download_auto_examples_05_warping_plot_01_dtw_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/05_warping/plot_01_dtw_basics.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_dtw_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_dtw_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_dtw_basics.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_