mixle.stats.graphs.erdos_renyi_graph module

Create, estimate, and sample from an Erdos-Renyi graph distribution.

Data type: a binary graph observation represented as a square adjacency matrix, a NetworkX-like graph, or a mapping accepted by GraphDataEncoder.

class ErdosRenyiGraphDistribution(p, directed=False, self_loops=False, num_nodes=None, name=None, keys=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Independent Bernoulli distribution over binary graph edges.

Parameters:
classmethod compute_capabilities()[source]
classmethod compute_declaration()[source]
classmethod from_model(model)[source]
Parameters:

model (Any)

Return type:

ErdosRenyiGraphDistribution

to_model()[source]
Return type:

Any

density(x)[source]

Return the probability density or mass at a single observation.

Concrete default: exponentiate log_density (the abstract method subclasses must provide). Leaves with a cheaper closed form may override this.

Parameters:

x (Any)

Return type:

float

log_density(x)[source]

Return the log-density or log-mass at a single observation.

Parameters:

x (Any)

Return type:

float

seq_log_density(x)[source]

Return vectorized log-density values for sequence-encoded observations.

Parameters:

x (Sequence[GraphObservation])

Return type:

ndarray

backend_seq_log_density(x, engine)[source]

Engine-routed Bernoulli edge log-likelihood.

Per-graph edge opportunities/successes are extracted host-side (the graphs are ragged object data), but the Bernoulli reduction runs on the active engine, so the model’s scoring math is engine-native (and differentiable in p on torch).

Parameters:
Return type:

Any

edge_probability(i=None, j=None, context=None)[source]
Parameters:
  • i (int | None)

  • j (int | None)

  • context (Any | None)

Return type:

float

edge_marginals(num_nodes=None)[source]
Parameters:

num_nodes (int | None)

Return type:

ndarray

posterior(x)[source]
Parameters:

x (Any)

Return type:

dict[str, float]

sampler(seed=None)[source]

Return a sampler for drawing observations from this distribution.

Parameters:

seed (int | None)

Return type:

ErdosRenyiGraphSampler

enumerator()[source]

Enumerate binary graphs in descending probability order (requires num_nodes).

The edges are independent Bernoulli(p) over the free positions _edge_indices(n, directed, self_loops), so the graph distribution is a product of edge factors and enumerates by best-first over the per-edge supports – exactly like a composite of Bernoullis, with the combined value assembled into an adjacency matrix (mirrored for the undirected case). Each graph carries its exact log_density. num_nodes must be set so the edge set is finite.

Return type:

DistributionEnumerator

estimator(pseudo_count=None)[source]

Return an estimator for fitting this distribution from data.

Parameters:

pseudo_count (float | None)

Return type:

ErdosRenyiGraphEstimator

dist_to_encoder()[source]

Return the data encoder used by this distribution for vectorized methods.

Return type:

GraphDataEncoder

class ErdosRenyiGraphEnumerator(dist)[source]

Bases: DistributionEnumerator

Parameters:

dist (ErdosRenyiGraphDistribution)

class ErdosRenyiGraphSampler(dist, seed=None)[source]

Bases: DistributionSampler

Sample binary graphs from an Erdos-Renyi distribution.

Parameters:
  • dist (ErdosRenyiGraphDistribution)

  • seed (int | None)

sample_graph(num_nodes=None)[source]
Parameters:

num_nodes (int | None)

Return type:

ndarray

sample(size=None, num_nodes=None)[source]

Draw observations.

Combinator samplers (mixture/sequence/…) accept batched. With batched=True (the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independent RandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path. batched=False forces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.

Parameters:
  • size (int | None)

  • num_nodes (int | None)

Return type:

ndarray | list[ndarray]

class ErdosRenyiGraphAccumulator(directed=False, self_loops=False, name=None, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate edge counts for Erdos-Renyi graph fitting.

Parameters:
  • directed (bool)

  • self_loops (bool)

  • name (str | None)

  • keys (str | None)

update(x, weight, estimate)[source]
Parameters:
  • x (Any)

  • weight (float)

  • estimate (ErdosRenyiGraphDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]
Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]
Parameters:
  • x (Sequence[GraphObservation])

  • weights (ndarray)

  • estimate (ErdosRenyiGraphDistribution | None)

Return type:

None

seq_initialize(x, weights, rng)[source]
Parameters:
Return type:

None

combine(suff_stat)[source]
Parameters:

suff_stat (tuple[float, float])

Return type:

ErdosRenyiGraphAccumulator

value()[source]
Return type:

tuple[float, float]

from_value(x)[source]
Parameters:

x (tuple[float, float])

Return type:

ErdosRenyiGraphAccumulator

key_merge(stats_dict)[source]

Pool this accumulator’s statistics into stats_dict under its merge key.

The structural default implements the common single-key pattern: store the accumulator under self.keys the first time the key is seen, else combine into 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. A keys of None (the default) is a no-op.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator’s statistics from the pooled stats_dict entry (see key_merge).

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]
Return type:

GraphDataEncoder

class ErdosRenyiGraphAccumulatorFactory(directed=False, self_loops=False, name=None, keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for ErdosRenyiGraphAccumulator.

Parameters:
  • directed (bool)

  • self_loops (bool)

  • name (str | None)

  • keys (str | None)

make()[source]
Return type:

ErdosRenyiGraphAccumulator

class ErdosRenyiGraphEstimator(directed=False, self_loops=False, pseudo_count=None, prior_p=0.5, num_nodes=None, name=None, keys=None)[source]

Bases: ParameterEstimator

Estimate an Erdos-Renyi graph distribution from edge counts.

Parameters:
  • directed (bool)

  • self_loops (bool)

  • pseudo_count (float | None)

  • prior_p (float)

  • num_nodes (int | None)

  • name (str | None)

  • keys (str | None)

accumulator_factory()[source]
Return type:

ErdosRenyiGraphAccumulatorFactory

estimate(nobs, suff_stat)[source]
Parameters:
Return type:

ErdosRenyiGraphDistribution