• Save
  • Run All Cells
  • Clear All Output
  • Runtime
  • Download
  • Difficulty Rating

Loading Runtime

Create a function called correlation_coefficient.

This function will be passed two lists of numbers of equal length and should return the correlation coefficient of the two lists.

There are many ways to write the equation for the correlation coefficient r. If you have completed the Standard Deviation of a List code challenge as well as the Sample Covariance code challenge, then the following equation for the correlation coefficient of two variables (x and y) will be particularly useful:

Correlation Coefficient Equation

Where:

  • r is the sample correlation coefficient
  • Cov(x,y) is the sample covariance of variables x and y
  • s_x is the sample standard deviation of x
  • s_y is the sample standard deviation of y

From this equation, we see that the correlation coefficient is equivalent to the covariance of the two variables divided by the product of their standard deviations.


Example:

correlation_coef([3,6,8,4,5,9], [8,5,3,0,4,3])

Returns: -0.2998310519154245

Reset Code