Sunday, 12 October 2025

A Practical Example of a Convolution

Over the past decade, for some reason, I've chosen to ignore \( \textbf{convolutions} \). Whenever they were mentioned in an OEIS sequence, I simply skipped over the reference. However, I'm now attempting to redress that neglect and to that end I was lucky to find two excellent YouTube videos about convolutions made by 3Blue1Brown (this guy has 7.76 million subscribers and for good reason). The two videos are:

One example of the use of a convolution is that of a weighted die with probabilities of a particular face showing up being given by:
  • p(1) = 0.1
  • p(2) = 0.2
  • p(3) = 0.3
  • p(4) = 0.2
  • p(5) = 0.1
  • p(6) = 0.1
If this die twice is rolled twice, what are the probabilities of throwing a 2, 3, 4, ..., 10, 11, 12? Well, the convolution of the two sequences representing the probabilities of the two dice rolls we tell you. Let's call the sequence A = [0.1, 0.2, 0.3, 0.2, 0.1, 0.1] and the classic way to facilitate the convolution is the so-called "slide and roll". We'll flip A so that it becomes B = [0.1, 0.1, 0.2, 0.3, 0.2, 0.1] and slide B progressively over A. Figure 1 shows the situation for the initial moves.


Figure 1

Of course it would be tedious to have to construct this every time we needed to evaluate a convolution and so Python makes it easier by use of the following code (input in blue, output in red):

A=[0.1,0.2, 0.3, 0.2,0.1,0.1]
result=convolution(A,A)
L=[]
for n in result:
    L.append(numerical_approx(n,digits=2))
print(L)

[0.010, 0.040, 0.10, 0.16, 0.19, 0.18, 0.14, 0.10, 0.050, 0.020, 0.010] 

Reading the output we can see that the probabilities of the various sums are as follows:
  • p(2) = 0.01
  • p(3) = 0.04
  • p(4) = 0.10
  • p(5) = 0.16
  • p(6) = 0.19
  • p(7) = 0.18
  • p(8) = 0,14
  • p(9) = 0.10
  • p(10) = 0.05
  • p(11) = 0.02
  • p(12) = 0.01
Figure 2 shows another view of what's going on. In this case, the various sum are calculated by adding up along the marked diagonals:


Figure 2

This post is simply the first in what I hope will be a series of posts relating to convolutions. As I've already discovered, convolutions linked to Fourier transforms and Laplace transformations so it's a big topic to investigate but at least I've finally made a start.

No comments:

Post a Comment