QT26-QC / evaluation /bootstrap.py
leoncca's picture
Add files using upload-large-folder tool
fb37bc0 verified
Raw
History Blame Contribute Delete
1.04 kB
#!/usr/bin/env python3
"""Species-cluster bootstrap utilities for QT26-QC scoring."""
from __future__ import annotations
import numpy as np
def cluster_interval(
values: np.ndarray,
species: np.ndarray,
*,
replicates: int = 20_000,
seed: int = 20260730,
macro: bool = False,
) -> tuple[float, float, float]:
keys, inverse = np.unique(species, return_inverse=True)
clusters = [values[inverse == i] for i in range(len(keys))]
rng = np.random.default_rng(seed)
estimates = np.empty(replicates, dtype=np.float64)
for r in range(replicates):
selected = rng.integers(0, len(clusters), size=len(clusters))
if macro:
estimates[r] = np.mean([clusters[i].mean() for i in selected])
else:
estimates[r] = np.concatenate([clusters[i] for i in selected]).mean()
point = np.mean([cluster.mean() for cluster in clusters]) if macro else values.mean()
low, high = np.quantile(estimates, [0.025, 0.975])
return float(point), float(low), float(high)