Skip to content

Instantly share code, notes, and snippets.

@Choumingzhao
Created March 30, 2026 08:34
Show Gist options
  • Select an option

  • Save Choumingzhao/58870c7519503234c3bc95f971f68a4d to your computer and use it in GitHub Desktop.

Select an option

Save Choumingzhao/58870c7519503234c3bc95f971f68a4d to your computer and use it in GitHub Desktop.
Calculate the critical value of Pearson's r for a given alpha and sample size.
import scipy.stats
from math import sqrt
def get_critical_r(alpha, n, two_tailed=True):
"""Calculate the critical value of Pearson's r for a given alpha and sample size.
Args:
alpha: The significance level.
n: The sample size.
two_tailed: Whether to return a two-tailed critical value.
Returns:
The critical value of Pearson's r.
"""
df = n - 2
if two_tailed:
t = scipy.stats.t.ppf(1-alpha/2, df)
else:
t = scipy.stats.t.ppf(1-alpha, df)
return t / sqrt(t**2 + df)
get_critical_r(0.01, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment