Mathematical Foundations

VII.1 · Information Theory

Why can a positive probability disappear during multiplication?

Change actual probability factors, then compare their exact product with direct binary64 multiplication and a sum of logarithms.

Products and log-likelihood

Log-likelihood by repeated block
Log-likelihood by repeated blockExact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.0-2.3025900.51

Horizontal: repeated blocks · vertical: approximate natural log of the product; impossible blocks have −∞, outside finite axes

Explorer: Actual factors in one repeated block
FactorExact plog p (nats)
10.5≈ -0.693147
20.25≈ -1.38629
30.8≈ -0.223144

Exact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.

Exact joint probability
0.1
Direct binary64 multiplication
0.1
Log-likelihood (nats)
−2.302585
Exponentiated log-likelihood
0.1

= exact rational or mathematical boundary · ≈ approximate binary64 evaluation

from 0 to 1, up to 2 decimal places

from 0 to 1, up to 2 decimal places

from 0 to 1, up to 2 decimal places

from 1 to 1200, whole numbers only

Exact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.

ℓ=∑j=1r∑ilog⁡pi\ell=\sum_{j=1}^{r}\sum_i\log p_i
log⁡(ab)=log⁡a+log⁡b(a,b>0)\log(ab)=\log a+\log b\quad(a,b>0)

Exact probability sources

Factors: 0.5, 0.25, 0.8. Repeat count 1.

L=(∏ipi)rL=\left(\prod_i p_i\right)^r

Change actual probability factors, then compare their exact product with direct binary64 multiplication and a sum of logarithms.

Intuition

A long chain of likely events can have a tiny joint probability. The multiplication path can lose that positive value to underflow. Adding logs retains a useful finite score even when converting the score back to a probability underflows.

Definition

For independent observed events with probabilities pi∈[0,1]p_i\in[0,1], L=∏ipiL=\prod_i p_i. For strictly positive factors, the natural log-likelihood is ℓ=log⁡L=∑ilog⁡pi\ell=\log L=\sum_i\log p_i. Here a repeated block gives L=(∏ipi)rL=(\prod_i p_i)^r. Independence is an explicit modeling assumption; correlated events require conditional factors.

Mechanics

The three factor controls and repeated-block count own the state. The table shows exact rational factors and approximate log factors. The graph plots log-likelihood after each sampled block; the inspector shows the same final quantity alongside the actual direct multiplication result.

Derivation

The identity log⁡(ab)=log⁡a+log⁡b\log(ab)=\log a+\log b follows because if a=eua=e^u and b=evb=e^v, then ab=eu+vab=e^{u+v}. Repeating it yields ℓ\ell. Logs are increasing, so maximizing positive likelihood and maximizing its log choose the same parameters. Changing to negative log-likelihood turns that maximization into minimization.

Worked examples

At p=(1/2,1/4,4/5)p=(1/2,1/4,4/5), L=1/10L=1/10 and ℓ=−log⁡10≈−2.302585\ell=-\log 10\approx-2.302585 nats. At three factors of 1/21/2 repeated 400 times, L=2−1200>0L=2^{-1200}>0 and ℓ=−1200log⁡2≈−831.776617\ell=-1200\log2\approx-831.776617. Binary64 cannot represent that positive product, but the log remains finite.

Worked example: Products and log-likelihood

Log-likelihood by repeated block
Log-likelihood by repeated blockExact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.0-2.3025900.51

Horizontal: repeated blocks · vertical: approximate natural log of the product; impossible blocks have −∞, outside finite axes

Worked example: Actual factors in one repeated block
FactorExact plog p (nats)
10.5≈ -0.693147
20.25≈ -1.38629
30.8≈ -0.223144

Exact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.

Exact joint probability
0.1
Direct binary64 multiplication
0.1
Log-likelihood (nats)
−2.302585
Exponentiated log-likelihood
0.1

= exact rational or mathematical boundary · ≈ approximate binary64 evaluation

from 0 to 1, up to 2 decimal places

from 0 to 1, up to 2 decimal places

from 0 to 1, up to 2 decimal places

from 1 to 1200, whole numbers only

Exact product = 0.1. Direct multiplication ≈ 0.1. Log-likelihood ≈ -2.30259 nats. All factors are positive.

ℓ=∑j=1r∑ilog⁡pi\ell=\sum_{j=1}^{r}\sum_i\log p_i
log⁡(ab)=log⁡a+log⁡b(a,b>0)\log(ab)=\log a+\log b\quad(a,b>0)

