Do people who spend more time studying tend to score higher on tests? Answering that isn't about either variable alone, 02-expectation-variance's mean and variance describe study time and test scores separately, it's about whether they move together: when study time is above its own average, is score usually above its own average too? Covariance is the number that answers exactly that question, and correlation is the same idea, rescaled so its size doesn't depend on which units you happened to measure in (hours vs minutes, percent vs raw score).
This exact computation, "do these two variables move together," is what a covariance matrix full of, one entry per pair of features, and PCA (later, in Unsupervised Learning) literally finds its most informative directions by eigendecomposing exactly that matrix.
Theory defines covariance as the average product of each variable's deviation from its own mean, and correlation as covariance divided by both variables' standard deviations. Implement covariance first (reusing the same ddof convention 02-expectation-variance established), then correlation directly in terms of it.
Implement covariance(x, y, ddof=0) and correlation(x, y) against that reasoning. The signatures and docstrings are already in the editor.
x and y are 1D arrays of the same length.covariance(x, x, ddof) must equal 02-expectation-variance's sample_variance(x, ddof), they're the same formula with y set to x.correlation's result must always fall in [-1, 1] (up to floating-point tolerance).Open one at a time. Each gives away a little more than the last.
Covariance is mean((x - mean(x)) * (y - mean(y))), with the same n vs n-ddof divisor choice 02-expectation-variance uses.
correlation doesn't need its own formula from scratch, it's covariance(x, y) divided by np.std(x) * np.std(y).
Click "Run Tests" to test your implementation