-
Notifications
You must be signed in to change notification settings - Fork 101
Methods for confidence sets for IV models that are robust to weak instruments #318
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2c3df56
first version, no tests
esmucler a7d1e3b
add quadratic inequality
david26694 79599d2
remove comments and remove min max logic
david26694 ee1d3f1
Merge pull request #1 from david26694/unifconfset
david26694 7e0d5eb
add test for unif confset
david26694 075bfdc
add better comments
david26694 7bdfe2c
add better comment
david26694 3306b7e
Merge pull request #2 from david26694/unifconfset
esmucler dddfa31
Merge branch 'DoubleML:main' into unifconfset
esmucler 8a8a5fe
black, flake, rename
esmucler 3c2d5b4
extra comment, fix typo
esmucler 7d01ffa
add special cases and np.polynomial to quadratic inequality
SvenKlaassen a2566a5
change name to robust_confset
SvenKlaassen 80ce23f
update cov test
SvenKlaassen 20a4d6d
add exception tests
SvenKlaassen 776e644
add robust ci to str
SvenKlaassen 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import numpy as np | ||
| import pytest | ||
| from sklearn.ensemble import RandomForestClassifier | ||
| from sklearn.linear_model import LinearRegression, LogisticRegression | ||
|
|
||
| import doubleml as dml | ||
|
|
||
|
|
||
| def generate_weak_iv_data(n_samples, instrument_size, true_ATE): | ||
| u = np.random.normal(0, 2, size=n_samples) | ||
| X = np.random.normal(0, 1, size=n_samples) | ||
| Z = np.random.binomial(1, 0.5, size=n_samples) | ||
| A = instrument_size * Z + u | ||
| A = np.array(A > 0, dtype=int) | ||
| Y = true_ATE * A + np.sign(u) | ||
| dml_data = dml.DoubleMLData.from_arrays(x=X, y=Y, d=A, z=Z) | ||
| return dml_data | ||
|
|
||
|
|
||
| @pytest.mark.ci | ||
| def test_coverage_robust_confset(): | ||
| # Test parameters | ||
| true_ATE = 0.5 | ||
| instrument_size = 0.005 | ||
| n_samples = 1000 | ||
| n_simulations = 100 | ||
|
|
||
| np.random.seed(3141) | ||
| coverage = [] | ||
| for _ in range(n_simulations): | ||
| data = generate_weak_iv_data(n_samples, instrument_size, true_ATE) | ||
|
|
||
| # Set machine learning methods | ||
| learner_g = LinearRegression() | ||
| classifier_m = LogisticRegression() | ||
| classifier_r = RandomForestClassifier(n_estimators=20, max_depth=5) | ||
|
|
||
| # Create and fit new model | ||
| dml_iivm_obj = dml.DoubleMLIIVM(data, learner_g, classifier_m, classifier_r) | ||
| dml_iivm_obj.fit() | ||
|
|
||
| # Get confidence set | ||
| conf_set = dml_iivm_obj.robust_confset() | ||
|
|
||
| # check if conf_set is a list of tuples | ||
| assert isinstance(conf_set, list) | ||
| assert all(isinstance(x, tuple) and len(x) == 2 for x in conf_set) | ||
|
|
||
| # Check if true ATE is in confidence set | ||
| ate_in_confset = any(x[0] < true_ATE < x[1] for x in conf_set) | ||
| coverage.append(ate_in_confset) | ||
|
|
||
| # Calculate coverage rate | ||
| coverage_rate = np.mean(coverage) | ||
| assert coverage_rate >= 0.9, f"Coverage rate {coverage_rate} is below 0.9" | ||
|
|
||
|
|
||
| @pytest.mark.ci | ||
| def test_exceptions_robust_confset(): | ||
| # Test parameters | ||
| true_ATE = 0.5 | ||
| instrument_size = 0.005 | ||
| n_samples = 1000 | ||
|
|
||
| np.random.seed(3141) | ||
| data = generate_weak_iv_data(n_samples, instrument_size, true_ATE) | ||
|
|
||
| # create new model | ||
| learner_g = LinearRegression() | ||
| classifier_m = LogisticRegression() | ||
| classifier_r = RandomForestClassifier(n_estimators=20, max_depth=5) | ||
| dml_iivm_obj = dml.DoubleMLIIVM(data, learner_g, classifier_m, classifier_r) | ||
|
|
||
| # Check if the robust_confset method raises an exception when called before fitting | ||
| msg = r"Apply fit\(\) before robust_confset\(\)." | ||
| with pytest.raises(ValueError, match=msg): | ||
| dml_iivm_obj.robust_confset() | ||
|
|
||
| # Check if str representation of the object is working | ||
| str_repr = str(dml_iivm_obj) | ||
| assert isinstance(str_repr, str) | ||
| assert "Robust" not in str_repr | ||
|
|
||
| # Fit the model | ||
| dml_iivm_obj.fit() | ||
|
|
||
| # Check invalid inputs | ||
| msg = "The confidence level must be of float type. 0.95 of type <class 'str'> was passed." | ||
| with pytest.raises(TypeError, match=msg): | ||
| dml_iivm_obj.robust_confset(level="0.95") | ||
| msg = r"The confidence level must be in \(0,1\). 1.5 was passed." | ||
| with pytest.raises(ValueError, match=msg): | ||
| dml_iivm_obj.robust_confset(level=1.5) | ||
|
|
||
| # Check if str representation of the object is working | ||
| str_repr = str(dml_iivm_obj) | ||
| assert isinstance(str_repr, str) | ||
| assert "Robust Confidence Set" in str_repr |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from doubleml.utils._estimation import _solve_quadratic_inequality | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "a, b, c, expected", | ||
| [ | ||
| (1, 0, -4, [(-2.0, 2.0)]), # happy quadratic, determinant > 0 | ||
| (-1, 0, 4, [(-np.inf, -2), (2, np.inf)]), # sad quadratic, determinant > 0 | ||
| (1, 0, 4, []), # happy quadratic, determinant < 0 | ||
| (-1, 0, -4, [(-np.inf, np.inf)]), # sad quadratic, determinant < 0 | ||
| (1, 0, 0, [(0.0, 0.0)]), # happy quadratic, determinant = 0 | ||
| (-1, 0, 0, [(-np.inf, np.inf)]), # sad quadratic, determinant = 0 | ||
| (1, 3, -4, [(-4.0, 1.0)]), # happy quadratic, determinant > 0 | ||
| (-1, 3, 4, [(-np.inf, -1), (4, np.inf)]), # sad quadratic, determinant > 0 | ||
| (-1, -3, 4, [(-np.inf, -4), (1, np.inf)]), # sad quadratic, determinant > 0 | ||
| (1, 3, 4, []), # happy quadratic, determinant < 0 | ||
| (-1, 3, -4, [(-np.inf, np.inf)]), # sad quadratic, determinant < 0 | ||
| (1, 4, 4, [(-2.0, -2.0)]), # happy quadratic, determinant = 0 | ||
| (-1, 4, -4, [(-np.inf, np.inf)]), # sad quadratic, determinant = 0 | ||
| (0, 0, 0, [(-np.inf, np.inf)]), # constant and equal to zero | ||
| (0, 0, 1, []), # constant and larger than zero | ||
| (0, 1, 0, [(-np.inf, 0.0)]), # increasing linear function | ||
| (0, -1, -1, [(-1.0, np.inf)]), # decreasing linear function | ||
| ], | ||
| ) | ||
| def test_solve_quadratic_inequation(a, b, c, expected): | ||
| result = _solve_quadratic_inequality(a, b, c) | ||
|
|
||
| assert len(result) == len(expected), f"Expected {len(expected)} intervals but got {len(result)}" | ||
|
|
||
| for i, tpl in enumerate(result): | ||
| if tpl[0] == -np.inf: | ||
| assert np.isinf(tpl[0]) | ||
| if tpl[1] == np.inf: | ||
| assert np.isinf(tpl[1]) | ||
| else: | ||
| assert np.isclose(tpl[0], expected[i][0]), f"Expected {expected[i][0]} but got {tpl[0]}" | ||
| assert np.isclose(tpl[1], expected[i][1]), f"Expected {expected[i][1]} but got {tpl[1]}" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.