File size: 1,038 Bytes
fb37bc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/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)