SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 1.6.1 Linear interpolationConcept · 25 min
      2. 1.6.2 Cubic splinesConcept · 30 min
      3. 1.6.3 Practice set: interpolationPractice set · 20 min
      4. 1.6.4 Case study: building a zero curveCase study · 30 min

Level 1, Precalculus 1.6 Interpolation Lesson 1 of 4

ConceptLesson 1.6.1 in Level 1, Precalculus. About 25 minutes.

Linear interpolation

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 18−106−2=2\frac{18 - 10}{6 - 2} = 2 cm per day (lesson 1.2.1). Day 3 is one day after day 2, so the estimate is 10+1×2=1210 + 1 \times 2 = 12 cm. Stepping one day at a time gives the table, which lands on the known 18 at day 6.

day xx23456
height yy (cm)1012141618

Rule: Linear interpolation

y=y0+(x−x0) y1−y0x1−x0y = y_0 + (x - x_0)\,\frac{y_1 - y_0}{x_1 - x_0}

The known points are (x0,y0)(x_0, y_0) on the left and (x1,y1)(x_1, y_1) on the right, and xx 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 y=10+(3−2)×18−106−2=10+1×2=12y = 10 + (3 - 2) \times \frac{18 - 10}{6 - 2} = 10 + 1 \times 2 = 12, as the table says.

The weight form

The same estimate is an average of the two known values, weighted by how close xx is to each. Write tt for the fraction of the gap that xx 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

t=x−x0x1−x0y=(1−t) y0+t y1\begin{gathered} t = \frac{x - x_0}{x_1 - x_0} \\ y = (1 - t)\,y_0 + t\,y_1 \end{gathered}

The line form is y0+t (y1−y0)y_0 + t\,(y_1 - y_0), and multiplying it out gives this. The weights 1−t1 - t and tt 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 t=0.25t = 0.25. The nearer point, (2,10)(2, 10), gets the larger weight, 0.75.

For day 3, t=3−26−2=0.25t = \frac{3 - 2}{6 - 2} = 0.25, so y=0.75×10+0.25×18=12y = 0.75 \times 10 + 0.25 \times 18 = 12 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: t=0t = 0 gives y0y_0 and t=1t = 1 gives y1y_1.
  • Between the points both weights are positive, so yy lies between y0y_0 and y1y_1. 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, t=2.5−1.54−1.5=0.4t = \frac{2.5 - 1.5}{4 - 1.5} = 0.4, so y=0.6×40+0.4×15=30y = 0.6 \times 40 + 0.4 \times 15 = 30 litres.

The line form agrees for the tank. Its slope is 15−404−1.5=−10\frac{15 - 40}{4 - 1.5} = -10 litres per hour, and one hour after 1.5 that gives 40−10=3040 - 10 = 30.

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.

⌘/Ctrl + Enter runs the cell

Outside the data: extrapolation

Beyond the right point, tt 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 t=1.5t = 1.5, weights −0.5-0.5 and 1.5, and y=−0.5×10+1.5×18=22y = -0.5 \times 10 + 1.5 \times 18 = 22 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

0≤t≤1: interpolationt<0 or t>1: extrapolation\begin{gathered} 0 \le t \le 1: \text{ interpolation} \\ t < 0 \text{ or } t > 1: \text{ extrapolation} \end{gathered}

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 y=xy = \sqrt{x} at the points (1,1)(1, 1) and (4,2)(4, 2), and compare the line through them with the curve.

xx236916
line1.33331.66672.66673.66676
x\sqrt{x}1.41421.73212.449534
line −x- \sqrt{x}−0.0809-0.0809−0.0654-0.06540.21720.66672

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.

⌘/Ctrl + Enter runs the cell

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 (0,1)(0, 1), (1,3)(1, 3), (2,2)(2, 2) and (4,6)(4, 6).

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 x=5x = 5, 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 −1-1, 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 t=3−24−2=0.5t = \frac{3 - 2}{4 - 2} = 0.5 and y=0.5×2+0.5×6=4y = 0.5 \times 2 + 0.5 \times 6 = 4.

np.interp

np.interp(x, xs, ys) finds the gap that contains each xx 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 x=1.5x = 1.5.

Outside the data, np.interp repeats the end value, here 6 for every xx 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.

⌘/Ctrl + Enter runs the cell

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 ln⁡100\ln 100 and ln⁡400\ln 400 is ln⁡200\ln 200. In weight form, a straight line in the logs becomes a weighted product of the values.

Rule: Interpolating in logs

y=y0 1−t y1 ty = y_0^{\,1 - t}\, y_1^{\,t}

With tt as before. Use it for data that grow or shrink by a steady factor. Both values must be positive, because ln⁡\ln 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 68÷3≈22.6768 \div 3 \approx 22.67 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.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Use the two neighbours of xx, not the first and last points.
  2. Divide by the width of the gap. For 3 between 2 and 4, t=0.5t = 0.5. Forgetting the width gives t=1t = 1 and the value at 4.
  3. The weight 1−t1 - t goes with y0y_0. The nearer point gets more. Swapped, day 3 gives 0.25×10+0.75×18=160.25 \times 10 + 0.75 \times 18 = 16 instead of 12.
  4. Sort the points for np.interp. Out of order, it gives 1.75 instead of 2.5 at 1.5, with no warning.
  5. A negative weight means extrapolation. Outside the data np.interp silently holds the end value. Any estimate there is a guess.
  6. The quantity matters. Values, logs and totals give different answers, and logs need positive values.

Linear interpolation

0 of 10 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 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, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for 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: y=y0+(x−x0)y1−y0x1−x0y = y_0 + (x - x_0)\frac{y_1 - y_0}{x_1 - x_0}, using the neighbours either side of xx.
  • Weight form: y=(1−t) y0+t y1y = (1 - t)\,y_0 + t\,y_1 with t=x−x0x1−x0t = \frac{x - x_0}{x_1 - x_0}. 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 increasing xs. Outside the data it repeats the end value, unless told left=np.nan, right=np.nan.
  • Interpolating logs, y01−ty1ty_0^{1 - t}y_1^{t}, suits steady growth by a factor; interpolating totals assumes a steady rate in each gap.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.