mml-book / mml-book.github.io

Companion webpage to the book "Mathematics For Machine Learning"
12.71k stars 2.36k forks source link

Typo in Example 11.5 (Weight Parameter Updates) #770

Closed ckwastra closed 6 months ago

ckwastra commented 6 months ago

Describe the mistake In the provided text (emphasis mine):

[...] This is also evidenced by the log-likelihood values, which increased from 28.3 (initialization) to 14.4 after one complete update cycle.

The correct values for these two numbers are -28.3 and -14.4, respectively.

Location

  1. Draft (2024-01-02)
  2. Chapter 11
  3. Page 360
  4. Line -8

Proposed solution Correct the values by changing 28.3 to -28.3 and 14.4 to -14.4.

Additional context To obtain the accurate values, run the following Python script:

from statistics import NormalDist
from math import log, sqrt

class Theta:
    def __init__(self, K, pi, mu, sigma):
        self.K = K
        self.pi = pi
        self.mu = mu
        self.sigma = sigma

def p(x, theta):
    v = 0
    for k in range(0, theta.K):
        v += theta.pi[k] * NormalDist(theta.mu[k], sqrt(theta.sigma[k])).pdf(x)
    return v

def logp(X, theta):
    v = 0
    for n in range(0, len(X)):
        v += log(p(X[n], theta))
    return v

X = [-3, -2.5, -1, 0, 2, 4, 5]
theta = Theta(3, [1 / 3] * 3, [-4, 0, 8], [1, 0.2, 3])
print("%.1f" % logp(X, theta))  # -28.3
theta = Theta(3, [0.29, 0.29, 0.42], [-2.7, -0.4, 3.7], [0.14, 0.44, 1.53])
print("%.1f" % logp(X, theta))  # -14.4
mpd37 commented 6 months ago

you are correct. thanks for pointing this out