Index Ai Academy
Phase 2 · Math, when you need it · Calculus

Partial derivatives

Change one input at a time when there are many.

About 25 minutes · A free account opens the whole lesson

Create a free account Try part of it here

Already have an account? Sign in to open this lesson

Why it matters

Most results you care about depend on more than one input. Weekly sales depend on search ads, social ads and emails. A model's error score, called the loss, depends on every one of its weights, and there can be billions of them. A partial derivative asks about one input at a time: if you nudge only this one, how much does the output move? With one answer per input, you know which lever matters most right now. Training asks exactly that question about every weight, on every step.

What you'll be able to do

  • Read functions of several inputs, like conversions from two ad channels
  • Compute a partial derivative by holding the other inputs fixed
  • Estimate partial derivatives numerically, one input at a time
  • Use partial derivatives to decide where the next dollar should go

What it covers

  1. Many inputs, one output
  2. Which slope?
  3. One input at a time
  4. When inputs team up
  5. Estimating partials with nudges
  6. Worked example: where should the next $1,000 go?

Many inputs, one output

Think about baking cookies. How they taste depends on the sugar, the butter and the time in the oven. Change any one of them and the cookies change. Most real results work like this: several inputs go in, one output comes out.

Here's a marketing version. Say your weekly conversions (sales or sign-ups) depend on two ad budgets. Call your search ad spend ss. Call your social ad spend mm. Both are counted in thousands of dollars, so s=4s = 4 means $4,000.

The function f(s,m)f(s, m) gives the conversions for any pair of budgets. Here's a made-up one with a little teamwork built in: people who see your social ads often go and search for you.

f(s,m)=40s+25m+3s mf(s, m) = 40\sqrt{s} + 25\sqrt{m} + 3\sqrt{s\,m}

In plain words: the   \sqrt{\;} sign is the square root. The square root of 9 is 3, because 3 × 3 = 9. Square roots grow fast at first and then more slowly, so the first $1,000 buys more than the tenth. Search earns 40 times the root of its budget. Social earns 25 times the root of its budget. The last part is a bonus for running both at once.

Here's a worked example. Spend $4,000 on search and $1,000 on social, so s=4s = 4 and m=1m = 1. The roots are 4=2\sqrt{4} = 2, 1=1\sqrt{1} = 1 and 4×1=2\sqrt{4 \times 1} = 2. So the conversions are 40 × 2 + 25 × 1 + 3 × 2 = 80 + 25 + 6 = 111.

Quick check
This is the same calculation in Python. What does it print?
import math

s, m = 4, 1
print(40 * math.sqrt(s) + 25 * math.sqrt(m) + 3 * math.sqrt(s * m))
  1. 111.0
  2. 191.0
  3. 111
  4. 105.0
Show the answer

111.0

80 + 25 + 6 = 111. math.sqrt always returns a decimal number, so Python prints 111.0. Leaving out the bonus gives 105, and forgetting the first square root gives 191.

Which slope?

Picture ff as a landscape. Lay the two budgets out on the ground: search runs one way, social runs the other. Above each spot, the ground rises to the number of conversions. You get a hilly surface.

Now stand somewhere on a hill. How steep is it? That depends on which way you walk. Heading east might be steep, and heading north might be gentle. So a surface has no single slope. You have to say which direction you mean.

Try it now

Move y and watch the slope in x change. That's what it means for two inputs to team up.

One input at a time

The simplest directions run straight along one budget. You change search spend and leave social alone, or the other way around. Think of a sound mixing board: you move one slider and keep your hands off the rest.

The slope in one of those directions is a partial derivative. It's the derivative you already know, with one extra rule: vary one input and hold all the others fixed. It's written with a curly d, ∂\partial, usually read "partial".

∂f∂s"how fast f changes as s changes, with m held fixed"\frac{\partial f}{\partial s} \quad \text{"how fast } f \text{ changes as } s \text{ changes, with } m \text{ held fixed"}

In plain words: read ∂f/∂s\partial f / \partial s as "partial f by partial s". It's roughly how many extra conversions one more $1,000 of search would bring, while social spend stays where it is.

Working one out by hand

To find a partial derivative, pretend the other inputs are ordinary fixed numbers. Then use the slope rules you already have. You need three of them:

  • x2x^2 has slope 2x2x;
  • a number times xx, like 5x5x, has slope 5;
  • a number on its own, like 7, has slope 0, because it never changes.

The derivative rules lesson covers these. If you skipped it, the nudge method from the derivatives lesson confirms each one.

Take a simpler function named gg. Its first input is xx. Its second input is yy.

g(x,y)=x2+3xy+y2g(x, y) = x^2 + 3xy + y^2

In plain words: gg adds up three parts. The first is xx times itself. The second is 3 times xx times yy. The third is yy times itself. At x=1x = 1 and y=2y = 2, that's 1 + 6 + 4 = 11.

Quick check
To find the partial derivative with respect to xx, what do you do with yy?
  1. Differentiate it too
  2. Set it to zero
  3. Remove it from the formula
  4. Treat it as a fixed number
Show the answer

Treat it as a fixed number

You ask how the output changes when only x moves. So y sits still, like any other number.

