mixle.stats.univariate.continuous.half_normal module¶
Create, estimate, and sample from a half-normal distribution (folded normal at 0).
Defines the HalfNormalDistribution, HalfNormalSampler, HalfNormalAccumulatorFactory, HalfNormalAccumulator, HalfNormalEstimator, and the HalfNormalDataEncoder classes for use with mixle.
Data type: (float): The HalfNormalDistribution with scale sigma > 0.0 has log-density
log(f(x; sigma)) = 0.5*log(2/pi) - log(sigma) - x**2 / (2*sigma**2), for x >= 0.0,
and -np.inf otherwise.
The half-normal is a one-parameter exponential family with sufficient statistic x**2:
- log(f) = base(x) + eta*x**2 - A(sigma),
base(x) = 0.5*log(2/pi), (a constant on the support x >= 0) eta = -1 / (2*sigma**2), A(sigma) = log(sigma).
Declaring those pieces gives the family generated NumPy/Torch/Numba scoring through the shared exponential-family compute path, exactly as for the Gamma and inverse Gaussian families.
Reference: Johnson, Kotz & Balakrishnan, Continuous Univariate Distributions (2nd ed., Wiley, 1994/95).
- class HalfNormalDistribution(sigma, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionHalf-normal distribution with scale sigma > 0 on x >= 0.
- classmethod compute_capabilities()[source]
- classmethod compute_declaration()[source]
- static exp_family_sufficient_statistics(x, engine)[source]
Return the (x**2,) sufficient statistic for generated scoring.
- static exp_family_natural_parameters(params, engine)[source]
Return the (-1/(2*sigma^2),) natural parameter for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return the half-normal log partition log(sigma).
- static exp_family_base_measure(x, engine)[source]
Return the support base measure 0.5*log(2/pi) (or -inf off support x >= 0).
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row (count, x**2) sufficient statistics in accumulator order.
- density(x)[source]
Return the probability density at a single observation.
- log_density(x)[source]
Return the log-density at a single observation (or -inf off support).
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- Parameters:
x (Tuple[ndarray, ndarray]) – Tuple of observations and squared observations produced by the HalfNormalDataEncoder.
- Returns:
Numpy array of log-density values, with -inf entries off the non-negative support.
- Return type:
- static backend_log_density_from_params(vals, sq_vals, sigma, engine)[source]
Engine-neutral half-normal log-density from explicit parameters.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded data.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of half-normal log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked sufficient statistics using engine-resident arrays.
- cdf(x)[source]
Cumulative distribution function P(X <= x) (0 for x < 0).
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
HalfNormalSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (Optional[float]) – Re-weight the second moment toward this instance’s own E[x**2] = sigma**2 when not None (a simple ridge toward the current parameter).
- Returns:
HalfNormalEstimator object.
- Return type:
HalfNormalEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
HalfNormalDataEncoder
- class HalfNormalSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid half-normal observations as |N(0, sigma**2)|.
- Parameters:
dist (HalfNormalDistribution)
seed (int | None)
- class HalfNormalAccumulator(keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum of squares for half-normal estimation.
- Parameters:
keys (str | None)
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (float)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
HalfNormalDataEncoder
- class HalfNormalAccumulatorFactory(keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for HalfNormalAccumulator.
- Parameters:
keys (str | None)
- make()[source]
- Return type:
HalfNormalAccumulator