typestar

Correlation and simple regression in Python

Pearson for linear, Spearman for monotonic, linregress for the line.

import numpy as np
from scipy import stats

rng = np.random.default_rng(4)
practice = np.arange(1, 41)
tpm = 70 + practice * 0.8 + rng.normal(0, 4, size=40)

pearson = stats.pearsonr(practice, tpm)
print(round(pearson.statistic, 4), f"{pearson.pvalue:.2e}")
print(round(stats.spearmanr(practice, tpm).statistic, 4))

fit = stats.linregress(practice, tpm)
print(round(fit.slope, 3), round(fit.intercept, 2))
print(round(fit.rvalue ** 2, 4), round(fit.stderr, 4))

How it works

  1. Each test returns the coefficient and a p-value.
  2. Spearman works on ranks, so outliers matter less.
  3. linregress gives slope, intercept and the standard error.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
461
Tokens
151
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 86 seconds.

Type this snippet

Step 1 of 2 in Correlation & resampling, step 8 of 23 in Scientific computing with SciPy.

← Previous Next →