Mathematical Foundations

VIII.10 · Numerical Computing

What must you record to reproduce an experiment?

Replay an explicit fixed pseudorandom stream from its seed or recorded state. Contrast exact discrete replay with a controlled floating-point association change and a stated numerical comparison budget.

Replay a seeded stream and compare numerical orders

Recorded uint32 initial state 42; replay of the same specified LCG32 algorithm matches all 6 discrete states exactly. LCG32: state = (1664525·state + 1013904223) mod 2^32; uniform = state/2^32. The two controlled binary64 associations do not satisfy the chosen numeric budget. A seed alone does not guarantee another RNG, library version, hardware or parallel reduction reproduces the same experiment.

Specified pseudorandom algorithm
LCG32, fixed constants and uint32 modulus
Discrete state replay matches exactly
true
Left-associated binary64 result
1
Right-associated binary64 result
0
Exact intended mathematical sum
1
Exact signed left answer error
0
Exact signed right answer error
-1
Relative left error (nonzero reference)
0
Relative right error (nonzero reference)
1
Measured association difference
1
Chosen absolute/relative comparison budget
0.000000000002
Within chosen budget?
false

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

sk+1=(1664525sk+1013904223) mod 232,uk=sk+1/232s_{k+1}=(1664525s_k+1013904223)\bmod 2^{32},\quad u_k=s_{k+1}/2^{32}
∣x−y∣≤atol+rtolmax⁡(∣x∣,∣y∣)|x-y|\le a_{\mathrm{tol}}+r_{\mathrm{tol}}\max(|x|,|y|)
(a+b)+c≠a+(b+c)can occur in floating-point arithmetic(a+b)+c\ne a+(b+c)\quad\text{can occur in floating-point arithmetic}
Actual stream and explicit replay. States are exact integers; uniforms are exact dyadic fractions.
samplebefore stateafter statereplayed afteruniform
042108381427310838142731083814273/4294967296
1108381427337849418837849418894623547/1073741824
2378494188247940386724794038672479403867/4294967296
32479403867955863294955863294477931647/2147483648
4955863294161344826116134482611613448261/4294967296
516134482611102256321102256323444551/134217728
Same represented operands, different recorded association.
orderfirst additionfinal resultexact signed answer error
(a+b)+c≈ 0≈ 10
a+(b+c)≈ −1×10¹⁶≈ 0-1

LCG sample 0

Replay uses the fixed uint32 algorithm and the recorded state. A seed alone is not a cross-implementation guarantee.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
before42
after1083814273
uniform1083814273/4294967296

Finite decimal values within ±1e300. Both associations use the same represented binary64 inputs.

Finite decimal in [0,1]; select from the meaning and scale of the experiment.

Finite decimal in [0,1]; discrete RNG states still compare exactly.

from 0 to 4294967295, whole numbers only

from 1 to 16, whole numbers only

Replay a fixed seeded uint32 stream and a controlled binary64 association change. Exact state checks and budgeted numerical comparisons remain separate.

Intuition: a reproducible computation has a specification

A seed names a starting point only after the pseudorandom generator and its state interpretation are specified. An operation sequence, input values and comparison policy are likewise part of a numerical experiment. Two runs can share a seed yet use different random algorithms or arithmetic orders.

The explorer uses one explicit uint32 generator, records every before/after state and exposes the actual uniform fractions. It independently replays the same specified stream and checks discrete states exactly. Rendering never draws from a global or unseeded random source.

The contrasting arithmetic experiment is also deterministic. It evaluates the same three represented operands with two association orders, producing a controlled numerical variation. This demonstrates a reason parallel or changed reductions can differ; it does not pretend that this sequential simulator is itself nondeterministic hardware.

Definition: seed, state and numeric agreement

The fixed generator is a 32-bit linear congruential generator:

sk+1=(1664525sk+1013904223) mod 232,s_{k+1}=(1664525s_k+1013904223)\bmod2^{32}, uk=sk+1/232.u_k=s_{k+1}/2^{32}.

