# What distance function is used in \`DeepPumas.cluster\_subjects\` DTW?

**URL:** <https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369>\
**Category:** Uncategorized\
**Created:** [February 8, 2026, 5:29am UTC](https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369 "2026-02-08T05:29:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dom-linkevicius](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/dom-linkevicius/32/446_2.png) [@dom-linkevicius](https://discourse.pumas.ai/u/dom-linkevicius)\
**Post date:** [February 8, 2026, 5:29am UTC](https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369/1 "2026-02-08T05:29:09Z")

</div>

As per title, say I’ve done a

```auto
res = cluster_subjects(pop_df, :dv, 2);

```

and see something like

```auto
julia> res.assignments
10×4 DataFrame
 Row │ subject cluster cost cluster_center 
     │ String Int64 Float64 Int64
─────┼─────────────────────────────────────────────
   1 │ c1403 1 5.25774 7
   2 │ c1406 1 32.7788 7
   3 │ c1409 1 4.9941 7
   4 │ c1411 1 13.8512 7
   5 │ c1413 2 0.0 5
   6 │ c15023 1 6.81329 7
   7 │ c15025 1 0.0 7
   8 │ c15027 2 0.345323 5
   9 │ c15029 1 4.26129 7
  10 │ c15031 1 5.39411 7

```

What is the distance function used in DTW to calculate `cost` between the subject and the medoid? I want to run some analyses on top of this, to validate the clustering (calculate silhouette values, etc.) and wanted to use the same distance function in `DynamicAxistWarping.dtw` that was used in fitting for consistency. Are there any plans to allow the usage of a user-defined DTW cost function in the future?

---

<div class="post-metadata">

**Author:** ![dom-linkevicius](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/dom-linkevicius/32/446_2.png) [@dom-linkevicius](https://discourse.pumas.ai/u/dom-linkevicius)\
**Post date:** [February 9, 2026, 3:25am UTC](https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369/2 "2026-02-09T03:25:31Z")

</div>

While using the `cluster_subjects` functionality another question/point arose (might add more in the future if more things pop up, if you’d rather me create a new post - please let me know). Why does `medoids(ClusterResults)` return an unordered array of medoids? That is, the first entry in it isn’t necessarily the medoid of the first cluster, which is a bit confusing and not what I would’ve expected.

```auto
julia> res
DeepPumas.ClusterResults(10×4 DataFrame
 Row │ subject cluster cost cluster_center 
     │ String Int64 Float64 Int64
─────┼─────────────────────────────────────────────
   1 │ c1403 2 1.44789 10
   2 │ c1406 2 6.48461 10
   3 │ c1409 2 0.910235 10
   4 │ c1411 1 0.0 4
   5 │ c1413 3 0.0 5
   6 │ c15023 1 1.53086 4
   7 │ c15025 2 2.51494 10
   8 │ c15027 3 0.171713 5
   9 │ c15029 2 0.0955006 10
  10 │ c15031 2 0.0 10, 2, true)

julia> medoids(res)
3-element Vector{Int64}:
 10
  4
  5

```

---

<div class="post-metadata">

**Author:** ![LucasMatSP](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/lucasmatsp/32/452_2.png) [@LucasMatSP](https://discourse.pumas.ai/u/LucasMatSP)\
**Post date:** [February 9, 2026, 2:16pm UTC](https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369/3 "2026-02-09T14:16:59Z")

</div>

Hi, Domas.

Answering both questions:

1. The default distance is `SqEuclidean`. You can change it with the `dist` kwarg
2. `medoids(ClusterResults)` calls `unique` in `clusterResults.assignments.cluster_center`, so the vector is ordered as the cluster centers show up in that dataframe’s column. This can be verified by comparing the `cluster_center` column and `medoids(res)` shown in the question

Hope this helps.

Lucas.

---

<div class="post-metadata">

**Author:** ![dom-linkevicius](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/dom-linkevicius/32/446_2.png) [@dom-linkevicius](https://discourse.pumas.ai/u/dom-linkevicius)\
**Post date:** [February 16, 2026, 5:56am UTC](https://discourse.pumas.ai/t/what-distance-function-is-used-in-deeppumas-cluster-subjects-dtw/1369/4 "2026-02-16T05:56:33Z")

</div>

Hi Lucas,

Many thanks for the reply!

1. Ah, the `dist` kwarg didn’t seem to be listed in the docs, many thanks for clarifying!
2. My question was rather not what it does, but why does it do that, as I found it somewhat confusing and counterintuitive that the medoids would not be ordered according to the cluster number (the medoid of the first cluster being the first element in the returned medoids).
