def test_permutation(): dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 1000) x = dat[:, 0] y = dat[:, 1] stats = two_sample_permutation(x, y, tail=1) assert (stats['mean'] < -2) & (stats['mean'] > -6) & (stats['p'] < .001) stats = one_sample_permutation(x - y, tail=1) assert (stats['mean'] < -2) & (stats['mean'] > -6) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='pearson', tail=1) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='spearman', tail=1) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='kendall', tail=2) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='kendall',tail=3) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='doesntwork',tail=3) s = np.random.normal(0, 1, 10000) two_sided = _calc_pvalue(all_p=s, stat=1.96, tail=2) upper_p = _calc_pvalue(all_p=s, stat=1.96, tail=1) lower_p = _calc_pvalue(all_p=s, stat=-1.96, tail=1) sum_p = upper_p + lower_p np.testing.assert_almost_equal(two_sided, sum_p)
def test_permutation(): dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 1000) x = dat[:, 0] y = dat[:, 1] stats = two_sample_permutation(x, y, tail=1, n_permute=1000) assert (stats['mean'] < -2) & (stats['mean'] > -6) & (stats['p'] < .001) stats = one_sample_permutation(x - y, tail=1, n_permute=1000) assert (stats['mean'] < -2) & (stats['mean'] > -6) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='pearson', tail=1) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='spearman', tail=1) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) stats = correlation_permutation(x, y, metric='kendall', tail=2) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='kendall',tail=3) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='doesntwork',tail=3) s = np.random.normal(0, 1, 10000) two_sided = _calc_pvalue(all_p=s, stat=1.96, tail=2) upper_p = _calc_pvalue(all_p=s, stat=1.96, tail=1) lower_p = _calc_pvalue(all_p=s, stat=-1.96, tail=1) sum_p = upper_p + lower_p np.testing.assert_almost_equal(two_sided, sum_p) # Test matrix_permutation dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 190) x = squareform(dat[:, 0]) y = squareform(dat[:, 1]) stats = matrix_permutation(x, y, n_permute=1000) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) & (stats['p'] < .001) # Test jackknife_permutation dat = np.random.multivariate_normal( [5, 10, 15, 25, 35, 45], [[1, .2, .5, .7, .8, .9], [.2, 1, .4, .1, .1, .1], [.5, .4, 1, .1, .1, .1], [.7, .1, .1, 1, .3, .6], [.8, .1, .1, .3, 1, .5], [.9, .1, .1, .6, .5, 1]], 200) dat = dat + np.random.randn(dat.shape[0], dat.shape[1]) * .5 data1 = pairwise_distances(dat[0:100, :].T, metric='correlation') data2 = pairwise_distances(dat[100:, :].T, metric='correlation') stats = jackknife_permutation(data1, data2) print(stats) assert (stats['correlation'] >= .4) & (stats['correlation'] <= .99) & (stats['p'] <= .05)
def similarity(self, data, plot=False, **kwargs): ''' Calculate similarity between two Adjacency matrices. Default is to use spearman correlation and permutation test.''' if not isinstance(data, Adjacency): data2 = Adjacency(data) else: data2 = data.copy() if self.is_single_matrix: if plot: plot_stacked_adjacency(self, data) return correlation_permutation(self.data, data2.data, **kwargs) else: if plot: _, a = plt.subplots(len(self)) for i in a: plot_stacked_adjacency(self, data, ax=i) return [correlation_permutation(x.data, data2.data, **kwargs) for x in self]
def test_permutation(): dat = np.random.multivariate_normal([2, 6], [[.5, 2], [.5, 3]], 100) x = dat[:, 0] y = dat[:, 1] stats = two_sample_permutation(x, y) assert (stats['mean'] < -2) & (stats['mean'] > -6) assert stats['p'] < .001 print(stats) stats = one_sample_permutation(x - y) assert (stats['mean'] < -2) & (stats['mean'] > -6) assert stats['p'] < .001 print(stats) stats = correlation_permutation(x, y) assert (stats['correlation'] > .4) & (stats['correlation'] < .85) assert stats['p'] < .001 stats = correlation_permutation(x, y, metric='kendall') assert (stats['correlation'] > .4) & (stats['correlation'] < .85) assert stats['p'] < .001
def test_permutation(): dat = np.random.multivariate_normal([2, 6], [[0.5, 2], [0.5, 3]], 1000) x = dat[:, 0] y = dat[:, 1] stats = two_sample_permutation(x, y, tail=1, n_permute=1000) assert (stats["mean"] < -2) & (stats["mean"] > -6) & (stats["p"] < 0.001) stats = one_sample_permutation(x - y, tail=1, n_permute=1000) assert (stats["mean"] < -2) & (stats["mean"] > -6) & (stats["p"] < 0.001) for method in ["permute", "circle_shift", "phase_randomize"]: for metric in ["spearman", "kendall", "pearson"]: stats = correlation_permutation(x, y, metric=metric, method=method, n_permute=500, tail=1) assert ((stats["correlation"] > 0.4) & (stats["correlation"] < 0.85) & (stats["p"] < 0.05)) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='kendall',tail=3) # with pytest.raises(ValueError): # correlation_permutation(x, y, metric='doesntwork',tail=3) s = np.random.normal(0, 1, 10000) two_sided = _calc_pvalue(all_p=s, stat=1.96, tail=2) upper_p = _calc_pvalue(all_p=s, stat=1.96, tail=1) lower_p = _calc_pvalue(all_p=s, stat=-1.96, tail=1) sum_p = upper_p + lower_p np.testing.assert_almost_equal(two_sided, sum_p, decimal=3) # Test matrix_permutation dat = np.random.multivariate_normal([2, 6], [[0.5, 2], [0.5, 3]], 190) x = squareform(dat[:, 0]) y = squareform(dat[:, 1]) stats = matrix_permutation(x, y, n_permute=1000) assert ((stats["correlation"] > 0.4) & (stats["correlation"] < 0.85) & (stats["p"] < 0.001))