Skip to content
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

Add length method to confidence_set. #112

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Changelog

- The Wald test now supports robust covariance estimation.

- New method `length` for :class:`~ivmodels.confidence_set.ConfidenceSet`.

**Other changes:**

- One can now pass the tolerance parameter `tol` to the optimization algorithm in
Expand Down
4 changes: 4 additions & 0 deletions ivmodels/confidence_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ def from_quadric(quadric):
return ConfidenceSet([(-np.inf, np.inf)])
else:
return ConfidenceSet([(-np.inf, boundary[0]), (boundary[1], np.inf)])

def length(self):
"""Return the length of the confidence set."""
return sum(right - left for left, right in self.boundaries)
13 changes: 7 additions & 6 deletions tests/test_confidence_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@


@pytest.mark.parametrize(
"boundaries, values, finite, empty",
"boundaries, values, finite, empty, length",
[
([], {0: 1}, True, True),
([(-1, 1)], {-2: 1, 1: -1, 0: -1}, True, False),
([(-np.inf, np.inf)], {-7: -1}, False, False),
([(-np.inf, 0), (1, 20)], {0.5: 1, 12: -1}, False, False),
([], {0: 1}, True, True, 0),
([(-1, 1)], {-2: 1, 1: -1, 0: -1}, True, False, 2),
([(-np.inf, np.inf)], {-7: -1}, False, False, np.inf),
([(-np.inf, 0), (1, 20)], {0.5: 1, 12: -1}, False, False, np.inf),
],
)
def test_confidence_set_call(boundaries, values, finite, empty):
def test_confidence_set_call(boundaries, values, finite, empty, length):
confidence_set = ConfidenceSet(boundaries)
for value, expected in values.items():
assert confidence_set(value) == expected
assert confidence_set.is_finite() == finite
assert confidence_set.is_empty() == empty
assert confidence_set.length() == length
Loading