mixle.stats.combinator.conditional module¶
Create, estimate, and sample from a Conditional distribution.
Defines the ConditionalDistribution, ConditionalDistributionSampler, ConditionalDistributionAccumulatorFactory, ConditionalDistributionAccumulator, ConditionalDistributionEstimator, and the ConditionalDistributionDataEncoder classes for use with mixle.
- Data type: (Tuple[T0, T1]): The ConditionalDistribution if given by density,
P(X0,X1) = P_cond(X1|X0)*P_given(X0).
The ConditionalDistribution allows for user defined conditional distributions P_cond(X1|X0), and given distributions P_given(X0).
- class ConditionalDistribution(dmap, default_dist=NullDistribution(), given_dist=NullDistribution(), name=None, keys=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionConditionalDistribution models pairs (x0, x1) with density P_cond(x1 | x0) * P_given(x0), where the conditional distributions are looked up from a dictionary keyed by x0.
- Parameters:
- get_prior()[source]
Return the joint prior as
(per_branch_priors, default_prior, given_prior).per_branch_priorsis a dict keyed likedmapof each conditional branch’s prior.
- set_prior(prior)[source]
Distribute per-branch priors to the conditional branches, default, and given distributions.
prior=Noneis a no-op (children keep their existing priors, leaving the MLE path byte-identical). Otherwiseprioris(per_branch_priors, default_prior, given_prior): eachdmapbranch prior is pushed to the matching child viaset_prior, and the default/given priors are pushed todefault_dist/given_dist.
- expected_log_density(x)[source]
Prior-expected log-density: selected branch
expected_log_density+ given term.Mirrors
log_density: unmatched conditioning values with no default score-inf.
- seq_expected_log_density(x)[source]
Vectorized prior-expected log-density mirroring
seq_log_density.- Parameters:
x (E0)
- Return type:
- compute_capabilities()[source]
- compute_declaration()[source]
- density(x)[source]
Evaluates density of ConditionalDistribution at Tuple x.
Calls log_density() and returns the exponentiated result. See log_density() for details.
- Parameters:
x (Tuple[T0, T1]) – T0 data type much match keys of dmap, T1 much match value of dmap distribution for key value.
- Returns:
Density of ConditionalDistribution at Tuple x
- Return type:
- log_density(x)[source]
Evaluate log-density of ConditionalDistribution at Tuple x.
- Log-density:
log(P(x)) = log(P_cond(x[1] | x[0])) + log(P_given(x[0])), where log(P_cond(x[1] | x[0])) is defined from dmap, and log(P_given(x[0])) is defined from given_dist.
Note: Log-density is evaluated to -np.inf, if x[0] not in dmap and default_dist is NullDistribution().
- Parameters:
x (Tuple[T0, T1]) – T0 data type much match keys of dmap, T1 much match value of dmap distribution for key value.
- Returns:
Log-density of ConditionalDistribution at Tuple x.
- Return type:
- seq_log_density(x)[source]
Arkouda vectorized evaluation of the log-density on sequence encoded data x.
- x Tuple of length 5:
x[0] (int): length of x (i.e. total observations). x[1] (Tuple[T0]): Unique conditional values in data. x[2] (Tuple[E0,…]): Tuple of encoded data sequences for each given key. x[3] (Tuple[ak.pdarray,…]): Tuple containing idxs for observation corresponding to x[1] values. x[4] (Optional[Encoded[T0]]): If the given_encoder is not the NullDataEncoder, the
observed conditional values of data type T0 are sequence encoded by given_encoder. Else return None.
- Parameters:
x (E0) – See above for details.
- Returns:
Numpy array of log-density evaluated at each encoded data point.
- Return type:
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for grouped conditional encodings.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked child routes for homogeneous conditional mixtures.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of conditional log densities.
- classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]
Return per-component legacy conditional sufficient statistics.
- gradient_fit_state(engine, torch, leaves, recurse, tensor_param)[source]
Return distribution-owned state for autograd fitting.
- sampler(seed=None)[source]
Creates ConditionalDistributionSampler object for sampling from ConditionalDistribution instance.
- Parameters:
seed (Optional[int]) – Set seed for sampling from ConditionalDistributionSampler object.
- Returns:
ConditionalDistributionSampler object.
- Return type:
ConditionalDistributionSampler
- estimator(pseudo_count=None)[source]
Creates ConditionalDistributionEstimator object from sufficient statistics of ConditionalDistribution object.
Used to estimate a ConditionalDistribution from data observations.
- Parameters:
pseudo_count (Optional[float]) – Used to inflate the sufficient statistics of ConditionalDistribution.
- Returns:
ConditionalDistributionEstimator object.
- Return type:
ConditionalDistributionEstimator
- dist_to_encoder()[source]
Creates ConditionalDistributionDataEncoder object for encoding sequences of ConditionalDistribution data.
- Return type:
ConditionalDistributionDataEncoder
- enumerator()[source]
Creates a ConditionalDistributionEnumerator iterating (given, value) pairs in descending joint probability order.
Requires an enumerable given distribution and enumerable conditional distributions; raises EnumerationError otherwise.
- Returns:
ConditionalDistributionEnumerator object.
- Return type:
ConditionalDistributionEnumerator
- class ConditionalDistributionEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerates (given, value) pairs of a ConditionalDistribution in descending joint probability order.
- Parameters:
dist (ConditionalDistribution)
- class ConditionalDistributionSampler(dist, seed=None)[source]
Bases:
ConditionalSampler,DistributionSamplerConditionalDistributionSampler draws (given, value) pairs from a ConditionalDistribution, or values conditioned on a fixed given value via sample_given().
- Parameters:
dist (ConditionalDistribution)
seed (int | None)
- single_sample()[source]
Generates a simple sample from the ConditionalDistribution.
Returns Tuple of T0 and T1, where T1 is the data type of the conditional distribution, and T0 is the type of the given distribution.
- sample(size=None)[source]
Sample ‘size’ independent samples from ConditionalDistribution.
Sequence of ‘size’ calls to single_sample(). If size is None, size is taken to be 1.
Data type returned is a Tuple[T0, T1], where T0 and T1 are the respective data types of the given_dist and dmap defined in the CompositeDistribution instance ‘dist’.
- sample_given(x)[source]
Sample from conditional distribution of ConditionalDistribution object with given value x.
Return data type T1 as defined for dictionary of ConditionalDistribution instance.
- Parameters:
x (T0) – Value of given/conditional value for ConditionalDistribution.
- Returns:
Single sample from ConditionalDistribution object ‘dist.dmap’ given x.
- Return type:
- class ConditionalDistributionAccumulator(accumulator_map, default_accumulator=NullAccumulator(), given_accumulator=NullAccumulator(), keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorConditionalDistributionAccumulator accumulates sufficient statistics for each conditional distribution, the default distribution, and the given distribution.
- Parameters:
- update(x, weight, estimate)[source]
Updates sufficient statistics of ConditionalDistributionAccumulator for one weighted observation x.
Single weighted observation used to update the sufficient statistics of ConditionalDistributionAccumulator.
- Parameters:
x (Tuple[T0, T1]) – Tuple observation of ConditionalDistribution.
weight (float) – Weight for observation.
estimate (Optional['ConditionalDistribution']) – Sufficient statistics from ConditionalDistribution are aggregated with weighted observation x.
- Returns:
None.
- Return type:
None
- initialize(x, weight, rng)[source]
Initialize ConditionalDistributionAccumulator with single weighted observation.
Note: _rng_initialize is called if _init_rng is False. This allows consistency between seq_initialize and initialize.
- Parameters:
x (Tuple[T0, T1]) – Tuple observation of ConditionalDistribution.
weight (float) – Weight for observation.
rng (RandomState) – RandomState used to set seed in initialize calls to member accumulators.
- Returns:
None
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialize ConditionalDistributionAccumulator for a sequence encoded x.
Input x must be an encoded sequence produces from ConditionalDistributionDataEncoder.seq_encode() called on a list iid sequence of ConditionalDistribution observations.
Calls seq_initialize on accumulator_map, default_accumulator, and given_accumulator.
Note: _rng_initialize is called if _init_rng is False. This allows consistency between seq_initialize and initialize.
- E Tuple of length 5:
E[0] (int): length of x (i.e. total observations). E[1] (Tuple[T0]): Unique conditional values in data. E[2] (Tuple[Encoded[T1]): Tuple of sequence encoded data of type T1 encoded by
encoder_map[key] or default_encoder if key not in default_encoder and default_encoder is not the NullDataEncoder.
- E[3] (Tuple[np.ndarray,…]): Tuple of length equal to the number of unique conditional
values encountered in the data. Each entry contains a numpy array for the indices of x that correspond to a unique conditional value.
- E[4] (Optional[Encoded[T0]]): If the given_encoder is not the NullDataEncoder, the
observed conditional values of data type T0 are sequence encoded by given_encoder. Else return None.
- Parameters:
x (E0) – See description above for details.
weights (ndarray) – Numpy array of floats containing weights for each observation.
rng (RandomState) – RandomState used to set seed in initialize calls to member accumulators.
- Returns:
None.
- Return type:
None
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics of ConditionalDistributionAccumulator for a sequence encoded x.
Input x must be an encoded sequence produces from ConditionalDistributionDataEncoder.seq_encode() called on a list iid sequence of ConditionalDistribution observations.
Calls seq_update on accumulator_map, default_accumulator, and given_accumualtor.
- E Tuple of length 5:
E[0] (int): length of x (i.e. total observations). E[1] (Tuple[T0]): Unique conditional values in data. E[2] (Tuple[Encoded[T1]): Tuple of sequence encoded data of type T1 encoded by
encoder_map[key] or default_encoder if key not in default_encoder and default_encoder is not the NullDataEncoder.
- E[3] (Tuple[np.ndarray,…]): Tuple of length equal to the number of unique conditional
values encountered in the data. Each entry contains a numpy array for the indices of x that correspond to a unique conditional value.
- E[4] (Optional[Encoded[T0]]): If the given_encoder is not the NullDataEncoder, the
observed conditional values of data type T0 are sequence encoded by given_encoder. Else return None.
- Parameters:
x (E0) – See description above for details.
weights (ndarray) – Numpy array of floats containing weights for each observation.
estimate (Optional['ConditionalDistribution']) – Sufficient statistics from ConditionalDistribution are used in merged with aggregated statistics from input x.
- Returns:
None.
- Return type:
None
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident E-step: per-conditional-value subgroup weights are gathered on the active engine and the matching child accumulators (and the given accumulator) are routed through the engine. Matches seq_update.
- combine(suff_stat)[source]
- Aggregate sufficient statistics (suff_stat) with sufficient statistics of ConditionalDistributionAccumulator
instance.
- Parameters:
suff_stat (tuple[dict[T0, SS0], SS1 | None, SS2 | None]) – Tuple of length 3 containing the sufficient statistics of the conditional distributions, default distribution, and given distribution.
- Returns:
ConditionalDistributionAccumulator with aggregated sufficient statistics.
- Return type:
ConditionalDistributionAccumulator
- value()[source]
Get sufficient statistics of CompositeDistributionAccumulator.
- from_value(x)[source]
Set ConditionalDistributionAccumulator member instances to x.
Input x must be sufficient statistic tuple compatible with ConditionalDistributionAccumulator.
- scale(c)[source]
Scale linear sufficient statistics in-place by
c.The structural default is correct for ordinary weighted sums, nested tuples/lists/dicts, and numeric arrays. Families whose
value()payload includes non-linear metadata such as support bounds must override this method and leave that metadata unscaled.- Parameters:
c (float)
- Return type:
ConditionalDistributionAccumulator
- key_merge(stats_dict)[source]
- Aggregate the sufficient statistics of ConditionalDistributionAccumulator with member instance key in
stats_dict.
- Parameters:
stats_dict (Dict[str, Any]) – Key of dict are the ‘keys’ for ConditionalDistributionAccumulator that represent the same distribution.
- Returns:
None
- Return type:
None
- key_replace(stats_dict)[source]
- Invoke key_replace on each member SequenceEncodableStatisticAccumulator of
ConditionalDistributionAccumulator instance.
- Parameters:
stats_dict (Dict[str, Any]) – Key of dict are the ‘keys’ for ConditionalDistributionAccumulator that represent the same distribution.
- Returns:
None
- Return type:
None
- acc_to_encoder()[source]
Creates ConditionalDistributionDataEncoder object for encoding sequences of ConditionalDistribution data.
- Return type:
ConditionalDistributionDataEncoder
- class ConditionalDistributionAccumulatorFactory(factory_map, default_factory=NullAccumulatorFactory(), given_factory=NullAccumulatorFactory(), keys=None)[source]
Bases:
StatisticAccumulatorFactoryConditionalDistributionAccumulatorFactory creates ConditionalDistributionAccumulator objects from the per-key, default, and given factories.
- Parameters:
- make()[source]
Create ConditionalAccumulator object from StatisticAccumulatorFactory member instances.
SequenceEncodableStatisticAccumulator objects are created for accumulator_map, default_accumulator, and given_accumulator in ConditionalAccumulator object.
- Returns:
ConditionalDistributionAccumulator
- Return type:
ConditionalDistributionAccumulator
- class ConditionalDistributionEstimator(estimator_map, default_estimator=NullEstimator(), given_estimator=NullEstimator(), name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorConditionalDistributionEstimator estimates a ConditionalDistribution from aggregated sufficient statistics.
- Parameters:
- get_prior()[source]
Return the joint prior as
(per_branch_priors, default_prior, given_prior)from child estimators.
- set_prior(prior)[source]
Distribute per-branch priors to the child estimators (branches, default, given).
prior=Noneis a no-op. Each branch prior is pushed to the matching estimator viaset_prior; default/given priors go to the default/given estimators.
- model_log_density(model)[source]
Sum each branch’s estimator
model_log_densityplus the default and given terms.- Parameters:
model (ConditionalDistribution)
- Return type:
- accumulator_factory()[source]
Creates ConditionalDistributionAccumulatorFactory from estimator member values.
- Returns:
ConditionalDistributionAccumulatorFactory object.
- Return type:
ConditionalDistributionAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a ConditionalDistribution from aggregated data.
Calls the estimate() member function of each ParameterEstimator instance for estimator_map, default_estimator, and given_estimator.
Input suff_stat if a Tuple of size three containing sufficient statistics compatible with each respective ParameterEstimator. Entry one of the Tuple must be a dict with keys of data type T0, matching the data type for the given distribution.
Returns a ConditionalDistribution object estimated from the sufficient statistics in suff_stat.
- class ConditionalDistributionDataEncoder(encoder_map, default_encoder=NullDataEncoder(), given_encoder=NullDataEncoder())[source]
Bases:
DataSequenceEncoderConditionalDistributionDataEncoder encodes sequences of (given, value) pairs, grouping the values by given value and delegating each group to the matching conditional encoder.
- Parameters:
encoder_map (dict[T0, DataSequenceEncoder])
default_encoder (DataSequenceEncoder)
given_encoder (DataSequenceEncoder)
- seq_encode(x)[source]
Encode sequence of iid observations from ConditionalDistribution for vectorized “seq_” function calls.
Data must be a List of Tuple of two types, T0 and T1. T0 is the data type compatible with the conditional values of the ConditionalDistribution. T1 must be consistent with the data type of the conditional distributions.
- E Tuple of length 5:
E[0] (int): length of x (i.e. total observations). E[1] (Tuple[T0]): Unique conditional values in data. E[2] (Tuple[Encoded[T1]): Tuple of sequence encoded data of type T1 encoded by
encoder_map[key] or default_encoder if key not in default_encoder and default_encoder is not the NullDataEncoder.
- E[3] (Tuple[np.ndarray,…]): Tuple of length equal to the number of unique conditional
values encountered in the data. Each entry contains a numpy array for the indices of x that correspond to a unique conditional value.
- E[4] (Optional[Encoded[T0]]): If the given_encoder is not the NullDataEncoder, the
observed conditional values of data type T0 are sequence encoded by given_encoder. Else return None.
- ConditionalAccumulator
alias of
ConditionalDistributionAccumulator
- ConditionalAccumulatorFactory
alias of
ConditionalDistributionAccumulatorFactory
- ConditionalDataEncoder
alias of
ConditionalDistributionDataEncoder
- ConditionalEnumerator
alias of
ConditionalDistributionEnumerator
- ConditionalEstimator
alias of
ConditionalDistributionEstimator