Calculate Correlation

Medium
statistics

Compute the Pearson correlation coefficient between two lists of numbers x and y. The formula is:

r = Σ((xi - x̄)(yi - ȳ)) / sqrt(Σ(xi - x̄)² * Σ(yi - ȳ)²)

Return the result rounded to 4 decimal places. You may assume both lists have the same length (>= 2).

Example:

Input:  x = [1, 2, 3, 4, 5], y = [2, 4, 6, 8, 10]
Output: 1.0

Test Cases

Python Editor

Output

Click "Run" to see results...