Correlation summarizes how two variables move together. A positive correlation means they tend to rise together; a negative correlation means one tends to fall as the other rises. No correlation means no suggestive relationship. Correlation can motivate questions, decisions, and predictions, but it is only a clue—not proof that one variable causes the other. section 7.1 section 7.5
Temperature and sports-drink sales
Plot temperature against sales and the points show an upward, roughly straight-line trend: warmer days align with higher sales. A scatterplot is a useful first check because it reveals the pattern—and any messiness—behind a single summary number. section 7.1
Table 7.1 Weather temperature vs. sports drink sales
| Temperature (Fahrenheit) |
Sports drink sales (dollars) |
|---|---|
| 51.5 |
100.98 |
| 55.1 |
137.44 |
| 63.0 |
237.8 |
| 71.2 |
254.96 |
| 73.0 |
263.45 |
| 77.1 |
353.89 |
| 88.1 |
450.32 |
| 91.2 |
521.34 |
| 93.0 |
503.13 |
| 101.6 |
573.91 |
Inspect the paired temperature and sales values used to make the scatterplot and calculate their correlation. section 7.1
For linear relationships, the Pearson correlation coefficient, r, scores both direction and strength on a scale from -1 to 1. Values near 1 indicate a strong positive linear pattern; values near -1 a strong negative one; values near 0 indicate little or no linear correlation. Tight, line-like points produce stronger values than widely scattered points. section 7.2
Pearson correlation compares how paired values vary around their means:
- It uses covariance, scaled by the variables’ standard deviations, so the result is standardized as r. section 7.2
- It can be calculated manually or with SciPy’s pearsonr, which also returns a p-value. section 7.2
- It measures linear alignment; a strong curved relationship can exist without being a linear one. section 7.1
from scipy.stats import pearsonr
import numpy as np
x = np.array([ 51.5, 55.1, 63.0, 71.2, 73.0 , 77.1,
88.1, 91.2, 93.0, 101.6])
y = np.array([100.98, 137.44, 237.8 , 254.96, 263.45,
353.89, 450.32, 521.34, 503.13, 573.91])
r = (sum((x - np.mean(x)) * (y - np.mean(y))) / #1
np.sqrt(sum((x-np.mean(x))**2) *
sum((y - np.mean(y))**2))) #1
print(f"Pearson correlation (manual): {r:.4f}")
r, p = pearsonr(x, y) #2
print(f"Pearson correlation (SciPy): {r:.4f}, "
f"p-value: {p:.10f}") #2
Compare a manual Pearson calculation with scipy.stats.pearsonr on the temperature-and-sales data. section 7.2
Treat a correlation as a hypothesis worth investigating, not a verdict. A third variable may drive both measurements, or an apparent pattern may be coincidence. Plot the data, avoid extrapolating beyond its observed range, and use experiments, new data, context, and skepticism when evaluating causal claims. section 7.13 section 7.5
A memorable false lead
Pool drownings and Nicolas Cage film appearances can correlate despite having no meaningful causal connection. The example illustrates why even a high correlation and low p-value need real-world scrutiny. section 7.5















