[1bd6b5]: / helpers / stats.py

Download this file

16 lines (13 with data), 399 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import random
from typing import Iterable, Callable
def bootstrap(data: Iterable, stat: Callable, ci=.95, r=1000):
if not isinstance(data, list):
data = list(data)
distribution = sorted([
stat(random.choices(data, k=len(data)))
for i in range(r)
])
return {
'lower': distribution[int((1 - ci) * r)],
'upper': distribution[int(ci * r)]
}