-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampling.py
47 lines (39 loc) · 1.31 KB
/
sampling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
def bootstrap_sample(x, n_samples, smoothed=True, replace=True, p=None):
"""
Parameters
--------------
x : 1-D array-like.
Sample to compute bootstrap statistics from.
n_samples : int
Number of bootstrap samples to return.
smoothed : bool
Whether smoothed sampling is used.
replace : bool
Whether sampling with replacement is used.
p : 1-D array-like
Probability for each value of x for weighted sampling.
Notes
--------------
References: https://en.wikipedia.org/wiki/Bootstrapping_(statistics)#Smoothed_bootstrap
"""
size = (n_samples, len(x))
bootstrap_samples = np.random.choice(x, size, replace, p)
bootstrap_result = np.mean(bootstrap_samples, axis=1)
if smoothed:
noise = gaussian_noise(x, n_samples)
return bootstrap_result + noise
return bootstrap_result
def gaussian_noise(x, n_samples)->np.ndarray:
"""Create guassian noise of len(n_samples).
Parameters
------------
x : ndarray
n_samples : int
References
------------
researchgate.net/publication/292390585_Unbiasing_the_bootstrap-bootknife_sampling_vs_smoothing
"""
sigma = np.std(x)/np.sqrt(len(x))
noise = np.random.normal(0, sigma, n_samples)
return noise