|
| 1 | +""" |
| 2 | +Interdependence Score (IDS) computation. |
| 3 | +Based on the work by Adityanarayanan Radhakrishnan (MIT License) |
| 4 | +Original: https://github.com/aradha/interdependence_scores |
| 5 | +""" |
| 6 | +import numpy as np |
| 7 | +import math |
| 8 | +import sys |
| 9 | +from tqdm import tqdm |
| 10 | + |
| 11 | +SEED = 1717 |
| 12 | +np.random.seed(SEED) |
| 13 | + |
| 14 | +EPSILON = sys.float_info.epsilon |
| 15 | + |
| 16 | +def transform(y, num_terms=6, bandwidth_term=1/2): |
| 17 | + B = bandwidth_term |
| 18 | + exp = np.exp(-B * y**2) |
| 19 | + terms = [] |
| 20 | + for i in range(num_terms): |
| 21 | + terms.append(exp * (y)**i / math.sqrt(math.factorial(i) *1.)) |
| 22 | + y_ = np.concatenate(terms, axis=-1) |
| 23 | + return y_ |
| 24 | + |
| 25 | +def center(X): |
| 26 | + return X - np.mean(X, axis=0, keepdims=True) |
| 27 | + |
| 28 | + |
| 29 | +def compute_p_val(C, X, Y=None, num_terms=6, p_norm='max', n_tests=100, bandwidth_term=1/2): |
| 30 | + |
| 31 | + gt = C |
| 32 | + count = 0 |
| 33 | + |
| 34 | + n, dx = X.shape |
| 35 | + for i in tqdm(range(n_tests)): |
| 36 | + |
| 37 | + # Used to shuffle data |
| 38 | + random_noise = np.random.normal(size=(n, dx)) |
| 39 | + permutations = np.argsort(random_noise, axis=0) |
| 40 | + X_permuted = X[permutations, np.arange(dx)[None, :]] |
| 41 | + |
| 42 | + if Y is not None: |
| 43 | + n, dy = Y.shape |
| 44 | + random_noise = np.random.normal(size=(n, dy)) |
| 45 | + permutations = np.argsort(random_noise, axis=0) |
| 46 | + Y_permuted = Y[permutations, np.arange(dy)[None, :]] |
| 47 | + null = compute_IDS_numpy(X_permuted, Y=Y_permuted, num_terms=num_terms, |
| 48 | + p_norm=p_norm, bandwidth_term=bandwidth_term) |
| 49 | + else: |
| 50 | + null = compute_IDS_numpy(X_permuted, Y=Y, num_terms=num_terms, |
| 51 | + p_norm=p_norm, bandwidth_term=bandwidth_term) |
| 52 | + |
| 53 | + |
| 54 | + count += np.where(null > gt, 1, 0) |
| 55 | + |
| 56 | + p_vals = count / n_tests |
| 57 | + return p_vals |
| 58 | + |
| 59 | + |
| 60 | +def compute_IDS_numpy(X, Y=None, num_terms=6, p_norm='max', |
| 61 | + p_val=False, num_tests=100, bandwidth_term=1/2): |
| 62 | + n, dx = X.shape |
| 63 | + X_t = transform(X, num_terms=num_terms, bandwidth_term=bandwidth_term) |
| 64 | + X_t = center(X_t) |
| 65 | + |
| 66 | + if Y is not None: |
| 67 | + _, dy = Y.shape |
| 68 | + Y_t = transform(Y, num_terms=num_terms, bandwidth_term=bandwidth_term) |
| 69 | + Y_t = center(Y_t) |
| 70 | + cov = X_t.T @ Y_t |
| 71 | + X_std = np.sqrt(np.sum(X_t**2, axis=0)) |
| 72 | + Y_std = np.sqrt(np.sum(Y_t**2, axis=0)) |
| 73 | + correlations = cov / (X_std.reshape(-1, 1) + EPSILON) |
| 74 | + C = correlations / (Y_std.reshape(1, -1) + EPSILON) |
| 75 | + C = C.reshape(num_terms, dx, num_terms, dy) |
| 76 | + else: |
| 77 | + C = np.corrcoef(X_t.T) |
| 78 | + C = C.reshape(num_terms, dx, num_terms, dx) |
| 79 | + |
| 80 | + C = np.nan_to_num(C, nan=0, posinf=0, neginf=0) |
| 81 | + C = np.abs(C) |
| 82 | + |
| 83 | + if p_norm == 'max': |
| 84 | + C = np.amax(C, axis=(0, 2)) |
| 85 | + elif p_norm == 2: |
| 86 | + C = C**2 |
| 87 | + C = np.mean(C, axis=0) |
| 88 | + C = np.mean(C, axis=1) |
| 89 | + C = np.sqrt(C) |
| 90 | + elif p_norm == 1: |
| 91 | + C = np.mean(C, axis=0) |
| 92 | + C = np.mean(C, axis=1) |
| 93 | + |
| 94 | + if p_val: |
| 95 | + p_vals = compute_p_val(C, X, Y=Y, num_terms=num_terms, p_norm=p_norm, |
| 96 | + n_tests=num_tests, bandwidth_term=bandwidth_term) |
| 97 | + return C, p_vals |
| 98 | + else: |
| 99 | + return C |
0 commit comments