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

New function: inverse_lagrange_multiplier_test. #88

Merged
merged 9 commits into from
Jul 5, 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
17 changes: 15 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
Changelog
=========

0.2.0 - 2024-06-7
-----------------
0.3.0 - 2024-07-xx
------------------

**New features:**

- New function :func:`~ivmodels.tests.inverse_lagrange_multiplier_test`.

- New class :class:`~ivmodels.confidence_sets.ConfidenceSet`.

**Other changes:**

- The function :func:`~ivmodels.tests.lagrange_multiplier_test` is now slightly faster.

0.2.0 - 2024-06-07
------------------

**New features:**

Expand Down
37 changes: 31 additions & 6 deletions benchmarks/benchmarks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from ivmodels.tests import (
anderson_rubin_test,
conditional_likelihood_ratio_test,
inverse_anderson_rubin_test,
inverse_lagrange_multiplier_test,
inverse_likelihood_ratio_test,
inverse_wald_test,
lagrange_multiplier_test,
likelihood_ratio_test,
rank_test,
Expand Down Expand Up @@ -36,17 +40,15 @@ def setup(self, n, data):
def time_anderson_rubin_test(self, n, data):
_, _ = anderson_rubin_test(**self.data)

def time_inverse_anderson_rubin_test(self, n, data):
data = {k: v for k, v in self.data.items() if k != "beta"}
_ = inverse_anderson_rubin_test(**data)

@skip_for_params(
[
(100, (1, 1, 0, 0)),
(100, (4, 2, 0, 0)),
(100, (4, 2, 0, 2)),
(1000, (1, 1, 0, 0)),
(1000, (4, 2, 0, 0)),
(1000, (4, 2, 0, 2)),
(10000, (1, 1, 0, 0)),
(10000, (4, 2, 0, 0)),
(10000, (4, 2, 0, 2)),
]
)
def time_anderson_rubin_test_guggenberger19(self, n, data):
Expand All @@ -55,9 +57,24 @@ def time_anderson_rubin_test_guggenberger19(self, n, data):
def time_lagrange_multiplier_test(self, n, data):
_, _ = lagrange_multiplier_test(**self.data)

@skip_for_params(
[
(100, (4, 2, 2, 2)),
(1000, (4, 2, 2, 2)),
(10000, (4, 2, 0, 2)),
]
)
def time_inverse_lagrange_multiplier_test(self, n, data):
data = {k: v for k, v in self.data.items() if k != "beta"}
_ = inverse_lagrange_multiplier_test(**data)

def time_likelihood_ratio_test(self, n, data):
_, _ = likelihood_ratio_test(**self.data)

def time_inverse_likelihood_ratio_test(self, n, data):
data = {k: v for k, v in self.data.items() if k != "beta"}
_ = inverse_likelihood_ratio_test(**data)

def time_conditional_likelihood_ratio_test_numerical_integration(self, n, data):
_, _ = conditional_likelihood_ratio_test(
**self.data, method="numerical_integration"
Expand All @@ -69,8 +86,16 @@ def time_conditional_likelihood_ratio_test_power_series(self, n, data):
def time_wald_test_tsls(self, n, data):
_, _ = wald_test(**self.data, estimator="tsls")

def time_inverse_wald_test_tsls(self, n, data):
data = {k: v for k, v in self.data.items() if k != "beta"}
_ = inverse_wald_test(**data, estimator="tsls")

def time_wald_test_liml(self, n, data):
_, _ = wald_test(**self.data, estimator="liml")

def time_inverse_wald_test_liml(self, n, data):
data = {k: v for k, v in self.data.items() if k != "beta"}
_ = inverse_wald_test(**data, estimator="liml")

def test_rank_test(self, n, data):
_, _ = rank_test(Z=self.data["Z"], X=self.data["X"])
38 changes: 38 additions & 0 deletions ivmodels/confidence_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np


class ConfidenceSet:
"""A class to represent a confidence set."""

def __init__(self, left, right, convex, empty=False, message=None):
self.left = left
self.right = right
self.convex = convex
self.empty = empty
self.message = message

def __call__(self, x): # noqa D
if self.empty:
return 1

between = self.left <= x <= self.right
if self.convex and between:
return -1
elif not self.convex and not between:
return -1
else:
return 1

def __str__(self): # noqa D
if self.empty:
return "[]"
elif self.convex:
return f"[{self.left}, {self.right}]"
else:
return f"(-infty, {self.left}] U [{self.right}, infty)"

def _boundary(self, error=True):
if self.empty:
return np.zeros(shape=(0, 1))
else:
return np.array([[self.left], [self.right]])
24 changes: 22 additions & 2 deletions ivmodels/quadric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import numpy as np
import scipy

from ivmodels.confidence_set import ConfidenceSet

class Quadric:

class Quadric(ConfidenceSet):
"""
A class to represent a quadric :math:`x^T A x + b^T x + c \\leq 0`.

Expand Down Expand Up @@ -57,6 +59,21 @@ def __init__(self, A, b, c):
self.D = eigenvalues
self.V = eigenvectors

if A.shape[0] == 1:
if (self.D[0] < 0) and (self.c_standardized <= 0):
left, right = -np.inf, np.inf
elif self.D[0] * self.c_standardized > 0:
left, right = np.nan, np.nan
else:
boundary = self._boundary()
left, right = boundary[0], boundary[1]

volume = self.volume()
super().__init__(left, right, convex=np.isfinite(volume), empty=volume == 0)
else:
volume = self.volume()
super().__init__(None, None, np.isfinite(volume), empty=volume == 0)

def __call__(self, x):
"""Evaluate the quadric at :math:`x`.

Expand Down Expand Up @@ -116,7 +133,10 @@ def _boundary(self, num=200, error=True):

if len(self.D) == 1:
if self.D[0] * self.c_standardized > 0:
raise ValueError("Quadric is empty.")
if error:
raise ValueError("Quadric is empty.")
else:
return np.zeros(shape=(0, 1))
else:
return np.array(
[
Expand Down
6 changes: 5 additions & 1 deletion ivmodels/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from .anderson_rubin import anderson_rubin_test, inverse_anderson_rubin_test
from .conditional_likelihood_ratio import conditional_likelihood_ratio_test
from .lagrange_multiplier import lagrange_multiplier_test
from .lagrange_multiplier import (
inverse_lagrange_multiplier_test,
lagrange_multiplier_test,
)
from .likelihood_ratio import inverse_likelihood_ratio_test, likelihood_ratio_test
from .pulse import inverse_pulse_test, pulse_test
from .rank import rank_test
Expand All @@ -11,6 +14,7 @@
"inverse_anderson_rubin_test",
"conditional_likelihood_ratio_test",
"lagrange_multiplier_test",
"inverse_lagrange_multiplier_test",
"likelihood_ratio_test",
"inverse_likelihood_ratio_test",
"pulse_test",
Expand Down
Loading
Loading