Filling the gaps
Data often arrives at a few points only: a reading every hour, a price on a handful of dates. Interpolation estimates the values in between, from the known points either side. The estimate is in the same units as the data.
Extrapolation estimates values beyond the first or the last known point. It is far less reliable: there is no data point on the far side to hold the estimate in check. This lesson does both with straight lines, and measures how far each one can go wrong.
Between two points: the line form
A seedling is 10 cm tall on day 2 and 18 cm tall on day 6. To estimate its height on day 3, join the two points with a straight line and read off the line at 3.
The slope of that line is cm per day (lesson 1.2.1). Day 3 is one day after day 2, so the estimate is cm. Stepping one day at a time gives the table, which lands on the known 18 at day 6.
| day | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| height (cm) | 10 | 12 | 14 | 16 | 18 |
Rule: Linear interpolation
The known points are on the left and on the right, and lies between them. This is the point-and-slope line of lesson 1.2.1: start at the left point and move along the slope towards the right one.
By algebra, day 3 gives , as the table says.
The weight form
The same estimate is an average of the two known values, weighted by how close is to each. Write for the fraction of the gap that has covered: 0 at the left point, 1 at the right. It has no units, because it divides one length by another.
Rule: Weight form
The line form is , and multiplying it out gives this. The weights and add up to 1, and the nearer point gets the larger weight.
Day 3 has covered a quarter of the gap from day 2 to day 6, so . The nearer point, , gets the larger weight, 0.75.
For day 3, , so cm. Day 3 is 1 day from day 2 and 3 days from day 6, so 10 gets three times the weight of 18.
- At a known point the estimate is the data: gives and gives .
- Between the points both weights are positive, so lies between and . Linear interpolation never goes above the higher neighbour or below the lower one.
- Falling data work the same way. A tank holds 40 litres after 1.5 hours and 15 litres after 4 hours. At 2.5 hours, , so litres.
The line form agrees for the tank. Its slope is litres per hour, and one hour after 1.5 that gives .
Try it
lerp is the line form and lerp_weights the weight form. Each line prints both, for day 3, for the tank at 2.5 hours, and at a known point.
Both forms always agree, because they are the same formula rearranged.
Outside the data: extrapolation
Beyond the right point, is above 1; before the left point, it is below 0. The formulas still give a number, but one weight is now negative. Day 8 gives , weights and 1.5, and cm.
A negative weight is the warning sign. The estimate is no longer an average of the data, so nothing holds it in check. The line just carries on, even if the seedling stopped growing on day 7.
Rule: Inside or outside
Inside the gap, the estimate is a weighted average of the two known values. Outside, it is a guess that the straight line continues.
To see how fast the error grows, use a curve whose true values are known. Take at the points and , and compare the line through them with the curve.
| 2 | 3 | 6 | 9 | 16 | |
|---|---|---|---|---|---|
| line | 1.3333 | 1.6667 | 2.6667 | 3.6667 | 6 |
| 1.4142 | 1.7321 | 2.4495 | 3 | 4 | |
| line | 0.2172 | 0.6667 | 2 |
Between 1 and 4 the line is never more than about 0.0833 off. Beyond 4 the error keeps growing: at 16 the line says 6, and the true value is 4.
Try it
The cell prints the table, then the worst error between 1 and 4, found by checking 3,001 points.
round(…, 4) keeps four decimal places.
Many points: a chain of lines
With more than two points, interpolate between each neighbouring pair. Each gap gets its own line, so the result is a chain of straight pieces, called piecewise-linear interpolation. Take the points , , and .
A chain of lines through four points, with a corner at each inner point. Past the last point, two simple rules disagree: holding the end value gives 6 at , continuing the last line gives 8.
The chain passes through every point, with no gaps or jumps. But its slope changes suddenly at each inner point: 2, then , then 2. These sudden changes of direction are corners, and lesson 1.6.2 smooths them away.
The points need not be evenly spaced. To estimate at 3, use the last gap, from 2 to 4, which is 2 wide. Then and .
np.interp
np.interp(x, xs, ys) finds the gap that contains each and interpolates in it. The known inputs xs must increase. In the wrong order it returns a wrong answer with no warning: 1.75 instead of 2.5 at .
Outside the data, np.interp repeats the end value, here 6 for every past 4. That is extrapolation by a flat line, done silently. Pass right=np.nan (and left=np.nan) to get nan, "not a number", instead: any sum that uses it then shows that a value is missing.
Try it
The first line estimates at 0.5, 1.5, 3 and 5; the last is outside the data, so it repeats 6.
Then right=np.nan refuses to extrapolate, and the shuffled inputs give 1.75 at 1.5, where the right answer is 2.5.
Interpolating in another quantity
A straight line is a choice, and you can make it in another quantity: convert the data, interpolate, then convert back. The answer changes, so choose the quantity that changes most steadily between the points.
Logs. A colony of bacteria counts 100 at hour 0 and 400 at hour 2. A straight line gives 250 at hour 1. But a colony grows by a fixed factor each hour (lesson 1.4.2), here 2, so 200 is the better estimate.
Interpolating the logs (lesson 1.4.3) builds that in. Halfway between and is . In weight form, a straight line in the logs becomes a weighted product of the values.
Rule: Interpolating in logs
With as before. Use it for data that grow or shrink by a steady factor. Both values must be positive, because is defined only for positive numbers.
Totals. A cyclist averages 20 km/h over the first 2 hours of a ride and 24 km/h over the first 4. A straight line in the averages gives 22 km/h for the first 3 hours.
Interpolate the distances instead: 40 km after 2 hours and 96 km after 4 give 68 km after 3. That is an average of km/h. It assumes a steady 28 km/h between hours 2 and 4, a natural guess about the gap.
The average is a rate, and the total is the rate times the time. Case study 1.6.4 makes the same choice for interest rates.
Try it
The first line is the colony: straight line in the values, then in the logs. The second is the cyclist: in the averages, then in the totals.
The log answer prints as 200.0000000000001: a tiny rounding error from exp and log.
Common mistakes
- Use the two neighbours of , not the first and last points.
- Divide by the width of the gap. For 3 between 2 and 4, . Forgetting the width gives and the value at 4.
- The weight goes with . The nearer point gets more. Swapped, day 3 gives instead of 12.
- Sort the points for
np.interp. Out of order, it gives 1.75 instead of 2.5 at 1.5, with no warning. - A negative weight means extrapolation. Outside the data
np.interpsilently holds the end value. Any estimate there is a guess. - The quantity matters. Values, logs and totals give different answers, and logs need positive values.
Linear interpolation
0 of 10 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- Interpolation estimates between known points; extrapolation guesses beyond them, and its error can grow without limit.
- Line form: , using the neighbours either side of .
- Weight form: with . The nearer point weighs more; a negative weight means extrapolation.
- Many points give a chain of lines, with a corner at each inner point. Each gap uses its own width.
np.interp(x, xs, ys)needs increasingxs. Outside the data it repeats the end value, unless toldleft=np.nan, right=np.nan.- Interpolating logs, , suits steady growth by a factor; interpolating totals assumes a steady rate in each gap.