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:
- How to add the option of with and without replacement.
- 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.
- 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.