boot() equivalent in python? [closed]
Asked Answered
C

3

7

Is there an equivalent of boot and boot.ci in python? In R I would do

library(boot)
result <- boot(data,bootfun,10000)
boot.ci(result)
Compressed answered 27/3, 2018 at 20:54 Comment(1)
You might want to look at the scikits.bootstrap package. But it would probably be helpful to potential answerers to this question if you explain what R's boot and boot.ci do (or at least link to appropriate R resources)Panoply
B
2

There's now also a bootstrap function in the scipy.stats module scipy.stats.bootstrap

from scipy import stats

results = stats.bootstrap(data, bootfun, n_resamples=10000)
ci = results.confidence_interval

The confidence interval is returned as a named tuple of low, high values.

Buddybuderus answered 19/3 at 10:17 Comment(2)
Hi Riccardo C., welcome to SO. While the information in your link may solve the OP's question, link-only answer are discouraged for these reasons. Please consider updating your answer so that it is self-contained with a minimal amount of code so others can verify that it works. ThanksAgrimony
@LTyrone Thanks for the advice. I updated the answer adding a bit of context and a minimal code.Buddybuderus
C
2

I can point to specific bootstrap usage in python. I am assuming you are looking for similar methods for bootstrapping in python as we do in R.

import numpy as np
import bootstrapped.bootstrap as bs
import bootstrapped.stats_functions as bs_stats

mean = 100
stdev = 10

population = np.random.normal(loc=mean, scale=stdev, size=50000)

# take 1k 'samples' from the larger population
samples = population[:1000]

print(bs.bootstrap(samples, stat_func=bs_stats.mean))
>> 100.08  (99.46, 100.69)

print(bs.bootstrap(samples, stat_func=bs_stats.std))
>> 9.49  (9.92, 10.36)

The specific packages used here are bootstrapped.bootstrap and bootstrapped.stats_functions. You can explore more about this module here

Craniology answered 27/3, 2018 at 21:19 Comment(0)
B
2

There's now also a bootstrap function in the scipy.stats module scipy.stats.bootstrap

from scipy import stats

results = stats.bootstrap(data, bootfun, n_resamples=10000)
ci = results.confidence_interval

The confidence interval is returned as a named tuple of low, high values.

Buddybuderus answered 19/3 at 10:17 Comment(2)
Hi Riccardo C., welcome to SO. While the information in your link may solve the OP's question, link-only answer are discouraged for these reasons. Please consider updating your answer so that it is self-contained with a minimal amount of code so others can verify that it works. ThanksAgrimony
@LTyrone Thanks for the advice. I updated the answer adding a bit of context and a minimal code.Buddybuderus
C
1

There's also the resample package available via pip. Here's the Github page: https://github.com/dsaxton/resample.

Regarding your example you could do the following (there's also a ci_method argument you can tweak for either the percentile, BCA or Studentized bootstrap interval):

from resample.bootstrap import bootstrap_ci

bootstrap_ci(a=data, f=bootfun, b=10000)
Cussedness answered 5/9, 2018 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.