Pratham Patel
· 35 min read

Mathematical Prerequisites for Mixture of Experts

Building the math foundations you need for Mixture of Experts — expected value, Gaussian densities, likelihood, Bayes' theorem, softmax, mixture models, conditional probability, multinomial distributions, and the Sherman-Morrison-Woodbury formula — all derived step by step with one consistent example.

Before diving into Mixture of Experts, you need a handful of mathematical ideas. This post builds every one of them inside a single, consistent example so nothing feels abstract or disconnected. By the end, you will have all the tools required to derive the MoE framework — cooperative and competitive errors, posterior responsibilities, the mixture-of-Gaussians interpretation, hierarchical mixtures, and the EM algorithm — from scratch in Part 1.


The Running Example

Imagine two weather forecasters predicting tomorrow’s temperature. Forecaster A predicts 20°C. Forecaster B predicts 26°C. The actual temperature turns out to be 22°C.

We will use this setup — two forecasters, one observed outcome — for every concept in this post. The forecasters will become “experts” in the MoE framework, the actual temperature will become the “target,” and the question of how to combine or select between forecasters will motivate every mathematical tool we build.

Throughout, we will use the following concrete numbers:

ForecasterPrediction
AμA=20\mu_A = 20
BμB=26\mu_B = 26

Observed temperature: y=22y = 22.


1. Squared Error

We need a way to measure how wrong a prediction is. The most natural choice is the squared error: take the difference between the prediction and the actual value, then square it.

SE=(yμ)2\text{SE} = (y - \mu)^2

Why square? Two reasons. First, squaring makes all errors positive — a prediction that is 2 degrees too high is just as “wrong” as one that is 2 degrees too low. Second, squaring penalises large errors more heavily than small ones: an error of 4 is penalised 16 times, not 4 times.

When dealing with vector-valued outputs (predictions with multiple components), we use the squared norm:

yμ2=k(ykμk)2\|\mathbf{y} - \boldsymbol{\mu}\|^2 = \sum_k (y_k - \mu_k)^2

This sums the squared errors across all components. For our scalar example, yμ2\|\mathbf{y} - \boldsymbol{\mu}\|^2 reduces to (yμ)2(y - \mu)^2.

Numerical check

For forecaster A: (2220)2=4(22 - 20)^2 = 4.

For forecaster B: (2226)2=16(22 - 26)^2 = 16.

Forecaster A has a squared error of 4, forecaster B has a squared error of 16. By this measure, A is four times better than B on this particular observation. We will use these two numbers — 4 and 16 — throughout the rest of the post.


2. Expected Value

Suppose we do not know which forecaster will be selected — a gating mechanism picks forecaster A with probability pp and forecaster B with probability 1p1 - p. What is the average squared error we would see across many such selections?

The expected value answers this question. It multiplies each possible outcome by its probability, then sums:

E[X]=ixiP(X=xi)\boxed{\mathbb{E}[X] = \sum_i x_i \cdot P(X = x_i)}

For the expected squared error when the gating mechanism assigns p=0.6p = 0.6 to A and 1p=0.41 - p = 0.4 to B:

E[SE]=p(yμA)2+(1p)(yμB)2\mathbb{E}[\text{SE}] = p \cdot (y - \mu_A)^2 + (1-p) \cdot (y - \mu_B)^2

Numerical check

E[SE]=0.6×4+0.4×16=2.4+6.4=8.8\mathbb{E}[\text{SE}] = 0.6 \times 4 + 0.4 \times 16 = 2.4 + 6.4 = 8.8

On average, if we randomly pick a forecaster according to these probabilities and measure the squared error, we get 8.8. This formula is exactly the competitive error function in MoE — each expert is measured against the full target independently, and the errors are averaged using the gating probabilities as weights. We will see this in Part 1 as equation (1.2) from Jacobs et al. (1991).


3. The Softmax Function

In MoE, a gating network must produce probabilities p1,p2,,pnp_1, p_2, \ldots, p_n that are all positive and sum to 1. Given arbitrary real-valued scores s1,s2,,sns_1, s_2, \ldots, s_n (which could be any real numbers — positive, negative, or zero), how do we convert them into valid probabilities?

The softmax function does exactly this:

pj=esjkesk\boxed{p_j = \frac{e^{s_j}}{\sum_k e^{s_k}}}

Each score is exponentiated (making it positive, since ex>0e^x > 0 for all xx), then divided by the sum of all exponentiated scores (ensuring the result sums to 1).

Let us verify both requirements. Positivity: since esj>0e^{s_j} > 0 and kesk>0\sum_k e^{s_k} > 0, we have pj>0p_j > 0 for all jj. Summation: jpj=jesjkesk=jesjkesk=1\sum_j p_j = \sum_j \frac{e^{s_j}}{\sum_k e^{s_k}} = \frac{\sum_j e^{s_j}}{\sum_k e^{s_k}} = 1.

An important property. Softmax preserves the ranking: if s1>s2s_1 > s_2, then p1>p2p_1 > p_2 (because the exponential is monotonically increasing: es1>es2e^{s_1} > e^{s_2}, so es1sum>es2sum\frac{e^{s_1}}{\text{sum}} > \frac{e^{s_2}}{\text{sum}}). The forecaster with the higher score gets the higher probability.

Numerical check

Suppose the gating network produces scores sA=1.5s_A = 1.5 and sB=0.5s_B = 0.5 for our two forecasters. Then:

e1.5=4.482,e0.5=1.649e^{1.5} = 4.482, \quad e^{0.5} = 1.649 pA=4.4824.482+1.649=4.4826.131=0.731p_A = \frac{4.482}{4.482 + 1.649} = \frac{4.482}{6.131} = 0.731 pB=1.6496.131=0.269p_B = \frac{1.649}{6.131} = 0.269

Check: 0.731+0.269=1.0000.731 + 0.269 = 1.000. Both positive, sum to 1. Forecaster A, which had the higher score (sA=1.5>sB=0.5s_A = 1.5 > s_B = 0.5), gets the higher probability (pA=0.731>pB=0.269p_A = 0.731 > p_B = 0.269). In Part 1, the gating network computes these scores as linear functions of the input: sj=vjTxs_j = \mathbf{v}_j^T \mathbf{x}.


4. The Gaussian Density

We now arrive at the concept that connects squared error to probability. The Gaussian (or normal) probability density function describes how likely a value yy is, given that it was drawn from a bell-shaped distribution centred at μ\mu with spread controlled by the variance σ2\sigma^2:

