mixle.stats.graphs.erdos_renyi_graph module

Erdos-Renyi graph distributions for binary graph observations.

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]

Return backend capabilities for Bernoulli graph scoring.

classmethod compute_declaration()[source]

Return the structured declaration for the Erdos-Renyi graph family.

classmethod from_model(model)[source]

Create a distribution wrapper from an Erdos-Renyi model.

Parameters:

model (Any)

Return type:

ErdosRenyiGraphDistribution

to_model()[source]

Convert this distribution to the corresponding random-graph model.

Return type:

Any

density(x)[source]

Return the probability mass of one graph observation.

Parameters:

x (Any)

Return type:

float

log_density(x)[source]

Return the Bernoulli edge log probability of one graph.

Parameters:

x (Any)

Return type:

float

seq_log_density(x)[source]

Score a batch of graph 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]

Return the common edge probability p.

Parameters:
  • i (int | None)

  • j (int | None)

  • context (Any | None)

Return type:

float

edge_marginals(num_nodes=None)[source]

Return the matrix of marginal edge probabilities for num_nodes.

Parameters:

num_nodes (int | None)

Return type:

ndarray

posterior(x)[source]

Return edge opportunities, observed edge count, and fitted probability for x.

Parameters:

x (Any)

Return type:

dict[str, float]

sampler(seed=None)[source]

Return a sampler for Erdos-Renyi graph observations.

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 the edge-count estimator for this graph family.

Parameters:

pseudo_count (float | None)

Return type:

ErdosRenyiGraphEstimator

dist_to_encoder()[source]

Return the graph encoder used by vectorized scoring and fitting.

Return type:

GraphDataEncoder

class ErdosRenyiGraphEnumerator(dist)[source]

Bases: DistributionEnumerator

Enumerator over finite Erdos-Renyi binary graph support.

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]

Draw one binary graph adjacency matrix.

Parameters:

num_nodes (int | None)

Return type:

ndarray

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

Draw one graph or a list of graphs.

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]

Accumulate weighted edge opportunities and successes from one graph.

Parameters:
  • x (Any)

  • weight (float)

  • estimate (ErdosRenyiGraphDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]

Initialize sufficient statistics from one weighted graph.

Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate weighted edge counts from a batch of graphs.

Parameters:
  • x (Sequence[GraphObservation])

  • weights (ndarray)

  • estimate (ErdosRenyiGraphDistribution | None)

Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize sufficient statistics from a weighted graph batch.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge serialized edge-count sufficient statistics.

Parameters:

suff_stat (tuple[float, float])

Return type:

ErdosRenyiGraphAccumulator

value()[source]

Return serialized edge-count sufficient statistics.

Return type:

tuple[float, float]

from_value(x)[source]

Restore accumulator state from serialized edge counts.

Parameters:

x (tuple[float, float])

Return type:

ErdosRenyiGraphAccumulator

key_merge(stats_dict)[source]

Merge tied edge-count statistics into stats_dict.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace tied edge-count statistics from stats_dict.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Return the encoder associated with this accumulator.

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]

Create a fresh Erdos-Renyi graph accumulator.

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 the accumulator factory used by this estimator.

Return type:

ErdosRenyiGraphAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate the Bernoulli edge probability from edge-count statistics.

Parameters:
Return type:

ErdosRenyiGraphDistribution