Seed and state lie in 0 through 232−12^{32}-1. The uniform values are exact dyadic fractions in [0,1); this simple educational stream is not a cryptographic generator or a general randomness-quality recommendation.

For finite numerical results x,y, the chosen comparison accepts

∣x−y∣≤atol+rtolmax⁡(∣x∣,∣y∣).|x-y|\le a_{\mathrm{tol}}+r_{\mathrm{tol}}\max(|x|,|y|).

Absolute tolerance carries the result’s units; relative tolerance scales with magnitude. Exact discrete outcomes such as generator states, shape dimensions and sample counts are compared exactly. Non-finite arithmetic requires its own explicit policy and is not silently accepted by this finite-value comparator.

Mechanics: record what actually happened

Each sample record contains index, prior uint32 state, next uint32 state, actual uniform value and its exact fraction. The replay column records the same algorithm run again from the recorded initial state. The semantic trace is immutable: stepping backward selects a recorded event rather than trying to reverse an RNG mutation.

The association experiment records stored a,b,c, the first sum a+b, the first sum b+c, both final answers, an exact intended-decimal reference and signed/absolute/relative errors. The tolerance comparison between the two computed answers is separate from either answer’s error to the mathematical reference.

Changing the seed or a numeric operand changes canonical state and recomputes the experiment. A rejected seed, count, non-finite operand or invalid tolerance never becomes part of mathematical rendering.

Derivation: state recurrence and nonassociativity

The recurrence advances exactly one specified uint32 state per sample. Since the modulus is a power of two, its recorded state identifies the next experiment step unambiguously for this algorithm. Resuming from a saved state reproduces a suffix of its stream when the same operations are used.

Real addition is associative, but a floating-point addition generally rounds after each operation. Thus

fl⁡(fl⁡(a+b)+c)\operatorname{fl}(\operatorname{fl}(a+b)+c)

can differ from

fl⁡(a+fl⁡(b+c)).\operatorname{fl}(a+\operatorname{fl}(b+c)).

Changing grouping changes which small terms may be lost before later cancellation. A compiler, library, vectorized reduction or parallel execution can choose a different sequence. Identical mathematical notation does not fully specify that numerical sequence.

A numerical budget should come from input uncertainty, algorithm error and required output accuracy. Making a budget large enough to hide a difference is a demonstration of acceptance under that budget, not a justification for choosing it.

Worked examples: replay and a changed association

Starting from state 42, the first two next states are exactly 1083814273 and 378494188. Their uniforms are those integers divided by 2322^{32}. The next states are 2479403867, 955863294, 1613448261 and 110225632. These are the actual committed sample sequence, not decorative histogram bins.

The resume preset starts at recorded state 378494188 and requests four samples. It reproduces the final four states of the six-sample experiment. Record the algorithm and the state, not just a statement that “seed 42 was used.”

For represented a=1e16, b=−1e16 and c=1, the exact mathematical sum is one. Left association first forms a+b=0 and returns one. Right association forms b+c=−1e16 because the one is rounded away, then returns zero. The absolute answer errors are zero and one respectively.

With both default tolerances 1e-12, the measured difference one exceeds the comparison budget. The loose-budget preset instead uses absolute tolerance one and relative tolerance zero, so the comparison accepts. The right answer still has exact error one; the acceptance flag did not change the mathematical reference.

Worked example: Replay a seeded stream and compare numerical orders

Recorded uint32 initial state 42; replay of the same specified LCG32 algorithm matches all 6 discrete states exactly. LCG32: state = (1664525·state + 1013904223) mod 2^32; uniform = state/2^32. The two controlled binary64 associations do not satisfy the chosen numeric budget. A seed alone does not guarantee another RNG, library version, hardware or parallel reduction reproduces the same experiment.

Specified pseudorandom algorithm
LCG32, fixed constants and uint32 modulus
Discrete state replay matches exactly
true
Left-associated binary64 result
1
Right-associated binary64 result
0
Exact intended mathematical sum
1
Exact signed left answer error
0
Exact signed right answer error
-1
Relative left error (nonzero reference)
0
Relative right error (nonzero reference)
1
Measured association difference
1
Chosen absolute/relative comparison budget
0.000000000002
Within chosen budget?
false

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

