def find_permutation(true, permuted):
"""
Finds the most probable permutation of true time series in between permuted time series
:param true: true ordered time series of shape T times X
:param permuted: Permuted time series of shape P times T. P > K
:return: A dict containing {true idx: permuted idx}
"""
N = true.shape[1]
max_comps = permuted.shape[0]
permutation_dict = {}
used_comps = []
corr_matrix = np.zeros((N, max_comps))
# Find correlations
for i in range(N):
for j in range(max_comps):
corr_matrix[i, j] = np.corrcoef(true[:, i], permuted[j, :])[0, 1]
# Find best order
per_matrix = np.argsort(-np.abs(corr_matrix), axis=1)
for i in range(N):
for j in per_matrix[i, :]:
if j in used_comps:
continue
else:
permutation_dict[i] = j
used_comps.append(j)
break
return permutation_dict
if __name__ == "__main__":
import numpy as np
a = np.array([1, 2, 3, 4.])
b = np.array([4, 8, 9, 12.])
c = np.array([9, 5, 8, 9.])
true = np.vstack([a, b, c]).transpose()
permuted = np.vstack([b*0.2, c*0.5, a*0.7])
print(find_permutation(true, permuted))
# {0: 2, 1: 0, 2: 1}