mixle.stats.combinator.transform module

Invertible-transform wrappers for sequence-encodable distributions.

The module implements identity, affine, exponential, logit, and custom transforms plus the distribution, sampler, accumulator, and encoder plumbing needed to apply them inside Mixle combinators.

exception TransformDomainError[source]

Bases: ValueError

Raised when an observation lies outside a transform’s declared domain.

exception TransformCompatibilityError[source]

Bases: TypeError

Raised when a transform contract is incompatible with its child distribution.

class TransformContract(input_domain, output_domain, measure='adaptive', bijective=True)[source]

Bases: object

Machine-checkable declaration for a deterministic bijective transform.

input_domain and output_domain use the same coarse vocabulary as compute-declaration support tags. measure is "adaptive" when the transform supports both counting measure and continuous change-of-variables, or "discrete" for a counting-measure-only mapping.

Parameters:
  • input_domain (str)

  • output_domain (str)

  • measure (str)

  • bijective (bool)

class TransformFitReceipt(accepted_weight, rejected_weight, rejected_fraction)[source]

Bases: NamedTuple

Accepted/rejected evidence accounting attached to a fitted transform wrapper.

Parameters:
  • accepted_weight (float)

  • rejected_weight (float)

  • rejected_fraction (float)

accepted_weight: float

Alias for field number 0

rejected_weight: float

Alias for field number 1

rejected_fraction: float

Alias for field number 2

class TransformStatistics(schema_version, child, accepted_weight, rejected_weight)[source]

Bases: NamedTuple

Versioned child statistics plus transform-domain evidence accounting.

Parameters:
  • schema_version (int)

  • child (Any)

  • accepted_weight (float)

  • rejected_weight (float)

schema_version: int

Alias for field number 0

child: Any

Alias for field number 1

accepted_weight: float

Alias for field number 2

rejected_weight: float

Alias for field number 3

class IdentityTransform[source]

Bases: object

Identity transform y = x.

forward(x)[source]

Return x unchanged.

Parameters:

x (Any)

Return type:

Any

inverse(y)[source]

Return y unchanged.

Parameters:

y (Any)

Return type:

Any

log_abs_det_inverse_jacobian(y)[source]

Return the log absolute inverse-Jacobian determinant.

Parameters:

y (Any)

Return type:

float

invalid_inverse_value()[source]

Return a safe child-space fill value for invalid inverses.

Return type:

float

class AffineTransform(loc=0.0, scale=1.0)[source]

Bases: object

Affine transform y = loc + scale * x.

Parameters:
forward(x)[source]

Apply the affine map to a child-space value.

Parameters:

x (Any)

Return type:

Any

inverse(y)[source]

Map a transformed-space value back to child space.

Parameters:

y (Any)

Return type:

Any

log_abs_det_inverse_jacobian(y)[source]

Return the constant affine inverse-Jacobian correction.

Parameters:

y (Any)

Return type:

float

invalid_inverse_value()[source]

Return a safe child-space fill value for invalid inverses.

Return type:

float

class ExpTransform[source]

Bases: object

Exponential transform y = exp(x), mapping real x to positive y.

forward(x)[source]

Map a real value to the positive scale.

Parameters:

x (Any)

Return type:

Any

inverse(y)[source]

Map a positive transformed value back to the real line.

Parameters:

y (Any)

Return type:

Any

log_abs_det_inverse_jacobian(y)[source]

Return the log inverse-Jacobian correction for log(y).

Parameters:

y (Any)

Return type:

float

invalid_inverse_value()[source]

Return a safe child-space fill value for invalid inverses.

Return type:

float

class LogTransform[source]

Bases: object

Log transform y = log(x), mapping positive x to real y.

forward(x)[source]

Map a positive child value to the real line.

Parameters:

x (Any)

Return type:

Any

inverse(y)[source]

Map a real transformed value back to the positive scale.

Parameters:

y (Any)

Return type:

Any

log_abs_det_inverse_jacobian(y)[source]

Return the log inverse-Jacobian correction for exp(y).

Parameters:

y (Any)

Return type:

float

invalid_inverse_value()[source]

Return a safe positive child-space fill value for invalid inverses.

Return type:

float

class LogitTransform[source]

Bases: object

Logistic transform y = 1 / (1 + exp(-x)), mapping real x to (0, 1).

forward(x)[source]

Map a real value into the open unit interval.

Parameters:

x (Any)

Return type:

Any

inverse(y)[source]

Map a unit-interval value back to the real line.

Parameters:

y (Any)

Return type:

Any

log_abs_det_inverse_jacobian(y)[source]

Return the log inverse-Jacobian correction for the logit map.

Parameters:

y (Any)

Return type:

float

invalid_inverse_value()[source]

Return a safe child-space fill value for invalid inverses.

Return type:

float