sk+1=(1664525sk+1013904223) mod 232,uk=sk+1/232s_{k+1}=(1664525s_k+1013904223)\bmod 2^{32},\quad u_k=s_{k+1}/2^{32}
∣x−y∣≤atol+rtolmax⁡(∣x∣,∣y∣)|x-y|\le a_{\mathrm{tol}}+r_{\mathrm{tol}}\max(|x|,|y|)
(a+b)+c≠a+(b+c)can occur in floating-point arithmetic(a+b)+c\ne a+(b+c)\quad\text{can occur in floating-point arithmetic}
Actual stream and explicit replay. States are exact integers; uniforms are exact dyadic fractions.
samplebefore stateafter statereplayed afteruniform
042108381427310838142731083814273/4294967296
1108381427337849418837849418894623547/1073741824
2378494188247940386724794038672479403867/4294967296
32479403867955863294955863294477931647/2147483648
4955863294161344826116134482611613448261/4294967296
516134482611102256321102256323444551/134217728
Same represented operands, different recorded association.
orderfirst additionfinal resultexact signed answer error
(a+b)+c≈ 0≈ 10
a+(b+c)≈ −1×10¹⁶≈ 0-1

LCG sample 0

Replay uses the fixed uint32 algorithm and the recorded state. A seed alone is not a cross-implementation guarantee.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
before42
after1083814273
uniform1083814273/4294967296

Select the recorded intermediate values. Previous, next and direct replay navigation preserve immutable numerical history.

Executable reference code

The standard-library program uses independent Python integer modular arithmetic to replay LCG states and explicit floating additions to preserve each association. It does not use Python’s built-in sum as a substitute for a specified left fold: implementations such as Python 3.12 can use improved summation.

The NumPy reference records an actual generator’s algorithm name, NumPy version and state, then restores that state into the same generator implementation for replay. It demonstrates a separately seeded NumPy stream alongside the specified educational LCG; the same seed does not make those two streams identical. Every sample is explicitly seeded or restored before it is generated.

code/python/reproducibility_seeds.py
"""Explicit uint32 algorithm and recorded state; no unseeded source."""
from fractions import Fraction as F
def generate(state,count):
    assert type(state) is int and 0<=state<2**32 and 1<=count<=16
    output=[]
    for index in range(count):
        before=state;state=(1664525*state+1013904223)%2**32
        output.append((index,before,state,F(state,2**32)))
    return output
if __name__=='__main__':
    records=generate(42,6)
    assert [row[2] for row in records]==[1083814273,378494188,2479403867,955863294,1613448261,110225632]
    assert [r[2] for r in generate(records[1][2],4)]==[r[2] for r in records[2:]]
    assert records==generate(42,6)
    a,b,c=1e16,-1e16,1.;left=(a+b)+c;right=a+(b+c)
    assert left==1 and right==0 and F.from_float(a)+F.from_float(b)+F.from_float(c)==1
    budget=1e-12+1e-12*max(abs(left),abs(right))
    assert abs(left-right)>budget
    print('recorded state and sequence',records,'controlled associations',left,right,'budget',budget)
code/python/reproducibility_seeds_numpy.py
"""Actual NumPy seeded generator replay is scoped to its named algorithm/version/state."""
import copy
import numpy as np
from reproducibility_seeds import generate
if __name__=='__main__':
    seed=42;rng=np.random.default_rng(seed)
    saved=copy.deepcopy(rng.bit_generator.state);samples=rng.random(6)
    # Explicitly seed a same-type generator, then restore the exact recorded state before drawing.
    replay=np.random.Generator(type(rng.bit_generator)(0));replay.bit_generator.state=saved
    np.testing.assert_array_equal(samples,replay.random(6))
    custom=np.array([float(row[3]) for row in generate(seed,6)])
    assert not np.array_equal(samples,custom)
    a,b,c=np.float64(1e16),np.float64(-1e16),np.float64(1)
    assert (a+b)+c==1 and a+(b+c)==0
    assert not np.isclose((a+b)+c,a+(b+c),rtol=1e-12,atol=1e-12)
    print('NumPy version',np.__version__,'algorithm',type(rng.bit_generator).__name__,'seed',seed,'state',saved,'samples',samples,'LCG samples',custom)