Hold yy fixed at 2. Then gg becomes x2+6x+4x^2 + 6x + 4. That's an ordinary function of one input. Its slope is 2x+62x + 6, and the 6 there is 3 times yy. Keep yy as a letter instead of 2, and you get the general answer. Holding xx fixed instead gives the other partial:

∂g∂x=2x+3y∂g∂y=3x+2y\frac{\partial g}{\partial x} = 2x + 3y \qquad \frac{\partial g}{\partial y} = 3x + 2y

In plain words: for ∂g/∂x\partial g / \partial x, treat yy as a fixed number. The x2x^2 term gives 2x2x. The 3xy3xy term is "3y3y times xx", so it gives 3y3y. The y2y^2 term doesn't move when only xx moves, so it gives 0. For ∂g/∂y\partial g / \partial y, swap the roles.

Worked with small numbers, at x=1x = 1 and y=2y = 2:

  • ∂g/∂x=2×1+3×2=8\partial g / \partial x = 2 \times 1 + 3 \times 2 = 8;
  • ∂g/∂y=3×1+2×2=7\partial g / \partial y = 3 \times 1 + 2 \times 2 = 7.

At this point, nudging xx moves gg a little more than nudging yy does. You'll confirm both numbers with Python further down.

Go deeper Checking the 3xy term with a nudge

Hold y=2y = 2 and start at x=1x = 1. The term 3xy3xy is 3×1×2=63 \times 1 \times 2 = 6. Nudge xx up by a small amount hh. The term becomes 3(1+h)×2=6+6h3(1 + h) \times 2 = 6 + 6h.

So the output grew by 6h6h. Divide by the nudge hh and the slope is 6, which is 3y3y. The y2y^2 term stays at 4 the whole time, so its slope is 0.

Quick check
For f(x, y) = 5x + xy², what is ∂f/∂y?
  1. y²
  2. 5x + 2y
  3. 5 + y²
  4. 2xy
Show the answer

2xy

Hold x fixed. Then 5x is a constant, so its slope is 0. And xy² is "x times y²", which gives x · 2y = 2xy.

When inputs team up

Look again at ∂g/∂x=2x+3y\partial g / \partial x = 2x + 3y. It contains yy. So how much xx matters depends on where yy is. That's called an interaction.

You know interactions from everyday life. An extra bag of chips is worth more when there's also salsa on the table. In marketing terms, the return on search spend depends on how much you spend on social.

Compare a function with no teamwork, such as k(x,y)=4x+7yk(x, y) = 4x + 7y. Here ∂k/∂x=4\partial k / \partial x = 4, whatever yy is. Each input does its own job, and neither changes the other's effect.

Slices of the surface

Go back to the conversions landscape. Fix social spend mm at one value, and ff becomes an ordinary curve in ss. It's like slicing a loaf of bread: each slice shows the shape at one social budget. The partial derivative ∂f/∂s\partial f / \partial s is that curve's slope.

Different values of mm give different slices, with different slopes. The code below draws three slices: social at $1,000, $5,000 and $15,000. Then it measures the search slope at s=10s = 10 for two of them. It uses the central difference from the derivatives lesson: nudge up and down by a tiny hh of 0.00001, written 1e-5 in code.

import numpy as np
import matplotlib.pyplot as plt

def f(s, m):
    return 40 * np.sqrt(s) + 25 * np.sqrt(m) + 3 * np.sqrt(s * m)

s = np.linspace(0.1, 20, 200)
for m in [1, 5, 15]:
    plt.plot(s, f(s, m), label=f"social = ${m}k")
plt.xlabel("search spend ($ thousands)")
plt.ylabel("conversions")
plt.legend()

h = 1e-5
for m in [1, 15]:
    slope = (f(10 + h, m) - f(10 - h, m)) / (2 * h)
    print(f"∂f/∂s at s=10, social={m}k: {slope:.2f} conversions per extra $1k of search")
It runs right here in your browser, with nothing to install.

With $1,000 of social, the search slope is 6.80: roughly 6.8 extra conversions per extra $1,000 of search. With $15,000 of social, it's 8.16. More social spend makes each search dollar a bit more productive, so the curves are steeper. That's the bonus term at work.

Go deeper The exact slope by rule

A square root is a power of one half, so the power rule gives s\sqrt{s} a slope of 1/(2s)1 / (2\sqrt{s}). The bonus term 3s m3\sqrt{s\,m} equals 3m×s3\sqrt{m} \times \sqrt{s}. With mm held fixed, 3m3\sqrt{m} is the number in front. The 25m25\sqrt{m} term doesn't change when only ss moves, so its slope is 0. Put it together:

∂f∂s=20s+3m2s\frac{\partial f}{\partial s} = \frac{20}{\sqrt{s}} + \frac{3\sqrt{m}}{2\sqrt{s}}

In plain words: the first part comes from search alone. The second part contains mm, so more social spend makes it bigger.

At s=10s = 10 and m=1m = 1, that's (20+1.5)/3.162≈6.80(20 + 1.5) / 3.162 \approx 6.80. At m=15m = 15, it's (20+1.5×3.873)/3.162≈8.16(20 + 1.5 \times 3.873) / 3.162 \approx 8.16. Both match the code.

