-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bootstrap weights #485
base: main
Are you sure you want to change the base?
Bootstrap weights #485
Changes from 9 commits
3ca1a39
38bdd3e
3d04678
7029aba
f3933df
b8644dd
1556ecb
7b3146a
a39bf15
b33835d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,13 @@ | |
import pandas as pd | ||
|
||
|
||
def get_bootstrap_indices(data, rng, cluster_by=None, n_draws=1000): | ||
def get_bootstrap_indices( | ||
data, | ||
rng, | ||
weight_by=None, | ||
cluster_by=None, | ||
n_draws=1000, | ||
): | ||
"""Draw positional indices for the construction of bootstrap samples. | ||
|
||
Storing the positional indices instead of the full bootstrap samples saves a lot | ||
|
@@ -11,6 +17,7 @@ def get_bootstrap_indices(data, rng, cluster_by=None, n_draws=1000): | |
Args: | ||
data (pandas.DataFrame): original dataset. | ||
rng (numpy.random.Generator): A random number generator. | ||
weight_by (str): column name of the variable with weights. | ||
cluster_by (str): column name of the variable to cluster by. | ||
n_draws (int): number of draws, only relevant if seeds is None. | ||
|
||
|
@@ -19,12 +26,16 @@ def get_bootstrap_indices(data, rng, cluster_by=None, n_draws=1000): | |
|
||
""" | ||
n_obs = len(data) | ||
probs = _get_probs_for_bootstrap_indices(data, weight_by, cluster_by) | ||
|
||
if cluster_by is None: | ||
bootstrap_indices = list(rng.integers(0, n_obs, size=(n_draws, n_obs))) | ||
bootstrap_indices = list( | ||
rng.choice(n_obs, size=(n_draws, n_obs), replace=True, p=probs) | ||
) | ||
else: | ||
clusters = data[cluster_by].unique() | ||
drawn_clusters = rng.choice( | ||
clusters, size=(n_draws, len(clusters)), replace=True | ||
clusters, size=(n_draws, len(clusters)), replace=True, p=probs | ||
) | ||
|
||
bootstrap_indices = _convert_cluster_ids_to_indices( | ||
|
@@ -34,6 +45,29 @@ def get_bootstrap_indices(data, rng, cluster_by=None, n_draws=1000): | |
return bootstrap_indices | ||
|
||
|
||
def _get_probs_for_bootstrap_indices(data, weight_by, cluster_by): | ||
"""Calculate probabilities for drawing bootstrap indices. | ||
|
||
Args: | ||
data (pandas.DataFrame): original dataset. | ||
weight_by (str): column name of the variable with weights. | ||
cluster_by (str): column name of the variable to cluster by. | ||
|
||
Returns: | ||
list: numpy array with probabilities. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the code, the output will be either None or a |
||
|
||
""" | ||
if weight_by is None: | ||
probs = None | ||
else: | ||
if cluster_by is None: | ||
probs = data[weight_by] / data[weight_by].sum() | ||
else: | ||
cluster_weights = data.groupby(cluster_by, sort=False)[weight_by].sum() | ||
probs = cluster_weights / cluster_weights.sum() | ||
return probs | ||
|
||
|
||
def _convert_cluster_ids_to_indices(cluster_col, drawn_clusters): | ||
"""Convert the drawn clusters to positional indices of individual observations. | ||
|
||
|
@@ -48,7 +82,13 @@ def _convert_cluster_ids_to_indices(cluster_col, drawn_clusters): | |
return bootstrap_indices | ||
|
||
|
||
def get_bootstrap_samples(data, rng, cluster_by=None, n_draws=1000): | ||
def get_bootstrap_samples( | ||
data, | ||
rng, | ||
weight_by=None, | ||
cluster_by=None, | ||
n_draws=1000, | ||
): | ||
"""Draw bootstrap samples. | ||
|
||
If you have memory issues you should use get_bootstrap_indices instead and construct | ||
|
@@ -57,6 +97,7 @@ def get_bootstrap_samples(data, rng, cluster_by=None, n_draws=1000): | |
Args: | ||
data (pandas.DataFrame): original dataset. | ||
rng (numpy.random.Generator): A random number generator. | ||
weight_by (str): weights for the observations. | ||
cluster_by (str): column name of the variable to cluster by. | ||
n_draws (int): number of draws, only relevant if seeds is None. | ||
|
||
|
@@ -67,6 +108,7 @@ def get_bootstrap_samples(data, rng, cluster_by=None, n_draws=1000): | |
indices = get_bootstrap_indices( | ||
data=data, | ||
rng=rng, | ||
weight_by=weight_by, | ||
cluster_by=cluster_by, | ||
n_draws=n_draws, | ||
) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -88,6 +88,15 @@ def test_check_inputs_data(): | |||||||||||||||||||||
assert str(error.value) == expected_msg | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_check_inputs_weight_by(setup): | ||||||||||||||||||||||
weights = "this is not a column name of df" | ||||||||||||||||||||||
expected = "Input 'weight_by' must be None or a column name of 'data'." | ||||||||||||||||||||||
|
||||||||||||||||||||||
with pytest.raises(ValueError) as error: | ||||||||||||||||||||||
check_inputs(data=setup["df"], weight_by=weights) | ||||||||||||||||||||||
assert str(error.value) == expected | ||||||||||||||||||||||
Comment on lines
+92
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def test_check_inputs_cluster_by(setup): | ||||||||||||||||||||||
cluster_by = "this is not a column name of df" | ||||||||||||||||||||||
expected_msg = "Input 'cluster_by' must be None or a column name of 'data'." | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only check that the
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the function name
_calculate_bootstrap_indices_weights
. In that case, the first line of the docstring needs to be updated as well.