def simplified_algo(R, iterations, warp=0): S = best_embedding_on_sphere(R, ndim=3) R_order = scale_score(R).astype('int16') for i in range(iterations): # @UnusedVariable C = np.dot(S.T, S) C_sorted = np.sort(C.flat) R = C_sorted[R_order] S = best_embedding_on_sphere(R, ndim=3) if warp > 0: base_D = distances_from_directions(S) diameter = base_D.max() min_ratio = 0.1 max_ratio = np.pi / diameter ratios = np.exp(np.linspace(np.log(min_ratio), np.log(max_ratio), warp)) scores = [] guesses = [] for ratio in ratios: Cwarp = cosines_from_distances(base_D * ratio) Sw = best_embedding_on_sphere(Cwarp, ndim=3) Cw_order = scale_score(cosines_from_directions(Sw)) scores.append(correlation_coefficient(Cw_order, R_order)) guesses.append(Sw) best = np.argmax(scores) print('Best warp: %d (%f)' % (best, ratios[best])) S = guesses[best] return S
def seq(): N = 180 iterations = 10 nradii = 100 radii = np.linspace(5, 180, nradii) K = 1 for radius_deg, i in itertools.product(radii, range(K)): print radius_deg, i # Generate a random symmetric matrix # x = np.random.rand(N, N) S = random_directions_bounded(3, np.radians(radius_deg), N) C = np.dot(S.T, S) alpha = 1 f = lambda x: np.exp(-alpha * (1 - x)) # f = lambda x : x R = f(C) # Normalize in [0,1] R1 = (R - R.min()) / (R.max() - R.min()) # Normalize in [-1,1] R2 = (R1 - 0.5) * 2 S1 = simplified_algo(R1, iterations) S1w = simplified_algo(R1, iterations, warp=50) S2 = simplified_algo(R2, iterations) s1 = spearman(cosines_from_directions(S1), R1) s1w = spearman(cosines_from_directions(S1w), R1) s2 = spearman(cosines_from_directions(S2), R2) e1 = np.degrees(overlap_error_after_orthogonal_transform(S, S1)) e1w = np.degrees(overlap_error_after_orthogonal_transform(S, S1w)) e2 = np.degrees(overlap_error_after_orthogonal_transform(S, S2)) r0 = np.degrees(distribution_radius(S)) r1 = np.degrees(distribution_radius(S1)) r1w = np.degrees(distribution_radius(S1w)) r2 = np.degrees(distribution_radius(S2)) yield dict(R0=r0, R1=r1, R1w=r1w, R2=r2, e1=e1, e2=e2, s1=s1, s2=s2, s1w=s1w, e1w=e1w)