in tseval/qats.py [0:0]
def pearsonr_with_confidence_interval(x, y, alpha=0.05):
''' calculate Pearson correlation along with the confidence interval using scipy and numpy
Parameters
----------
x, y : iterable object such as a list or np.array
Input for correlation calculation
alpha : float
Significance level. 0.05 by default
Returns
-------
r : float
Pearson's correlation coefficient
pval : float
The corresponding p value
lo, hi : float
The lower and upper bound of confidence intervals
'''
r, p = scipy.stats.pearsonr(x, y)
r_z = np.arctanh(r)
se = 1/np.sqrt(len(x) - 3)
z = scipy.stats.norm.ppf(1 - alpha/2)
lo_z, hi_z = r_z - z*se, r_z + z*se
lo, hi = np.tanh((lo_z, hi_z))
return r, p, lo, hi