mixle.stats.compute.exp_family module¶
Uniform exponential-family canonical map for mixle distributions.
Every exponential-family (and conditional-exponential-family) model is expressed in the canonical form
p(x) = h(x) * exp( <eta, T(x)> - A(eta) ) # unconditional p(y|x) = h(y) * exp( <eta(x), T(y)> - A(eta(x)) ) # conditional
This module surfaces that form as a first-class object. The per-family math already
lives in each distribution’s ExponentialFamilySpec
(sufficient_statistics T, natural_parameters eta, log_partition A,
base_measure h); to_exponential_family() reads that declaration and wraps it,
so adding a family is a matter of providing its spec – there is no type switch here.
The container threads a compute engine (numpy by default) so sufficient-statistic maps preserve the selected backend, including its device and gradient lineage.
- class NumericalEstimate(value, error_estimate, method, evaluations, n_samples=None)[source]
Bases:
objectA numerical value together with its method and elementwise error estimate.
- class ExponentialFamilyForm(distribution, spec, engine=<mixle.engines.numpy_engine.NumpyEngine object>)[source]
Bases:
objectCanonical exponential-family view of a single distribution.
Holds the source distribution and its
ExponentialFamilySpec, and exposes the canonical pieces (eta, T(x), A(eta), log h(x)) plus derived quantities. All array methods are vectorized over a leading sample axis and routed throughengine(numpy by default).- Parameters:
distribution (ProbabilityDistribution)
spec (ExponentialFamilySpec)
engine (Any)
- property dim: int
Length of the natural-parameter / sufficient-statistic vector.
- natural_parameters()[source]
Return the natural parameters
eta(theta)for the current parameters.- Return type:
- sufficient_statistics(x)[source]
Return the sufficient statistics
T(x)with shape(n, dim).
- log_partition(eta=None)[source]
Return the log-partition
A.With
eta=None(default) this isA(eta(theta))for the current parameters. Supplyingetarequires the dual maptheta(eta); it is evaluated by reconstructing a distribution viafrom_natural()and is only available wherefrom_naturalhas a closed form.
- log_base_measure(x)[source]
Return
log h(x)(in log-space to avoid e.g.1/x!overflow).
- log_density(x)[source]
Return
<eta, T(x)> - A(eta) + log h(x)– the reconstructed log-density.
- analytic_mean_parameters()[source]
Return an explicitly declared analytic
grad Aimplementation.- Return type:
- autodiff_mean_parameters()[source]
Return
grad Athrough an explicitly declared natural-space autodiff map.- Return type:
- finite_difference_mean_parameters(eps=1.0e-6)[source]
Estimate
grad Aby guarded central differences with a truncation-error estimate.- Parameters:
eps (float)
- Return type:
NumericalEstimate
- monte_carlo_mean_parameters(n_samples=200000, seed=0)[source]
Estimate
E[T]by sampling and report its elementwise standard error.
- estimate_mean_parameters(*, method='auto', eps=1.0e-6, n_samples=200000, seed=0, error_tolerance=None)[source]
Return mean parameters with explicit method and numerical-error provenance.
- mean_parameters(eps=1.0e-6, n_samples=200000, seed=0, *, method='auto', error_tolerance=None)[source]
Return
grad A = E[T]using an explicit or automatically selected method.Use
estimate_mean_parameters()when method/error provenance is needed.autoprefers declared analytic and autodiff maps, then guarded finite differences, and finally Monte Carlo.
- estimate_fisher_information(n_samples=200000, seed=0, *, error_tolerance=None)[source]
Return the Fisher information in natural coordinates,
I(eta) = Cov[T(x)] = grad^2 A(eta).For an exponential family the Fisher information with respect to the natural parameters is exactly the covariance of the sufficient statistic (equivalently the Hessian of the log-partition). This is the second-order companion to
mean_parameters()(grad A = E[T]); it is estimated by the sample covariance ofT(x)overn_samplesdraws – approximate, but universal for any samplable family – and returned as a(dim, dim)symmetric positive-semidefinite matrix.
- fisher_information(n_samples=200000, seed=0, *, error_tolerance=None)[source]
Return a Monte-Carlo Fisher estimate; use
estimate_fisher_informationfor its error.
- class ProductExponentialFamilyForm(distribution, components, engine=<mixle.engines.numpy_engine.NumpyEngine object>, extract=None)[source]
Bases:
objectCanonical exponential-family view of an independent product of distributions.
A product of exponential families is itself an exponential family with
eta = concat(eta_i),T(x) = concat(T_i(x_i)),A = sum A_i, andlog h(x) = sum log h_i(x_i)(the closure rule for an independent product). Used forCompositeDistribution.- Parameters:
- property dim: int
Total natural-parameter dimension (sum over components).
- natural_parameters()[source]
Return the concatenated natural parameters of all components.
- Return type:
- sufficient_statistics(x)[source]
Return concatenated per-component sufficient statistics
(n, dim).
- log_partition(eta=None)[source]
Return
A = sum_i A_i(current parameters only;etaoverride unsupported).
- log_base_measure(x)[source]
Return
log h(x) = sum_i log h_i(x_i)row-wise.
- log_density(x)[source]
Return the reconstructed log-density
sum_i log p_i(x_i)row-wise.
- class IIDExponentialFamilyForm(distribution, element, engine=<mixle.engines.numpy_engine.NumpyEngine object>)[source]
Bases:
objectCanonical exponential-family view of an iid sequence of a fixed leaf family.
For a fixed-length-agnostic iid sequence the joint sufficient statistic is the sum of the per-element statistics,
T(x) = sum_t T_0(x_t), the natural parameters are shared (eta = eta_0), andA/log hscale with the element count. Used forSequenceDistributionwhen the length is not separately modeled.- Parameters:
distribution (ProbabilityDistribution)
element (ExponentialFamilyForm)
engine (Any)
- property dim: int
Natural-parameter dimension (same as the element family).
- sufficient_statistics(x)[source]
Return per-sequence summed element statistics
(n, dim).
- log_partition(eta=None)[source]
Return the per-element
A(the joint scales by the element count).
- class MultinomialExponentialFamilyForm(distribution, element, engine=<mixle.engines.numpy_engine.NumpyEngine object>)[source]
Bases:
objectCanonical exponential-family view of a multinomial over an exp-family element.
A multinomial observation is a bag
{(v_j, c_j)}of values with counts, and the (non length-normalized, no separate trial distribution) log-density is the multinomial coefficient plus the count-weighted sum of the element log-densities. So the natural parameters are the element’s (eta = eta_0), the sufficient statistic is the count-weighted sumT(x) = sum_j c_j T_0(v_j),log h(x) = log(n! / prod_j c_j!) + sum_j c_j log h_0(v_j), andAis the element’s per-trial partition (the joint scales by the total countn = sum_j c_j). Built byto_exponential_family().- Parameters:
distribution (ProbabilityDistribution)
element (ExponentialFamilyForm)
engine (Any)
- property dim: int
Natural-parameter dimension (same as the element family).
- sufficient_statistics(x)[source]
Return the per-observation count-weighted element statistics
(n, dim).
- log_partition(eta=None)[source]
Return the per-trial
A(the joint scales by the total count).
- log_base_measure(x)[source]
Return the multinomial coefficient plus child base measures.
- log_density(x)[source]
Return the normalized multinomial count-vector log mass per observation.
- class ConditionalExponentialFamilyForm(response_family, natural_fn, log_partition_fn=None, mean_fn=None, dispersion=None, engine=<mixle.engines.numpy_engine.NumpyEngine object>)[source]
Bases:
objectCanonical exponential-family view of a conditional model
p(y | x).The response family fixes
T,A, andh;natural_parameters(x)supplies the per-row natural parameterseta(x)(the linear predictor for a canonical link).dispersioncarries a nuisance/dispersion parameter when the response family has one (e.g. a Gaussian variance).- Parameters:
- natural_parameters(x)[source]
Return the per-row natural parameters
eta(x).
- sufficient_statistics(y)[source]
Return the response sufficient statistics
T(y).
- log_base_measure(y)[source]
Return
log h(y)for the response family.
- log_partition(eta)[source]
Return
A(eta)row-wise for a stack of natural parameters.Uses the family-specific
log_partition_fn(supplied by the GLM wiring, which knows the response family) when present; otherwise falls back to the response family’s dual map (only available wherefrom_naturalis closed-form).
- mean(x)[source]
Return the conditional mean
E[y|x] = link_inv(eta(x))(the inverse link).
- to_exponential_family(dist, engine=NUMPY_ENGINE)[source]
Return the canonical exponential-family view of
distorNone.Mirrors
mixle.inference.fisher.to_fisher(): a thin top-level helper that defers toProbabilityDistribution.to_exponential_family(). ReturnsNonewhendistis not a (single) exponential family.- Parameters:
dist (ProbabilityDistribution)
engine (Any)
- Return type:
ExponentialFamilyForm | None