f(yμ,σ2)=12πσ2exp ⁣((yμ)22σ2)\boxed{f(y \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left( -\frac{(y - \mu)^2}{2\sigma^2} \right)}

Let us unpack each piece:

  • (yμ)2(y - \mu)^2 is the squared error between the observation and the centre — the same squared error from Section 1.
  • Dividing by 2σ22\sigma^2 scales the error by the variance. Larger variance means the distribution is wider and more tolerant of errors.
  • The exponential exp()\exp(-\cdot) converts the scaled squared error into a positive number (since ex>0e^x > 0 for all xx). Smaller errors give larger density values.
  • The factor 12πσ2\frac{1}{\sqrt{2\pi\sigma^2}} is the normalising constant — it ensures the density integrates to 1 over all possible yy values.

A common simplification. When comparing two forecasters on the same observation, the normalising constant is the same for both (since σ2\sigma^2 is the same). In MoE, Jacobs et al. (1991) use unit variance (σ2=1\sigma^2 = 1) for simplicity. With σ2=1\sigma^2 = 1, the Gaussian density simplifies to:

f(yμ,1)=12πexp ⁣((yμ)22)f(y \mid \mu, 1) = \frac{1}{\sqrt{2\pi}} \exp\!\left( -\frac{(y - \mu)^2}{2} \right)

The normalising constant 12π0.399\frac{1}{\sqrt{2\pi}} \approx 0.399 is now the same for all experts and does not affect which expert is “better.” This is why the MoE papers often write e12do2e^{-\frac{1}{2}\|d - o\|^2} and drop the constant.

Numerical check

Using unit variance (σ2=1\sigma^2 = 1):

Forecaster A (μA=20\mu_A = 20, y=22y = 22):

f(2220,1)=12πexp ⁣((2220)22)=0.399×e2=0.399×0.135=0.054f(22 \mid 20, 1) = \frac{1}{\sqrt{2\pi}} \exp\!\left( -\frac{(22-20)^2}{2} \right) = 0.399 \times e^{-2} = 0.399 \times 0.135 = 0.054

Forecaster B (μB=26\mu_B = 26, y=22y = 22):

f(2226,1)=12πexp ⁣((2226)22)=0.399×e8=0.399×0.000335=0.000134f(22 \mid 26, 1) = \frac{1}{\sqrt{2\pi}} \exp\!\left( -\frac{(22-26)^2}{2} \right) = 0.399 \times e^{-8} = 0.399 \times 0.000335 = 0.000134

Forecaster A’s density is 0.0540.054, roughly 400 times larger than forecaster B’s density of 0.0001340.000134. The Gaussian density is telling us: if the true temperature-generating process is centred at 20 (forecaster A’s prediction) with unit variance, the observed value of 22 is plausible. If the process is centred at 26, the value 22 is extremely unlikely. This ratio — 400 to 1 — is exactly the kind of comparison that will drive the posterior responsibilities in MoE.

Why the densities are small

Both values — 0.0540.054 and 0.0001340.000134 — are small numbers. That is normal. A continuous density is not a probability. It measures how densely the probability is concentrated around yy. The actual probability of observing exactly 22.000… is zero for any continuous distribution. What matters is the relative comparison between densities, not their absolute size. Forecaster A’s density is 400 times larger than forecaster B’s — that is the relevant information.


5. Likelihood

We now flip the perspective. In Section 4, we fixed a forecaster (μ\mu) and asked how probable the observation yy is. Now we fix the observation (y=22y = 22) and ask: which forecaster makes the observation more plausible?

The likelihood function uses the same formula as the probability density, but views it as a function of μ\mu (the forecaster’s prediction) rather than yy (the observation):

L(μ)=f(yμ)=12πexp ⁣((yμ)22)\boxed{\mathcal{L}(\mu) = f(y \mid \mu) = \frac{1}{\sqrt{2\pi}} \exp\!\left( -\frac{(y - \mu)^2}{2} \right)}

Same formula. Different question.

Probability asks: given forecaster A’s prediction μA=20\mu_A = 20, how likely is the observation y=22y = 22?

Likelihood asks: given the observation y=22y = 22, how plausible is it that the generating process was centred at μA=20\mu_A = 20?

The answer is the same number — 0.0540.054 — but the interpretation is different. Probability varies yy while holding μ\mu fixed. Likelihood varies μ\mu while holding yy fixed.

Numerical check

L(20)=0.054,L(26)=0.000134\mathcal{L}(20) = 0.054, \quad \mathcal{L}(26) = 0.000134

The likelihood of forecaster A is 400 times larger than the likelihood of forecaster B. If we had to pick one forecaster based solely on this observation, we would pick A — it assigns far more plausibility to the data we actually saw.

This is exactly what happens inside MoE. Each expert’s output oio_i defines a Gaussian centre, and the likelihood e12doi2e^{-\frac{1}{2}\|d - o_i\|^2} measures how well that expert explains the observed target. Experts with higher likelihood get more responsibility. We will formalise this in the Bayes’ theorem section.


6. Log-Likelihood and the Logarithm

Working directly with likelihoods leads to numerical problems — when you multiply many small numbers together (one per data point), the product quickly underflows to zero. The logarithm fixes this by converting products into sums.

The natural logarithm ln(x)\ln(x) is the inverse of the exponential: if ea=be^a = b, then ln(b)=a\ln(b) = a. We need three properties.

Property 1: Monotonically increasing. If a>b>0a > b > 0, then lna>lnb\ln a > \ln b. Maximising lnf\ln f is equivalent to maximising ff — the logarithm preserves the location of maxima and minima.

Property 2: Log of a product is a sum of logs. This is the logarithm product rule:

ln(ab)=lna+lnb\boxed{\ln(a \cdot b) = \ln a + \ln b}

More generally, ln(abc)=lna+lnb+lnc\ln(a \cdot b \cdot c) = \ln a + \ln b + \ln c. This property is what makes the EM algorithm tractable — it will allow us to bring the logarithm inside the summation signs and separate the parameters of different experts.

Property 3: Log of an exponential cancels. ln(ex)=x\ln(e^x) = x. This converts the Gaussian density into a simple quadratic.

The log-likelihood of a Gaussian

Applying property 3 to our Gaussian likelihood:

lnL(μ)=ln[12πexp ⁣((yμ)22)]\ln \mathcal{L}(\mu) = \ln \left[ \frac{1}{\sqrt{2\pi}} \exp\!\left( -\frac{(y - \mu)^2}{2} \right) \right]

By property 2 (log of a product):

=ln12π+lnexp ⁣((yμ)22)= \ln \frac{1}{\sqrt{2\pi}} + \ln \exp\!\left( -\frac{(y - \mu)^2}{2} \right)

By property 3 (log of exponential cancels):

=12ln(2π)(yμ)22= -\frac{1}{2}\ln(2\pi) - \frac{(y - \mu)^2}{2}

The first term is a constant (it does not depend on μ\mu). So maximising the log-likelihood is equivalent to minimising (yμ)2(y - \mu)^2 — the squared error from Section 1.

lnL(μ)=const(yμ)22\boxed{\ln \mathcal{L}(\mu) = \text{const} - \frac{(y - \mu)^2}{2}}

This is a deep result: maximum likelihood estimation with a Gaussian model is equivalent to minimising squared error. When you see squared error in MoE, you are implicitly doing maximum likelihood under a Gaussian assumption.

Numerical check

Forecaster A:

lnL(20)=12ln(2π)42=0.9192.0=2.919\ln \mathcal{L}(20) = -\frac{1}{2}\ln(2\pi) - \frac{4}{2} = -0.919 - 2.0 = -2.919

Verify: ln(0.054)=2.919\ln(0.054) = -2.919. Correct.

Forecaster B:

lnL(26)=0.919162=0.9198.0=8.919\ln \mathcal{L}(26) = -0.919 - \frac{16}{2} = -0.919 - 8.0 = -8.919

Verify: ln(0.000134)=8.919\ln(0.000134) = -8.919. Correct.

The log-likelihood difference is 2.919(8.919)=6.0-2.919 - (-8.919) = 6.0. We can verify: ln(0.054/0.000134)=ln(403)=5.9996.0\ln(0.054 / 0.000134) = \ln(403) = 5.999 \approx 6.0. The logarithm has turned the ratio of 400:1 in likelihoods into an additive difference of 6.0 in log-likelihoods — much easier to work with numerically.


7. Negative Log-Likelihood as a Loss Function

In optimisation, we typically minimise a loss function. But likelihood and log-likelihood are things we want to maximise — higher likelihood means the model fits the data better. The standard trick is to negate the log-likelihood, turning maximisation into minimisation:

Loss=lnL(μ)\text{Loss} = -\ln \mathcal{L}(\mu)

This is the negative log-likelihood (NLL). Minimising the NLL is mathematically identical to maximising the likelihood.

For a single Gaussian:

lnL(μ)=12ln(2π)+(yμ)22-\ln \mathcal{L}(\mu) = \frac{1}{2}\ln(2\pi) + \frac{(y - \mu)^2}{2}

The constant term does not affect the optimisation, so the NLL is proportional to the squared error.

In MoE, the error function from Jacobs et al. (1991) is:

E=logipie12doi2E = -\log \sum_i p_i \, e^{-\frac{1}{2}\|d - o_i\|^2}

This is the negative log-likelihood of a mixture model — we will build up to it in Section 10. The negative sign converts “maximise likelihood” into “minimise error.”

Numerical check

Forecaster A: lnL(20)=2.919-\ln \mathcal{L}(20) = 2.919.

Forecaster B: lnL(26)=8.919-\ln \mathcal{L}(26) = 8.919.

Minimising the NLL means preferring forecaster A (loss 2.919) over forecaster B (loss 8.919). This is the same conclusion as before — just expressed as a minimisation problem.


8. Bayes’ Theorem

We now have all the ingredients for the most important result in this post. Suppose we have two forecasters, and before seeing the actual temperature, a gating network assigns prior probabilities pAp_A and pB=1pAp_B = 1 - p_A to each. After seeing the actual temperature y=22y = 22, we want to update these probabilities to reflect how well each forecaster did. The updated probabilities are called posterior probabilities.

Bayes’ theorem tells us exactly how to update:

P(forecaster iy)=pif(yμi)jpjf(yμj)\boxed{P(\text{forecaster } i \mid y) = \frac{p_i \cdot f(y \mid \mu_i)}{\sum_j p_j \cdot f(y \mid \mu_j)}}

Let us read this formula piece by piece:

  • pip_i is the prior probability — the gating network’s belief about forecaster ii before seeing the data. In MoE, these are the softmax outputs from Section 3.
  • f(yμi)f(y \mid \mu_i) is the likelihood — how well forecaster ii‘s prediction explains the observed data. This is the Gaussian density from Section 4.
  • pif(yμi)p_i \cdot f(y \mid \mu_i) is the joint probability — the probability of choosing forecaster ii AND observing yy from it.
  • jpjf(yμj)\sum_j p_j \cdot f(y \mid \mu_j) is the total probability of observing yy under the full mixture. We will call this the marginal likelihood. It sums over all possible forecasters.
  • The ratio gives the posterior probability P(forecaster iy)P(\text{forecaster } i \mid y) — the updated belief about forecaster ii after seeing the data.

Derivation

Bayes’ theorem follows directly from the definition of conditional probability. The conditional probability of event AA given event BB is:

P(AB)=P(A and B)P(B)P(A \mid B) = \frac{P(A \text{ and } B)}{P(B)}

Let AA = “forecaster ii was selected” and BB = “we observed yy.” Then:

  • P(A and B)=pif(yμi)P(A \text{ and } B) = p_i \cdot f(y \mid \mu_i). This is the probability of picking forecaster ii (probability pip_i) and then observing yy from it (density f(yμi)f(y \mid \mu_i)).
  • P(B)=jpjf(yμj)P(B) = \sum_j p_j \cdot f(y \mid \mu_j). This is the total probability of observing yy, obtained by summing over all possible forecasters. This step uses the law of total probability: the probability of yy is the sum of the probabilities of yy through each possible path.

Substituting:

P(forecaster iy)=pif(yμi)jpjf(yμj)P(\text{forecaster } i \mid y) = \frac{p_i \cdot f(y \mid \mu_i)}{\sum_j p_j \cdot f(y \mid \mu_j)}

That is Bayes’ theorem. Nothing more.

Numerical check

Let the gating network assign prior probabilities pA=0.6p_A = 0.6, pB=0.4p_B = 0.4 (same as Section 2). Using unit-variance Gaussian likelihoods (dropping the normalising constant, since it cancels in the ratio):

fA=e12(2220)2=e2=0.135f_A = e^{-\frac{1}{2}(22-20)^2} = e^{-2} = 0.135 fB=e12(2226)2=e8=0.000335f_B = e^{-\frac{1}{2}(22-26)^2} = e^{-8} = 0.000335

Joint probabilities:

pAfA=0.6×0.135=0.0812p_A \cdot f_A = 0.6 \times 0.135 = 0.0812 pBfB=0.4×0.000335=0.000134p_B \cdot f_B = 0.4 \times 0.000335 = 0.000134

Marginal likelihood (total probability):

L=0.0812+0.000134=0.08133L = 0.0812 + 0.000134 = 0.08133

Posterior probabilities:

hA=0.08120.08133=0.9984h_A = \frac{0.0812}{0.08133} = 0.9984 hB=0.0001340.08133=0.0016h_B = \frac{0.000134}{0.08133} = 0.0016

Check: 0.9984+0.0016=1.00000.9984 + 0.0016 = 1.0000. The posteriors sum to 1, as they must.

What happened. Forecaster A started with a prior of pA=0.6p_A = 0.6 and ended with a posterior of hA=0.9984h_A = 0.9984. Its responsibility increased dramatically because it fits the data much better (squared error 4 vs. 16). Forecaster B started with a prior of pB=0.4p_B = 0.4 and ended with a posterior of hB=0.0016h_B = 0.0016 — nearly zero.

This is exactly the mechanism that drives specialisation in MoE. The posteriors hih_i replace the priors pip_i in the gradient computation, ensuring that experts who fit the data well get larger gradients and learn faster on the cases they handle best. In Part 1, these posteriors are called hich_i^c in equation (1.5).

Prior vs. posterior: the key distinction

This distinction is easy to gloss over, but the entire MoE framework depends on it. Let us pin it down precisely.

Prior pip_i depends on the input x\mathbf{x} only. It is computed before seeing the target yy. It answers: “Based on the input alone, which expert should handle this case?”

Posterior hih_i depends on both the input x\mathbf{x} and the target yy. It is computed after seeing the target. It answers: “Given that we now see the target, which expert actually explains it best?”

The prior is the gating network’s best guess. The posterior is reality’s correction. In MoE, using posteriors instead of priors in the gradient is what makes the system discover the right task decomposition — it is not just a mathematical nicety, it is the mechanism that makes experts specialise.


9. Mixture Models

We now combine all the tools we have built. A mixture model says that the data was generated by one of nn component distributions, but we do not know which one. The overall probability of observing yy is a weighted sum of the component densities:

P(y)=i=1npif(yμi,σi2)\boxed{P(y) = \sum_{i=1}^{n} p_i \cdot f(y \mid \mu_i, \sigma_i^2)}

Each term has two parts:

  • pip_i is the mixing weight (or prior probability) for component ii. These are non-negative and sum to 1 — exactly the output of a softmax (Section 3).
  • f(yμi,σi2)f(y \mid \mu_i, \sigma_i^2) is the component density — for Gaussians, this is the formula from Section 4.

This is the law of total probability applied to continuous densities. The observation yy could have come from any component, so we sum over all possible sources, weighting each by its probability.

The mixture model as a generative story

A mixture model describes a two-step process for generating data:

  1. Select a component ii with probability pip_i. (Roll a weighted die.)
  2. Generate the observation yy from component ii‘s density f(yμi,σi2)f(y \mid \mu_i, \sigma_i^2). (Draw from the selected distribution.)

In MoE, step 1 is the gating network selecting an expert, and step 2 is the selected expert generating a prediction (with Gaussian noise). The entire MoE framework is a mixture model where the mixing weights and component means are input-dependent.

Mixture of Gaussians

When every component density f(yμi,σi2)f(y \mid \mu_i, \sigma_i^2) is a Gaussian (Section 4), the mixture model is called a mixture of Gaussians (also known as a Gaussian mixture model or GMM). The total density becomes:

P(y)=i=1npi12πσi2exp ⁣((yμi)22σi2)P(y) = \sum_{i=1}^{n} p_i \cdot \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp\!\left( -\frac{(y - \mu_i)^2}{2\sigma_i^2} \right)

Each component is a bell curve centred at μi\mu_i with width σi\sigma_i, and the mixture is a weighted sum of these bell curves. The result is a density that can have multiple peaks — one per component — allowing the model to capture data that clusters in several regions.

This is exactly the interpretation Jacobs et al. (1991) give to MoE in Part 1: each expert defines a Gaussian centred at its output, the gating network provides the mixing weights, and the combined model is a mixture of Gaussians. The term e12doi2e^{-\frac{1}{2}\|d - o_i\|^2} in the MoE error function is the Gaussian component (with unit variance, normalising constant dropped), and ipie12doi2\sum_i p_i e^{-\frac{1}{2}\|d - o_i\|^2} is the mixture.

Numerical check

Using our two forecasters with unit variance and mixing weights pA=0.6p_A = 0.6, pB=0.4p_B = 0.4:

P(22)=pAf(2220,1)+pBf(2226,1)P(22) = p_A \cdot f(22 \mid 20, 1) + p_B \cdot f(22 \mid 26, 1) =0.6×0.054+0.4×0.000134= 0.6 \times 0.054 + 0.4 \times 0.000134 =0.0324+0.0000536= 0.0324 + 0.0000536 =0.0325= 0.0325

This is the marginal likelihood — the total probability of observing y=22y = 22 under the mixture. It is dominated by forecaster A’s contribution (0.03240.0324 out of 0.03250.0325), reflecting A’s much better fit to the data.

Notice that we computed this same quantity (up to the normalising constant) in the Bayes’ theorem section as the denominator L=0.08133L = 0.08133. The difference is the normalising constant 12π=0.399\frac{1}{\sqrt{2\pi}} = 0.399: 0.08133×0.399=0.03250.08133 \times 0.399 = 0.0325. Both are computing the total probability of the data under the mixture — one with the constant, one without.


10. The Log-Likelihood of a Mixture

The loss function in MoE is the negative log of the mixture probability:

E=lnP(y)=lni=1npif(yμi)\boxed{E = -\ln P(y) = -\ln \sum_{i=1}^{n} p_i \cdot f(y \mid \mu_i)}

This is the negative log-likelihood of a mixture model. It is equation (1.3) in Jacobs et al. (1991), and equation (7) in Jordan & Jacobs (1993).

For a dataset of NN observations, the total log-likelihood is the sum over all data points:

l(θ)=t=1Nlnipi(t)f(y(t)μi(t))l(\theta) = \sum_{t=1}^{N} \ln \sum_i p_i^{(t)} \cdot f(y^{(t)} \mid \mu_i^{(t)})

Here the logarithm product rule (Section 6, property 2) has already done its work: the log of the product of NN independent likelihoods becomes a sum of NN individual log-likelihoods.

The log-sum problem

Notice that the logarithm sits outside the sum over experts: lni()\ln \sum_i (\cdots). This is fundamentally different from iln()\sum_i \ln(\cdots), which would be much easier to work with. The log of a sum does not simplify nicely — we cannot separate the parameters of different experts.

Compare:

  • Log of a product (easy): ln(ab)=lna+lnb\ln(a \cdot b) = \ln a + \ln b — the terms separate.
  • Log of a sum (hard): ln(a+b)lna+lnb\ln(a + b) \neq \ln a + \ln b — the terms stay coupled.

This is the central computational difficulty of mixture models. The EM algorithm, which we will derive in Part 1, exists specifically to solve this problem. It introduces hidden indicator variables that tell us which expert generated each data point, converting the log-of-a-sum into a sum-of-logs.

Numerical check

E=ln(0.0325)=3.426E = -\ln(0.0325) = 3.426

If we had used forecaster A alone: EA=ln(0.054)=2.919E_A = -\ln(0.054) = 2.919. If forecaster B alone: EB=ln(0.000134)=8.919E_B = -\ln(0.000134) = 8.919. The mixture loss (3.426) is slightly worse than using A alone (2.919) because the mixture includes B, which hurts the likelihood. But the mixture model has a crucial advantage: it can learn which forecaster to trust for each input, achieving better overall performance across many data points from different regions.


11. Conditional Probability and Nested Decisions

In the flat MoE, the gating network makes a single decision: which expert to use. In the Hierarchical Mixture of Experts (HME), decisions are nested: first pick a branch, then pick an expert within that branch. To handle this, we need conditional probability.

The conditional probability of event BB given that event AA has occurred is:

P(BA)=P(A and B)P(A)P(B \mid A) = \frac{P(A \text{ and } B)}{P(A)}

Rearranging gives the multiplication rule:

P(A and B)=P(A)P(BA)\boxed{P(A \text{ and } B) = P(A) \cdot P(B \mid A)}

This says: the probability of both AA and BB happening is the probability that AA happens, times the probability that BB happens given that AA has already happened.

Nested decisions in our example

Suppose we organise our two forecasters into a hierarchy. We have a “weather service” that first picks a forecasting agency (branch), then picks a specific forecaster within that agency.

  • Branch decision: Pick agency 1 with probability g1=0.7g_1 = 0.7 or agency 2 with probability g2=0.3g_2 = 0.3.
  • Within-branch decision: If agency 1 is picked, choose forecaster A with probability gA1=0.8g_{A|1} = 0.8 or forecaster B with probability gB1=0.2g_{B|1} = 0.2.

The probability of reaching forecaster A through agency 1 is a nested decision — two choices in sequence:

P(agency 1 and forecaster A)=P(agency 1)P(forecaster Aagency 1)P(\text{agency 1 and forecaster A}) = P(\text{agency 1}) \cdot P(\text{forecaster A} \mid \text{agency 1}) =g1gA1=0.7×0.8=0.56= g_1 \cdot g_{A|1} = 0.7 \times 0.8 = 0.56

This is the multiplication rule in action. The notation gA1g_{A|1} means “the probability of choosing forecaster A given that we are in agency 1.” The vertical bar | always means “given that” — it separates what we are asking about from what we are conditioning on.

The law of total probability with nested decisions

If the observation yy can be generated through multiple paths (agency 1 → forecaster A, agency 1 → forecaster B, agency 2 → forecaster A, etc.), the total probability of yy sums over all paths:

P(y)=igijgjif(yμij)P(y) = \sum_i g_i \sum_j g_{j|i} \cdot f(y \mid \mu_{ij})

This is the law of total probability applied to a two-level hierarchy. Each path has probability gigjig_i \cdot g_{j|i} (by the multiplication rule), and generates yy with density f(yμij)f(y \mid \mu_{ij}). We sum over all possible paths.

In Part 1, this is exactly equation (4) from Jordan & Jacobs (1993) — the probability model for the Hierarchical Mixture of Experts.

Numerical check

Suppose agency 2 has two different forecasters, C and D, with gC2=0.5g_{C|2} = 0.5, gD2=0.5g_{D|2} = 0.5, predictions μC=19\mu_C = 19, μD=25\mu_D = 25. Using unit-variance Gaussians:

Path probabilities and likelihoods for y=22y = 22:

  • Agency 1, forecaster A: 0.7×0.8×e12(2220)2=0.56×0.135=0.07580.7 \times 0.8 \times e^{-\frac{1}{2}(22-20)^2} = 0.56 \times 0.135 = 0.0758
  • Agency 1, forecaster B: 0.7×0.2×e12(2226)2=0.14×0.000335=0.00004690.7 \times 0.2 \times e^{-\frac{1}{2}(22-26)^2} = 0.14 \times 0.000335 = 0.0000469
  • Agency 2, forecaster C: 0.3×0.5×e12(2219)2=0.15×e4.5=0.15×0.0111=0.001670.3 \times 0.5 \times e^{-\frac{1}{2}(22-19)^2} = 0.15 \times e^{-4.5} = 0.15 \times 0.0111 = 0.00167
  • Agency 2, forecaster D: 0.3×0.5×e12(2225)2=0.15×e4.5=0.15×0.0111=0.001670.3 \times 0.5 \times e^{-\frac{1}{2}(22-25)^2} = 0.15 \times e^{-4.5} = 0.15 \times 0.0111 = 0.00167

Total: P(22)=0.0758+0.0000469+0.00167+0.00167=0.0792P(22) = 0.0758 + 0.0000469 + 0.00167 + 0.00167 = 0.0792.

Forecaster A through agency 1 dominates, contributing 0.07580.0758 out of 0.07920.0792 — about 96% of the total probability. The hierarchy has identified the right path.


12. The Multinomial Distribution

When the gating network picks one of nn experts, it is making a multinomial (or categorical) decision. A multinomial distribution generalises a coin flip to nn outcomes. Instead of just Heads or Tails, we have outcomes 1,2,,n1, 2, \ldots, n with probabilities p1,p2,,pnp_1, p_2, \ldots, p_n where ipi=1\sum_i p_i = 1.

For our two-forecaster example (n=2n = 2), the multinomial reduces to a single coin flip: choose forecaster A with probability pAp_A or forecaster B with probability pB=1pAp_B = 1 - p_A. But in MoE, the gating network may choose among many experts — 4, 8, or even thousands. The multinomial distribution handles any number of outcomes.

The probability of outcome ii in a single draw is simply:

P(outcome =i)=piP(\text{outcome } = i) = p_i

That is literally it. The term “multinomial” sounds imposing, but for a single draw it is just “pick option ii with probability pip_i.” The softmax function (Section 3) is the standard way to parameterise a multinomial distribution — it takes arbitrary scores and produces valid multinomial probabilities.

Where this appears in MoE

In the HME, there are multiple multinomial decisions:

  • The top-level gating network makes a multinomial decision over branches: pick branch ii with probability gig_i.
  • Each lower-level gating network makes a multinomial decision over experts within a branch: pick expert jj with probability gjig_{j|i}.

Each of these is a separate multinomial distribution, parameterised by its own softmax. The nested structure — first a multinomial over branches, then a conditional multinomial over experts — is what creates the tree-structured probability model from Section 11.

Numerical check

With 4 forecasters and scores s1=2.0s_1 = 2.0, s2=1.0s_2 = 1.0, s3=0.5s_3 = 0.5, s4=0.5s_4 = -0.5:

e2.0=7.389,e1.0=2.718,e0.5=1.649,e0.5=0.607e^{2.0} = 7.389, \quad e^{1.0} = 2.718, \quad e^{0.5} = 1.649, \quad e^{-0.5} = 0.607

Sum: 7.389+2.718+1.649+0.607=12.3637.389 + 2.718 + 1.649 + 0.607 = 12.363.

p1=7.38912.363=0.598,p2=2.71812.363=0.220,p3=1.64912.363=0.133,p4=0.60712.363=0.049p_1 = \frac{7.389}{12.363} = 0.598, \quad p_2 = \frac{2.718}{12.363} = 0.220, \quad p_3 = \frac{1.649}{12.363} = 0.133, \quad p_4 = \frac{0.607}{12.363} = 0.049

Check: 0.598+0.220+0.133+0.049=1.0000.598 + 0.220 + 0.133 + 0.049 = 1.000. This is a valid multinomial distribution over 4 outcomes. The gating network rolls this 4-sided weighted die to pick an expert.


13. The Gradient of the Mixture Loss

To train MoE, we need the derivative of the loss E=lnLE = -\ln L with respect to each expert’s prediction μi\mu_i, where L=jpjfjL = \sum_j p_j f_j is the mixture likelihood. Let us derive this step by step.

Step 1. Since E=lnLE = -\ln L, by the chain rule:

Eμi=1LLμi\frac{\partial E}{\partial \mu_i} = -\frac{1}{L} \cdot \frac{\partial L}{\partial \mu_i}

Step 2. Since L=jpjfjL = \sum_j p_j f_j and only the ii-th term depends on μi\mu_i:

Lμi=pifiμi\frac{\partial L}{\partial \mu_i} = p_i \cdot \frac{\partial f_i}{\partial \mu_i}

Step 3. For the Gaussian fi=exp ⁣((yμi)22)f_i = \exp\!\left(-\frac{(y - \mu_i)^2}{2}\right) (dropping the constant), we need the derivative. Let g=(yμi)22g = -\frac{(y - \mu_i)^2}{2}, so fi=egf_i = e^g. By the chain rule, fiμi=eggμi\frac{\partial f_i}{\partial \mu_i} = e^g \cdot \frac{\partial g}{\partial \mu_i}. Now:

gμi=122(yμi)(1)=(yμi)\frac{\partial g}{\partial \mu_i} = -\frac{1}{2} \cdot 2(y - \mu_i) \cdot (-1) = (y - \mu_i)

The 12-\frac{1}{2} and the 22 from the power rule cancel, and the (1)(-1) comes from differentiating (yμi)(y - \mu_i) with respect to μi\mu_i. So:

fiμi=fi(yμi)\frac{\partial f_i}{\partial \mu_i} = f_i \cdot (y - \mu_i)

Step 4. Combining steps 1–3:

Eμi=pifiL(yμi)\frac{\partial E}{\partial \mu_i} = -\frac{p_i \cdot f_i}{L} \cdot (y - \mu_i)

Step 5. Recognise pifiL\frac{p_i \cdot f_i}{L} as the posterior probability hih_i from Bayes’ theorem (Section 8):

Eμi=hi(yμi)\boxed{\frac{\partial E}{\partial \mu_i} = -h_i (y - \mu_i)}

This is equation (1.5) from Jacobs et al. (1991). The gradient for expert ii is proportional to two things: the posterior probability hih_i (how responsible this expert is for the data point) and the prediction error (yμi)(y - \mu_i) (how far off the expert’s prediction is). If hih_i is small — meaning the expert is not responsible — the gradient is small and the expert barely updates. If hih_i is large, the expert gets a strong push toward the target. This is the mathematical mechanism behind expert specialisation.

Numerical check

Using pA=0.6p_A = 0.6, fA=0.135f_A = 0.135, L=0.08133L = 0.08133, y=22y = 22, μA=20\mu_A = 20:

hA=0.6×0.1350.08133=0.08120.08133=0.9984h_A = \frac{0.6 \times 0.135}{0.08133} = \frac{0.0812}{0.08133} = 0.9984 EμA=0.9984×(2220)=0.9984×2=1.997\frac{\partial E}{\partial \mu_A} = -0.9984 \times (22 - 20) = -0.9984 \times 2 = -1.997

The negative gradient means the loss decreases when μA\mu_A increases — forecaster A should increase its prediction from 20 toward the target 22. This is the correct direction.

For forecaster B:

hB=0.0016h_B = 0.0016 EμB=0.0016×(2226)=0.0016×(4)=+0.0064\frac{\partial E}{\partial \mu_B} = -0.0016 \times (22 - 26) = -0.0016 \times (-4) = +0.0064

The gradient for B is +0.0064+0.0064. The gradient for A is 1.997-1.997. Which gradient is “larger”? This brings us to a subtle but important distinction.


14. Gradient Magnitude vs. Signed Value

Gradients are signed quantities — they can be positive or negative. The sign tells you the direction: a negative gradient means “increase the parameter to decrease the loss,” and a positive gradient means “decrease the parameter to decrease the loss.” But when we ask which expert is learning faster, we care about the magnitude (absolute value) of the gradient, not its sign.

The magnitude of a number xx is x|x| — its distance from zero on the number line, ignoring the sign:

5=5,+3=3,5>+3|{-5}| = 5, \quad |{+3}| = 3, \quad |{-5}| > |{+3}|

Note that 5<+3-5 < +3 as signed numbers, but 5>+3|{-5}| > |{+3}| — negative five is smaller than positive three, but it is farther from zero.

Why this matters in MoE

From our gradient computation:

EμA=1.997,EμB=+0.0064\frac{\partial E}{\partial \mu_A} = -1.997, \quad \frac{\partial E}{\partial \mu_B} = +0.0064

As signed numbers, +0.0064>1.997+0.0064 > -1.997. But the magnitudes tell a different story:

1.997=1.997,+0.0064=0.0064|{-1.997}| = 1.997, \quad |{+0.0064}| = 0.0064

The magnitude of A’s gradient (1.9971.997) is 312 times larger than B’s (0.00640.0064). Forecaster A is learning 312 times faster from this data point.

When you read in Part 1 that “expert 2 gets a larger gradient magnitude,” this means gradient2>gradient1|\text{gradient}_2| > |\text{gradient}_1| — the absolute value is bigger, meaning a stronger push. The sign tells the direction of the push; the magnitude tells how hard the push is. In MoE, the posterior hih_i controls the magnitude: an expert with high posterior gets a large hi(yμi)|h_i \cdot (y - \mu_i)| and learns aggressively; an expert with near-zero posterior barely moves.


15. Indicator Variables and the Complete-Data Log-Likelihood

The EM algorithm, which we will derive in Part 1, relies on a clever trick: imagine that for each data point, we know which expert generated it. We represent this knowledge with indicator variables.

Define zi=1z_i = 1 if forecaster ii generated the observation, and zi=0z_i = 0 otherwise. Exactly one indicator is 1 for each data point — the data came from exactly one forecaster.

If we knew the ziz_i‘s, the complete-data log-likelihood would be:

lnP(y,z)=iziln(pifi)=izi[lnpi+lnfi]\ln P(y, z) = \sum_i z_i \ln(p_i \cdot f_i) = \sum_i z_i [\ln p_i + \ln f_i]

The second step uses the logarithm product rule from Section 6: ln(pifi)=lnpi+lnfi\ln(p_i \cdot f_i) = \ln p_i + \ln f_i.

Compare this to the incomplete-data log-likelihood from Section 10:

lnP(y)=lnipifi\ln P(y) = \ln \sum_i p_i \cdot f_i

The indicator variables ziz_i have performed a crucial transformation: the logarithm has moved inside the summation. Instead of lni()\ln \sum_i (\cdots) (log of a sum — hard), we have iziln()\sum_i z_i \ln(\cdots) (sum of logs — easy). The parameters of different experts now appear in separate terms, making maximisation straightforward.

Numerical check

Suppose we knew forecaster A generated the data (zA=1z_A = 1, zB=0z_B = 0):

lnP(y,z)=1[ln(0.6)+ln(0.135)]+0[ln(0.4)+ln(0.000335)]\ln P(y, z) = 1 \cdot [\ln(0.6) + \ln(0.135)] + 0 \cdot [\ln(0.4) + \ln(0.000335)] =ln(0.6)+ln(0.135)=0.511+(2.0)=2.511= \ln(0.6) + \ln(0.135) = -0.511 + (-2.0) = -2.511

If instead forecaster B generated it (zA=0z_A = 0, zB=1z_B = 1):

lnP(y,z)=0+1[ln(0.4)+ln(0.000335)]=0.916+(8.0)=8.916\ln P(y, z) = 0 + 1 \cdot [\ln(0.4) + \ln(0.000335)] = -0.916 + (-8.0) = -8.916

The complete-data likelihood sharply favours the hypothesis that A generated the data (2.511-2.511 vs. 8.916-8.916).

Of course, we do not know the ziz_i‘s. The EM algorithm handles this by replacing ziz_i with its expected value — the posterior probability hih_i from Bayes’ theorem (Section 8). For our numbers: E[zA]=hA=0.9984E[z_A] = h_A = 0.9984 and E[zB]=hB=0.0016E[z_B] = h_B = 0.0016. This is the E step. The M step then maximises the expected complete-data log-likelihood with respect to the model parameters. The full derivation appears in Part 1.


16. The Matrix Inverse Update: Sherman-Morrison-Woodbury

The on-line version of MoE (Section 11 of Part 1) updates expert parameters after each data point, without re-processing the entire dataset. This requires maintaining a running estimate of an inverse matrix. The Sherman-Morrison-Woodbury formula tells us how to update a matrix inverse when a small change is made to the original matrix.

The problem

Suppose we have a matrix AA and we know its inverse A1A^{-1}. Now AA changes by a small amount — specifically, a rank-1 update: Anew=A+uvTA_{\text{new}} = A + \mathbf{u}\mathbf{v}^T, where u\mathbf{u} and v\mathbf{v} are column vectors and uvT\mathbf{u}\mathbf{v}^T is their outer product (a matrix where entry (i,j)(i,j) is uivju_i v_j).

Computing Anew1A_{\text{new}}^{-1} from scratch is expensive — it takes O(n3)O(n^3) operations for an n×nn \times n matrix. But if we already know A1A^{-1}, the Sherman-Morrison formula gives us Anew1A_{\text{new}}^{-1} in only O(n2)O(n^2) operations:

(A+uvT)1=A1A1uvTA11+vTA1u\boxed{(A + \mathbf{u}\mathbf{v}^T)^{-1} = A^{-1} - \frac{A^{-1}\mathbf{u}\mathbf{v}^T A^{-1}}{1 + \mathbf{v}^T A^{-1} \mathbf{u}}}

The formula says: start with the old inverse A1A^{-1}, then subtract a correction term. The correction term involves multiplying the old inverse by u\mathbf{u} and v\mathbf{v} — all O(n2)O(n^2) or cheaper operations.

Verification

We can verify this by checking that (A+uvT)(A+uvT)1=I(A + \mathbf{u}\mathbf{v}^T) \cdot (A + \mathbf{u}\mathbf{v}^T)^{-1} = I. Multiply the original matrix by the proposed inverse:

(A+uvT)(A1A1uvTA11+vTA1u)(A + \mathbf{u}\mathbf{v}^T)\left(A^{-1} - \frac{A^{-1}\mathbf{u}\mathbf{v}^T A^{-1}}{1 + \mathbf{v}^T A^{-1}\mathbf{u}}\right)

Distribute:

=AA1AA1uvTA11+vTA1u+uvTA1uvTA1uvTA11+vTA1u= AA^{-1} - \frac{AA^{-1}\mathbf{u}\mathbf{v}^T A^{-1}}{1 + \mathbf{v}^T A^{-1}\mathbf{u}} + \mathbf{u}\mathbf{v}^T A^{-1} - \frac{\mathbf{u}\mathbf{v}^T A^{-1}\mathbf{u}\mathbf{v}^T A^{-1}}{1 + \mathbf{v}^T A^{-1}\mathbf{u}}

Since AA1=IAA^{-1} = I, the first term is II. Simplify AA1u=uAA^{-1}\mathbf{u} = \mathbf{u} in the second term. In the fourth term, vTA1u\mathbf{v}^T A^{-1}\mathbf{u} is a scalar, call it cc. So:

=IuvTA11+c+uvTA1cuvTA11+c= I - \frac{\mathbf{u}\mathbf{v}^T A^{-1}}{1 + c} + \mathbf{u}\mathbf{v}^T A^{-1} - \frac{c \cdot \mathbf{u}\mathbf{v}^T A^{-1}}{1 + c}

Combine the last three terms (they all contain uvTA1\mathbf{u}\mathbf{v}^T A^{-1}):

=I+uvTA1(11+c+1c1+c)= I + \mathbf{u}\mathbf{v}^T A^{-1}\left(- \frac{1}{1 + c} + 1 - \frac{c}{1 + c}\right)

The expression in parentheses is (1)+(1+c)c1+c=01+c=0\frac{-(1) + (1+c) - c}{1+c} = \frac{0}{1+c} = 0.

So the result is II. The formula is correct.

Numerical check

Let A=(2113)A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}, so A1=15(3112)=(0.60.20.20.4)A^{-1} = \frac{1}{5}\begin{pmatrix} 3 & -1 \\ -1 & 2 \end{pmatrix} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix}.

