Section 1

1 Introduction

In lesson 3.1 every curve had one input: time. Real models have many inputs. A risk score for heart disease uses age, blood pressure, cholesterol and smoking. A neural network that reads a chest X-ray has millions of weights. To understand such a model, we ask how its output changes when we change one input. That is a partial derivative. Put all of them together and you get the gradient: the arrow that tells a model how to learn.

Sometimes we need the opposite of a rate: a total. An infusion pump gives 50 mg per hour for 2 hours, then 100 mg per hour for 2 more hours. How much drug went in? Rate times time, added up: \(50 \times 2 + 100 \times 2 = 300\) mg. On a graph of the rate, this total is the area under the line.

Try it

A total is an area under a rate

Change the two pump rates. Each shaded block is rate × time, so the total amount of drug given is the shaded area.

When the rate changes all the time, as a drug level does after a tablet, the area under a curve needs the second half of calculus: the integral. It also turns out that every probability for a measurement such as blood pressure is an area under a curve.

What you will be able to do

  • Read \(\frac{\partial f}{\partial x}\) and say what it means to change one input and hold the others fixed.
  • Say which way the gradient points, and why a model steps the other way.
  • Multiply rates along a chain, and know that this is how neural networks learn.
  • Read \(\int_a^b f(x)\,dx\) as an area: a total drug exposure, or a probability.
The same thing in Python
import numpy as np
rate = np.array([50, 50, 100, 100])   # mg per hour, in each of 4 hours
print(np.sum(rate * 1))               # 300  mg: rate x 1 hour, added up
Section 2

2 Partial derivatives

Two blood-pressure drugs, A and B, are given together. The fall in systolic blood pressure, \(R\) (in mmHg), depends on both doses: \(a\) mg of drug A and \(b\) mg of drug B. A function of two inputs, \(R(a, b)\), cannot be drawn as one curve. Instead, we draw a contour map, like a hiking map: each line joins the dose pairs that give the same response.

Now ask: if I give a little more of drug A, and keep drug B the same, how much more does the blood pressure fall? That is a slope in one direction only. It is called a partial derivative:

$$\frac{\partial R}{\partial a} \qquad\text{and}\qquad \frac{\partial R}{\partial b}$$Read: "partial R by partial a". The curly ∂ says that there are other inputs, and they are held fixed.
  • \(\frac{\partial R}{\partial a}\): change the dose of A only. Hold the dose of B fixed.
  • \(\frac{\partial R}{\partial b}\): change the dose of B only. Hold the dose of A fixed.

Each partial derivative is an ordinary slope from lesson 3.1. You walk across the map along a straight line in one direction, and measure how steeply the function rises. The animation does this for a model's loss \(L\), which depends on two weights, \(w_1\) and \(w_2\).

A contour map of a model's loss over two weights, w1 across and w2 up, with rings around the lowest point at w1 = 2, w2 = 1. An orange point at w1 = 3.5, w2 = 2 has a loss of 4.25. Moving only w1, along a horizontal dashed line, the loss rises 3 for each unit: partial L by partial w1 equals 3. Moving only w2, along a vertical dashed line, the loss rises 4 for each unit: partial L by partial w2 equals 4.
Try it

Change one dose, hold the other fixed

Drag the point on the map, or use the sliders. Choose which dose to walk along: the lower plot shows the response along that dashed line only, with its slope. Each map line joins dose pairs with the same fall in blood pressure (in mmHg). The model is made up for teaching.

Notice that the slope for drug A changes a little when you change the dose of drug B. In this made-up model, drug B makes drug A work slightly better. So "how much does one more mg of A help?" has a different answer at different doses of B. This is why a partial derivative always comes with the words "at this point, holding the others fixed".

Why you will care later

In a regression with several inputs, \(y = \beta_0 + \beta_1 x_1 + \beta_2 x_2\), each coefficient is a partial derivative: \(\beta_1 = \frac{\partial y}{\partial x_1}\). This is what a paper means by an effect "adjusted for" the other variables: the change in the outcome when one input goes up by 1 and the others are held fixed.
The same thing in Python
def R(a, b):   # fall in BP (mmHg) for a mg of drug A and b mg of drug B
    return 18 * a / (a + 40) + 12 * b / (b + 6) + 0.004 * a * b

