How to make a sample from the empirical distributi

2019-06-28 07:02发布

问题:

I'm trying to implement the nonparametric bootstrapping on Python. It requires to take a sample, build an empirical distribution function from it and then to generate a bunch of samples from this edf. How can I do it? In scipy I found only how to make your own distribution function if you know the exact formula describing it, but I have only an edf.

回答1:

The edf you get by sorting the samples:

N = samples.size
ss = np.sort(samples) # these are the x-values of the edf
                      # the y-values are 1/(2N), 3/(2N), 5/(2N) etc.
edf = lambda x: np.searchsorted(ss, x) / N

However, if you only want to resample then you simply draw from your sample with equal probability and replacement.

If this is too "steppy" for your liking, you can probably use some kind of interpolation to get a smooth distribution.