Skip to content

[BUG] Petitjean barycentre averaging with "ddtw"/"wddtw" returns NaN in the last two time points #3920

Done

Description

@MatthewMiddlehurst

Describe the bug

elastic_barycenter_average(X, distance="ddtw") (default method="petitjean") returns a barycentre whose last two time points are NaN; "wddtw" is the same. As a result TimeSeriesKMeans(distance="ddtw", averaging_method="ba") (and "wddtw") cannot fit. Reproduced on main (5392155), 1.3.0, 1.2.0 and 1.0.0, so this predates the averaging refactor.

Cause. ddtw_alignment_path and wddtw_alignment_path align the derivative series, which are n−2 points long, so path indices run from 0 to n−3. _ba_one_iter_petitjean applies them directly to the original series and barycentre:

for j, k in curr_alignment:
    local_alignments[i, :, k] += curr_ts[:, j] * weights[i]
    local_sums[i, k] += weights[i]
...
return alignment / sum

Positions n−2 and n−1 never get a contribution, so they are 0/0. Derivative point j is centred on original point j+1 (slope_derivative), so the remaining positions are off by one: position k is updated from the pairs aligned to original point k+1.

k-means. Every centre has NaN, so its distance to every series is NaN. argmin(axis=1) returns the NaN column, so all series go to cluster 0 and the other clusters stay empty. Replacing an empty centre with a series does not help, since argmin still picks the NaN column, so fit fails on every init. Every random dataset / n_clusters combination tried failed.

Subgradient and KASBA. _ba_one_iter_subgradient and _kasba_refine_one_iter use the same (j, k) loop. They don't produce NaN because uncovered positions get a zero update, but those positions are never updated: the last two points stay at the initial barycentre (the mean by default), and the off-by-one applies to the rest. Neither update accounts for the derivative transform (it uses b[k] - x[j] on the original values).

The averaging tests use TEST_DISTANCES_WITH_FULL_ALIGNMENT_PATH, which excludes ddtw and wddtw, although both are in VALID_BA_DISTANCE_METHODS.

Steps/Code to reproduce the bug

import numpy as np
from aeon.clustering import TimeSeriesKMeans
from aeon.clustering.averaging import elastic_barycenter_average

X = np.random.default_rng(0).normal(size=(8, 1, 24))

for distance in ["ddtw", "wddtw"]:
    for method in ["petitjean", "subgradient"]:
        b = elastic_barycenter_average(X, distance=distance, method=method)
        print(
            distance,
            method,
            "NaN at:", np.isnan(b).nonzero()[1],
            "last 2 == mean init:", np.allclose(b[:, -2:], X.mean(axis=0)[:, -2:]),
        )

TimeSeriesKMeans(n_clusters=2, distance="ddtw", averaging_method="ba").fit(X)

Expected results

Either a finite barycentre in which every time point is updated from the alignment, for all BA methods, or a clear error saying ddtw/wddtw are not supported for BA.

Actual results

ddtw petitjean NaN at: [22 23] last 2 == mean init: False
ddtw subgradient NaN at: [] last 2 == mean init: True
wddtw petitjean NaN at: [22 23] last 2 == mean init: False
wddtw subgradient NaN at: [] last 2 == mean init: True
  File "aeon/clustering/_k_means.py", line 231, in _fit
    raise ValueError(
ValueError: Unable to find a valid cluster configuration with parameters specified (empty clusters kept forming). Try lowering your n_clusters or raising n_init.

Suggested fix

Either remove them from the valid list, or implement a fix so that the reasonably function. I personally have no suggestion for how to do that, but output from Claude:

  • To keep support instead: map derivative path pairs (j, k) to (j + 1, k + 1) and add the boundary pairs (0, 0) and (n - 1, m - 1), so every position is covered. Doing this where _get_alignment_path handles ddtw/wddtw fixes Petitjean, subgradient and KASBA together. Averaging in the derivative domain is an alternative, but it needs a way back to the original series (the slope derivative loses two degrees of freedom).
    Tests: if removed, check that ddtw/wddtw raise. TEST_DISTANCES_WITH_FULL_ALIGNMENT_PATH (aeon/testing/utils/_distance_parameters.py) would then equal VALID_BA_DISTANCE_METHODS. Its comment, "All the distances that return a full alignment path", is already inaccurate (erp and dtw_gi also return full paths), so it should be updated or the list replaced. If kept, add ddtw/wddtw to the averaging tests with a NaN check.

Versions

Details
System:
    python: 3.13.9 (tags/v3.13.9:8183fa5, Oct 14 2025, 14:09:13) [MSC v.1944 64 bit (AMD64)]
   machine: Windows-11-10.0.26300-SP0
Python dependencies:
         aeon: 1.6.0 (main @ 5392155a6)
          pip: 25.1.1
   setuptools: 82.0.0
 scikit-learn: 1.7.2
        numpy: 2.3.5
        numba: 0.63.1
        scipy: 1.17.0
       pandas: 2.3.3

Activity

  1. added
    bug
    distances
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclusteringdistances

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions