Correlation
A curved relationship with zero correlation
Start with five pairs lying on y = x. Every increase in x is matched by an increase in y, so Pearson's r is 1.
Replace y with x². The relationship is still exact, but upward and downward contributions cancel. Follow the centred products below to see why r becomes zero.
Look for the direction and closeness of a straight-line pattern
Each dot is one observation with an x value and a y value. An upward linear pattern gives positive r. A downward linear pattern gives negative r. Points closer to a straight line give r closer to +1 or −1.
Worked comparison
Follow the calculation
What the symbols mean
r measures the direction and strength of a straight-line association. It has no units. It is undefined if either variable has no variation.
Assumptions and full calculation
The slider constructs points with the selected sample correlation. It does not draw a random sample from a population with that correlation. New pattern changes the points while keeping r fixed. Manual input calculates r from your paired observations. A value of zero means no linear association. A curved relationship may still exist. Outliers can change r substantially. Correlation alone cannot establish causation.
How is r calculated? Follow the vectors and their angle
This walkthrough follows the observations in the scatterplot. Start with a small example to see every number, then use the same steps with any data.
These buttons change the actual paired observations above. Reset restores the original example.
1 · Read each column as one vector
Each dot in the scatterplot has an x value and a y value. Read down the x column to make one ordered list, then do the same for y. Each list is a vector. We can represent it as an arrow with one component per observation. Keep the observations in the same order in both lists.
Multiply matching components and add the products to get the dot product. The dot product depends on both vector lengths and the angle between them. The next steps remove the effects of the means and lengths to calculate Pearson's r.
The diagram below represents the two whole lists as arrows. Each arrow has one component per observation. Two arrows from the same origin fit in a shared plane. Drawing them in that plane preserves their angle, even when they have many components.
2 · Measure deviations from each mean
Subtract the x mean from every x value and the y mean from every y value. Call the new vectors a and b. A positive component is above that variable's mean. A negative component is below it.
When both deviations have the same sign, their product is positive. Opposite signs give a negative product. Their sum measures whether the two variables tend to deviate together.
| x | y | a = x − x̄ | b = y − ȳ | a × b |
|---|
Try adding 10 to every y. Its mean also increases by 10, so its deviations stay the same. The angle between the raw vectors can change. The angle between the centred vectors stays the same.
3 · Keep direction and set each length to 1
To find each centred vector's length, square its components, add the squares, then take the square root.
Divide every component by its own vector's length. The resulting unit vectors u and v have length 1. They keep the directions of a and b, so the angle stays the same.
Unit length means the squared components add to 1. It differs from setting the sample standard deviation to 1.
Try multiplying every y by 2. Its centred vector doubles in length. Dividing by that new length returns the same unit vector.
4 · Calculate r from the angle
The dot product of two arrows equals the product of their lengths times the cosine of their angle. When both lengths are 1, the dot product equals the cosine of the angle. For the centred vectors, this is Pearson's r.
The dotted line projects v onto the direction of u. The green segment shows that projection. Its signed length is r. A projection in the direction of u is positive. A projection in the opposite direction is negative.
| Angle | r = cos θ | Directions |
|---|---|---|
| 0° | +1 | Same |
| 60° | +0.5 | Partly aligned |
| 90° | 0 | Perpendicular |
| 120° | −0.5 | Partly opposed |
| 180° | −1 | Opposite |
Adding a constant or multiplying by a positive number preserves r. Multiplying one variable by a negative number reverses its direction and flips the sign of r.
Connect this to the familiar Pearson formula
The numerator is the centred dot product. The denominator is the product of the two centred vector lengths. The formula using sample covariance and sample standard deviations gives the same result. Its n − 1 factors cancel.
The two vectors in their shared plane
We turn the view so the first vector points right. This preserves lengths and the angle between the vectors. The axes here describe vector directions. The scatterplot axes describe measurements. We resize each view to fit, so use the printed lengths to compare steps.
This explanation was inspired by Brendan O'Connor's 2012 article, "Cosine similarity, Pearson correlation, and OLS coefficients".