VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-correlation-coefficient/

⇱ Program to Find Correlation Coefficient - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to Find Correlation Coefficient

Last Updated : 21 Mar, 2025

The correlation coefficient is a statistical measure that helps determine the strength and direction of the relationship between two variables. It quantifies how changes in one variable correspond to changes in another. This coefficient, sometimes referred to as the cross-correlation coefficient, always lies between -1 and +1:

  • -1: Strong negative correlation (when one variable increases, the other decreases).
  • 0: No correlation (no relationship between the variables).
  • +1: Strong positive correlation (both variables increase or decrease together).

Formula for Correlation Coefficient

The correlation coefficient (r) is calculated using the formula:

Where:

  • n = Number of data points
  • x, y = Data values of two variables
  • Σxy = Sum of the product of corresponding x and y values
  • Σx², Σy² = Sum of squares of x and y values

Example Calculation

Let's calculate the correlation coefficient for the given dataset:

X

Y

15

25

18

25

21

27

24

31

27

32

ΣX = 105

ΣY = 140

Additional calculations:

X × Y

375

225

625

450

324

625

567

441

729

744

576

961

864

729

1024

Σ(X × Y) = 3000

ΣX² = 2295

ΣY² = 3964

Now, applying the formula:

Example Inputs & Outputs

Example 1

Input:
X = {43, 21, 25, 42, 57, 59}
Y = {99, 65, 79, 75, 87, 81}

Output:
r = 0.529809

Example 2

Input:
X = {15, 18, 21, 24, 27}
Y = {25, 25, 27, 31, 32}

Output:
r = 0.953463

Program to Computing the Correlation Coefficient in Python


Output
0.953463

Complexity Analysis

  • Time Complexity: O(n) where n is the size of the given arrays, as each element is processed once.
  • Auxiliary Space: O(1), since only a few extra variables are used, regardless of input size.

This efficient approach enables quick computation of the correlation coefficient, helping you analyze relationships between datasets. Whether in statistics, finance, or other domains, understanding correlation is essential for data-driven decision-making.

Comment