mixle.stats.latent.heterogeneous_pcfg module¶
Probabilistic context-free grammar with heterogeneous terminal emissions.
This module implements a small Chomsky-normal-form PCFG whose terminal rules
emit observations through arbitrary mixle.stats distributions. A terminal
distribution can be scalar-valued, tuple-valued, set-valued, sequence-valued,
or any other sequence-encodable distribution; each terminal rule keeps its own
encoder.
Data type: an observation is a finite sequence of terminal observations. The
grammar supports binary rules A -> B C and terminal rules A -> emission.
Epsilon productions and unary nonterminal productions are intentionally not
modeled.
- class HeterogeneousPCFGDistribution(binary_rules, terminal_rules, start=None, nonterminals=None, name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionCNF PCFG whose terminal productions emit arbitrary distributions.
- Parameters:
binary_rules (BinaryRuleInput | None) – Either
{parent: [(left, right, prob), ...]}or a flat sequence of(parent, left, right, prob)rules.terminal_rules (TerminalRuleInput) – Either
{parent: [(emission_dist, prob), ...]}or a flat sequence of(parent, emission_dist, prob)rules.start (Any | None) – Start nonterminal. If omitted, the first nonterminal is used.
nonterminals (Sequence[Any] | None) – Optional explicit nonterminal order.
name (str | None) – Optional model name.
Rule probabilities are normalized over all rules sharing a parent.
- density(x)[source]
Return the probability density or mass at a single observation.
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- compute_capabilities()[source]
Engine readiness intersected from the terminal emission distributions.
The CKY inside dynamic program is expressed in ComputeEngine ops (see
backend_seq_log_density), so the grammar is engine-ready on whatever engines all of its terminal emission leaves support (numpy plus, e.g., torch for autograd/GPU).
- backend_seq_log_density(x, engine)[source]
Engine-routed CKY inside scoring (numpy + torch).
Terminal log-densities come from each emission leaf’s own engine backend score, and the inside dynamic program runs in ComputeEngine ops. Per-sequence charts are still a Python loop (variable-length parses), so this trades the tuned NumPy
_insidefor engine portability and differentiability rather than raw speed.
- to_fisher(**kwargs)[source]
Inside-outside Fisher view for the PCFG.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
HeterogeneousPCFGSampler
- enumerator()[source]
Return an exact support enumerator for acyclic enumerable PCFGs.
- Return type:
HeterogeneousPCFGEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded index with an inside-style DP over quantized derivation costs.
Terminal emission distributions must themselves support
quantized_index. The DP counts grammar derivations by additive quantized bit cost and lazily unranks those derivations into emitted terminal sequences. For unambiguous grammars this is an index over distinct sequences; for ambiguous grammars the same sequence can be represented by multiple derivations, and the returned log probability is still the exact PCFGlog_densityof that sequence.
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
HeterogeneousPCFGEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
HeterogeneousPCFGDataEncoder
- class HeterogeneousPCFGEnumerator(dist)[source]
Bases:
DistributionEnumeratorExact best-first enumerator for acyclic heterogeneous PCFGs.
- Parameters:
dist (HeterogeneousPCFGDistribution)
- class HeterogeneousPCFGSampler(dist, seed=None, max_depth=100, max_steps=10000)[source]
Bases:
DistributionSamplerSampler for HeterogeneousPCFGDistribution.
- class HeterogeneousPCFGAccumulator(emission_accumulators, num_binary_rules, terminal_parents, binary_parents, keys=(None, None))[source]
Bases:
SequenceEncodableStatisticAccumulatorInside-outside sufficient-statistic accumulator.
- Parameters:
- update(x, weight, estimate)[source]
Update sufficient statistics for one observed sequence.
- initialize(x, weight, rng)[source]
Randomly initialize sufficient statistics for one observed sequence.
- Parameters:
weight (float)
rng (RandomState)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Randomly initialize rule and emission statistics for encoded sequences.
- seq_update(x, weights, estimate)[source]
Update encoded-sequence statistics with inside-outside posteriors.
- combine(suff_stat)[source]
Merge another accumulator value into this accumulator.
- value()[source]
Return rule counts and emission sufficient statistics.
- from_value(x)[source]
Replace this accumulator from a serialized sufficient-statistic value.
- key_merge(stats_dict)[source]
Merge keyed rule and emission statistics into
stats_dict.
- key_replace(stats_dict)[source]
Replace keyed rule and emission statistics from
stats_dict.
- acc_to_encoder()[source]
Return an encoder compatible with this accumulator’s emissions.
- Return type:
HeterogeneousPCFGDataEncoder
- class HeterogeneousPCFGAccumulatorFactory(factories, num_binary_rules, terminal_parents, binary_parents, keys=(None, None))[source]
Bases:
StatisticAccumulatorFactoryFactory for HeterogeneousPCFGAccumulator.
- Parameters:
- make()[source]
Create a fresh heterogeneous PCFG accumulator.
- Return type:
HeterogeneousPCFGAccumulator
- class HeterogeneousPCFGEstimator(binary_rules, terminal_rules, start=None, nonterminals=None, pseudo_count=None, name=None, keys=(None, None))[source]
Bases:
ParameterEstimatorEstimator for a fixed-topology heterogeneous PCFG.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for this fixed grammar topology.
- Return type:
HeterogeneousPCFGAccumulatorFactory
- class InducedHeterogeneousPCFGEstimator(max_nonterminals, terminal_estimators, start='S', nonterminal_prefix='NT', terminal_rule_mass=0.5, rule_pseudo_count=1.0e-3, prune_threshold=0.0, min_rule_prob=0.0, name=None, keys=(None, None))[source]
Bases:
ParameterEstimatorOvercomplete sparse PCFG structure learner.
This estimator builds a finite grammar skeleton automatically:
Knonterminals namedstart, prefix1, ..., prefixK-1.every binary rule
A -> B C.every terminal rule
A -> terminal_family_j.
EM learns rule probabilities and terminal emission parameters. Rules whose expected count is below
prune_thresholdor whose normalized probability is belowmin_rule_probare assigned probability zero, but the rule layout is kept stable so repeated calls toseq_estimateremain compatible.- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for the induced grammar skeleton.
- Return type:
HeterogeneousPCFGAccumulatorFactory
- initial_model(terminal_distributions, rng=None, jitter=0.0)[source]
Create an overcomplete starting grammar without hand-written rules.
terminal_distributionsmay contain one distribution per terminal family, in which case each nonterminal gets its own copy of the same family list, or one distribution per generated terminal rule.- Parameters:
terminal_distributions (Sequence[SequenceEncodableProbabilityDistribution])
rng (RandomState | None)
jitter (float)
- Return type:
HeterogeneousPCFGDistribution
- class HeterogeneousPCFGDataEncoder(terminal_encoders)[source]
Bases:
DataSequenceEncoderEncode iid PCFG observations by flattening terminal observations.
- Parameters:
terminal_encoders (Sequence[DataSequenceEncoder])
- class HeterogeneousPCFGFisherView(dist)[source]
Bases:
FixedFisherViewInside-outside Fisher view for heterogeneous PCFGs.
Coordinates are expected complete-data rule counts followed by terminal emission sufficient statistics gated by the posterior probability of the corresponding terminal rule at each token. For finite enumerable PCFGs, the model Fisher is the exact observed Fisher covariance of these posterior-expected complete-data statistics under the model distribution. Recursive or infinite-support grammars should use observed_fisher_* on data.
- Parameters:
dist (Any)
- fisher_information(stats=None, diagonal=False, ridge=1.0e-8, **kwargs)[source]
Return model Fisher information, falling back to observed statistics.
- fisher_vectors(stats=None, metric='diagonal', center=None, fisher=None, ridge=1.0e-8, **kwargs)[source]
Whiten PCFG statistics under the requested Fisher metric.