# How to cluster data series with different intervals?

I have data, effectively a set of event series, which should cluster well over their overlaps, but have different intervals where the data is available. This can be simplified to overlapping noisy $$sin$$ and $$cos$$ datasets:

SeedRandom[9];
ListPlot[
Table[
With[{offset = RandomChoice[{0, 5}]},
Table[{t, f[t] + RandomReal[]/2},
{t, -5 + offset, 5 + offset, 0.05}]],
{f, RandomChoice[{Sin, Cos}, 15]}]]


It should be intuitively clear that the data clusters according to the function used.

When I try FindClusters on this, it doesn't cluster according to the function, but the offset:

SeedRandom[9];
FindClusters[
Table[
With[{offset = RandomChoice[{0, 5}]},
Table[{t, f[t] + RandomReal[]/2},
{t, -5 + offset, 5 + offset, 0.05}] -> {f, offset}],
{f, RandomChoice[{Sin, Cos}, 15]}],
2]

(* {{{Sin, 5}, {Cos, 5}, {Sin, 5}, {Cos, 5}, {Cos, 5}, {Sin, 5}, {Sin, 5}, {Cos, 5}},
{{Cos, 0}, {Cos, 0}, {Sin, 0}, {Cos, 0}, {Sin, 0}, {Cos, 0}, {Cos, 0}}} *)


This is clearly not what I intended. Also, feeding EventSeries or TimeSeries doesn't seem to work with FindClusters. What to do?

EDIT:

It would seem that if I reformulate my data to be regularly formed with Missing[] values, using "Multinormal" or "GaussianMixture" methods for MissingValueSynthesis produce the expected result, but I have absolutely no idea if this happens for the right reasons:

SeedRandom[9];
FindClusters[
Table[
With[{offset = RandomChoice[{0, 5}]},
Table[
If[-5 + offset <= t <= 5 + offset, f[t] + RandomReal[]/2, Missing[]],
{t, -5, 10, 0.05}] -> {f, offset}],
{f, RandomChoice[{Sin, Cos}, 15]}],
2, MissingValueSynthesis -> "Multinormal"]

(* {{{Sin, 5}, {Sin, 5}, {Sin, 0}, {Sin, 0}, {Sin, 5}, {Sin, 5}},
{{Cos, 0}, {Cos, 0}, {Cos, 5}, {Cos, 5}, {Cos, 0}, {Cos, 0}, {Cos,  5}, {Cos, 0}, {Cos, 5}}} *)


I assume you want to gather all dataset that have the same Cos[x+offset] behavior.

Toward this aim we may define a function that checks if all the y values of a data set are within an interval of some size around the cosine:

test[dat_, off_] := AllTrue[dat, Abs[1.2 Cos[ #[[1]] + off] - #[[2]]] < 1 &]


We create the data sets by:

SeedRandom[9];
dat = Table[
With[{offset = RandomChoice[{0, 5}]},
Table[{t, f[t] + RandomReal[]/2}, {t, offset, offset + 10,
0.05}]], {f, RandomChoice[{Sin, Cos}, 15]}];


To get an idea of the offset, we need to plot the first few datasets:

ListLinePlot[{dat[[1]], dat[[4]]}]


(* The image upload is not working *)

From this we guess offsets of: 0 and -1.3.

to get all datasets with an offset of 0:

off=0;
dat1 = Select[dat, test[#, off] &];


and their position inside dat:

Position[dat, #] & /@ dat1

(* {2, 3, 4, 7, 8, 10, 11, 12, 15} *)


The same for offset:[[1,1]] -1.3

dat2 = Select[dat, test[#, off] &];
Position[dat, #][[1, 1]] & /@ dat2

(* {1, 5, 6, 9, 13, 14} *)


This accounts for all the datasets.