Skip to content

Commit

Permalink
FIX: wrong size specification in bootstrap (mne-tools#4666)
Browse files Browse the repository at this point in the history
* wrong size specification in bootstrap

* stat_fun
  • Loading branch information
dengemann authored and larsoner committed Oct 16, 2017
1 parent f26a15d commit ec9e827
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
ax.plot(times, gfp, label=freq_name, color=color, linewidth=2.5)
ax.axhline(0, linestyle='--', color='grey', linewidth=2)
ci_low, ci_up = _bootstrap_ci(average.data, random_state=0,
statfun=lambda x: np.sum(x ** 2, axis=0))
stat_fun=lambda x: np.sum(x ** 2, axis=0))
ci_low = rescale(ci_low, average.times, baseline=(None, 0))
ci_up = rescale(ci_up, average.times, baseline=(None, 0))
ax.fill_between(times, gfp + ci_up, gfp - ci_low, color=color, alpha=0.3)
Expand Down
18 changes: 9 additions & 9 deletions mne/stats/permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,21 @@ def permutation_t_test(X, n_permutations=10000, tail=0, n_jobs=1,
return T_obs, p_values, H0


def _bootstrap_ci(arr, ci=.95, n_bootstraps=2000, statfun='mean',
def _bootstrap_ci(arr, ci=.95, n_bootstraps=2000, stat_fun='mean',
random_state=None):
"""Get confidence intervals from non-parametric bootstrap."""
if statfun == "mean":
statfun = lambda x: x.mean(axis=0) # noqa
elif statfun == 'median':
statfun = lambda x: np.median(x, axis=0) # noqa
elif not callable(statfun):
raise ValueError("statfun must be 'mean', 'median' or callable.")
if stat_fun == "mean":
stat_fun = lambda x: x.mean(axis=0) # noqa
elif stat_fun == 'median':
stat_fun = lambda x: np.median(x, axis=0) # noqa
elif not callable(stat_fun):
raise ValueError("stat_fun must be 'mean', 'median' or callable.")
n_trials = arr.shape[0]
indices = np.arange(n_trials, dtype=int) # BCA would be cool to have too
rng = check_random_state(random_state)
boot_indices = rng.choice(indices, replace=True,
size=(n_trials, len(indices)))
stat = np.array([statfun(arr[inds]) for inds in boot_indices])
size=(n_bootstraps, len(indices)))
stat = np.array([stat_fun(arr[inds]) for inds in boot_indices])
ci = (((1 - ci) / 2) * 100, ((1 - ((1 - ci) / 2))) * 100)
ci_low, ci_up = np.percentile(stat, ci, axis=0)
return np.array([ci_low, ci_up])
Expand Down
4 changes: 2 additions & 2 deletions mne/stats/tests/test_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_ci():
arr = np.linspace(0, 1, 1000)[..., np.newaxis]
assert_allclose(_ci(arr, method="parametric"),
_ci(arr, method="bootstrap"), rtol=.005)
assert_allclose(_bootstrap_ci(arr, statfun="median", random_state=0),
_bootstrap_ci(arr, statfun="mean", random_state=0),
assert_allclose(_bootstrap_ci(arr, stat_fun="median", random_state=0),
_bootstrap_ci(arr, stat_fun="mean", random_state=0),
rtol=.1)

run_tests_if_main()

0 comments on commit ec9e827

Please sign in to comment.