Section 1

1 Introduction

A patient takes a tablet. Twice in the next few hours, a blood test shows the same drug level: 4 mg/L. Do the two results mean the same thing?

A drug concentration curve rises to a peak of about 5.3 milligrams per litre at 2 hours, then falls slowly. A dashed line at 4 milligrams per litre cuts the curve twice: at 0.8 hours, where a green arrow shows it rising by 2.8 milligrams per litre per hour, and at 4.4 hours, where a red arrow shows it falling by 0.7 milligrams per litre per hour.

No. At 0.8 hours the drug is still being absorbed, and the level is rising by about 2.8 mg/L every hour. At 4.4 hours the body is clearing the drug, and the level is falling by about 0.7 mg/L every hour. The value tells you where the patient is now. The rate of change tells you where they are going next.

Questions about rates come up all the time in medicine:

  • Drug levels. How fast does the concentration rise after a dose, and how fast is the drug cleared? (mg/L per hour)
  • Tumour growth. How fast is a tumour growing between two scans? (cm³ per month)
  • Heart rate. How fast does it climb at the start of exercise, and how fast does it recover afterwards? (beats per minute, per minute)

The mathematics of change is called calculus. It has two halves. The derivative measures how fast something is changing. That is this lesson. The integral adds up a total, such as the total amount of drug a patient was exposed to. That is the next lesson.

What you will be able to do

  • Work out an average rate of change and give it the right units.
  • Read \(\frac{dy}{dx}\) and \(f'(x)\) aloud, and say what a derivative tells you about a curve.
  • Find a peak, or the best model, by looking for the place where the slope is zero.

With measurements taken at regular times, the simplest look at change is the difference from one measurement to the next. NumPy does this with np.diff:

The same thing in Python
import numpy as np
hr = np.array([72, 95, 118, 132, 140, 144])  # beats/min, once a minute
print(np.diff(hr))   # [23 23 14  8  4]  the change in each minute

The heart rate climbs fast at first (23 beats per minute, each minute), then more and more slowly as it levels off.

Section 2

2 Average rate of change

Start with the simplest question. A patient's drug level is 4.31 mg/L at 4 hours after the tablet, and 2.02 mg/L at 8 hours. On average, how fast did it fall between these two times?

The drug concentration curve with two orange points: 4.31 milligrams per litre at 4 hours and 2.02 at 8 hours. A dashed run of 4 hours and a dashed rise of minus 2.29 milligrams per litre form a triangle. A straight orange line joins the two points. Rise over run, minus 2.29 over 4, gives minus 0.57 milligrams per litre per hour.

Divide the change in concentration by the change in time:

$$\frac{\Delta C}{\Delta t} = \frac{2.02 - 4.31}{8 - 4} = -0.57$$The average rate of change: the change in C divided by the change in t. Δ (the capital Greek letter delta) means "the change in".
  • The average rate of change is the change in the output divided by the change in the input: "rise over run". It is the slope \(m\) of lesson 1.2, for the straight line that joins the two points.
  • A straight line that joins two points on a curve is called a secant line.
  • The sign gives the direction. Here the rate is negative, so the level is falling.
  • The units are the units of the output per unit of the input. Concentration is in mg/L and time is in hours, so the rate is in mg/L per hour.
Try it

Rise over run between two blood tests

Drag either point along the curve, or use the sliders. Watch the sign of the average rate: positive while the drug is absorbed, negative while it is cleared.

An average can hide what happened

Between 0 and 12 hours the average rate is only \((0.91 - 0)/12 \approx 0.08\) mg/L per hour. That small number hides a rise to more than 5 mg/L and a fall back down. The shorter the gap between the two times, the more the average rate tells you about one moment. The next section takes this idea as far as it can go.
The same thing in Python
import numpy as np
def C(t):                                 # mg/L, t hours after the tablet
    return 10 * (np.exp(-0.2 * t) - np.exp(-t))

print(round(C(4), 2), round(C(8), 2))     # 4.31 2.02
rate = (C(8) - C(4)) / (8 - 4)            # rise over run
print(round(rate, 2))                     # -0.57  mg/L per hour
Quick check

A tumour has a volume of 2.0 cm³ at diagnosis and 3.5 cm³ six months later. What is its average rate of growth?

Section 3

3 The derivative: the rate at one moment

An average over 4 hours is useful. But a doctor may want to know how fast the level is changing right now, at one moment. At a single moment there is no gap: the run is 0, and "rise over run" would be \(0/0\), which has no meaning.

The way out is to make the gap small, then smaller still, and watch what happens to the average rate. Take the moment \(t = 1\) hour after the tablet, and a gap of \(h\) hours:

The concentration curve for the first 4 hours with a blue point at 1 hour. An orange line joins it to a second point 2 hours later. The second point slides closer: gaps of 2, 1, 0.5 and 0.1 hours give average slopes of 0.24, 0.84, 1.34 and 1.88. As the gap shrinks to 0 the slope settles on 2.04, and the orange line becomes the tangent. The result is written dC by dt equals 2.04 at t equals 1.
Gap \(h\)Average rate from 1 hour to \(1 + h\) hours
2 hours0.24 mg/L per hour
1 hour0.84 mg/L per hour
0.5 hour1.34 mg/L per hour
0.1 hour1.88 mg/L per hour
0.01 hour2.02 mg/L per hour
closer and closer to 02.04 mg/L per hour

As the gap shrinks, the average rates settle on one number: 2.04 mg/L per hour. This number is the derivative, the rate of change at one moment. In the picture, the secant line turns into the tangent line. The tangent touches the curve at one point and runs in the same direction as the curve there. The derivative is the slope of the tangent.

$$f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}$$Read it as: "f prime of x is the limit, as h goes to 0, of f of x plus h, minus f of x, all over h".

