Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add (source to target) causality between two 1D time series? (without using numpy tril) #159

Open
bartmch opened this issue Feb 21, 2022 · 1 comment

Comments

@bartmch
Copy link

bartmch commented Feb 21, 2022

Is there a better (computationally cheaper) way to quantify the relationship between a source>target time series instead of setting the lower triangle matrix to inf after computing it?

# These 2 lines are not really important but adding for any recommendations
s1 = zscore(dummy['ets_flow']); s2 = zscore(dummy['return_water_temp'])
s1_diff = preprocessing.differencing(s1.values[None,:], smooth=0.15).squeeze()
s2_diff = preprocessing.differencing(s2.values[None,:], smooth=0.15).squeeze()
# Calculate full matrix
d, paths = dtw.warping_paths(s1_diff, s2_diff, window=30, use_pruning=True)
# Set lower triangle part of the matrix to inf so it will not be considered by best_path
lower = np.tril_indices(paths.shape[0], 0)
paths[lower] = paths[0,-1] #inf
best_path = dtw.best_path(paths)
# Distance is inf?
dtwvis.plot_warpingpaths(s1, s2, paths, best_path)
plt.plot([0, len(paths)], [0, len(paths)],color='white')

image

Besides the computational overhead, I cannot seem to compute the best path if values above the matrix diagonal are set inf:

image

@wannesm
Copy link
Owner

wannesm commented Feb 21, 2022

There is no argument that does just that. But this seems easy to achieve by using a window and pre-/post-pending values to make it a onesided window (and optionally psi-relaxation to avoid the extra penalty from pre-/post-pending values).

# s2 can only map to later timestamps in s1
onesidedwindow = 20
window=int(onesidedwindow/2)
s1b = np.concatenate((np.full((window,), s1[0]), s1))
s2b = np.concatenate((s2, np.full((window,), s2[0])))
d, paths = dtw.warping_paths(s1b, s2b, window=window,
                             psi=(window,0,0,window))
best_path = dtw.best_path(paths)
dtwvis.plot_warpingpaths(s1b, s2b, paths, best_path)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants