Repository navigation
Description
These behaviours of the expectation propagation passes (lib/variational.c, driven from tsdate/variational.py) were kept as-is when the numerical code moved from numba to C (#534), and are worth a look.
The reproducers that call internal functions of _tsdate (e.g. _tsdate.moments) run at commit d8a95ac; later commits in #534 removed those exports and test the functions in lib/tests/tests.c instead.
max_shape == 1 is accepted but zeroes the posteriors
tsd_propagate_likelihood and tsd_propagate_prior accept max_shape >= 1. With exactly 1, tsd_rescale returns a factor of 0 for any posterior with shape above the cap, so rescale_posterior zeroes the posterior and its scale; every later update of that node is then NaN. Through the public API this ends in an unrelated-looking error:
import msprime, tsdate
ts = msprime.sim_ancestry(5, sequence_length=1e5, population_size=1e4,
recombination_rate=1e-8, random_seed=2)
ts = msprime.sim_mutations(ts, rate=1e-8, random_seed=2)
tsdate.variational_gamma(ts, mutation_rate=1e-8, max_shape=1.0)
# _tsdate.LibraryError: The fitted regularisation penalty is not positive
tsdate.variational_gamma(ts, mutation_rate=1e-8, max_shape=1.5) # fineimport _tsdate
_tsdate.rescale(2.0, 1.0, 1.0) # 0.0max_shape should probably be required to be > 1 (and checked in Python with a clear message).
Failed projections are silent
When a projection's moments are invalid (NaN, non-positive variance, outside the domain of the hypergeometric approximations), the projection returns NaN in-band: the node posterior is set to the cavity, the factor is shrunk by the damping, and the edge's log-normaliser is NaN. Nothing is raised or warned. ExpectationPropagation.infer only logs, at info level, the number of edges whose log-normaliser is NaN after the last iteration ("Skipped N edges with invalid factors").
The same applies to mutations: in tsd_propagate_mutations, a mutation whose projection fails is left with a NaN posterior (and NaN phase for unphased singletons) in the result, and only the count is logged ("Skipped N mutations with invalid posteriors").
Example of a failed edge projection (large shapes, where the variance cancels to a negative number):
_tsdate.gamma_projection(1e8 - 1, 1e5, 1e8 - 1, 1e5, 1.0, 1e-4)
# (nan, 99999999.0, 100000.0, 99999999.0, 100000.0)damp ignores a NaN rate in the message
tsd_damp takes d = min(a, b) with tsd_min(a, b) = b < a ? b : a. If the message's rate y[1] is NaN, b is NaN and min returns a, so the damping is silently computed from the shape alone (a NaN shape, by contrast, is reported as an invalid damping factor):
_tsdate.damp(1.0, 1.0, 0.0, float("nan"), 0.5) # 1.0
_tsdate.damp(1.0, 1.0, 8.0, float("nan"), 0.5) # 0.125
_tsdate.damp(1.0, 1.0, float("nan"), 0.0, 0.5) # LibraryError: The damping factor is not ...constraints_are_consistent depends on edge order and ignores NaN bounds
tsd_constraints_are_consistent (called by ExpectationPropagation._check_valid_constraints) propagates lower bounds from child to parent in a single pass over the edges, in table order. It only detects an inconsistency if edges are ordered children-first, and a NaN bound is never flagged:
import numpy as np
c = np.array([[5, 5], [0, np.inf], [0, 4]], float) # node 0 fixed at 5, node 2 at most 4
i32 = lambda *x: np.array(x, dtype=np.int32)
_tsdate.constraints_are_consistent(c, i32(1, 2), i32(0, 1)) # False (0 -> 1 -> 2)
_tsdate.constraints_are_consistent(c, i32(2, 1), i32(1, 0)) # True (same edges, reversed)
_tsdate.constraints_are_consistent(np.array([[np.nan, np.nan], [0, 1]]), i32(1), i32(0)) # TrueTree sequence edges are sorted by parent time, so this works for tskit input, but the function does not say so.
A zero posterior with a nonzero message is an error rather than a skip
If a node posterior is exactly [0, 0] (the "no information" state) and its message is nonzero, tsd_damp fails with TSD_ERR_INVALID_NODE_POSTERIOR ("A node posterior has shape not greater than -1 or rate not greater than zero"), which aborts the whole EP run, rather than skipping the update:
_tsdate.damp(0.0, 0.0, 1.0, 1.0, 0.5) # LibraryErrorThe regularisation penalty check is reachable
tsd_propagate_prior fits an exponential penalty and fails with TSD_ERR_NONPOSITIVE_PENALTY ("The fitted regularisation penalty is not positive") if it is not positive. This is reachable when every likelihood update of a pass is skipped (for example with a huge mutational target), which leaves the roots with a zero posterior; the max_shape=1 example above also ends here.
Relatedly, the initial penalty is 1 / mean(shape / rate) over the free nodes' cavities. If every free node's cavity has shape -1, the mean is zero and the penalty is inf; the fixed-point loop then does not run, the check passes, and the posterior rates become infinite. (No test reaches this case.)
Follow-up: a convergence diagnostic for skipped updates
Rather than one info-level count after the fact, EP could warn (or raise) when skipped updates persist across iterations, or when a node never gets a valid posterior. Such a diagnostic could also replace the arbitrary exact-zero checks in the update helpers (for example the TSD_ERR_ZERO_DIVISION guards in tsd_damp and tsd_rescale), which are kept for parity with the previous implementation.
Activity