mixle.stats.compute.declarations module¶
Declarative metadata for distribution parameters and sufficient statistics.
- class ParameterSpec(name, constraint='real', differentiable=True)[source]
Bases:
objectA fitted distribution parameter used by scoring.
Constraints are interpreted by generic generated/scoring utilities. In addition to scalar constraints such as
positiveand vector/matrix constraints such assimplex_vector,row_simplex_matrix, andcolumn_simplex_matrix,greater_than:<parameter>marks a coupled ordered bound.- name: str
- constraint: str = 'real'
- differentiable: bool = True
- class StatisticSpec(name, kind='moment', additive=True, scales=True)[source]
Bases:
objectA sufficient-statistic entry produced by accumulation.
- name: str
- kind: str = 'moment'
- additive: bool = True
- scales: bool = True
- class ExponentialFamilySpec(sufficient_statistics, natural_parameters, log_partition, base_measure=None, sufficient_statistics_from_params=None, base_measure_from_params=None, legacy_sufficient_statistics=None, fixed_base=True, runtime_scoring=True)[source]
Bases:
objectConditional exponential-family pieces for generated scalar scoring.
- Parameters:
sufficient_statistics (Callable[[Any, Any], tuple[Any, ...]])
natural_parameters (Callable[[dict[str, Any], Any], tuple[Any, ...]])
sufficient_statistics_from_params (Callable[[Any, dict[str, Any], Any], tuple[Any, ...]] | None)
base_measure_from_params (Callable[[Any, dict[str, Any], Any], Any] | None)
legacy_sufficient_statistics (Callable[[Any, dict[str, Any], Any], tuple[Any, ...]] | None)
fixed_base (bool)
runtime_scoring (bool)
- sufficient_statistics_from_params: Callable[[Any, dict[str, Any], Any], tuple[Any, ...]] | None = None
- fixed_base: bool = True
- runtime_scoring: bool = True
Whether the generated exp-family form may drive runtime scoring (scalar + stacked + numba).
Set
Falsewhen the canonical<eta, T(x)>dot form is numerically unsafe as a runtime scorer even though the family is a valid exponential family. The motivating case is the categorical:eta = log(p)has-infentries for zero-probability categories, so the generic dot product hits0 * -inf = NaNfor observations of other categories (its ownseq_log_densityavoids this by indexing). Withruntime_scoring=Falsethe family keeps itsbackend_*scoring path whileto_exponential_familystill exposes the canonical map (valid wherep > 0).
- class DistributionDeclaration(name, distribution_type, parameters, statistics, support, children=(), child_roles=(), differentiable=True, exponential_family=None, legacy_sufficient_statistics=None)[source]
Bases:
objectMetadata needed by generated kernels and future autograd paths.
- Parameters:
name (str)
parameters (tuple[ParameterSpec, ...])
statistics (tuple[StatisticSpec, ...])
support (str)
children (tuple[DistributionDeclaration, ...])
differentiable (bool)
exponential_family (ExponentialFamilySpec | None)
legacy_sufficient_statistics (Callable[[Any, dict[str, Any], Any], tuple[Any, ...]] | None)
- name: str
- parameters: tuple[ParameterSpec, ...]
- statistics: tuple[StatisticSpec, ...]
- support: str
- children: tuple[DistributionDeclaration, ...] = ()
- differentiable: bool = True
- exponential_family: ExponentialFamilySpec | None = None
- parameter_values(dist)[source]
Extract declared parameter values from a distribution instance.
- statistic_values(suff_stat)[source]
Map a legacy accumulator value into declared statistic names.
- property statistic_names: tuple[str, ...]
Return declared sufficient-statistic names in accumulator order.
- property has_exponential_family: bool
Return whether generated exponential-family scoring is available.
- register_declaration(declaration)[source]
Register a declaration for a distribution class.
- Parameters:
declaration (DistributionDeclaration)
- Return type:
None
- declaration_for(x)[source]
Return a declaration for a distribution instance or class, if present.
- Parameters:
x (Any)
- Return type:
DistributionDeclaration | None
- declared_distribution_types()[source]
Return classes that currently have declarations.
- declaration_issues(x)[source]
Return structural issues in a distribution declaration.
This is intentionally schema-level validation: it checks names, constraints, child roles, and callable exponential-family pieces without importing or special-casing concrete distribution implementations.
- validate_declaration(x)[source]
Return a declaration or raise
ValueErrorwith schema issues.- Parameters:
x (Any)
- Return type:
DistributionDeclaration
- statistic_layout_issues(x, suff_stat)[source]
Return issues mapping a legacy sufficient-statistic payload to a declaration.
This validates the top-level
statistic_values(...)arity and, when a declaration exposes child roles, recursively validates child statistic payloads for tuple/list/map-shaped entries. It stays schema-driven and does not import concrete distribution implementations.
- validate_statistic_layout(x, suff_stat)[source]
Return a declaration or raise
ValueErrorwith statistic-layout issues.
- generated_stacked_available(dist_type)[source]
Return true when declarations can generate stacked leaf scoring.
- generated_stacked_preferred(dist_type)[source]
Return true when a family explicitly opts into declaration-generated scoring.
- generated_stacked_strategy(dist_type)[source]
Describe the declaration-generated stacked scoring route for a family.
- generated_log_density_diagnostics(x, encoded_symbols=None)[source]
Trace a generated scalar log-density formula with the symbolic engine.
The returned dictionary contains the symbolic expression string, referenced data/parameter symbols, operation counts, and expression depth. This is an author-facing inspection tool for declaration-generated kernels; it does not select or execute a runtime backend.
- generated_stacked_params(dists, engine)[source]
Stack declared distribution parameters for generated homogeneous-mixture scoring.
The generated path is intentionally conservative: it supports scalar, vector, or matrix per-component parameters that can be broadcast over per-row encoded fields. Non-differentiable support metadata such as integer bounds must still be shared across components unless a family keeps an explicit
backend_stacked_*route.
- generated_stacked_log_density(enc, params, engine)[source]
Return an
(n, k)log-density matrix from declaration-stacked params.
- generated_log_density(dist, enc, engine)[source]
Return per-row log densities from declaration-owned scoring metadata.
This is the single-distribution counterpart of
generated_stacked_log_density. It gives generic kernels an engine-aware fallback before they drop to legacy NumPyseq_log_densitymethods, and it stays fully metadata-driven: no caller imports or switches on concrete distribution implementations.
- generated_stacked_sufficient_statistics_available(x)[source]
Return true when a declaration can generate resident stacked stats.
- generated_sufficient_statistics_available(x)[source]
Return true when a declaration can generate single-distribution stats.
- generated_sufficient_statistics(dist, enc, weights, engine)[source]
Return legacy sufficient statistics from declaration-owned row stats.
This is the single-distribution analogue of
generated_stacked_sufficient_statistics. It performs row-wise sufficient-statistic algebra on the active engine, then converts only the small legacy payload back across the boundary for the existing estimator M-step.
- generated_numba_log_density_available(x)[source]
Return true when declarations can emit a generated numba leaf scorer.
Exponential-family leaves use the stacked exp-family loop; other leaves with a
backend_log_density_from_paramshook whose per-row formula lowers cleanly to a numba scalar loop (single encoded array, supported ops, scalar parameters) use the generic symbolic-to-numba compiler in_build_generic_numba_kernel().
- generated_numba_stacked_available(x)[source]
Return true when declarations can emit a generated stacked numba scorer.
- generated_numba_log_density(dist, enc)[source]
Return per-row log densities from a declaration-generated numba loop.
The generated loop evaluates the exponential-family form
base(x) + T(x) dot eta(theta) - A(theta). Distribution declarations still own the statistical metadata; this helper only lowers the row fold to a nopython scalar loop.
- generated_numba_stacked_log_density(enc, params)[source]
Return an
(n, k)score matrix from a declaration-generated numba loop.