.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_warping/plot_03_soft_dtw.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_03_soft_dtw.py: Soft-DTW: a differentiable alignment and its uncertainty ======================================================== Soft-DTW (Cuturi and Blondel, 2017) replaces the minimum of the DTW recursion by a soft minimum with temperature :math:`\gamma`. The value is differentiable, its gradient with respect to the cost matrix is the *expected alignment* -- a probability map that shows how certain the alignment is -- and barycenters can be computed by gradient descent. The soft-DTW divergence (Blondel et al., 2021) is non-negative and zero only for identical series. .. GENERATED FROM PYTHON SOURCE LINES 13-25 .. 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 t = np.linspace(0, 1, 80) x = np.sin(2 * np.pi * 2 * t) * np.exp(-2 * t) y = random_time_warp(x, strength=0.2, random_state=3) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Expected alignment for three temperatures ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(9, 3.2)) for ax, g in zip(axes, (0.01, 0.1, 1.0), strict=True): value, E = wp.soft_dtw_alignment(x, y, gamma=g) viz.plot_alignment_uncertainty(E, path=wp.dtw_path(x, y).path, ax=ax) ax.set_title(f"$\\gamma$={g}, soft-DTW={value:.2f}") fig.tight_layout() .. image-sg:: /auto_examples/05_warping/images/sphx_glr_plot_03_soft_dtw_001.png :alt: $\gamma$=0.01, soft-DTW=-0.96, $\gamma$=0.1, soft-DTW=-12.31, $\gamma$=1.0, soft-DTW=-132.17 :srcset: /auto_examples/05_warping/images/sphx_glr_plot_03_soft_dtw_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-39 Divergence versus soft-DTW -------------------------- Soft-DTW of a series with itself is negative; the divergence corrects it. .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: Python for g in (0.01, 0.1, 1.0): print( f"gamma {g}: sdtw(x,x)={wp.soft_dtw(x, x, gamma=g):8.3f} " f"divergence(x,y)={wp.soft_dtw_divergence(x, y, g):.3f}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none gamma 0.01: sdtw(x,x)= -1.000 divergence(x,y)=0.051 gamma 0.1: sdtw(x,x)= -12.588 divergence(x,y)=0.274 gamma 1.0: sdtw(x,x)=-133.852 divergence(x,y)=1.676 .. GENERATED FROM PYTHON SOURCE LINES 45-47 Barycenters: DBA and soft-DTW ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-68 .. code-block:: Python rng = np.random.default_rng(0) coll = [ random_time_warp(x, 0.25, random_state=s) + 0.03 * rng.normal(size=80) for s in range(12) ] avg_dba = wp.dba(coll, n_iter=10) avg_soft = wp.soft_dtw_barycenter(coll, gamma=0.1) fig, ax = plt.subplots(figsize=(6, 3)) for c in coll: ax.plot(c, color="0.8", lw=0.8) ax.plot( np.mean(coll, axis=0), color="#999999", lw=1.5, ls="--", label="arithmetic mean", ) ax.plot(avg_dba, color="#285780", lw=2, label="DBA") ax.plot(avg_soft, color="#E69F00", lw=2, label="soft-DTW barycenter") ax.legend(frameon=False) plt.show() .. image-sg:: /auto_examples/05_warping/images/sphx_glr_plot_03_soft_dtw_002.png :alt: plot 03 soft dtw :srcset: /auto_examples/05_warping/images/sphx_glr_plot_03_soft_dtw_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.990 seconds) .. _sphx_glr_download_auto_examples_05_warping_plot_03_soft_dtw.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_03_soft_dtw.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_soft_dtw.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_soft_dtw.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_soft_dtw.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_