mixle.stats.combinator.schema module

Typed structured schema – named, type-validated fields over a CompositeDistribution.

mixle’s identity is composable models of heterogeneous data; CompositeDistribution already models a product of differently-typed fields, but only positionally (a bare tuple, no names, no validation). A Schema puts a typed record front-end on it: declare {name: (type, distribution)}, score/sample with ordinary dicts, and have observations validated against their declared types before they reach the model. The field types reuse the same friendly specs as SelectDistribution.by_type (‘str’, ‘int’, ‘float’, ‘number’, …), numpy-scalar aware.

class Field(name, type_spec, dist)[source]

Bases: object

One schema field: a name, the type(s) its value must be, and the distribution that models it.

Parameters:
name: str
type_spec: Any
dist: Any
class Schema(fields)[source]

Bases: object

An ordered set of named, typed fields backed by a CompositeDistribution.

Parameters:

fields (list[Field])

classmethod from_fields(specs)[source]

Build from [(name, type_spec, distribution), ...].

Parameters:

specs (list[tuple[str, Any, Any]])

Return type:

Schema

validate(record)[source]

Raise if record is missing/has extra fields or a value’s type violates its declaration.

Parameters:

record (dict[str, Any])

Return type:

None

to_tuple(record)[source]

Validate record and order its values into the composite’s positional tuple.

Parameters:

record (dict[str, Any])

Return type:

tuple[Any, …]

from_tuple(values)[source]
Parameters:

values (tuple[Any, ...])

Return type:

dict[str, Any]

log_density(record)[source]

Validated, named log-density: delegates to the backing composite after the type check.

Parameters:

record (dict[str, Any])

Return type:

float

density(record)[source]
Parameters:

record (dict[str, Any])

Return type:

float

sample(seed=None, size=None)[source]

Draw record dict(s) from the model.

Parameters:
  • seed (int | None)

  • size (int | None)

Return type:

Any

marginal(names)[source]

The sub-schema over a subset of fields (a marginal of the joint, in the given order).

Parameters:

names (list[str])

Return type:

Schema

field_distribution(name)[source]
Parameters:

name (str)

Return type:

Any