trim_mean#

bamengine.utils.trim_mean(values, trim_pct=0.05)[source]#

Calculate two-sided trimmed mean (robust statistic).

Computes the mean after removing a percentage of the smallest and largest values. This provides a robust estimate of central tendency that is less sensitive to outliers than the arithmetic mean.

Parameters:
  • values (Float1D) – 1D array of values to average.

  • trim_pct (float, optional) – Proportion of values to trim from each tail (default: 0.05 = 5%). For example, 0.05 removes the bottom 5% and top 5% of values.

Returns:

Trimmed mean of the values. Returns 0.0 if input array is empty.

Return type:

float

Examples

Calculate trimmed mean removing 10% from each tail:

>>> import numpy as np
>>> from bamengine.utils import trim_mean
>>> values = np.array([1, 2, 3, 4, 5, 100])  # 100 is an outlier
>>> trim_mean(values, trim_pct=0.10)
3.5

Default 5% trimming:

>>> values = np.arange(1, 101)  # 1 to 100
>>> mean = trim_mean(values)  # Removes bottom/top 5% (5 values each)

Notes

  • Uses np.argpartition for O(n) selection instead of O(n log n) sorting

  • If trim_pct results in k=0, returns regular mean

  • Compatible with scipy.stats.trim_mean behavior

  • Widely used in BAM Engine for initializing new firms/banks from survivors

See also

trimmed_weighted_mean

Weighted version with optional weight filtering

scipy.stats.trim_mean

SciPy implementation