Last active
April 22, 2021 11:14
-
-
Save dbalabka/68fb2cdd6353a549273b56f57515637b to your computer and use it in GitHub Desktop.
scikits-bootstrap Jackknife resampling with Numba and performance testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scikits.bootstrap as bootstrap | |
import numpy as np | |
import time | |
import numba | |
@numba.njit(parallel=True, fastmath=True) | |
def _calculate_jackknife_mean_stat(data: np.ndarray) -> np.ndarray: | |
n = data.shape[0] | |
jstat = np.zeros(n) | |
sum = data.sum() | |
for i in numba.prange(n): | |
# Alternative solution, which can be used when we want use custom stat function | |
# jstat[i] = np.concatenate((data[:i], data[i+1:])).mean() | |
jstat[i] = (sum - data[i]) / (n - 1) | |
return jstat | |
tdata = (np.random.randint(0, 5, 100_000), ) | |
start = time.time() | |
jackindices = bootstrap.jackknife_indices(tdata[0]) | |
jstat_old = [np.mean(*(x[indices] for x in tdata)) | |
for indices in jackindices] | |
end = time.time() | |
print(end - start) | |
start = time.time() | |
jstat_new = _calculate_jackknife_mean_stat(tdata[0]) | |
end = time.time() | |
print(end - start) | |
np.testing.assert_array_equal(jstat_old, jstat_new) | |
# Numba debug | |
# bootstrap._calculate_jackknife_mean_stat.parallel_diagnostics(level=4) | |
# bootstrap._calculate_jackknife_mean_stat.inspect_types() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JIT enabled:
JIT disabled