Exact probability sources

Factors: 0.5, 0.25, 0.8. Repeat count 1.

L=(∏ipi)rL=\left(\prod_i p_i\right)^r

Replay the actual mathematical inputs, terms and result.

Executable reference code

Run the standalone pedagogical version to see each operation; the NumPy companion shows the practical array form. Both include assertions so you can check the worked examples as you run them.

code/python/information_logs.py
"""Exact independent-event product and approximate natural-log likelihood."""
from fractions import Fraction
from math import log, inf, prod, isfinite
def product(probabilities, repeat=1):
    if not 1 <= len(probabilities) <= 6 or type(repeat) is not int or not 1 <= repeat <= 1200:
        raise ValueError('Use 1–6 factors and 1–1200 repetitions')
    masses = [Fraction(str(p)) for p in probabilities]
    if any(p < 0 or p > 1 for p in masses): raise ValueError('Probabilities in [0,1]')
    exact = prod(masses) ** repeat
    log_likelihood = -inf if not exact else sum(log(p.numerator)-log(p.denominator) for p in masses) * repeat
    direct = 1.0
    for _ in range(repeat):
        for p in masses: direct *= float(p)
    return exact, direct, log_likelihood
if __name__ == '__main__':
    assert product([.5,.25,.8])[0] == Fraction(1,10)
    exact, direct, ll = product([.5,.5,.5],400)
    assert exact > 0 and direct == 0 and isfinite(ll)
    assert product([0,.5])[2] == -inf
    print(product([.5,.25,.8]))
code/python/information_logs_numpy.py
"""NumPy binary64 multiplication versus log-space accumulation; zeros stay impossible."""
import numpy as np
def product(probabilities, repeat=1):
    p = np.asarray(probabilities, dtype=float)
    if p.ndim != 1 or not 1 <= p.size <= 6 or not np.all(np.isfinite(p)) or np.any((p<0)|(p>1)) or type(repeat) is not int or not 1<=repeat<=1200:
        raise ValueError('Finite probabilities in [0,1]; bounded repetition')
    with np.errstate(divide='ignore', under='ignore'):
        return float(np.prod(np.tile(p,repeat))), float(repeat*np.log(p).sum())
if __name__ == '__main__':
    assert np.isclose(product([.5,.25,.8])[0],.1)
    direct,ll=product([.5,.5,.5],400)
    assert direct==0 and np.isfinite(ll)
    assert product([0,.5])[1]==-np.inf
    print(product([.5,.25,.8]))

Explore

Predict what happens when one factor reaches zero. Then select Positive product underflows and distinguish the exact product from the displayed direct zero. Raise a probability and explain why the log-likelihood increases toward zero.

Common mistakes and counterexamples

Log probability is nonpositive; surprise is its negative. A finite log does not make an impossible event possible. Substituting epsilon for zero changes the model. Multiplying marginal probabilities without independence is not generally a joint probability.

Numerical considerations

Factors use exact decimal probabilities with at most two places; there are 1–6 supported factors and 1–1200 repetitions. Exact products remain BigInt fractions. Long exact products are displayed as an equivalent power of the exact block product. The approximate log is evaluated from the factors, independently checked against Decimal100 log of the exact product. Direct multiplication and exponentiating the log can underflow. The extended log of zero is −∞; negative, non-finite or out-of-range probability inputs are rejected.

Engineering and ML connections

The Statistics likelihood chapters treat observed data as fixed while candidate parameters vary. Log-likelihood makes long sequences additive. Classification and token loss will negate these log scores to give an optimization objective.

Continue from the preceding mathematical connection.

Exercises

Predict, compute and explain the actual quantities. Each question offers corrective feedback and a worked solution.

calculation

A probability product

Compute the joint probability of independent events with probabilities 1/2, 1/4 and 4/5.

calculation

Add base-2 logs

For probabilities 1/2 and 1/4, compute the sum of base-2 log probabilities.

prediction

Predict underflow

The exact product (1/2)^1200 is positive, but binary64 direct multiplication gives zero. What does a finite log mean?

Choose one answer

explanation

Zero factor

What is the extended log of a product containing a zero probability?

Choose one answer

construction

Repair a domain error

A probability input is −0.2. What should the computation do?

Choose one answer

Summary and checkpoints

A long chain of likely events can have a tiny joint probability. The multiplication path can lose that positive value to underflow. Adding logs retains a useful finite score even when converting the score back to a probability underflows. Check the assumptions, unit and boundary before interpreting the computed result.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available