barrel = [] for index_ in range(len(u2d_adj_close_index) - u2d_split_length): feature_point_index_0, feature_point_index_1 = u2d_adj_close_index[ index_], u2d_adj_close_index[index_ + u2d_split_length] feature_point_values = train_data[ feature_point_index_0:feature_point_index_1] barrel.append(feature_point_values) barrel_length = len(barrel) nn_distance = np.zeros([barrel_length, barrel_length], dtype=np.float) distance_list = [] for i_ in range(barrel_length - 1): for j_ in range(i_ + 1, barrel_length): dist, cost, acc, path = dtw(barrel[i_], barrel[j_], dist=lambda x, y: abs(x - y)) nn_distance[i_, j_] = dist nn_distance[j_, i_] = dist distance_list.append(dist) distance_array = np.array(distance_list) cutoff_position = int(len(distance_array) * cutoff_present) sorted_arr = np.sort(distance_array) cutoff_distance = sorted_arr[cutoff_position] rho = np.zeros(barrel_length, dtype=np.float) for i in range(barrel_length - 1): for j in range(i, barrel_length): dist = np.exp(-(nn_distance[i][j] / cutoff_distance)**2) rho[i] += dist
prices = pd.read_csv("../data/^GSPC.csv") adj_close = prices.iloc[:, 3] index = prices.index start = 0 offset = 100 a1 = adj_close[start: start+offset].values start = 300 a2 = adj_close[start: start+offset].values dist = lambda x, y: np.linalg.norm(np.array(x)-np.array(y), ord=2) # res, path, acc = DTW(a1, a2, fun=dist) import math res, cost, acc, path = dtw(np.array(a1).reshape(-1, 1), np.array(a2).reshape(-1, 1), dist=lambda x, y: abs(x-y)) print("res1", res) dist2, cost2, acc2, path2 = thedtw.dtw(np.array(a1).reshape(-1, 1), np.array(a2).reshape(-1, 1), dist=lambda x, y: np.linalg.norm(x - y, ord=1)) print("res2", dist2) print("acc1") print(acc) print("acc2") print(acc2) print("path1") print(path) print("path2") print(path2)