0

Somehow, the scipy.stats.bootstrap is not working in my Jupyter notebook. Therefore, I decide to write a sample function for bootstrap estimation.

Here is what I did.

def bootstrap(x, Nboot, statfun):
    '''Bootstrap code'''
    x = np.array(x)
    
    resampled_stat = []
    for k in range(Nboot):
        index = np.random.randint(0, len(x), len(x))
        sample = x[index]
        bstatistic = statfun(sample)
        resampled_stat.append(bstatistic)
        
    return np.array(resampled_stat)

Data Set:

data=(0.045494, 0.065669, 0.073061, 0.104542, 0.296978, 0.498353, 0.503342, 0.515458, 0.660300, 0.663664, 0.677255, 0.724817, 0.805800, 0.899355, 0.987775, 2.121619, 2.165055, 2.196833, 3.265653, 3.479858, 8.702472, 10.070092, 10.720080, 12.896169, 12.912647, 14.244361, 14.287428, 17.337397, 18.903783, 20.940314, 21.404639, 22.234169)
result=bootstrap(data, 1000, np.mean)

So far, my script working well, but I need to modify this script for the following tasks:

  1. How to add the option of with and without replacement.
  2. How to play around the window length, for example, I want to add an option of with length 30 points bootstrap or 50, or 100 etc. that can make this more flexible.
  3. last thing is most important, I want to compute the slop [number of observation will be used as x and y is the bootstrap sample, e.g. if the length of bootstrap window is 30 then x is an array from 1 to 30, and we will plot x verses bootstrap sample to compute slope] for each random resampling iteration along with the confidence interval.

May someone suggest how can I fix this, or any other possible approach to complete this task.

Adnan
  • 51
  • 6
  • For slope, don't you need two variables? Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with sample input and desired output. Also, bootstrapping is, by definition, sampling *with* replacement ([Wiki](https://en.wikipedia.org/wiki/Bootstrapping_(statistics))). – AlexK Aug 14 '22 at 00:58
  • AlexK, for slope the x-parameter is the number of data points and y is the bootstrap sample. For example, if I set 30 as the length of bootstrap sample, the x is an array from 1 to 30. So we will plot the bootstrap sample against the x array. – Adnan Aug 14 '22 at 12:05

0 Answers0