Quick check
For k(x, y) = 4x + 7y, what is ∂k/∂x when y = 10?
  1. 7
  2. 74
  3. 40
  4. 4
Show the answer

4

Hold y fixed, and 7y is a constant with slope 0. The 4x term gives 4. The answer is 4 for any y, because these inputs don't interact.

Estimating partials with nudges

The nudge recipe from the derivatives lesson works with many inputs too. Number the inputs. Then xix_i means input number ii. The dots in the formula stand for every other input, left unchanged.

∂f∂xi≈f(…,xi+h,…)−f(…,xi−h,…)2h\frac{\partial f}{\partial x_i} \approx \frac{f(\ldots, x_i + h, \ldots) - f(\ldots, x_i - h, \ldots)}{2h}

In plain words: move input number ii up a little, by hh. Then move it down by hh. Leave every other input where it is. Take the change in output and divide by the distance between the two points, which is 2h2h.

Worked by hand with gg at x=1x = 1, y=2y = 2, using a big nudge of h=1h = 1:

  • For xx: g(2,2)=20g(2, 2) = 20 and g(0,2)=4g(0, 2) = 4. So (20−4)/2=8(20 - 4) / 2 = 8.
  • For yy: g(1,3)=19g(1, 3) = 19 and g(1,1)=5g(1, 1) = 5. So (19−5)/2=7(19 - 5) / 2 = 7.

Both match the rule. For gg, this recipe happens to be exact for any hh. Most functions need a tiny hh to get close.

The code does the same with a tiny hh. The partial function makes two copies of the point, up and down. It nudges input i in each copy and returns the slope. The copies matter: if you nudged the original point, you'd change it for any code that uses it next. Python counts from 0, so i = 0 is xx and i = 1 is yy.

import numpy as np

def g(p):
    x, y = p
    return x ** 2 + 3 * x * y + y ** 2

def partial(f, point, i, h=1e-5):
    up = np.array(point, dtype=float)
    down = np.array(point, dtype=float)
    up[i] += h
    down[i] -= h
    return (f(up) - f(down)) / (2 * h)

p = [1.0, 2.0]
print("∂g/∂x:", round(partial(g, p, 0), 6), " rule: 2x + 3y =", 2 * 1 + 3 * 2)
print("∂g/∂y:", round(partial(g, p, 1), 6), " rule: 3x + 2y =", 3 * 1 + 2 * 2)

The nudges give 8.0 and 7.0, the same as the rule.

The cost of nudging

Each partial needs two runs of the function. A model with nn weights needs 2n2n runs of the loss to get every partial. With 1,000 weights, that's 2,000 runs. That's fine for checking code on a small model. For a billion weights it's impossible.

Training uses a method called backpropagation instead, which you'll meet later. It gets all nn partials in one backward pass through the model.

Quick check
A model has 500 weights. How many times does the nudge method run the loss to get every partial derivative?
  1. 250,000
  2. 500
  3. 2
  4. 1,000
Show the answer

1,000

Each weight needs one run nudged up and one nudged down: 2 × 500 = 1,000.

Worked example

Worked example: where should the next $1,000 go?

You spend $9,000 on search and $4,000 on social, with the conversions function f from this lesson. Measure both partials. Then give $1,000 more to social and measure again. Each printed line shows the social budget, search's partial, and social's partial.

import math

def f(s, m):
    return 40 * math.sqrt(s) + 25 * math.sqrt(m) + 3 * math.sqrt(s * m)

h = 1e-5
s = 9
for m in [4, 5]:
    by_search = (f(s + h, m) - f(s - h, m)) / (2 * h)   # social held at m
    by_social = (f(s, m + h) - f(s, m - h)) / (2 * h)   # search held at 9
    print(m, round(by_search, 2), round(by_social, 2))
In the lesson you can change this and run it.

At $4,000 of social, search's partial is 7.67 and social's is 8.5. So the next $1,000 should go to social: about 8.5 extra conversions against 7.67.

After social rises to $5,000, the picture flips. Social's partial drops to 7.6 and search's rises to 7.78. Two things happened. Social's returns shrank as its budget grew, because the square root flattens out. And search got a small boost from the extra social, which is the interaction. That's why you measure again after every shift.

Quick check
In the worked example, why measure the partials again after moving $1,000 into social?
  1. Returns shrink as a channel grows, and channels interact, so the best choice can change
  2. There's no need: the first winner always stays the winner
  3. Partial derivatives give a different answer each time you compute them
  4. The conversions function f changes every week
Show the answer

Returns shrink as a channel grows, and channels interact, so the best choice can change

Each partial describes one budget point. Move the budget and both partials change: social's fell from 8.5 to 7.6 and search's rose from 7.67 to 7.78.

Practice what you just read

  • 3 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 4 review cards for spaced repetition

Create a free account

Try a quiz question

What does ∂f/∂x measure for a function f(x, y)?

  1. How f changes as x changes, with y held fixed
  2. How f changes as both x and y change together
  3. The value of f at x
  4. The average slope over all inputs

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson