Skip to content

Instantly share code, notes, and snippets.

@davidclarance
Created August 11, 2024 09:06
Show Gist options
  • Save davidclarance/8e93dcc4fe541a8f17afb7effc2590de to your computer and use it in GitHub Desktop.
Save davidclarance/8e93dcc4fe541a8f17afb7effc2590de to your computer and use it in GitHub Desktop.
Poisson bootstrapping
def poisson_bootstrap_nth_percentile_and_ci(
data: List[float], n_iter: int = 1000, alpha: float = 0.95, percentile: float = 95
) -> Tuple[float, float, float, float]:
data = np.array(data)
n = len(data)
weights = np.random.poisson(1, (n_iter, n))
percentiles = []
for weight in weights:
resampled_data = np.repeat(data, weight)
percentile = np.percentile(resampled_data, 95)
percentiles.append(percentile)
mean_percentile = np.mean(percentiles)
lower_bound = np.percentile(percentiles, (1 - alpha) / 2 * 100)
upper_bound = np.percentile(percentiles, (1 + alpha) / 2 * 100)
return percentiles, mean_percentile, lower_bound, upper_bound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment