# Example usage:
# Here is a function I wrote (adapted from the source) which does this:
import numpy as np
import scipy.stats as st
def get_correlation_confidence_interval(correl_coeff, sample_size, confidence):
"""
Description: Function to calculate the confidence interval for a given
correlation score and sample size. Adapted from:
https:
Inputs: correl_coeff, a Pearson correlation coefficient: [-1, 1]
sample_size, the number of values that were used in the correlation
confidence, the desired confidence for the interval. E.g., 95
means that you want the interval that contains the true
correlation with 95% confidence.
Outputs: (lower, upper), a tuple of floats which are the lower and upper
bounds of the confidence interval, rounded to 4 decimals, e.g.:
(0.9456, 0.9678)
"""
# Transform correlation to z scale using the Fisher transformation
z = np.log((1 + correl_coeff) / (1 - correl_coeff)) / 2
# Calculate the standard error of the transformed correlation
standard_err = 1 / np.sqrt(sample_size - 3)
# Obtain the z statistic for the desired confidence level
z_stat = st.norm.ppf(1 - ((1 - (confidence * 0.01)) * 0.5))
# Get the upper and lower bounds of the interval in z space
lower_z = z - z_stat * standard_err
upper_z = z + z_stat * standard_err
# Transform the upper and lower bounds back to correlation scale and limit
# them to 4 decimals
lower = format(round((e**(2 * lower_z) - 1) / (e**(2 * lower_z) + 1), 4), '.4f')
upper = format(round((e**(2 * upper_z) - 1) / (e**(2 * upper_z) + 1), 4), '.4f')
return (lower, upper)