Tivadar Danka Profile picture
Nov 16, 2022 18 tweets 6 min read Read on X
Matrix multiplication is not easy to understand.

Even looking at the definition used to make me sweat, let alone trying to comprehend the pattern. Yet, there is a stunningly simple explanation behind it.

Let's pull back the curtain! Image
First, the raw definition.

This is how the product of A and B is given. Not the easiest (or most pleasant) to look at.

We are going to unwrap this. Image
Here is a quick visualization before the technical details.

The element in the i-th row and j-th column of AB is the dot product of A's i-th row and B's j-th column. Image
Now, let's look at a special case: multiplying the matrix A with a (column) vector whose first component is 1, and the rest is 0.

Let's name this special vector e₁.

Turns out that the product of A and e₁ is the first column of A. Image
Similarly, multiplying A with a (column) vector whose second component is 1 and the rest is 0 yields the second column of A.

That's a pattern! Image
By the same logic, we conclude that A times eₖ equals the k-th column of A.

This sounds a bit algebra-y, so let's see this idea in geometric terms.

Yes, you heard right: geometric terms. Image
Matrices represent linear transformations. You know, those that stretch, skew, rotate, flip, or otherwise linearly distort the space.

The images of basis vectors form the columns of the matrix.

We can visualize this in two dimensions. Image
Moreover, we can look at a matrix-vector product as a linear combination of the column vectors.

Make a mental note of this, because it is important. Image
(If unwrapping the matrix-vector product seems too complex, I got you.

The computation below is the same as in the above tweet, only in vectorized form.) Image
Now, about the matrix product formula.

From a geometric perspective, the product AB is the same as first applying B, then A to our underlying space. Image
Recall that matrix-vector products are linear combinations of column vectors.

With this in mind, we see that the first column of AB is the linear combination of A's columns. (With coefficients from the first column of B.) Image
We can collapse the linear combination into a single vector, resulting in a formula for the first column of AB.

This is straight from the mysterious matrix product formula. Image
The same logic can be applied, thus giving an explicit formula to calculate the elements of a matrix product. Image
Linear algebra is powerful exactly because it abstracts away the complexity of manipulating data structures like vectors and matrices.

Instead of explicitly dealing with arrays and convoluted sums, we can use simple expressions AB.

That's a huge deal.
Peter Lax sums it up perfectly: "So what is gained by abstraction? First of all, the freedom to use a single symbol for an array; this way we can think of vectors as basic building blocks, unencumbered by components."
Without a doubt, linear algebra is one of the most important mathematical tools for a machine learning practitioner.

I wrote the book to get you from high school math to linear algebra mastery. Get your copy now!

(Lifetime updates included.)

tivadardanka.com/books/linear-a…
Read the unrolled thread here:

tivadardanka.com/blog/behind-ma…
If you have enjoyed this explanation, share it with your friends and give me a follow! I regularly post deep-dive explainers such as this.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Tivadar Danka

Tivadar Danka Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TivadarDanka

Sep 8
Matrix multiplication is not easy to understand.

Even looking at the definition used to make me sweat, let alone trying to comprehend the pattern. Yet, there is a stunningly simple explanation behind it.

Let's pull back the curtain! Image
First, the raw definition.

This is how the product of A and B is given. Not the easiest (or most pleasant) to look at.

We are going to unwrap this. Image
Here is a quick visualization before the technical details.

The element in the i-th row and j-th column of AB is the dot product of A's i-th row and B's j-th column. Image
Read 16 tweets
Sep 7
Behold one of the mightiest tools in mathematics: the camel principle.

I am dead serious. Deep down, this tiny rule is the cog in many methods. Ones that you use every day.

Here is what it is, how it works, and why it is essential: Image
First, the story:

The old Arab passes away, leaving half of his fortune to his eldest son, third to his middle son, and ninth to his smallest.

Upon opening the stable, they realize that the old man had 17 camels. Image
This is a problem, as they cannot split 17 camels into 1/2, 1/3, and 1/9 without cutting some in half.

So, they turn to the wise neighbor for advice. Image
Read 18 tweets
Sep 7
The way you think about the exponential function is wrong.

Don't think so? I'll convince you. Did you realize that multiplying e by itself π times doesn't make sense?

Here is what's really behind the most important function of all time: Image
First things first: terminologies.

The expression aᵇ is read "a raised to the power of b."

(Or a to the b in short.) Image
The number a is called the base, and b is called the exponent.

Let's start with the basics: positive integer exponents. By definition, aⁿ is the repeated multiplication of a by itself n times.

Sounds simple enough. Image
Read 18 tweets
Sep 5
In machine learning, we use the dot product every day.

However, its definition is far from revealing. For instance, what does it have to do with similarity?

There is a beautiful geometric explanation behind: Image
By definition, the dot product (or inner product) of two vectors is defined by the sum of coordinate products. Image
To peek behind the curtain, there are three key properties that we have to understand.

First, the dot product is linear in both variables. This property is called bilinearity. Image
Read 16 tweets
Sep 5
The single biggest argument about statistics: is probability frequentist or Bayesian?

It's neither, and I'll explain why.

Deep-dive explanation incoming: Image
First, let's look at what probability is.

Probability quantitatively measures the likelihood of events, like rolling six with a die. It's a number between zero and one.

This is independent of interpretation; it’s a rule set in stone. Image
In the language of probability theory, the events are formalized by sets within an event space.

The event space is also a set, usually denoted by Ω.) Image
Read 34 tweets
Sep 4
You have seen the famous bell curve hundreds of times before.

Contrary to popular belief, this is NOT a probability, but a probability density.

What are densities, and why do we need them? Read on: Image
First, let's talk about probability.

The gist is, probability is a function P(A) that takes an event (that is, a set), and returns a real number between 0 and 1.

The event is a subset of the so-called sample space, a set often denoted with the capital Greek omega (Ω). Image
Every probability measure must satisfy three conditions: nonnegativity, additivity, and the probability of the entire sample space must be 1.

These are called the Kolmogorov axioms of probability, named after Andrey Kolmogorov, who first formalized them. Image
Read 22 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(