-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
71 lines (57 loc) · 2.38 KB
/
bootstrap.py
File metadata and controls
71 lines (57 loc) · 2.38 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import List, Tuple
import itertools
import numpy as np
import pandas as pd
from statsmodels.formula.api import ols
from scipy.stats import linregress
from confusion import ConfusionMatrix
from sensitivity import get_interstim_sensitivities_for_confusion_matrix, InterstimSensitivities
from rmcorr import get_rmcorr_for_sensitivities_by_participant
rng = np.random.default_rng(8675309)
def _get_slope_for_sensitivites_by_participant(
sensitivities_by_participant: List[List[InterstimSensitivities]],
):
flattened_sensitivities = itertools.chain(*sensitivities_by_participant)
x, y = zip(*flattened_sensitivities)
linreg = linregress(x, y)
return linreg.slope, linreg.intercept
def _get_rmcorr_slope_for_sensitivities_by_participant(
sensitivities_by_participant: List[List[InterstimSensitivities]],
):
slope, *_ = get_rmcorr_for_sensitivities_by_participant(sensitivities_by_participant)
return slope
def bootstrap_dprime_slope(
matrices: List[ConfusionMatrix],
sample_size=12,
iterations=10000,
use_rmcorr=False,
):
get_slope = (
_get_rmcorr_slope_for_sensitivities_by_participant
if use_rmcorr
else _get_slope_for_sensitivites_by_participant
)
sensitivities = [get_interstim_sensitivities_for_confusion_matrix(cm) for cm in matrices]
overall_slope, overall_intercept = get_slope(sensitivities)
slopes = []
r2s = []
for i in range(iterations):
psuedosample_sensitivities = rng.choice(sensitivities, sample_size)
slope, _ = get_slope(psuedosample_sensitivities)
slopes.append(slope)
return (overall_slope, overall_intercept), list(sorted(slopes))
def percentiles(values):
lower = np.percentile(values, 0.025)
median = np.median(values)
upper = np.percentile(values, 100 - 0.025)
return (lower, median, upper)
def bootstrap_dprime_rmcorr_ci95(matrices: List[ConfusionMatrix], sample_size=12, iterations=10000):
sensitivities = [get_interstim_sensitivities_for_confusion_matrix(cm) for cm in matrices]
slopes = []
rm_corrs = []
for i in range(iterations):
psuedosample_sensitivities = rng.choice(sensitivities, sample_size)
slope, rm_corr, *_ = get_rmcorr_for_sensitivities_by_participant(psuedosample_sensitivities)
slopes.append(slope)
rm_corrs.append(rm_corr)
return percentiles(slopes), percentiles(rm_corrs)