Pearson Correlation Coefficient

MediumStatisticsMathArray

Description

Given two equal-length non-constant arrays x and y, return the Pearson correlation coefficient, rounded to 4 decimal places.

Examples

Input:x = [1,2,3], y = [4,5,6]
Output:1
Explanation:

Dividing the covariation by the product of the spreads gives a correlation of 1.

Input:x = [1,2,3,4], y = [8,6,4,2]
Output:-1
Explanation:

Dividing the covariation by the product of the spreads gives a correlation of -1.

Input:x = [1,3,2,5], y = [2,5,3,8]
Output:0.9959
Explanation:

Dividing the covariation by the product of the spreads gives a correlation of 0.9959.

Constraints

  • 2 ≤ x.length ≤ 10⁴
  • x and y are not constant

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.