h = 1e-6                                           # a tiny step
print(round(R(50, 10), 1))                         # 19.5  mmHg
print(round((R(50 + h, 10) - R(50, 10)) / h, 3))   # 0.129  dR/da
print(round((R(50, 10 + h) - R(50, 10)) / h, 3))   # 0.481  dR/db
Quick check

A risk model uses age and systolic blood pressure (SBP). For one patient, \(\dfrac{\partial\,\text{risk}}{\partial\,\text{SBP}} = 0.002\) per mmHg. What does this mean?

Section 3

3 The gradient

A function of two inputs has two partial derivatives at each point. Stack them into a vector, as in Unit 2, and you get the gradient:

$$\nabla L = \begin{bmatrix} \partial L / \partial w_1 \\[2pt] \partial L / \partial w_2 \end{bmatrix}$$Read: "nabla L" or "grad L". It has one partial derivative for each input.
The same contour map. At the orange point, an arrow of 3 across and an arrow of 4 up combine into one orange arrow, the gradient, written as the vector 3, 4. Its length is the square root of 3 squared plus 4 squared, which is 5. A green arrow points the opposite way, downhill. A green ball then takes five small steps against the gradient and ends near the lowest point.

The gradient is an arrow on the map. It has three useful properties:

  • It points in the direction in which the function rises fastest: straight uphill.
  • Its length says how steep the slope is in that direction. At the point in the animation, \(\nabla L = (3, 4)\), and its length is \(\sqrt{3^2 + 4^2} = 5\).
  • It crosses the contour lines at a right angle.

The negative gradient, \(-\nabla L\), points the opposite way: straight downhill, towards a lower loss. That is the direction a model wants to move its weights.

Try it

The gradient points uphill; step the other way

Drag the orange point anywhere on the loss map. Blue and purple arrows are the two partial derivatives; the orange arrow is the gradient. Then press the step button a few times and watch the point walk downhill to the lowest loss.

Taking small steps downhill, again and again, is called gradient descent:

$$w_{\text{new}} = w - \alpha\,\nabla L(w)$$New weights = old weights minus a small step size α (the learning rate) times the gradient.

In the widget each step uses \(\alpha = 0.2\). From the start point, the first step goes from \((3.5, 2)\) to \((3.5 - 0.2 \times 3,\; 2 - 0.2 \times 4) = (2.9, 1.2)\). Near the bottom the gradient gets short, so the steps get small by themselves. Unit 6 covers gradient descent in full: how to choose the step size, and what can go wrong.

Why you will care later

A deep-learning model may have millions of weights, so its gradient is a vector with millions of entries. Each entry is still just a partial derivative, and each training step is still a small move against the gradient.
The same thing in Python
import numpy as np
w = np.array([3.5, 2.0])                            # the two weights now
grad = np.array([2 * (w[0] - 2), 4 * (w[1] - 1)])   # the 2 partials
print(grad)                     # [3. 4.]
print(np.linalg.norm(grad))     # 5.0        how steep: the length
print(w - 0.2 * grad)           # [2.9 1.2]  one small step downhill
Quick check

At the current weights, a model's gradient is \(\nabla L = \begin{bmatrix} -2 \\ 6 \end{bmatrix}\). Which way should the two weights move to lower the loss fastest?

Section 4

4 The chain rule

Many effects happen in steps. A larger dose raises the blood concentration of a drug. A higher concentration lowers the blood pressure. Each step has its own rate:

  • \(\frac{dC}{dD}\): how fast the concentration \(C\) rises with the dose \(D\), here 0.2 mg/L per mg;
  • \(\frac{dE}{dC}\): how fast the effect \(E\) (the fall in blood pressure) grows with the concentration, here 3 mmHg per mg/L.

How fast does the effect change with the dose? Follow a change along the chain. Give 10 mg more: the concentration rises by \(10 \times 0.2 = 2\) mg/L, and the effect grows by \(2 \times 3 = 6\) mmHg. That is 6 mmHg for 10 mg, or 0.6 mmHg per mg, which is \(3 \times 0.2\).

