Aligning shifted series with MP #1051
-
Hi, I'm just coming across this package and it looks great! I'm curious if the Matrix Profile would be a possible solution for aligning shifted time series? Here's an example of what I mean: import numpy as np
import polars as pl
import matplotlib.pyplot as plt
def generate_data():
np.random.seed(42)
timestamps = np.linspace(0, 10, 500)
base_signal = np.sin(2 * np.pi * timestamps)
shifts = [0, 0.5, 1, -0.3]
sensor_ids = [f"sensor_{i}" for i in range(len(shifts))]
data = []
for shift, sensor_id in zip(shifts, sensor_ids):
shift_samples = int(shift * 60)
signal = np.roll(base_signal, shift_samples) + np.random.normal(0, 0.1, len(base_signal))
for ts, val in zip(timestamps, signal):
data.append({"timestamp": ts, "value": val, "sensor_id": sensor_id})
df = pl.DataFrame(data)
return df, timestamps, sensor_ids
def plot_signals(df, title):
plt.figure(figsize = (10, 6))
sensor_ids = df["sensor_id"].unique().to_list()
for sensor_id in sensor_ids:
sensor_data = df.filter(pl.col("sensor_id") == sensor_id)
plt.plot(sensor_data["timestamp"].to_numpy(), sensor_data["value"].to_numpy(), label = sensor_id)
plt.title(title)
plt.xlabel("Timestamp")
plt.ylabel("Value")
plt.legend()
plt.show()
df, timestamps, sensor_ids = generate_data()
plot_signals(df, "Original Time Series") Is it possible to use the matrix profile (perhaps the multivariate |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@joshualeond Thank you for your question and welcome to the STUMPY community. So, I think the answer depends on your data and what you are hoping to accomplish. With this (contrived) example, it's basically a phase shift and so you might be better off computing autocorrelation to determine what kind of shift would give you the best alignment. Matrix profiles would only tell you, locally, where the nearest neighbor is across two time series if you performed an AB-join. Unless your time series experiences some compression/expansion then I'm not sure that DTW would be helpful either. Thus, the answer is really, "it depends" |
Beta Was this translation helpful? Give feedback.
It's a little hard to tell what you are asking but AB-joins are somewhat different from using
stumpy.mass
.For an AB-join, you have time series A, time series B, and a window size
m
. For every sliding window "subsequence" inA
, you are hunting for its one-nearest (subsequence) neighbor inB
. Note that the nearest neighbor can exist ANYWHERE inB
. For example, the first subsequence inA
can have its nearest neighbor located at the END ofB
while the second subsequence inA
(which is shifted over by one index value) can have its nearest neighbor located at the BEGINNING ofB
. In other words, there is no guaranteed "ordering" in the nearest neighbors (they are where they are and we can't mak…