Utilities#

Utility functions for BAM Engine.

This module provides common utility functions used throughout BAM Engine, including statistical operations (trimmed means), random sampling utilities, efficient array operations, and batch market matching primitives.

Constants#

EPSfloat

Machine epsilon for numerical comparisons (1.0e-9). Used to avoid division by zero and detect near-zero values.

Functions#

trim_mean

Calculate two-sided trimmed mean (SciPy-style).

trimmed_weighted_mean

Calculate weighted trimmed mean with optional weight filtering.

sample_beta_with_mean

Draw samples from Beta distribution with specified mean.

select_top_k_indices_sorted

Efficiently select and sort top-k elements using argpartition.

grouped_cumsum

Per-group prefix sums via the subtract-offset trick.

resolve_conflicts

Batch conflict resolution for oversubscribed targets.

See also

bamengine.typing

Type aliases used in this module

scipy.stats.trim_mean

SciPy implementation of trimmed mean

Notes

  • Trimmed means are robust statistics that exclude extreme values

  • Beta sampling is used for initialization with controlled variance

  • Top-k selection uses argpartition for O(n) performance vs O(n log n) for sort

  • grouped_cumsum and resolve_conflicts underpin vectorized market matching

Helper functions and utilities for BAM Engine.

Random Number Generation#

make_rng

Create a new random number generator.

Constants#

bamengine.utils.EPS#

Machine epsilon for numerical comparisons (1.0e-9).

Statistical Functions#

trim_mean

Calculate two-sided trimmed mean (robust statistic).

trimmed_weighted_mean

Calculate trimmed weighted mean with optional weight filtering.

Sampling Functions#

sample_beta_with_mean

Draw n samples from a Beta distribution scaled to [low, high), such that the scaled mean is approximately ``mean``.

Array Operations#

select_top_k_indices_sorted

Returns indices of k smallest/largest elements, sorted along the last axis.