Let u=(10)\mathbf{u} = \begin{pmatrix} 1 \\ 0 \end{pmatrix} and v=(01)\mathbf{v} = \begin{pmatrix} 0 \\ 1 \end{pmatrix}. Then uvT=(0100)\mathbf{u}\mathbf{v}^T = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix}.

So Anew=(2213)A_{\text{new}} = \begin{pmatrix} 2 & 2 \\ 1 & 3 \end{pmatrix}.

Applying the formula:

A1u=(0.60.2),vTA1=(0.20.4)A^{-1}\mathbf{u} = \begin{pmatrix} 0.6 \\ -0.2 \end{pmatrix}, \quad \mathbf{v}^T A^{-1} = \begin{pmatrix} -0.2 & 0.4 \end{pmatrix} vTA1u=0.2×1+0.4×0=0.2\mathbf{v}^T A^{-1}\mathbf{u} = -0.2 \times 1 + 0.4 \times 0 = -0.2 1+vTA1u=1+(0.2)=0.81 + \mathbf{v}^T A^{-1}\mathbf{u} = 1 + (-0.2) = 0.8 A1uvTA1=(0.60.2)(0.20.4)=(0.120.240.040.08)A^{-1}\mathbf{u} \cdot \mathbf{v}^T A^{-1} = \begin{pmatrix} 0.6 \\ -0.2 \end{pmatrix} \begin{pmatrix} -0.2 & 0.4 \end{pmatrix} = \begin{pmatrix} -0.12 & 0.24 \\ 0.04 & -0.08 \end{pmatrix} Anew1=(0.60.20.20.4)10.8(0.120.240.040.08)=(0.60.20.20.4)(0.150.300.050.10)A_{\text{new}}^{-1} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix} - \frac{1}{0.8}\begin{pmatrix} -0.12 & 0.24 \\ 0.04 & -0.08 \end{pmatrix} = \begin{pmatrix} 0.6 & -0.2 \\ -0.2 & 0.4 \end{pmatrix} - \begin{pmatrix} -0.15 & 0.30 \\ 0.05 & -0.10 \end{pmatrix} =(0.750.500.250.50)= \begin{pmatrix} 0.75 & -0.50 \\ -0.25 & 0.50 \end{pmatrix}

