Pearson Correlation from Scratch

easy
Unit 1 · Statistics I0/3 to milestone

The Pearson correlation coefficient measures how linearly related two variables are, from −1-1 to 11:

r=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2 ∑i(yi−yˉ)2r = \frac{\sum_i (x_i - \bar x)(y_i - \bar y)}{\sqrt{\sum_i (x_i - \bar x)^2}\,\sqrt{\sum_i (y_i - \bar y)^2}}

Implement pearson(x, y) without using np.corrcoef or any library correlation function. If either input has zero variance, return 0.0.

Example

pearson([1, 2, 3, 4], [2, 4, 6, 8])   # 1.0
#statistics#numpy3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Pearson Correlation from Scratch · TechGradify