Compute sample or population covariance between two variables. Visualize the relationship with a scatter plot and understand the direction of linear dependence.
Covariance measures the direction of the linear relationship between two variables. A positive covariance means that as X increases, Y tends to increase; a negative covariance means that as X increases, Y tends to decrease. The magnitude depends on the units of measurement, making it difficult to interpret directly – that’s why correlation is often used alongside.
Sample covariance: cov(X,Y) = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{n-1}
Population covariance: cov(X,Y) = \frac{\sum_{i=1}^{n} (x_i - \mu_x)(y_i - \mu_y)}{n}
where:
Because covariance is not scale‑invariant, its numerical value is rarely used alone. The Pearson correlation coefficient r = cov / (σx σy) normalizes the covariance to [‑1, 1] and is much easier to interpret.
Consider five people with heights (cm) [160, 170, 180, 190, 200] and weights (kg) [55, 68, 72, 85, 95]. The sample covariance is positive (≈ 312.5), indicating taller people tend to weigh more. The correlation is ≈ 0.97, showing a strong linear relationship. The scatter plot clearly shows an upward trend.
Always report both covariance and correlation. The covariance matrix is essential in multivariate statistics (e.g., PCA, linear discriminant analysis). For a deeper understanding, consider visualising the data with a scatter plot – provided below each calculation.