mixle.stats.compute.kernel module¶
Backend-neutral evaluation kernel contracts.
The generic kernel is a thin adapter over the existing seq_* protocol. It is the guaranteed fallback for engine-aware orchestration; specialized factories can override code shape for performance without changing estimators.
- exception EngineNotSupportedError[source]
Bases:
ValueErrorRaised when no kernel can safely evaluate a distribution on an engine.
- class Kernel[source]
Bases:
ABCEvaluation kernel for a fitted distribution.
- abstractmethod score(enc)[source]
Return per-row log densities for an encoded observation batch.
- component_scores(enc)[source]
Return per-row, per-component log densities where meaningful.
- abstractmethod accumulate(enc, weights)[source]
Return sufficient statistics in the legacy estimator format.
- abstractmethod refresh(dist)[source]
Refresh kernel parameters after an EM M-step without rebuilding structure.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
- Return type:
None
- class KernelFactory[source]
Bases:
ABCFactory that builds a Kernel for a distribution and engine.
- abstractmethod build(dist, engine, estimator=None)[source]
Return a kernel for
distonengine.estimatoris optional for pure scoring and required for kernels that need to emit sufficient statistics for an M-step.- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- Return type:
Kernel
- class GenericKernel(dist, engine=NUMPY_ENGINE, estimator=None)[source]
Bases:
KernelFallback kernel over distribution-owned backend hooks or existing seq_* methods.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- score(enc)[source]
Return per-row log densities using backend hooks when available.
- component_scores(enc)[source]
Return per-row component log densities for mixture-like models.
- accumulate(enc, weights)[source]
Accumulate weighted sufficient statistics in estimator-owned format.
- refresh(dist)[source]
Replace the fitted distribution while preserving kernel structure.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
- Return type:
None
- class GenericKernelFactory[source]
Bases:
KernelFactoryGuaranteed fallback factory for distributions that support the engine.
- build(dist, engine, estimator=None)[source]
Build a generic kernel or fail fast when the engine is unsupported.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- Return type:
GenericKernel
- class NumbaKernel(dist, engine=NUMPY_ENGINE, estimator=None)[source]
Bases:
KernelKernel adapter over the existing fused-numba
CompiledMixturepath.This kernel intentionally uses the columnar encoding returned by
encode(data)rather than the legacydist_to_encoder().seq_encodepayload. That keeps the high-performance path explicit while giving it the same score/accumulate/refresh surface as other engines.- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- encode(data)[source]
Encode raw observations into the fused columnar kernel format.
- score(enc)[source]
Return per-row log densities from the fused numba mixture kernel.
- component_scores(enc)[source]
Return per-row, per-component log densities from the fused kernel.
- accumulate(enc, weights)[source]
Use fused posteriors plus row weights to produce legacy statistics.
- refresh(dist)[source]
Refresh parameters after an M-step without rebuilding the compiled object.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
- Return type:
None
- class GeneratedNumbaKernel(dist, engine=NUMPY_ENGINE, estimator=None)[source]
Bases:
KernelGenerated numba kernel from declaration exponential-family metadata.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- encode(data)[source]
Encode raw observations with the distribution’s ordinary encoder.
- score(enc)[source]
Return per-row log densities from declaration-generated numba code.
- component_scores(enc)[source]
Return generated component scores for homogeneous generated mixtures.
- accumulate(enc, weights)[source]
Accumulate generated sufficient statistics for leaves or mixtures.
- posteriors(enc)[source]
Return normalized mixture posterior weights for generated mixtures.
- refresh(dist)[source]
Refresh the distribution and regenerated component metadata.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
- Return type:
None
- class NumbaKernelFactory[source]
Bases:
KernelFactoryFactory for generated declaration numba kernels with legacy fused fallback.
- build(dist, engine, estimator=None)[source]
Prefer generated numba kernels, then fused kernels, then stacked fallback.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- Return type:
Kernel
- class GeneratedNumbaKernelFactory(fallback=None)[source]
Bases:
KernelFactoryDefault-safe factory that prefers declaration-generated numba kernels.
Unlike
NumbaKernelFactory, this never selects the fusedCompiledMixtureadapter (whose columnar encoding is incompatible with the legacyseq_encodepayloads that the engine estimation path feeds kernels) and never raises: when a generated numba scorer is unavailable, or the engine is not numpy, it defers to a guaranteed fallback (the generic kernel). That makes it safe to register as a default on the kernel dispatch path while still accelerating mixtures of declared exponential-family leaves on the numpy engine.- Parameters:
fallback (KernelFactory | None)
- build(dist, engine, estimator=None)[source]
Build a generated numba kernel on numpy when available, else fall back.
- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine)
estimator (ParameterEstimator | None)
- Return type:
Kernel
- register_kernel_factory(dist_type, factory)[source]
Register a specialized kernel factory for a distribution class.
- kernel_for(dist, engine=None, estimator=None)[source]
Build the best registered kernel for
distandengine.- Parameters:
dist (SequenceEncodableProbabilityDistribution)
engine (ComputeEngine | None)
estimator (ParameterEstimator | None)
- Return type:
Kernel