- Podcasts
- BetterExplained
- Intuitive Guide to Convolution
About this episode
Like making engineering students squirm? Have them explain convolution and (if you're barbarous) the convolution theorem. They'll mutter something about sliding windows as they try to escape through one.
Convolution is usually introduced with its formal definition:
Yikes. Let's start without calculus: Convolution is fancy multiplication.
Part 1: Hospital AnalogyImagine you manage a hospital treating patients with a single disease. You have:
- A treatment plan: [3] Every patient gets 3 units of the cure on their first day.
- A list of patients: [1 2 3 4 5] Your patient count for the week (1 person Monday, 2 people on Tuesday, etc.).
Question: How much medicine do you use each day? Well, that's just a quick multiplication:
Plan * Patients = Daily Usage [3] * [1 2 3 4 5] = [3 6 9 12 15]Multiplying the plan by the patient list gives the usage for the upcoming days: [3 6 9 12 15]. Everyday multiplication (3 x 4) means using the plan with a single day of patients: [3] * [4] = [12].
Intuition For ConvolutionLet's say the disease mutates and requires a multi-day treatment. You create a new plan: Plan: [3 2 1]
That means 3 units of the cure on the first day, 2 on the second, and 1 on the third. Ok. Given the same patient schedule [1 2 3 4 5], what's our medicine usage each day?
Uh... shoot. It's not a quick multiplication:
- On Monday, 1 patient comes in. It's her first day, so she gets 3 units.
- On Tuesday, the Monday gal gets 2 units (her second day), but two new patients arrive, who get 3 each (2 * 3 = 6). The total is 2 + (2 * 3) = 8 units.
- On Wednesday, it's trickier: The Monday gal finishes (1 unit, her last day), the Tuesday people get 2 units (2 * 2), and there are 3 new Wednesday people... argh.
The patients are overlapping and it's hard to track. How can we organize this calculation?
An idea: imagine flipping the patient list, so the first patient is on the right:
Start of line 5 4 3 2 1Next, imagine we have 3 separate rooms where we apply the proper dose:
Rooms 3 2 1On your first day, you walk into the first room and get 3 units of medicine. The next day, you walk into room #2 and get 2 units. On the last day, you walk into room #3 and get 1 unit. There's no rooms afterwards, and your treatment is done.
To calculate the total medicine usage, line up the patients and walk them through the rooms:
Monday ---------------------------- Rooms 3 2 1 Patients 5 4 3 2 1 Usage 3On Monday (our first day), we have a single patient in the first room. She gets 3 units, for a total usage of 3. Makes sense, right?
On Tuesday, everyone takes a step forward:
Tuesday ---------------------------- Rooms 3 2 1 Patients -> 5 4 3 2 1 Usage 6 2 = 8The first patient is now in the second room, and there's 2 new patients in the first room. We multiply each room's dose by the patient count, then combine.
Every day we just walk the list forward:
Wednesday ---------------------------- Rooms 3 2 1 Patients -> 5 4 3 2 1 Usage 9 4 1 = 14 Thursday ----------------------------- Rooms 3 2 1 Patients -> 5 4 3 2 1 Usage 12 6 2 = 20 Friday ----------------------------- Rooms 3 2 1 Patients -> 5 4 3 2 1 Usage 15 8 3 = 26Whoa! It's intricate, but we figured it out, right? We can find the usage for any day by reversing the list, sliding it to the desired day, and combining the doses.
The total day-by-day usage looks like this (don't forget Sat and Sun, since some patients began on Friday):
Plan * Patient List = Total Daily Usage [3 2 1] * [1 2 3 4 5] = [3 8 14 20 26 14 5] M T W T F M T W T F S SThis calculation is the convolution of the plan and patient list. It's a fancy multiplication between a list of input numbers and a "program".
Interactive DemoHere's a live demo. Try changing F (the plan) or G (the patient list). The convolution matches our manual calculation above.
(We define functions and to pad each list with zero, and adjust for the list index starting at 1.)
You can do a quick convolution with Wolfram Alpha:
ListConvolve[{3, 2, 1}, {1, 2, 3, 4, 5}, {1, -1}, 0] {3, 8, 14, 20, 26, 14, 5}(The extra {1, -1}, 0 aligns the lists and pads with zero.)
Application: COVID Ventilator UsageI started this article 5 years ago (intuition takes a while...), but unfortunately the analogy is relevant today.
Let's use convolution to estimate ventilator usage for incoming patients.
- Set as the percent of patients needing ventilators. For example, [.05 .03 .01] means 5% of patients need ventilators the first week, 3% the second week, and 1% the third week.
- Set as the weekly incoming patients, in thousands.
- The convolution , shows how many ventilators are needed each week (in thousands). is how many ventilators are needed 5 weeks from now.
Let's try it out:
- F = [.05, .03, .01] is the ventilator use percentage by week
- G = [10, 20, 30, 20, 10, 10, 10], is the incoming hospitalized patients. It starts at 10k per week, rises to 30k, then decays to 10k.
With these numbers, we expect a max ventilator use of 2.2k in 2 weeks:
The convolution drops to 0 after 9 weeks because the patient list has run out. In this example, we're interested in the peak value the convolution hits, not the long-term total.
Other plans to convolve may be drug doses, vaccine appointments (one today, another a month from now), reinfections, and other complex interactions.
The hospital analogy is the mental model I wish I had when learning. Now that we've tried it with actual numbers, let's add the Math Juice and convert the analogy to calculus.
Part 2: The Calculus DefinitionSo, what happened in our example? We had a list of patients and a plan. If the plan were simple (single day [3]), regular multiplication would have worked. Because the plan was complex, we had to "convolve" it.
Time for some Fun Facts™:
Convolution is written , with an asterisk. Yes, an asterisk usually indicates multiplication, but in advanced calculus class, it indicates a convolution. Regular multiplication is just implied ().
The result of a convolution is a new function that gives the total usage for any day ("What was the total usage on day ?"). We can graph the convolution over time to see the day-by-day totals.
Now the big aha: Convolution reverses one of the lists! Here's why.
Let's call our treatment plan . In our example, we used [3 2 1].
The list of patients (inputs) is . However, we need to reverse this list as we slide it, so the earliest patient (Monday) enters the hospital first (first in, first out). This means we need to use , the horizontal reflection of . [1 2 3 4 5] becomes [5 4 3 2 1].
Now that we have the reversed list, pick a day to compute (). To slide our patient list by this much, we use: . That is, we reverse the list () and jump to the correct day ().
We have our scenario:
- is the plan to use
- is the list of inputs (flipped and slid to the right day).
To get the total usage on day , we multiply each patient with the plan, and sum the results (an integral). To account for any possible length, we go from -infinity to +infinity.
Now we can describe convolution formally using calculus:
(Like colorized math? There's more.)
Phew! That's quite few symbols. Some notes:
- We use a dummy variable (tau) for the intermediate computation. Imagine as knocking on each room (), finding the dosage [], the number of patients [], multiplying them, and totaling things in the integral. Yowza. The so-called "dummy" variable is like i in a for loop: it's temporary, but does the work. (By analogy, is a global variable has a fixed value during the loop: it's the day we're calculating the usage for, such as t = Day 5).
- In the official definition, you'll see instead of . The second version shows the flip () and slide (). Writing makes it seem like we're interested in the difference between the variables, which confused me.
- The treatment plan (program to run) is called the kernel: you convolve a kernel with an input.
Not too bad, right? The equation is a formal description of the analogy.
Part 3: Mathematical Properties of ConvolutionWe can't discover a new math operation without taking it for a spin. Let's see how it behaves.
Convolution is commutative: f * g = g * fIn our computation, we flipped the patient list and kept the plan the same. Could we have flipped the plan instead?
You bet. Imagine the patients are immobile, and stay in their rooms: [1 2 3 4 5]. To deliver the medicine, we have 3 medical carts that go to each room and deliver the dose. Each day, they slide forward one position.
Carts -> 1 2 3 1 2 3 4 5 PatientsAs before, though our plan is written [3 2 1] (3 units on the first day), we flip the order of the carts to[1 2 3]. That way, a patient gets 3 units on their first day, as we expect. Checking with Wolfram Alpha, the calculation is the same.
ListConvolve[{1, 2, 3, 4, 5}, {3, 2, 1}, {1, -1}, 0] {3, 8, 14, 20, 26, 14, 5}Cool! It looks like convolution is commutative:
and we can decide to flip either or when calculating the integral. Surprising, right?
The integral of the convolutionWhen all treatments are finished, what was the total medicine usage? This is the integral of the convolution. (A few minutes ago, that phrase would have you jumping out of a window.)
But it's a simple calculation. Our plan gives each patient sum([3 2 1]) = 6 units of medicine. And we have sum([1 2 3 4 5]) = 15 patients. The total usage is just 6 x 15 = 90 units.
Wow, that was easy: the usage for the entire convolution is just the product of the subtotals!
I hope this clicks intuitively. Note that this trick works for convolution, but not integrals in general. For example:
If we separate into two integrals we get: