Skip to content
All apps

Covariance & Regression

About this app

Eight draggable data points — signed rectangles around the means make covariance and the regression slope visible, including correlation sign and magnitude.

Covariance as a sum of areas

Covariance measures whether two variables tend to lie above or below their means together. For n pairs of values, sₓᵧ = Σ(xᵢ − x̄)(yᵢ − ȳ) / (n − 1). Each term is the area of a rectangle between the point and the centroid (x̄, ȳ). If the point lies above right or below left of the centroid, the area is positive, otherwise negative. Dividing covariance by both standard deviations gives the correlation coefficient r = sₓᵧ / (sₓ · sᵧ), which lies between −1 and +1.

What you see

The starting point is a scatter plot of eight measurements, an engine’s speed against its power output. Each point spans a coloured rectangle to the centroid, in one of two colours depending on its sign. The values panel lists x̄, ȳ, sₓ, sᵧ, sₓᵧ, r and the intercept a and slope b of the regression line. Four formulas for covariance, correlation, regression line and slope are shown with the current numbers plugged in. Rectangles, regression line and mean lines can each be toggled.

Things to try

Drag a point from the upper right into the lower right quadrant and watch its rectangle change colour and the covariance drop. Move all points close to a rising line and r approaches 1. Notice that the regression line always passes through the centroid and that its slope is b = sₓᵧ / sₓ². The reset button brings back the original data.

Frequently asked questions

What does covariance tell you?
Covariance indicates the direction of a linear relationship between two variables. If it is positive, high x values tend to go with high y values; if negative, with low ones. Its numerical value depends on the units, so it is not suitable for judging how strong the relationship is.
What is the difference between covariance and correlation?
Correlation is normalised covariance: r = sₓᵧ / (sₓ · sᵧ). This makes r unit-free and always between −1 and +1. Covariance only shows the direction, correlation also the strength of the linear relationship.
How do you calculate the slope of a regression line?
For a least squares regression of y on x, the slope is b = sₓᵧ / sₓ², the covariance divided by the variance of x. The intercept is a = ȳ − b · x̄. The line therefore always passes through the centroid (x̄, ȳ).
What can I try in this app?
You drag eight data points freely with mouse or finger. All statistics and the filled-in formulas update instantly. Rectangles, mean lines and the regression line can be shown or hidden, and a button restores the original data.

Subject: Probability & statistics

More from Probability & statistics

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps