The sample covariance between X and Y is calculated as follows:
Here,
And the sample variance of a random variable X is calculated as follows:
So, the Pearson R correlation coefficient between X and Y can be calculated as follows:
How to calculate the Pearson R correlation coefficient using Python?
We can use the pearsonr module from the scipy.stats module in Python to calculate the Pearson R correlation coefficient between two random variables X and Y.
from scipy.stats import pearsonr data1 = [1, 2, 3, 4, 5] data2 = [6, 7, 8, 9, 10] statistic, p_value = pearsonr(data1, data2) print("Pearson R Correlation Coefficient: ", statistic)
Here, the pearsonr() function returns the Pearson R correlation coefficient and the p-value. The output of the above program will be:
Pearson R Correlation Coefficient: 1.0
This value means that data1 and data2 are strongly and positively correlated.






0 Comments