Verify: AnewAnew1=(2213)(0.750.500.250.50)=(1.50.51.0+1.00.750.750.5+1.5)=(1001)A_{\text{new}} \cdot A_{\text{new}}^{-1} = \begin{pmatrix} 2 & 2 \\ 1 & 3 \end{pmatrix}\begin{pmatrix} 0.75 & -0.50 \\ -0.25 & 0.50 \end{pmatrix} = \begin{pmatrix} 1.5 - 0.5 & -1.0 + 1.0 \\ 0.75 - 0.75 & -0.5 + 1.5 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}. Correct.

How this appears in MoE

In the on-line MoE algorithm, each expert maintains a matrix RijR_{ij} (the inverse of a weighted sum of outer products xxT\mathbf{x}\mathbf{x}^T). When a new data point x(t)\mathbf{x}^{(t)} arrives, RijR_{ij} must be updated. The Sherman-Morrison formula gives:

Rij(t)=λ1Rij(t1)λ1Rij(t1)x(t)x(t)TRij(t1)λ[hij(t)]1+x(t)TRij(t1)x(t)R_{ij}^{(t)} = \lambda^{-1} R_{ij}^{(t-1)} - \lambda^{-1} \frac{R_{ij}^{(t-1)} \mathbf{x}^{(t)} \mathbf{x}^{(t)T} R_{ij}^{(t-1)}}{\lambda [h_{ij}^{(t)}]^{-1} + \mathbf{x}^{(t)T} R_{ij}^{(t-1)} \mathbf{x}^{(t)}}

