mixle.stats.trees.spanning_tree module¶
Create, estimate, sample, and enumerate a weighted spanning-tree distribution over a labeled graph.
Defines the SpanningTreeDistribution, SpanningTreeEnumerator, SpanningTreeSampler, SpanningTreeAccumulatorFactory, SpanningTreeAccumulator, SpanningTreeEstimator, and the SpanningTreeDataEncoder classes for use with mixle.
Data type: a spanning tree of n labeled nodes given as a sequence of n-1 undirected edges, each an
(i, j) pair (e.g. [(0, 1), (1, 2), (1, 3)]). Unlike ChowLiuTree (a tree-structured distribution
over vectors), this is a distribution over the tree STRUCTURES themselves.
Each undirected edge has a positive weight w[i, j]. A spanning tree T has probability
p(T) = prod_{(i,j) in T} w[i, j] / Z, Z = sum over all spanning trees of prod w[e],
and by the Matrix-Tree theorem Z equals any first cofactor of the weighted graph Laplacian
L = diag(W 1) - W, i.e. det(L[1:, 1:]). Sampling uses Wilson’s loop-erased-random-walk algorithm,
which draws exactly from this weighted uniform-spanning-tree law. Estimation matches empirical or
smoothed edge frequencies to the model edge marginals (an exponential family over trees, fit by
projected gradient ascent on the log-weights); the per-edge marginal w[i,j] * R_eff(i,j) is read
from the Laplacian pseudoinverse. Exact finite enumeration scans all positive-edge subsets of size
n-1, keeps the spanning trees, and sorts them by fitted probability.
- class SpanningTreeDistribution(weights, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionWeighted spanning-tree distribution over n labeled nodes with symmetric positive edge weights.
Data type: a sequence of n-1 undirected edges (i, j) forming a spanning tree of 0,…,n-1.
- classmethod compute_capabilities()[source]
- density(x)[source]
Return the probability of a spanning tree x (a sequence of edges).
- log_density(x)[source]
Return the log-probability of a spanning tree x (a sequence of n-1 edges).
- seq_log_density(x)[source]
Return vectorized log-probabilities for a sequence of canonical edge arrays.
- sampler(seed=None)[source]
Return a sampler for drawing spanning trees from this distribution.
- Parameters:
seed (int | None)
- Return type:
SpanningTreeSampler
- enumerator(max_edge_subsets=_DEFAULT_MAX_ENUMERATION_SUBSETS)[source]
Return an exact finite enumerator over all supported spanning trees in probability order.
- Parameters:
max_edge_subsets (int | None)
- Return type:
SpanningTreeEnumerator
- estimator(pseudo_count=None)[source]
Return an estimator that keeps the node count fixed at this distribution’s n.
- Parameters:
pseudo_count (float | None)
- Return type:
SpanningTreeEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
SpanningTreeDataEncoder
- class SpanningTreeEnumerator(dist, max_edge_subsets=_DEFAULT_MAX_ENUMERATION_SUBSETS)[source]
Bases:
DistributionEnumeratorEnumerate supported spanning trees in descending probability order, lazily.
A tree’s probability is the product of its edge weights, so descending probability is increasing total edge cost under
cost = -log(weights)(zero-weight edges become +inf, i.e. absent). Gabow’s k-best spanning-tree algorithm streams the trees in that order from one constrained-MST oracle per node, without scanning the exponential set of edge subsets.- Parameters:
dist (SpanningTreeDistribution)
max_edge_subsets (int | None)
- class SpanningTreeSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid spanning trees via Wilson’s loop-erased-random-walk algorithm.
- Parameters:
dist (SpanningTreeDistribution)
seed (int | None)
- class SpanningTreeAccumulator(dim, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the weighted edge-appearance counts (the sufficient statistic for the tree weights).
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
SpanningTreeDataEncoder
- class SpanningTreeAccumulatorFactory(dim, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for SpanningTreeAccumulator.
- make()[source]
- Return type:
SpanningTreeAccumulator
- class SpanningTreeEstimator(dim, pseudo_count=None, max_steps=500, learning_rate=1.0, tol=1.0e-7, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimate edge weights by matching empirical or smoothed tree edge marginals.
- Parameters:
- accumulator_factory()[source]
- Return type:
SpanningTreeAccumulatorFactory