mixle.stats.sets.integer_bernoulli_edit module¶
Create, estimate, and sample from an integer Bernoulli edit set distribution.
Defines the IntegerBernoulliEditDistribution, IntegerBernoulliEditSampler, IntegerBernoulliEditAccumulatorFactory, IntegerBernoulliEditAccumulator, IntegerBernoulliEditEstimator, and the IntegerBernoulliEditDataEncoder classes for use with mixle.
Data type: Tuple[Sequence[int], Sequence[int]]: An observation x = (x1, x2) is a pair of integer sets (prev set, next set), each a subset of S = {0,1,2,…N-1}.
Assume S = {0,1,2,…N-1} is a set of integers. The Bernoulli edit set distribution considers transitions between two random subsets. That is, let X1 and X2 be a random subsets of unique integers from S, s.t. X1 and X2 have at most N elements.
Consider observed subsets of S x1 and x2. The density is given by
p_mat(x2 | x1) = sum_{k=0}^{N-1} p_mat(k in x2 | k in x1) + p_mat(k in x2 | k not in x1) + p_mat(k not in x2 | k in x1) + p_mat(k not in x2 | k not in x1).
p_mat(x1,x2) = P_init(x1)*p_mat(x2|x1).
Note: In (1) only one of the summation terms in non-zero for a given value of k. In (2), P_init() is a distribution defining probabilities for an integer 0<=k<N being in a set (Generally a BernoulliSetDistribution is a good choice).
- class IntegerBernoulliEditDistribution(log_edit_pmat, init_dist=NullDistribution(), name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionBernoulli edit set distribution: each integer independently transitions in/out between two sets.
- Parameters:
- classmethod compute_capabilities()[source]
- density(x)[source]
Density of the Bernoulli edit set distribution at observation x.
See log_density() for details.
- log_density(x)[source]
Log-density of the joint observation (x[0], x[1]).
Computes log p(x[1] | x[0]) by summing per-integer edit log-probabilities for kept, added, and removed elements, plus log p(x[0]) under init_dist.
- seq_log_density(x)[source]
Vectorized evaluation of log-density at sequence encoded input x.
- Parameters:
x (E) – Sequence encoded (prev set, next set) observations from IntegerBernoulliEditDataEncoder.seq_encode().
- Returns:
Numpy array of log-density values, one per encoded observation.
- Return type:
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded integer edit-set observations.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked integer edit-set parameters for shared support and init policy.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of integer edit-set component log densities.
- classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]
Return per-component legacy
(edit_counts, total_weight, init_stat)statistics.
- sampler(seed=None)[source]
Create an IntegerBernoulliEditSampler object from this distribution.
- Parameters:
seed (Optional[int]) – Used to set seed in random sampler.
- Returns:
IntegerBernoulliEditSampler object.
- Return type:
IntegerBernoulliEditSampler
- estimator(pseudo_count=None)[source]
Create an IntegerBernoulliEditEstimator with matching num_vals.
- Parameters:
pseudo_count (Optional[float]) – Used to re-weight sufficient statistics in estimation.
- Returns:
IntegerBernoulliEditEstimator object.
- Return type:
IntegerBernoulliEditEstimator
- dist_to_encoder()[source]
Returns an IntegerBernoulliEditDataEncoder object for encoding sequences of data.
- Return type:
IntegerBernoulliEditDataEncoder
- enumerator()[source]
Returns IntegerBernoulliEditEnumerator iterating set-pairs in descending probability order.
- Return type:
IntegerBernoulliEditEnumerator
- class IntegerBernoulliEditEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerates finite previous/next integer-set pairs in descending probability order.
- Parameters:
dist (IntegerBernoulliEditDistribution)
- class IntegerBernoulliEditSampler(dist, seed=None)[source]
Bases:
DistributionSamplerIntegerBernoulliEditSampler object for drawing (prev set, next set) pairs from an IntegerBernoulliEditDistribution instance.
- Parameters:
dist (IntegerBernoulliEditDistribution)
seed (int | None)
- sample(size=None)[source]
Draw iid (prev set, next set) observations from the distribution.
- class IntegerBernoulliEditAccumulator(num_vals, init_acc=NullAccumulator(), keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorIntegerBernoulliEditAccumulator object for accumulating removed/added/kept counts from observed set pairs.
- Parameters:
- update(x, weight, estimate)[source]
Add weight to the removed/added/kept counts for the observed (prev set, next set) pair.
- initialize(x, weight, rng)[source]
Initialize the accumulator with a weighted observation.
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from sequence encoded observations.
- Parameters:
x (E) – Sequence encoded (prev set, next set) observations from IntegerBernoulliEditDataEncoder.seq_encode().
weights (np.ndarray) – Weights, one per encoded observation.
estimate (Optional[IntegerBernoulliEditDistribution]) – Previous estimate passed to the init accumulator.
- Return type:
None
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident accumulation of removed/added/kept edit counts (numpy or torch).
The three weighted edit-type histograms are reduced on the active engine; the fixed-size count matrix is host bookkeeping. The init child is routed through the engine. Matches seq_update.
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of sufficient statistics from sequence encoded observations.
- Parameters:
x (E) – Sequence encoded (prev set, next set) observations from IntegerBernoulliEditDataEncoder.seq_encode().
weights (np.ndarray) – Weights, one per encoded observation.
rng (np.random.RandomState) – Random number generator passed to the init accumulator.
- Return type:
None
- combine(suff_stat)[source]
Merge sufficient statistics of suff_stat into this accumulator.
- Parameters:
suff_stat (Tuple[np.ndarray, float, Optional[SS1]]) – Edit counts, total weight, and init suff stats.
- Returns:
This IntegerBernoulliEditAccumulator.
- Return type:
IntegerBernoulliEditAccumulator
- value()[source]
Returns the sufficient statistics: (edit counts, total weight, init suff stats).
- from_value(x)[source]
Set the sufficient statistics of this accumulator from x.
- Parameters:
x (Tuple[np.ndarray, float, Optional[SS1]]) – Edit counts, total weight, and init suff stats.
- Returns:
This IntegerBernoulliEditAccumulator.
- Return type:
IntegerBernoulliEditAccumulator
- key_merge(stats_dict)[source]
Merge this accumulator’s statistics into stats_dict under its key, if keyed.
- Parameters:
stats_dict (Dict[str, Any]) – Maps keys to merged sufficient statistics.
- Return type:
None
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics with the keyed statistics in stats_dict, if keyed.
- Parameters:
stats_dict (Dict[str, Any]) – Maps keys to merged sufficient statistics.
- Return type:
None
- acc_to_encoder()[source]
Returns an IntegerBernoulliEditDataEncoder object for encoding sequences of data.
- Return type:
IntegerBernoulliEditDataEncoder
- class IntegerBernoulliEditAccumulatorFactory(num_vals, init_factory=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryIntegerBernoulliEditAccumulatorFactory object for creating IntegerBernoulliEditAccumulator objects.
- make()[source]
Returns a new IntegerBernoulliEditAccumulator object.
- Return type:
IntegerBernoulliEditAccumulator
- class IntegerBernoulliEditEstimator(num_vals=MISSING, init_estimator=NullEstimator(), min_prob=1.0e-128, pseudo_count=None, suff_stat=None, name=None, keys=None, num_values=MISSING)[source]
Bases:
ParameterEstimatorIntegerBernoulliEditEstimator object for estimating an IntegerBernoulliEditDistribution from aggregated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Returns an IntegerBernoulliEditAccumulatorFactory for creating IntegerBernoulliEditAccumulator objects.
- Return type:
IntegerBernoulliEditAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate an IntegerBernoulliEditDistribution from aggregated sufficient statistics.
- class IntegerBernoulliEditDataEncoder(init_encoder)[source]
Bases:
DataSequenceEncoderIntegerBernoulliEditDataEncoder object for encoding sequences of iid (prev set, next set) observations.
- Parameters:
init_encoder (DataSequenceEncoder)
- seq_encode(x)[source]
Encode a sequence of iid (prev set, next set) observations for vectorized calculations.
- Return value ‘rv’ is a Tuple of length 6 containing:
rv[0] (int): Number of observed pairs. rv[1] (np.ndarray): Observation index for each flattened edit entry. rv[2] (np.ndarray): Flattened integer values of edited elements. rv[3] (np.ndarray): Edit type per entry: 0 (removed), 1 (added), 2 (kept). rv[4] (Tuple[np.ndarray, np.ndarray, np.ndarray]): Indices of entries with each edit type. rv[5] (Optional[Any]): Sequence encoding of the previous sets from init_encoder.