Explore: separate exact replay from approximate comparisons

Compare seeds 42 and 7, then resume from the recorded second state. Predict which exact discrete samples match. Change the sample count: the prefix of the same fixed stream remains unchanged, while the number of recorded draws changes.

Compare order variation with the exactly representable sum [1,2,3]. In the latter both associations return six. Try the zero-reference preset: its sum is zero, so the usual relative answer error is undefined even though absolute error and the finite comparison remain available.

Change absolute and relative tolerance independently. Explain the budget’s scale and why accepting two close numerical answers does not prove either one is close to the specified reference. Invalid or enormous input text is rejected before computation; experiment size is bounded to 16 stream samples and three operands.

Common mistakes and counterexamples

A seed alone does not identify arbitrary RNG implementations, initial state layouts or sample-consumption patterns. A fixed algorithm can also produce a different later draw if a previous code path consumed extra samples. Record version, algorithm, state and draw order when those matter.

Floating-point equality is suitable for exact discrete states and specified binary64 reference results. It is not a substitute for a budgeted approximation check on a computed transcendental answer. Conversely, a tolerance should never permit an incorrect index, sample count or RNG state to pass as “close enough.”

Numerical considerations

The LCG recurrence uses the accepted explicit uint32 seeded utility; Python independently computes the same modular integers without copying its floating implementation. Its uniforms fit exactly in binary64 because their denominator is 2322^{32}. The actual stream is deterministic under that specified contract.

The arithmetic-order comparison uses actual JavaScript binary64 operations at fixed represented inputs. Its exact intended reference remains rational. Across environments, elementary functions, fused operations, accumulation precision, subnormal handling and reduction order can still differ. After a platform or library upgrade, record the code version, repeat the same inputs and sample sequence, and compare approximate results using justified tolerances; a seed alone does not settle every difference.

Routine mathematical rendering contains no unseeded randomness. User-started trace playback changes only which immutable frame is selected; elapsed time does not generate a new numerical sample.

Engineering and ML connections

A useful experiment record includes code/source revision, inputs and their storage dtypes, random algorithm and seed/state, sample order, numerical algorithm, environment/library versions and comparison budgets. This supports reproducible diagnostics without pretending all hardware runs must be bit-identical.

In training, data order, stochastic sampling, parallel reductions and mixed precision can interact. Decide which outcomes must match exactly and which require a justified approximate allowance. Preserve evidence for both successful and failed checks so a corrected run remains independently reviewable.

Inspect the corresponding representation and error policy before interpreting a computed value.

Exercises

Predict first, then calculate or test the stated claim. Every answer includes corrective feedback and a worked solution.

calculation

Two associations

For binary64 a=1e16,b=−1e16,c=1, give (a+b)+c, then a+(b+c).

calculation

Comparison budget

x=10,y=10.001, absolute tolerance 0.0001 and relative tolerance 0.0002. Give the exact intended budget a+r max(|x|,|y|).

calculation

A recorded discrete state

For the specified LCG32 starting at 42, the first next state is 1083814273. Give its exact uniform fraction.

explanation

What a seed promises

Which record supports replay of a random computation?

Choose one answer

explanation

Exact versus approximate checks

How should discrete RNG states and approximate transcendental answers be checked?

Choose one answer

Summary and checkpoints

Replay the literal six-state stream and resume its suffix from the saved second state. Reproduce the left/right association difference, distinguish comparison acceptance from answer accuracy, and identify why RNG implementation, versions, draw order and arithmetic execution belong in a reproducibility record. This completes the book’s progression from mathematical definitions to numerically honest computation.

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