Three boxes in a row: dose, concentration and blood pressure fall. The first arrow has a rate of 0.2 mg/L per mg, the second a rate of 3 mmHg per mg/L. A dose increase of 10 mg becomes a rise of 2 mg/L in concentration, which becomes 6 mmHg more fall in blood pressure. The chain rule appears: dE by dD equals dE by dC times dC by dD, which is 3 times 0.2, which is 0.6 mmHg per mg.
$$\frac{dE}{dD} = \frac{dE}{dC} \times \frac{dC}{dD}$$The chain rule: multiply the rates along the chain. The units cancel like fractions: (mmHg per mg/L) × (mg/L per mg) = mmHg per mg.

The rule also works when the steps are curves rather than straight lines. Then each rate is the slope at the current point, and the answer holds for small changes around the current dose.

Try it

Rates multiply along a chain

Change the rate of each step and the change at the start. Watch the change pass along the chain, and check that the overall rate is the product of the two rates. Switch to the neural network view: it is the same rule.

Why you will care later: this is how neural networks learn

A neural network is a long chain. The input feeds a hidden layer, that layer feeds the next, and so on, to the output and then the loss. To find how the loss changes with a weight deep inside the network, the computer multiplies the rates along the chain, starting at the loss and working backwards to the weight. This is called backpropagation. It is the chain rule, used many times over, and it gives every entry of the gradient.
The same thing in Python
def conc(D):   return 0.2 * D             # mg/L for a dose of D mg
def effect(C): return 30 * C / (C + 4)    # BP fall (mmHg), at most 30

D = 20                                    # mg, so C = 4 mg/L
dC_dD = 0.2                               # mg/L per mg
dE_dC = 30 * 4 / (4 + 4) ** 2             # slope of effect at C = 4: 1.875
print(dE_dC * dC_dD)                      # 0.375  chain rule: mmHg per mg

h = 1e-6                                  # check: nudge the dose a tiny bit
print(round((effect(conc(D + h)) - effect(conc(D))) / h, 3))   # 0.375
Quick check

Each mg of a drug raises its blood concentration by 0.5 mg/L. Each mg/L of concentration lowers the heart rate by 4 beats per minute. How much does each extra mg of the drug lower the heart rate?

Section 5

5 The integral as an area

After a tablet, how much drug did the patient's body actually see? Not the peak, and not the level at one time, but the total over the whole day. Pharmacologists measure this as the area under the concentration–time curve, the AUC. Generic medicines are judged against the original product by comparing their AUC (together with Cmax). Some cancer drugs, such as carboplatin, are dosed to reach a target AUC.

How do you find the area under a curve? Cut it into thin vertical slices. Each slice is almost a rectangle: its height is the concentration, and its width is a short time \(\Delta t\). Then add up all the rectangles:

$$\text{AUC} \approx \sum_{i=1}^{n} C(t_i)\,\Delta t$$The Σ from lesson 1.1: add up height × width over all n slices.
The drug concentration curve over 12 hours, filled with rectangles. With 4 slices the rectangles add up to 37.7 milligram hours per litre; with 16 slices, 35.7; with 48 thin slices, 35.5, and the rectangles almost exactly fill the area under the curve. The result is written: the integral from 0 to 12 of C of t, d t, equals 35.5.

This is called a Riemann sum. With thinner slices the rectangles follow the curve more closely, and the sum gets closer to the true area. When the slices become infinitely thin, the sum becomes the integral:

$$\text{AUC} = \int_0^{12} C(t)\,dt \approx 35.5$$Read: "the integral from 0 to 12 of C of t, d t". For our tablet the AUC is about 35.5 mg·h/L.
  • The sign \(\int\) is a stretched S, for "sum".
  • The numbers at the bottom and the top, 0 and 12, say where to start and where to stop, just as with \(\Sigma\).
  • \(dt\) is the width of a very thin slice of time.
  • The units are the units of the height times the units of the width: mg/L × hours = mg·h/L.
Try it

More slices, closer to the true area

Change the number of slices and where each slice takes its height. Watch the sum of the rectangles approach the exact area, 35.46 mg·h/L.

Derivative and integral undo each other

