Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions aeon/distances/elastic/_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,10 @@ def _msm_independent_cost_matrix(
x_size = x.shape[1]
y_size = y.shape[1]
cost_matrix = np.zeros((x_size, y_size))
distance = 0
min_instances = min(x.shape[0], y.shape[0])
for i in range(min_instances):
curr_cost_matrix = _independent_cost_matrix(x[i], y[i], bounding_matrix, c)
cost_matrix = np.add(cost_matrix, curr_cost_matrix)
distance += curr_cost_matrix[-1, -1]
return cost_matrix


Expand All @@ -267,19 +265,19 @@ def _independent_cost_matrix(
for i in range(1, x_size):
if bounding_matrix[i, 0]:
cost = _cost_independent(x[i], x[i - 1], y[0], c)
cost_matrix[i][0] = cost_matrix[i - 1][0] + cost
cost_matrix[i, 0] = cost_matrix[i - 1, 0] + cost

for i in range(1, y_size):
if bounding_matrix[0, i]:
cost = _cost_independent(y[i], x[0], y[i - 1], c)
cost_matrix[0][i] = cost_matrix[0][i - 1] + cost
for j in range(1, y_size):
if bounding_matrix[0, j]:
cost = _cost_independent(y[j], x[0], y[j - 1], c)
cost_matrix[0, j] = cost_matrix[0, j - 1] + cost

for i in range(1, x_size):
for j in range(1, y_size):
if bounding_matrix[i, j]:
d1 = cost_matrix[i - 1][j - 1] + np.abs(x[i] - y[j])
d2 = cost_matrix[i - 1][j] + _cost_independent(x[i], x[i - 1], y[j], c)
d3 = cost_matrix[i][j - 1] + _cost_independent(y[j], x[i], y[j - 1], c)
d2 = cost_matrix[i - 1, j] + _cost_independent(x[i], x[i - 1], y[j], c)
d3 = cost_matrix[i, j - 1] + _cost_independent(y[j], x[i], y[j - 1], c)

cost_matrix[i, j] = min(d1, d2, d3)

Expand All @@ -292,40 +290,44 @@ def _msm_dependent_cost_matrix(
) -> np.ndarray:
x_size = x.shape[1]
y_size = y.shape[1]

cost_matrix = np.full((x_size, y_size), np.inf)
cost_matrix[0, 0] = np.sum(np.abs(x[:, 0] - y[:, 0]))
cost_matrix[0, 0] = _univariate_squared_distance(x[:, 0], y[:, 0])

for i in range(1, x_size):
if bounding_matrix[i, 0]:
cost = _cost_dependent(x[:, i], x[:, i - 1], y[:, 0], c)
cost_matrix[i][0] = cost_matrix[i - 1][0] + cost
for i in range(1, y_size):
if bounding_matrix[0, i]:
cost = _cost_dependent(y[:, i], x[:, 0], y[:, i - 1], c)
cost_matrix[0][i] = cost_matrix[0][i - 1] + cost
cost_matrix[i, 0] = cost_matrix[i - 1, 0] + cost

for j in range(1, y_size):
if bounding_matrix[0, j]:
cost = _cost_dependent(y[:, j], x[:, 0], y[:, j - 1], c)
cost_matrix[0, j] = cost_matrix[0, j - 1] + cost

for i in range(1, x_size):
for j in range(1, y_size):
if bounding_matrix[i, j]:
d1 = cost_matrix[i - 1][j - 1] + np.sum(np.abs(x[:, i] - y[:, j]))
d2 = cost_matrix[i - 1][j] + _cost_dependent(
d1 = cost_matrix[i - 1, j - 1] + _univariate_squared_distance(
x[:, i], y[:, j]
)
d2 = cost_matrix[i - 1, j] + _cost_dependent(
x[:, i], x[:, i - 1], y[:, j], c
)
d3 = cost_matrix[i][j - 1] + _cost_dependent(
d3 = cost_matrix[i, j - 1] + _cost_dependent(
y[:, j], x[:, i], y[:, j - 1], c
)

cost_matrix[i, j] = min(d1, d2, d3)

return cost_matrix


@njit(cache=True, fastmath=True)
def _cost_dependent(x: np.ndarray, y: np.ndarray, z: np.ndarray, c: float) -> float:
diameter = _univariate_squared_distance(y, z)
mid = (y + z) / 2
mid = (y + z) / 2.0
distance_to_mid = _univariate_squared_distance(mid, x)

if distance_to_mid <= (diameter / 2):
if distance_to_mid <= (diameter / 4.0):
return c
else:
dist_to_q_prev = _univariate_squared_distance(y, x)
Expand Down
4 changes: 2 additions & 2 deletions aeon/distances/elastic/tests/test_distance_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"wddtw": [38144.53125, 19121.4927, 1.34957],
"twe": [4536.0, 3192.0220, 3030.036000000001],
"msm_ind": [1515.0, 1517.8000000000004, 1557.0], # msm with independent distance
"msm_dep": [1897.0, 1898.6000000000001, 1921.0], # msm with dependent distance
"msm_dep": [190547.0, 190549.800000000020, 190589.0], # msm with dependent distance
}
basic_motions_distances = {
"euclidean": 27.51835240,
Expand All @@ -78,7 +78,7 @@
# msm with independent distance
"msm_ind": [84.36021099999999, 140.13788899999997, 262.6939920000001],
# msm with dependent distance
"msm_dep": [33.06825, 71.1408, 190.7397],
"msm_dep": [192.24477562339214, 205.82382238128477, 277.7315058567359],
}


Expand Down
2 changes: 1 addition & 1 deletion aeon/testing/expected_results/expected_distance_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"msm": [
[2.5687181410313746, 28.97170437295012],
[2.5687181410313746, 28.97170437295012],
[2.5687181410313746, 28.657118461088324],
[1.0924085990342982, 13.072037194954508],
[1.8756413986565008, 22.362537814430787],
],
"adtw": [
Expand Down