Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions machine_learning/pearson_correlation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np


def pearson_correlation(data_x: np.ndarray, data_y: np.ndarray) -> float:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/pearson_correlation.py, please provide doctest for the function pearson_correlation

"""
Calculate the Pearson correlation coefficient (PCC) between two arrays.

Pearson correlation measures the linear relationship between two datasets,
returning a value between -1 and 1:
- 1 indicates a perfect positive linear correlation
- 0 indicates no linear correlation
- -1 indicates a perfect negative linear correlation

Formula:
r = Σ((x - mean(x)) * (y - mean(y))) / sqrt(Σ(x - mean(x))^2 * Σ(y - mean(y))^2)

Reference: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

Parameters:
- x: 1D numpy array of values
- y: 1D numpy array of values

Returns:
- The Pearson correlation coefficient (float)

a = np.array([1, 2, 3, 4, 5])
b = np.array([2, 4, 6, 8, 10])
float(np.round(pearson_correlation(a, b), 5))
1.0
a = np.array([1, 2, 3, 4, 5])
b = np.array([10, 9, 2, 6, 4])
float(np.round(pearson_correlation(a, b), 5))
-0.18845
a = np.array([1, 2, 3])
b = np.array([1, 2])
pearson_correlation(a, b)
Traceback (most recent call last):
...
ValueError: Input arrays must have the same length.
"""
if len(data_x) != len(data_y):
raise ValueError("Input arrays must have the same length.")

x_mean = np.mean(data_x)
y_mean = np.mean(data_y)

numerator = np.sum((data_x - x_mean) * (data_y - y_mean))
denominator = np.sqrt(
np.sum((data_x - x_mean) ** 2) * np.sum((data_y - y_mean) ** 2)
)

if denominator == 0:
raise ValueError("Standard deviation of input arrays must not be zero.")

return numerator / denominator