-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap_pd.py
175 lines (140 loc) · 6.24 KB
/
bootstrap_pd.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import numpy as np
import pandas as pd
from scipy import stats
__all__ = ['bootci_pd',
'permtest_pd']
def bootci_pd(df, statfunction, alpha=0.05, n_samples=10000, method='bca'):
"""Estimate bootstrap CIs for a statfunction that operates along the rows of
a pandas.DataFrame and return a dict or pd.Series of results. Returning
a dict is typically faster.
This is about 10x slower than using scikits.bootstrap.ci for a statistic
doesn't require resampling the whole DataFrame. However, if the statistic
requires the whole DataFrame or you are computing many statistics on the
same DataFrame that all require CIs, then this function may be efficient.
Parameters
----------
df : pd.DataFrame
Data that will be passed to statfunction as a single parameter.
statfunction : function
Function that should operate along the rows of df and return a dict
alpha : float [0, 1]
Specify CI: [alpha/2, 1-alpha/2]
n_samples : int
Number of bootstrap samples.
method : str
Specify bias-corrected and accelerated ("bca") or percentile ("pi")
bootstrap.
Returns
-------
cis : pd.Series [est, lcl, ucl]
Point-estimate and CI of statfunction of df"""
alphas = np.array([alpha/2, 1-alpha/2])
# The value of the statistic function applied just to the actual data.
res = pd.Series(statfunction(df))
#st = time.time()
boot_res = []
for i in range(n_samples):
# rind = np.random.randint(df.shape[0], size=df.shape[0])
# boot_res.append(statfunction(df.iloc[rind]))
boot_res.append(statfunction(df.sample(frac=1, replace=True)))
boot_res = pd.DataFrame(boot_res)
#print(time.time() - st)
# Percentile Interval Method
if method == 'pi':
avals = np.tile(alphas, (boot_res.shape[1], 1)).T
# Bias-Corrected Accelerated Method
elif method == 'bca':
ind = np.ones(df.shape[0], dtype=bool)
jack_res = []
for i in range(df.shape[0]):
ind[i] = False
jack_res.append(statfunction(df.loc[ind]))
ind[i] = True
jack_res = pd.DataFrame(jack_res)
jmean = np.nanmean(jack_res, keepdims=True, axis=0)
bca_accel = np.nansum((jmean - jack_res.values)**3, axis=0) / (6.0 * np.nansum((jmean - jack_res.values)**2, axis=0)**1.5)
"""The bias correction value"""
z0 = stats.distributions.norm.ppf( (np.sum(boot_res.values < res.values[None, :], axis=0)) / np.sum(~np.isnan(boot_res.values), axis=0) )
zs = z0 + stats.distributions.norm.ppf(alphas).reshape(alphas.shape + (1,) * z0.ndim)
avals = stats.distributions.norm.cdf(z0 + zs / (1 - bca_accel * zs))
non_nan_ind = ~np.isnan(boot_res)
nvals = np.round((np.sum(non_nan_ind.values, axis=0) - 1) * avals).astype(int)
nvals = pd.DataFrame(nvals.T, columns=['%1.3f' % a for a in alphas], index=boot_res.columns)
if np.any(np.isnan(nvals)):
print('Nan values for some stats suggest there is no bootstrap variation.')
print(res.head(10))
cis = pd.DataFrame(np.zeros((len(boot_res.columns), len(avals) + 1)), index=boot_res.columns, columns=['est'] + ['%1.3f' % a for a in alphas])
for i,col in enumerate(boot_res.columns):
boot_res.values[:, i].sort()
cis.loc[col, 'est'] = res[col]
cis.loc[col, ['%1.3f' % a for a in alphas]] = boot_res[col].values[nvals.loc[col]]
if np.any(nvals < 10) or np.any(nvals > n_samples-10):
print('Extreme samples used: results unstable')
print(nvals)
return cis
def permtest_pd(df, statfunction, perm_cols, n_samples=9999, alternative='two-sided'):
"""Estimate a p-value for the statfunction against the permutation null.
Parameters
----------
df : pd.DataFrame
Observed data required as sole input for statfunction.
statfunction : function
Operates on df and returns a scalar statistic.
perm_cols : list of str
Columns that need to be permuted in df to generate a null dataset
n_samples : int
Number of permutations to test
alternative : str
Specify a "two-sided" test or one that tests that the observed data is "less" than
or "greater" than the null statistics.
Returns
-------
pvalue : float"""
n_samples = int(n_samples)
tmp = df.copy()
samples = np.zeros(n_samples)
for sampi in range(n_samples):
rind = np.random.permutation(df.shape[0])
tmp.loc[:, perm_cols] = tmp.loc[:, perm_cols].values[rind, :]
samples[sampi] = statfunction(tmp)
if alternative == 'two-sided':
pvalue = ((np.abs(samples) > np.abs(statfunction(df))).sum() + 1) / (n_samples + 1)
elif alternative == 'greater':
pvalue = ((samples > statfunction(df)).sum() + 1) / (n_samples + 1)
elif alternative == 'less':
pvalue = ((samples < statfunction(df)).sum() + 1) / (n_samples + 1)
return pvalue
def _test_permtest_pd(effect=0.5, n_samples=9999):
from scipy import stats
import time
df = pd.DataFrame(np.random.randn(100, 5))
df.loc[:, 0] = np.random.randint(2, size=df.shape[0])
df.loc[df[0] == 0, 1] = df.loc[df[0] == 0, 1] + effect
def func(d):
return np.mean(d.loc[d[0] == 0, 1]) - np.mean(d.loc[d[0] == 1, 1])
st = time.time()
res = permtest_pd(df, func, perm_cols=[0], n_samples=n_samples)
et = (time.time() - st)
print(res)
print('Time: %1.2f sec' % et)
print(stats.ttest_ind(df.loc[df[0] == 0, 1], df.loc[df[0] == 1, 1]))
def _test_bootci_pd(n_samples=10000, method='bca'):
import scikits.bootstrap as boot
import time
df = pd.DataFrame(np.random.randn(100, 5))
def func(d):
return {'MeanA':d[0].mean(), 'MedianB':np.median(d[1])}
def func2(d):
return d.mean()
st = time.time()
res = bootci_pd(df, func, alpha=0.05, n_samples=n_samples, method=method)
et = (time.time() - st)
print(res)
print('Time: %1.2f sec' % et)
st = time.time()
a = boot.ci(df[0].values, statfunction=np.mean, n_samples=n_samples, method=method)
b = boot.ci(df[1].values, statfunction=np.median, n_samples=n_samples, method=method)
et = (time.time() - st)
print('MeanA', a)
print('MedianB', b)
print('Time: %1.2f sec' % et)