class TransformDistribution(dist, transform=None, density_correction=None, name=None, keys=None, fit_receipt=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Push a child distribution through a fixed invertible transform.

Observations live in transformed space. For fixed continuous transforms, log-density uses the inverse transform and adds the inverse-Jacobian term. The transform is not learned; estimation inverse-transforms observations and delegates sufficient statistics to the child estimator.

Parameters:
  • dist (SequenceEncodableProbabilityDistribution)

  • transform (Any | None)

  • density_correction (bool | None)

  • name (str | None)

  • keys (str | None)

  • fit_receipt (TransformFitReceipt | None)

mean()[source]

Exact mean under an affine map: loc + scale * E[X].

An affine map is the one transform whose moments are exact in closed form, and the PPL’s .mean()/.var() fall back to Monte Carlo when a distribution defines neither – so 3 * Normal(0, 1) + 1 reported a sampled 1.02 and 8.95 beside prose calling the map exact (P08-F14). Every other transform has no closed form here and says so, which is what sends the caller back to the sampled estimate.

variance()[source]

Exact variance under an affine map: scale**2 * Var[X].

compute_capabilities()[source]

Return capabilities delegated from the child distribution where safe.

compute_declaration()[source]

Return a declaration describing this distribution as a transformed child.

density(x)[source]

Return the probability density or mass at a single observation.

Parameters:

x (Any)

Return type:

float

log_density(x)[source]

Return the log-density or log-mass at a single observation.

Parameters:

x (Any)

Return type:

float

seq_log_density(x)[source]

Return vectorized log-density values for sequence-encoded observations.

Parameters:

x (tuple[Any, ndarray, ndarray])

Return type:

ndarray

backend_seq_log_density(x, engine)[source]

Engine-neutral vectorized log-density for inverse-encoded observations.

Parameters:
Return type:

Any

classmethod backend_stacked_params(dists, engine)[source]

Return stacked child parameters for homogeneous fixed-transform mixtures.

Parameters:
Return type:

dict[str, Any]

classmethod backend_stacked_log_density(x, params, engine)[source]

Return an (n, k) matrix of transformed child log densities.

Parameters:
Return type:

Any

gradient_fit_state(engine, torch, leaves, recurse, tensor_param)[source]

Return distribution-owned state for autograd fitting.

Parameters:
Return type:

Any

sampler(seed=None)[source]

Return a sampler for drawing observations from this distribution.

Parameters:

seed (int | None)

Return type:

TransformSampler

estimator(pseudo_count=None)[source]

Return an estimator for fitting this distribution from data.

Parameters:

pseudo_count (float | None)

Return type:

TransformEstimator

dist_to_encoder()[source]

Return the data encoder used by this distribution for vectorized methods.

Return type:

TransformDataEncoder

enumerator()[source]

Return an enumerator over the distribution support when available.

Return type:

TransformEnumerator

class TransformEnumerator(dist)[source]

Bases: DistributionEnumerator

Enumerate transformed child support for discrete child distributions.

Parameters:

dist (TransformDistribution)

class TransformSampler(dist, seed=None)[source]

Bases: DistributionSampler

Sampler that transforms draws from the child distribution.

Parameters:
  • dist (TransformDistribution)

  • seed (int | None)

sample(size=None, *, batched=True)[source]

Draw child samples and map them through the transform.

Parameters:
class TransformAccumulator(accumulator, transform, density_correction=None, name=None, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulator that delegates inverse-transformed observations to the child.

Parameters:
  • accumulator (SequenceEncodableStatisticAccumulator)

  • transform (Any)

  • density_correction (bool | None)

  • name (str | None)

  • keys (str | None)

update(x, weight, estimate)[source]

Accumulate one inverse-transformed observation when it is valid.

Parameters:
  • x (Any)

  • weight (float)

  • estimate (TransformDistribution | None)

Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate a batch using validity-masked child weights.

Parameters:
Return type:

None

seq_update_engine(x, weights, estimate, engine)[source]

Engine-resident E-step: the validity-masked weights are formed on the active engine and the child accumulator is routed through the engine. Matches seq_update.

Parameters:
Return type:

None

initialize(x, weight, rng)[source]

Initialize from one inverse-transformed observation when it is valid.

Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize from a validity-masked encoded batch.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge child sufficient statistics and evidence accounting.

Parameters:

suff_stat (TransformStatistics)

Return type:

TransformAccumulator

value()[source]

Return versioned child statistics and transform-domain evidence accounting.

Return type:

TransformStatistics

from_value(x)[source]

Restore child statistics and evidence accounting.

Parameters:

x (TransformStatistics)

Return type:

TransformAccumulator

scale(c)[source]

Scale delegated sufficient statistics and evidence weights by c.

Parameters:

c (float)

Return type:

TransformAccumulator

key_merge(stats_dict)[source]

Pool the wrapper’s full child statistics under its own key, or delegate when unkeyed.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace from the wrapper pool, or delegate when this wrapper is unkeyed.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Return the encoder associated with this accumulator.

Return type:

TransformDataEncoder

class TransformAccumulatorFactory(factory, transform, density_correction=None, name=None, keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for TransformAccumulator.

Parameters:
  • factory (StatisticAccumulatorFactory)

  • transform (Any)

  • density_correction (bool | None)

  • name (str | None)

  • keys (str | None)

make()[source]

Create a fresh transform accumulator.

Return type:

TransformAccumulator

class TransformEstimator(estimator, transform=None, density_correction=None, name=None, keys=None)[source]

Bases: ParameterEstimator

Estimator for fixed-transform distributions.

Parameters:
  • estimator (ParameterEstimator)

  • transform (Any | None)

  • density_correction (bool | None)

  • name (str | None)

  • keys (str | None)

accumulator_factory()[source]

Return the accumulator factory for inverse-transformed observations.

Return type:

TransformAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate from accepted evidence and attach the rejected-weight receipt.

Parameters:
Return type:

TransformDistribution

class TransformDataEncoder(encoder, transform, density_correction=True)[source]

Bases: DataSequenceEncoder

Encode transformed observations as inverse child data plus Jacobian terms.

Parameters:
  • encoder (DataSequenceEncoder)

  • transform (Any)

  • density_correction (bool | None)

seq_encode(x)[source]

Encode observations as inverse child values, Jacobians, and validity flags.

Parameters:

x (Sequence[Any])

Return type:

tuple[Any, ndarray, ndarray]