Simulation and Resampling¶
Bootstrap, stratified, weighted, multinomial and Dirichlet sampling, custom discrete random variables, bootstrap confidence intervals and Mahalanobis-distance outlier detection.
bootstrap_sample(data, n_iterations=1000, seed=42)
¶
Generate bootstrap distribution of sample means.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Original sample data. |
required |
n_iterations
|
int
|
Number of bootstrap resamples. |
1000
|
seed
|
int
|
Random seed for reproducibility. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of n_iterations bootstrap sample means. |
Source code in utils/sim_utils.py
sample_uniform(data, n, replace=True, seed=42)
¶
Draw a uniform random sample from data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Population to sample from. |
required |
n
|
int
|
Number of samples to draw. |
required |
replace
|
bool
|
Whether to sample with replacement. |
True
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of n sampled values. |
Source code in utils/sim_utils.py
stratified_sample(df, stratify_col, frac=0.1, seed=42)
¶
Draw a proportionally stratified sample from a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame. |
required |
stratify_col
|
str
|
Column to stratify by (categorical grouping variable). |
required |
frac
|
float
|
Fraction of each group to sample. |
0.1
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Stratified sample preserving group proportions. |
Source code in utils/sim_utils.py
weighted_sample(data, weights, n, replace=True, seed=42)
¶
Draw a weighted random sample from data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Values to sample from. |
required |
weights
|
array - like
|
Probability weights for each element (must sum to 1). |
required |
n
|
int
|
Number of samples to draw. |
required |
replace
|
bool
|
Whether to sample with replacement. |
True
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of n sampled values. |
Source code in utils/sim_utils.py
draw_multinomial_sample(n, probs, size=1, seed=42)
¶
Draw samples from a multinomial distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of trials per draw. |
required |
probs
|
array - like
|
Probability of each category (must sum to 1). |
required |
size
|
int
|
Number of independent draws. |
1
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (size, len(probs)) with trial counts per category. |
Source code in utils/sim_utils.py
draw_dirichlet_sample(alpha, size=1, seed=42)
¶
Draw samples from a Dirichlet distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
array - like
|
Concentration parameters (positive values). Length determines number of categories. |
required |
size
|
int
|
Number of random samples to draw. |
1
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (size, len(alpha)) where each row sums to 1. |
Source code in utils/sim_utils.py
sample_custom_discrete(support_vals, probs, size=1000, seed=42)
¶
Sample from a custom discrete probability distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
support_vals
|
array-like of int
|
Discrete support values (e.g., [0, 1, 2, 3]). |
required |
probs
|
array-like of float
|
Probability for each value (must sum to 1). |
required |
size
|
int
|
Number of samples to draw. |
1000
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of sampled values from the custom distribution. |
Source code in utils/sim_utils.py
resample_with_replacement(data, n_samples=1000, seed=42)
¶
Resample from data with replacement (bootstrap base operation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Original data to resample from. |
required |
n_samples
|
int
|
Number of samples in the resample. |
1000
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Resampled array of length n_samples. |
Source code in utils/sim_utils.py
compute_ecdf(data)
¶
Compute the empirical cumulative distribution function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Observed data. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (np.ndarray, np.ndarray)
|
(x, y) where x is sorted data and y is cumulative probability [1/n ... 1]. |
Source code in utils/sim_utils.py
bootstrap_statistic(data, stat_func=np.mean, n_resamples=1000, seed=42)
¶
Compute a bootstrap distribution for any statistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Original sample data. |
required |
stat_func
|
callable
|
Statistic function to apply to each resample (e.g., np.median, np.std). |
np.mean
|
n_resamples
|
int
|
Number of bootstrap resamples. |
1000
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Bootstrap distribution of the statistic (length = n_resamples). |
Source code in utils/sim_utils.py
compute_bootstrap_ci(boot_stats, ci=95)
¶
Compute percentile-method confidence interval from bootstrap distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boot_stats
|
array - like
|
Bootstrap distribution of a statistic. |
required |
ci
|
float
|
Confidence level as a percentage (e.g., 95 for 95% CI). |
95
|
Returns:
| Type | Description |
|---|---|
tuple of (float, float)
|
(lower, upper) confidence interval bounds. |
Source code in utils/sim_utils.py
summarize_bootstrap(estimates, original_stat=None, ci=95)
¶
Generate a summary report of a bootstrap distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimates
|
array - like
|
Bootstrap distribution of a statistic. |
required |
original_stat
|
float
|
The original (observed) statistic from the full sample. |
None
|
ci
|
float
|
Confidence level as a percentage. |
95
|
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
|
Source code in utils/sim_utils.py
plot_bootstrap_distribution(estimates, ci_bounds=None, title='Bootstrap Distribution', bins=30)
¶
Plot a histogram of bootstrap estimates with optional CI bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimates
|
array - like
|
Bootstrap distribution values. |
required |
ci_bounds
|
tuple of (float, float)
|
(lower, upper) bounds to annotate as vertical lines. |
None
|
title
|
str
|
Plot title. |
'Bootstrap Distribution'
|
bins
|
int
|
Number of histogram bins. |
30
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in utils/sim_utils.py
compute_mahalanobis_distances(data)
¶
Computes Mahalanobis distance of each row from the multivariate mean.
Args: data (pd.DataFrame): Input data (n x d)
Returns: np.ndarray: Mahalanobis distances (n,)
Source code in utils/sim_utils.py
evaluate_mahalanobis_outliers(distances, df_dim, alpha=0.01)
¶
Compares Mahalanobis distances to Chi-Square threshold for outlier detection.
Args: distances (np.ndarray): Mahalanobis distances df_dim (int): Degrees of freedom (number of features) alpha (float): Significance level
Returns: pd.Series: Boolean mask of outliers