def correlation_p_pearsonr2(data0, data1): r = stats.pearsonr(data0, data1) if math.isnan(r): return float('NaN') if r == 1.: return 0. n = len(data0) assert n == len(data1) # Compute observed t statistic. t = r * math.sqrt((n - 2)/(1 - r**2)) # Compute p-value for two-sided t-test. return 2 * stats.t_cdf(-abs(t), n - 2)
def correlation_p_pearsonr2(data0, data1): r = stats.pearsonr(data0, data1) if math.isnan(r): return float('NaN') if r == 1.: return 0. n = len(data0) assert n == len(data1) # Compute observed t statistic. t = r * math.sqrt((n - 2) / (1 - r**2)) # Compute p-value for two-sided t-test. return 2 * stats.t_cdf(-abs(t), n - 2)
def test_t_cdf(): # Non-positive degrees of freedom should throw an error. with pytest.raises(ValueError): stats.t_cdf(0,0) with pytest.raises(ValueError): stats.t_cdf(2,-10) # CDF of x = 0 should be 0.5. assert relerr(.5, stats.t_cdf(0,12)) < .01 assert relerr(.5, stats.t_cdf(0,6)) < .01 assert relerr(.5, stats.t_cdf(0,130)) < .01 # Test against various reference values. assert relerr(.57484842931039226, stats.t_cdf(.8, .1)) < .05 assert relerr(.64922051214061649, stats.t_cdf(.6, .6)) < .05 assert relerr(.51046281131211058, stats.t_cdf(.1, .05)) < .05 assert relerr(.99999944795492968, stats.t_cdf(9, 12)) < .05 assert relerr(.92318422834700042, stats.t_cdf(1.9, 3)) < .05 assert relerr(.81430689864299455, stats.t_cdf(1, 4.2)) < .05 assert relerr(.99995442539414559, stats.t_cdf(8, 7)) < .05 assert relerr(.92010336338282994, stats.t_cdf(3.9, 1)) < .05 assert relerr(1.0, stats.t_cdf(193, 121)) < .05 assert relerr(.42515157068960779, stats.t_cdf(-.8, .1)) < .05 assert relerr(.35077948785938345, stats.t_cdf(-.6, .6)) < .05 assert relerr(.48953718868788948, stats.t_cdf(-.1, .05)) < .05 assert relerr(.076815771652999562, stats.t_cdf(-1.9, 3)) < .05 assert relerr(.18569310135700545, stats.t_cdf(-1, 4.2)) < .05 assert relerr(.17530833141010374, stats.t_cdf(-1, 7)) < .05 assert relerr(.079896636617170003, stats.t_cdf(-3.9, 1)) < .05 assert relerr(.30899158341328747, stats.t_cdf(-0.5, 121)) < .05 # Test against reference very close to zero. # XXX Why are we testing chi2_sf here? assert relerr(.346437e-4, stats.chi2_sf(193,121)) < .01