Probability and statistics ยท Part 5 of 5
Estimation and regression
Estimating center and spread from data with the sample mean and variance, then fitting and judging the least-squares regression line.
Probability and statistics8 min read
You weigh a bag of apples at the market and the scale says 1.02 kg. You do not learn the true weight of every apple โ you get one estimate from noisy data. Statistics faces the same situation constantly: a sample of a few numbers must stand in for a whole population, and we need principled rules for turning data into estimates.
The second half of this lesson tackles a related question: if two quantities move together (hours studied and exam score, say), can we draw the straight line that best summarizes the relationship, and use it to predict? The answer is least-squares regression, and it needs only a few sums over the data.
What makes an estimate good
An estimator is a rule that turns a sample into a number meant to approximate some population quantity. The sample mean estimates the population mean ; the sample variance estimates the population variance .
Two properties make an estimator trustworthy. It should be unbiased: averaged over all possible samples, it hits the true value exactly, not systematically above or below. And it should be consistent: with more data, the estimate gets closer to the truth. The sample mean has both properties, which is why averaging is the default way to estimate a typical value.
There is no estimator that works well for everything, but for means and variances the standard choices are so reliable that we rarely think twice. What we do have to think about is how to compute them correctly โ especially variance, where a small detail (dividing by instead of ) is exactly what buys us unbiasedness.
Sample mean and sample variance
For data the sample mean is
The sample variance uses in the denominator:
Dividing by instead of is called Bessel's correction. The deviations are measured from the sample's own center, so they are slightly smaller on average than deviations from the true mean ; dividing by inflates the result just enough to remove that bias. (With large the difference is negligible, but the convention is universal.)
A computationally friendlier form, which avoids computing every deviation separately, is
Example 1. Mean and sample variance of four numbers Find the mean and sample variance of the data .
Compute the mean first.
Use the shortcut form for the sum of squared deviations.
Divide by for the sample variance.
Check: the deviations are , whose squares sum to โ the shortcut agrees.
The idea of least squares
Suppose we have pairs and we want the line that fits them best. "Best" needs a definition, and the standard one is: minimize the sum of squared residuals, where the residual at a point is the vertical gap between the observed and the line's value :
Squaring does two things: it makes positive and negative gaps count the same way, and it penalizes large misses heavily. Minimizing over and is a calculus problem, and its solution is the least-squares regression line.
The slope has a clean interpretation: it is the predicted change in per unit increase in . The intercept anchors the line at . Together they are the two numbers that summarize a linear relationship.
Computing the regression line
Define the two centered sums
measures how and co-vary (it is times the sample covariance), and measures the spread of . Then
The intercept formula says the line always passes through the point of means โ a useful sanity check for any fit you compute.
Example 2. Fitting a line to four points Fit a least-squares line to .
Compute the means.
Compute the centered sums.
Then the slope and intercept.
The fitted line is . Check: at it gives , as it must.
Example 3. Fitting a line and predicting Fit a least-squares line to and predict at .
Compute the means.
Compute the centered sums.
Then the slope and intercept.
The line is , and at it predicts .
Using and judging the fit
Once you have , you can predict for a new , and you can ask how well the line actually fits. Compute the residuals ; small residuals mean a good fit. A standard summary is the coefficient of determination
the fraction of the variation in that the line explains. means all points lie exactly on the line; means the line explains nothing beyond the mean.
Two cautions. First, regression lines are reliable only within the range of the data: predicting at values far outside the sample (extrapolation) can be wildly wrong, because nothing in the data tells you the relationship continues. Second, a good fit shows association, not causation โ the line summarizes how the numbers move together, not why.
Example 4. Judging the quality of a fit For the fit on , compute .
Compute the fitted values and residuals.
Sum the squared residuals.
Sum the squared deviations of from its mean .
Then .
The line explains about of the variation in โ a very strong linear fit.
Common mistakes
- Dividing the sum of squared deviations by when computing a sample variance โ the unbiased sample variance divides by .
- Swapping the roles of and in the slope, or using โ the slope is , with the -spread in the denominator.
- Computing the intercept as instead of โ the intercept is minus times .
- Forgetting the or correction term and using raw sums or as or .
- Extrapolating the regression line far outside the range of the observed values and treating the prediction as reliable.
Practice
- Compute the sample mean of the data .
- Compute the sample variance (dividing by ) of the data .
- Fit the least-squares regression line to the points . What is the slope ?
- A dataset has , , , . Find the intercept of the least-squares regression line.
- Fit the least-squares regression line to the points and use it to predict at .
Answers
- , since .
- , since the squared deviations about the mean are and is their sum divided by .
- , since and .
- , since and .
- , since the fitted line is and .
Least squares finds the best line by minimizing a sum of squares โ the next lesson, Gradient descent, shows a general algorithm for minimizing such functions step by step.