Skip to content

Commit

Permalink
Add length method to confidence_set. (#112)
Browse files Browse the repository at this point in the history
* Add length method to confidence_set.

* Changes.
  • Loading branch information
mlondschien authored Aug 22, 2024
1 parent 8f829a1 commit 31e45e0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
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

0 comments on commit 31e45e0

Please sign in to comment.