If you add up a rate of change over time, you get the total change. The infusion pump in the introduction is an example: adding up the rate (mg per hour) over 4 hours gave the total amount (mg). The derivative turns a total into a rate; the integral turns a rate back into a total.
The same thing in Python
import numpy as np
t = np.linspace(0, 12, 1201)                 # slices 0.01 hour wide
C = 10 * (np.exp(-0.2 * t) - np.exp(-t))     # mg/L
dt = t[1] - t[0]
auc = np.sum((C[:-1] + C[1:]) / 2 * dt)      # height x width, added up
print(round(auc, 2))                         # 35.46  mg·h/L
# np.trapezoid(C, t) does the same in NumPy 2 (np.trapz in older NumPy)
Quick check

Two tablets of the same drug give the same peak concentration. With tablet B, the level stays near the peak for longer before it falls. Which statement is right?

Section 6

6 Area as probability

The same idea, "an area is a total", has a second use that you will meet in every statistics course. Systolic blood pressure varies from adult to adult. A density curve shows how the values are spread: it is high where many adults are, and low where few are. Here we use illustrative values: a mean of 125 mmHg and a standard deviation of 15 mmHg.

A bell-shaped density curve of systolic blood pressure, centred on 125 mmHg. The whole area under it is shaded and labelled 1, for 100 percent of adults. Then the part between 120 and 140 mmHg is shaded orange and labelled 0.47: 47 percent of adults are between 120 and 140. The result is written: the integral from 120 to 140 of f of x, d x, equals 0.47.
  • The area under the curve between two values is the probability that a person's value lies between them. The area between 120 and 140 mmHg is 0.47, so 47% of adults are in that range.
  • The total area under the whole curve is 1, because every adult has some blood pressure.
  • The height of the curve is not a probability. Only areas are.
$$P(a \le X \le b) = \int_a^b f(x)\,dx$$The probability that X lies between a and b is the area under the density f from a to b.
Try it

Probability is the shaded area

Drag the two orange limits, or use the sliders. The shaded area is the chance that an adult's blood pressure lies between them. The three tiles always add up to 100%.

The ROC AUC is an area too

To judge a diagnostic test or a risk model, papers report the area under the ROC curve (also called the AUC or the c-statistic). It is a different curve from the drug AUC, but it is again an area, and again a probability. It is the chance that a randomly chosen patient with the disease gets a higher score than a randomly chosen patient without it. An AUC of 0.5 is no better than guessing; an AUC of 1 is a perfect test.

Why you will care later

Unit 4 builds on this idea. The normal distribution and the other probability distributions are densities, and every probability you get from them is an area under a curve. Software works out these areas for you.
The same thing in Python
import numpy as np
def f(x):                     # density of systolic BP: mean 125, SD 15
    return np.exp(-0.5 * ((x - 125) / 15) ** 2) / (15 * np.sqrt(2 * np.pi))

def area(a, b, n=20000):      # add up thin trapezoid slices from a to b
    x = np.linspace(a, b, n + 1)
    return np.sum((f(x[:-1]) + f(x[1:])) / 2) * (x[1] - x[0])

print(round(area(120, 140), 3))   # 0.472  probability between 120 and 140
print(round(area(25, 225), 3))    # 1.0    the whole area
Quick check

In a clinic population, the area under the density curve of HbA1c between 6.5% and 8.0% is 0.18. What does this tell you?

Section 7

7 Check your understanding

Five short questions on this lesson. Nothing is graded or saved outside this browser. If you choose a wrong option you get a hint, and you can try again.

Check your understanding

A model predicts LDL cholesterol (mmol/L) as \(\hat{y} = 1.2 + 0.02 \times \text{age} + 0.05 \times \text{BMI}\). What is \(\dfrac{\partial \hat{y}}{\partial\,\text{BMI}}\)?

Check your understanding

At some weights, a loss has gradient \(\nabla L = \begin{bmatrix} 4 \\ -3 \end{bmatrix}\). How steep is it, and which way is downhill?

Check your understanding

In a small neural network, the output changes by 3 for each unit change of a hidden value, and the hidden value changes by 0.5 for each unit change of a weight. How fast does the output change with that weight?

Check your understanding

A drug level is estimated in the middle of four 2-hour slices: 3, 5, 4 and 2 mg/L. What is the Riemann-sum estimate of the AUC from 0 to 8 hours?

Check your understanding

For adult systolic blood pressure, the area under the density between 120 and 140 mmHg is 0.47. What is the probability that an adult is outside this range?