"""trimmed_mean — Arithmetic mean after trimming extreme percentiles.""" import math import numpy as np def trimmed_mean(values: list[float], trim: float = 0.05) -> float: """Return the trimmed arithmetic mean of values. Cuts the bottom `trim` and top `trim` percentiles before averaging. Returns math.nan for an empty list or when trimming removes all elements. Args: values: List of numeric values. trim: Fraction to cut from each tail (0 <= trim < 0.5). Returns: Trimmed mean as float, or math.nan if the list is empty. """ if not values: return math.nan arr = np.array(values, dtype=float) lo = np.percentile(arr, trim * 100) hi = np.percentile(arr, (1 - trim) * 100) trimmed = arr[(arr >= lo) & (arr <= hi)] if len(trimmed) == 0: return math.nan return float(np.mean(trimmed))