mixle.stats.univariate.continuous.half_normal module

Half-normal distributions over non-negative real values.

Observations are floats x >= 0. A half-normal distribution with scale sigma > 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: SequenceEncodableProbabilityDistribution

Half-normal distribution with scale sigma > 0 on x >= 0.

Parameters:
classmethod compute_capabilities()[source]

Describe backend support for generated half-normal kernels.

classmethod compute_declaration()[source]

Return the structured compute declaration for half-normal distributions.

static exp_family_sufficient_statistics(x, engine)[source]

Return the (x**2,) sufficient statistic for generated scoring.

Parameters:
Return type:

tuple[Any, …]

static exp_family_natural_parameters(params, engine)[source]

Return the (-1/(2*sigma^2),) natural parameter for generated scoring.

Parameters:
Return type:

tuple[Any, …]

static exp_family_log_partition(params, engine)[source]

Return the half-normal log partition log(sigma).

Parameters:
Return type:

Any

static exp_family_base_measure(x, engine)[source]

Return the support base measure 0.5*log(2/pi) (or -inf off support x >= 0).

Parameters:
Return type:

Any

static exp_family_legacy_sufficient_statistics(x, params, engine)[source]

Return per-row (count, x**2) sufficient statistics in accumulator order.

Parameters:
Return type:

tuple[Any, …]

density(x)[source]

Return the probability density at a single observation.

Parameters:

x (float)

Return type:

float

log_density(x)[source]

Return the log-density at a single observation (or -inf off support).

Parameters:

x (float)

Return type:

float

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:

ndarray

static backend_log_density_from_params(vals, sq_vals, sigma, engine)[source]

Engine-neutral half-normal log-density from explicit parameters.

Parameters:
Return type:

Any

backend_seq_log_density(x, engine)[source]

Engine-neutral vectorized log-density for encoded data.

Parameters:
Return type:

Any

classmethod backend_stacked_params(dists, engine)[source]

Return stacked parameters for a homogeneous mixture kernel.

Parameters:
  • dists (Sequence[HalfNormalDistribution])

  • engine (Any)

Return type:

dict[str, Any]

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

Return an (n, k) matrix of half-normal log densities.

Parameters:
Return type:

Any

classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]

Return stacked sufficient statistics using engine-resident arrays.

Parameters:
Return type:

tuple[Any, Any]

cdf(x)[source]

Cumulative distribution function P(X <= x) (0 for x < 0).

Parameters:

x (float)

Return type:

float

quantile(q)[source]

Inverse CDF F^{-1}(q).

Parameters:

q (float)

Return type:

float

entropy()[source]

Differential entropy 0.5*log(pi*sigma^2/2) + 1/2.

Return type:

float

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: DistributionSampler

Draw iid half-normal observations as |N(0, sigma**2)|.

Parameters:
  • dist (HalfNormalDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw size iid observations (a float when size is None).

Parameters:

size (int | None)

Return type:

float | ndarray

class HalfNormalAccumulator(keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate weighted count and sum of squares for half-normal estimation.

Parameters:

keys (str | None)

update(x, weight, estimate)[source]

Accumulate weighted squared observations for one non-negative sample.

Parameters:
  • x (float)

  • weight (float)

  • estimate (HalfNormalDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]

Initialize statistics from one observation.

Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate weighted squared observations from encoded data.

Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize statistics from encoded observations.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge another half-normal sufficient-statistic tuple.

Parameters:

suff_stat (tuple[float, float])

Return type:

HalfNormalAccumulator

value()[source]

Return count and squared-observation sum.

Return type:

tuple[float, float]

from_value(x)[source]

Replace accumulator contents from a sufficient-statistic tuple.

Parameters:

x (tuple[float, float])

Return type:

HalfNormalAccumulator

key_merge(stats_dict)[source]

Merge keyed statistics into stats_dict when keys are configured.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator from keyed statistics when available.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Return the encoder used by this accumulator.

Return type:

HalfNormalDataEncoder

class HalfNormalAccumulatorFactory(keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for HalfNormalAccumulator.

Parameters:

keys (str | None)

make()[source]

Create a fresh half-normal accumulator.

Return type:

HalfNormalAccumulator

class HalfNormalEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None)[source]

Bases: ParameterEstimator

Maximum-likelihood estimator for the half-normal scale: sigma = sqrt(mean(x**2)).

Parameters:
  • pseudo_count (float | None)

  • suff_stat (float | None)

  • name (str | None)

  • keys (str | None)

accumulator_factory()[source]

Return an accumulator factory for half-normal statistics.

Return type:

HalfNormalAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate the half-normal scale from weighted squared observations.

Parameters:
Return type:

HalfNormalDistribution

class HalfNormalDataEncoder[source]

Bases: DataSequenceEncoder

Encode half-normal observations as x and x**2.

seq_encode(x)[source]

Encode observations as values and squared values.

Parameters:

x (Sequence[float])

Return type:

tuple[ndarray, ndarray]