This has exactly the form of the Sherman-Morrison formula: the old inverse (λ1R(t1)\lambda^{-1} R^{(t-1)}) minus a correction term involving the outer product x(t)x(t)T\mathbf{x}^{(t)}\mathbf{x}^{(t)T}. The parameter λ\lambda is a decay factor that down-weights old data, and hij(t)h_{ij}^{(t)} is the posterior probability that controls how much this data point affects expert (i,j)(i,j)‘s update. Without this formula, the on-line algorithm would need to re-invert a matrix at every time step — making it impractical.


Summary

We have built a complete mathematical toolkit for Mixture of Experts. Squared error measures prediction quality. Expected value averages over stochastic selections, giving us the competitive error function. The softmax function converts arbitrary scores into valid probabilities for the gating network. The Gaussian density connects squared error to probability, and the likelihood function flips the perspective from “how probable is the data” to “how plausible is the model.” The logarithm converts products into sums, and the negative log-likelihood gives us a loss function to minimise. Bayes’ theorem updates prior gating probabilities into posterior responsibilities — the mechanism that makes experts specialise. Mixture models describe the generative process, and the log-of-a-sum structure of the mixture log-likelihood creates the computational challenge that the EM algorithm solves. Conditional probability and the multiplication rule handle nested decisions in the hierarchical architecture, while the multinomial distribution formalises the gating network’s multi-way choices. Differentiating the mixture loss yields the posterior-weighted gradient, where gradient magnitude — not signed value — determines how fast each expert learns. Indicator variables with the complete-data log-likelihood show how the EM algorithm brings the logarithm inside the sum. And the Sherman-Morrison-Woodbury formula enables efficient on-line updates without re-inverting matrices.

With these tools in hand, we are ready for Part 1, where we put them all together to derive the Mixture of Experts framework from scratch — cooperative and competitive errors, the mixture-of-Gaussians interpretation, hierarchical mixtures, and the EM algorithm.


Previous: MoE Load Balancing from Scratch
Next: Mixture of Experts from Scratch — Part 1

Enjoyed this post?

Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.