The word limit, \(\lim\), means "the value this settles on as \(h\) gets closer and closer to 0". You will not need to work out limits by hand. You only need the idea: a derivative is rise over run, with a very, very small run.

Papers write the derivative in two ways. You will meet both:

SymbolSay it asWhat it means
\(\Delta y\)delta yThe change in \(y\) between two points.
\(\dfrac{dy}{dx}\)d y by d xThe derivative: how fast \(y\) changes as \(x\) changes.
\(f'(x)\)f prime of xThe derivative of the function \(f\), at the input \(x\).
\(\dfrac{dC}{dt}\)d C by d tHow fast the concentration \(C\) changes with time \(t\).

Common mistake

\(\frac{dy}{dx}\) is not "\(d\) times \(y\), divided by \(d\) times \(x\)". The letter \(d\) means "a tiny change in". Read the whole symbol as one name: the rate of change of \(y\) as \(x\) changes.
Try it

Shrink the gap until the secant becomes the tangent

Make the gap h smaller and smaller. Watch the orange secant line turn into the dashed tangent, and the average rate settle on the derivative. Then try other times t.

Why you will care later

Training a model means changing its weights to make its error smaller. To know which way to move each weight, the computer needs a derivative: how fast the error changes when that weight changes a tiny amount. Every model you fit in Units 5 and 6 uses derivatives in this way.
The same thing in Python
import numpy as np
def C(t):
    return 10 * (np.exp(-0.2 * t) - np.exp(-t))

for h in [1, 0.1, 0.01, 0.001]:              # shrink the gap
    print(h, round((C(1 + h) - C(1)) / h, 3))
# 1 0.841   0.1 1.88   0.01 2.025   0.001 2.04   it settles on 2.04
Quick check

A paper reports \(\dfrac{dV}{dt} = 0.4\) cm³ per month for a tumour at month 6, where \(V\) is the volume and \(t\) is the time. What does this say?

Section 4

4 Reading a derivative

The sign and the size of a derivative tell you the shape of the curve at that point:

DerivativeThe curve isAfter a tablet
positive, \(f'(x) > 0\)risingthe drug is being absorbed
negative, \(f'(x) < 0\)fallingthe drug is being cleared
zero, \(f'(x) = 0\)flat, for a momentthe level is at its peak
far from 0steepthe level changes fast
close to 0nearly flatthe level changes slowly

Every point on a curve has its own slope. So we can work out the slope at every time and draw all of these slopes as a second curve. This new curve is the derivative \(f'\). It is a function in its own right.

Two plots, one above the other. The top plot is blood glucose after a meal: it rises from 5 to a peak of 8 millimoles per litre at 1 hour, then falls back to 5. An orange dot slides along it with its tangent line. The bottom plot draws the slope at each moment: positive while glucose rises (largest at 0.6 hours), zero at the peak, negative while it falls, and close to zero after 2 hours.

The drug after a tablet behaves in the same way. In the absorption phase the slope is positive: the drug enters the blood faster than the body clears it. At the peak, about 2 hours after the tablet, the slope is zero. In the elimination phase the slope is negative. It is most negative at about 4 hours, then moves back towards zero as less and less drug is left.

Try it

A curve and its derivative, side by side

Drag along either plot, or use the slider. The top plot shows the concentration and its tangent. The bottom plot shows the slope of that tangent. Find the time where the slope crosses zero.

The same thing in Python
import numpy as np
t = np.linspace(0, 12, 1201)              # every 0.01 hour
C = 10 * (np.exp(-0.2 * t) - np.exp(-t))
slope = np.gradient(C, t)                 # the slope at every time
# the slopes at 1, 2 and 4 hours:
print(np.round(slope[[100, 200, 400]], 2))   # [ 2.04  0.01 -0.72]
print(t[slope < 0][0])    # 2.02  first time the slope is negative
Quick check

After a meal, the slope of a patient's glucose curve is positive before 1 hour, zero at 1 hour, and negative after 1 hour. What happened at 1 hour?

Section 5

5 A few derivatives worth knowing

Nobody works out derivatives by hand in applied work: software does it for you. But a few results are worth knowing by sight, because they explain how drugs, tumours and models behave.

FunctionDerivativeWhat it says
\(c\)\(0\)A constant does not change, so its rate of change is 0.
\(mx + c\)\(m\)A straight line has the same slope everywhere: its slope \(m\).
\(x^2\)\(2x\)The further from 0, the steeper the curve.
\(e^x\)\(e^x\)The slope always equals the height. This is what makes \(e\) special.
\(C = C_0 e^{-kt}\)\(-kC\)Exponential decay: the slope is \(-k\) times the curve itself. The rate is proportional to the amount that is left.
\(\sigma(z)\)\(\sigma(1 - \sigma)\)The sigmoid is steepest in the middle, at \(z = 0\), where its slope is \(0.5 \times 0.5\), or 0.25.

Two more facts let software build any other derivative from these. Multiplying a function by a number multiplies its slope by the same number. The slope of a sum is the sum of the slopes. For example, the slope of \(3x^2 + 5\) is \(3 \times 2x + 0 = 6x\).

Try it

Check the table: a curve and its slope

Choose a function from the table, then move x. The blue curve is f(x) with its tangent. The orange dashed curve is the derivative: its height at each x is the slope of the blue curve there.

The fifth row deserves a closer look. In lesson 1.2, a drug cleared by the body followed \(C(t) = C_0 e^{-kt}\). Its derivative gives a very short rule:

$$\frac{dC}{dt} = -k\,C$$The rate of elimination is proportional to the amount of drug: twice the drug, twice the rate.
A concentration curve falls from 8 milligrams per litre after an injection. Tangent lines are drawn at three points. A table fills in: at 8 milligrams per litre the rate is minus 1.6 per hour, at 4 it is minus 0.8, at 2 it is minus 0.4. The formula dC by dt equals minus k C appears, with k equal to 0.2 per hour.

This is first-order elimination, and it describes most drugs at normal doses. The body clears a fixed fraction \(k\) of the drug each hour, so the more drug there is, the faster it goes. With \(k = 0.2\) per hour, a level of 8 mg/L falls by 1.6 mg/L per hour, but a level of 4 mg/L falls by only 0.8 mg/L per hour. As the level gets close to zero, so does the rate. This is why the curve never quite reaches zero.

Growth works the same way with a plus sign. Cells that divide freely follow \(\frac{dN}{dt} = +rN\): the more cells there are, the faster their number grows. This is exponential growth, from lesson 1.2.

Try it

First-order elimination: rate = −k × amount

Change the rate constant k and the time t. The orange line is the tangent at time t. Check the third tile: rate divided by concentration is always −k.

Why you will care later

An equation that links a quantity to its own rate of change, such as \(\frac{dC}{dt} = -kC\), is called a differential equation. Models of drug levels (pharmacokinetics), epidemics and tumour growth are built from equations like this. And the slope of the sigmoid, \(\sigma(1-\sigma)\), is used every time a logistic regression or a neural network is trained.
The same thing in Python
import numpy as np
k = 0.2                              # elimination rate constant, per hour
t = np.array([0, 3.47, 6.93])        # 0, 1 and 2 half-lives, hours
C = 8 * np.exp(-k * t)               # mg/L
print(np.round(C, 1))                # [8. 4. 2.]
print(np.round(-k * C, 1))           # [-1.6 -0.8 -0.4]  rate = -kC
Quick check

A drug is cleared by first-order elimination with \(k = 0.1\) per hour. When its concentration is 20 mg/L, how fast is the concentration falling?

Section 6

6 Maxima and minima

Look again at a curve with a peak. Just before the peak, the curve rises and the slope is positive. Just after, it falls and the slope is negative. At the very top, the slope passes through zero. The same happens at the bottom of a valley, the other way round. So:

$$f'(x) = 0$$At a highest point (maximum) or a lowest point (minimum), the slope is zero.

This one fact is behind two ideas that you will meet again and again.

Peak concentration. Pharmacologists sum up a concentration curve by its Cmax, the highest concentration, and its tmax, the time when it happens. For our tablet, the slope is zero at about 2.0 hours, and the level there is 5.35 mg/L. So \(t_{\max} \approx 2.0\) h and \(C_{\max} \approx 5.35\) mg/L.

The best model. A model's loss measures how badly its predictions miss the real values. Suppose we must predict the systolic blood pressure of the five patients from lesson 1.1 (128, 141, 119, 135 and 152 mmHg) with one single number \(m\). A common loss is the mean squared error:

$$L(m) = \frac{1}{n}\sum_{i=1}^{n} (x_i - m)^2$$The average of the squared misses when every patient is predicted to have the value m.

The loss curve is a valley. Its lowest point, where \(\frac{dL}{dm} = 0\), is at \(m = 135\) mmHg, which is exactly the mean of the five patients. The best model sits at the bottom of the loss curve.

Try it

Find the point where the slope is zero

Choose a curve. Then drag the point, or use the slider, until the slope reads 0. For the drug you are looking for the peak; for the loss you are looking for the bottom.

Zero slope: a peak, a valley, or a pause?

A zero slope only tells you that the curve is flat at that point. Check what happens on either side. Rising then falling is a peak. Falling then rising is a valley. A curve can also flatten for a moment and then carry on in the same direction.

Why you will care later

Training a model is a search for the bottom of its loss curve. With one weight, you can look for the place where the slope is zero. A real model has many weights, so its loss is a surface with many directions, not a single curve. Lesson 3.2 shows how slopes work with many inputs, and Unit 6 shows how a computer walks downhill to the bottom.
The same thing in Python
import numpy as np
t = np.linspace(0, 12, 1201)                 # every 0.01 hour
C = 10 * (np.exp(-0.2 * t) - np.exp(-t))
i = np.argmax(C)
print(round(t[i], 2), round(C[i], 2))        # 2.01 5.35   tmax and Cmax

bp = np.array([128, 141, 119, 135, 152])
m = np.linspace(110, 160, 501)               # candidate predictions
loss = ((bp[:, None] - m) ** 2).mean(axis=0) # mean squared error for each m
print(m[np.argmin(loss)], bp.mean())         # 135.0 135.0  same as the mean
Quick check

At the current value of a model's weight \(w\), the loss curve has slope \(\dfrac{dL}{dw} = -3\). To make the loss smaller, what should you do?

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

During 4 minutes of exercise, a patient's heart rate rises from 70 to 150 beats per minute. What is the average rate of change?

Check your understanding

Tumour volume \(V\) is measured in cm³ and time \(t\) in months. What are the units of \(\dfrac{dV}{dt}\)?

Check your understanding

A risk model turns a score \(z\) into a probability with the sigmoid, \(\sigma(z)\). Where does a small change in the score change the risk the most?

Check your understanding

A drug follows first-order elimination. At 12 mg/L its level falls by 1.2 mg/L per hour. How fast does it fall later, when the level is 6 mg/L?

Check your understanding

At \(t_{\max}\), the time of the peak concentration, what is the value of \(\dfrac{dC}{dt}\)?