-
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
Open
alanlujan91
wants to merge
10
commits into
optimagic-dev:main
Choose a base branch
from
alanlujan91:bootstrap_weights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−9
Open
Bootstrap weights #485
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ca1a39
add kwarg weights to get_bootstrap_indices
alanlujan91 38bdd3e
fix downstream
alanlujan91 3d04678
update get_moments_cov
alanlujan91 7029aba
add tests
alanlujan91 f3933df
Merge remote-tracking branch 'upstream/main' into bootstrap_weights
alanlujan91 b8644dd
change weights to weight_by
alanlujan91 1556ecb
Merge branch 'main' into bootstrap_weights
segsell 7b3146a
Merge remote-tracking branch 'upstream/main' into bootstrap_weights
alanlujan91 a39bf15
add _get_probs_for_bootstrap_indices
alanlujan91 b33835d
Merge branch 'main' into bootstrap_weights
janosg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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, | ||
) | ||
|
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
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'." | ||||||||||||||||||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.