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

Identity Weighting Matrix #477

Merged
merged 3 commits into from
Feb 10, 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: 1 addition & 1 deletion src/estimagic/estimation/estimate_msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def estimate_msm(
# Check and process inputs
# ==================================================================================

if weights not in ["diagonal", "optimal"]:
if weights not in ["diagonal", "optimal", "identity"]:
raise NotImplementedError("Custom weighting matrices are not yet implemented.")

is_optimized = optimize_options is False
Expand Down
4 changes: 3 additions & 1 deletion src/estimagic/estimation/msm_weighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_weighting_matrix(
Args:
moments_cov (pandas.DataFrame or numpy.ndarray): Square DataFrame or Array
with the covariance matrix of the moment conditions for msm estimation.
method (str): One of "optimal", "diagonal".
method (str): One of "optimal", "diagonal", or "identity".
empirical_moments (pytree): Pytree containing empirical moments. Used to get
the tree structure
clip_value (float): Bound at which diagonal elements of the moments_cov are
Expand Down Expand Up @@ -106,6 +106,8 @@ def get_weighting_matrix(
elif method == "diagonal":
diagonal_values = 1 / np.clip(np.diagonal(_internal_cov), clip_value, np.inf)
array_weights = np.diag(diagonal_values)
elif method == "identity":
array_weights = np.identity(_internal_cov.shape[0])
else:
raise ValueError(f"Invalid method: {method}")

Expand Down
8 changes: 6 additions & 2 deletions tests/estimation/test_msm_weighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def expected_values():
cov_np = np.diag([1, 2, 3])
cov_pd = pd.DataFrame(cov_np)

test_cases = itertools.product([cov_np, cov_pd], ["diagonal", "optimal"])
test_cases = itertools.product([cov_np, cov_pd], ["diagonal", "optimal", "identity"])


@pytest.mark.parametrize("moments_cov, method", test_cases)
Expand All @@ -38,7 +38,11 @@ def test_get_weighting_matrix(moments_cov, method):
assert calculated.columns.equals(moments_cov.columns)
calculated = calculated.to_numpy()

expected = np.diag(1 / np.array([1, 2, 3]))
if method == "identity":
expected = np.identity(cov_np.shape[0])
else:
expected = np.diag(1 / np.array([1, 2, 3]))

aaae(calculated, expected)


Expand Down
Loading