-
Notifications
You must be signed in to change notification settings - Fork 0
馃И Add tests for Luce Spectral Ranking (LSR) #590
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2fc3910
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 952b294
chore(tests): remove unrelated scaling formatter churn
seonghobae 9e02509
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae a309e22
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 05cf0c5
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 39b2f7b
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 0bc8e13
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 1a6dd8a
test(scaling): address current LSR review findings
seonghobae ea03aca
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 3069609
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae e390f19
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 21e4007
馃И Add tests for Luce Spectral Ranking (LSR) functionality
seonghobae 732ed19
test(scaling): address exact-head LSR review findings
seonghobae 8d13104
Merge branch 'main' into test-lsr-rankings-7967325111568967159
opencode-agent[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from fast_mlsirm.scaling import lsr_rankings | ||
|
|
||
|
|
||
| def lsr_oracle(rankings, n, alpha=0.0): | ||
| """Independent oracle computing LSR via continuous-time Markov chain eigenvalue.""" | ||
| A = np.full((n, n), alpha, dtype=float) | ||
| np.fill_diagonal(A, 0.0) | ||
| for rank in rankings: | ||
| for i, winner in enumerate(rank[:-1]): | ||
| rate = 1.0 / (len(rank) - i) | ||
| for loser in rank[i + 1 :]: | ||
| A[loser, winner] += rate | ||
| Q = A | ||
| np.fill_diagonal(Q, -Q.sum(axis=1)) | ||
| evals, evecs = np.linalg.eig(Q.T) | ||
| pi = np.real(evecs[:, np.argmin(np.abs(evals))]) | ||
| weights = pi / pi.sum() * n | ||
| log_pi = np.log(weights) | ||
| return log_pi - np.mean(log_pi), weights | ||
|
|
||
|
|
||
| def test_lsr_rankings_numerical_oracle(): | ||
| """Asserts that lsr_rankings matches an independent Markov-chain oracle exact calculation.""" | ||
| rankings = [[0, 1, 2], [2, 0]] | ||
| n = 3 | ||
| alpha = 0.1 | ||
|
|
||
| oracle_params, oracle_weights = lsr_oracle(rankings, n, alpha=alpha) | ||
| res = lsr_rankings(rankings, n, alpha=alpha) | ||
|
|
||
| np.testing.assert_allclose(res.params, oracle_params, rtol=1e-10, atol=1e-10) | ||
| np.testing.assert_allclose(res.weights, oracle_weights, rtol=1e-10, atol=1e-10) | ||
| assert res.iterations == 1 | ||
|
|
||
|
|
||
| def test_lsr_rankings_public_invariants(): | ||
| """Verifies public invariants: positive finite weights, weights summing to n, and parameter centering.""" | ||
| rankings = [[0, 1, 2], [2, 0, 1], [1, 0]] | ||
| n = 3 | ||
| res = lsr_rankings(rankings, n) | ||
|
|
||
| assert np.all(np.isfinite(res.weights)) | ||
| assert np.all(res.weights > 0) | ||
|
|
||
| # weights.sum() == n | ||
| np.testing.assert_allclose(res.weights.sum(), n, rtol=1e-12, atol=1e-12) | ||
|
|
||
| # centered params | ||
| np.testing.assert_allclose(res.params.sum(), 0.0, rtol=1e-12, atol=1e-12) | ||
|
|
||
| # params == log(weights) - mean(log(weights)) | ||
| expected_params = np.log(res.weights) - np.mean(np.log(res.weights)) | ||
| np.testing.assert_allclose(res.params, expected_params, rtol=1e-12, atol=1e-12) | ||
|
|
||
|
|
||
| def test_lsr_rankings_permutation_invariance_and_repeated(): | ||
| """Tests that rearranging the input order of independent rankings, or adding duplicates, behaves consistently.""" | ||
| rankings = [[0, 1, 2], [2, 1, 0], [0, 2]] | ||
| n = 3 | ||
|
|
||
| # Base calculation | ||
| res_base = lsr_rankings(rankings, n) | ||
|
|
||
| # Permute order of rankings | ||
| res_permuted = lsr_rankings([rankings[2], rankings[0], rankings[1]], n) | ||
| np.testing.assert_allclose( | ||
| res_base.params, res_permuted.params, rtol=1e-12, atol=1e-12 | ||
| ) | ||
|
|
||
| # Repeated rankings (weighting) | ||
| res_repeated = lsr_rankings(rankings * 3, n, alpha=0.0) | ||
| np.testing.assert_allclose( | ||
| res_base.params, res_repeated.params, rtol=1e-12, atol=1e-12 | ||
| ) | ||
|
|
||
| # Non-uniform repeated rankings compared to the independent oracle | ||
| non_uniform_rankings = rankings + [[0, 2]] * 5 | ||
| oracle_nu_params, oracle_nu_weights = lsr_oracle( | ||
| non_uniform_rankings, n, alpha=0.0 | ||
| ) | ||
| res_nu = lsr_rankings(non_uniform_rankings, n, alpha=0.0) | ||
| np.testing.assert_allclose(res_nu.params, oracle_nu_params, rtol=1e-10, atol=1e-10) | ||
| np.testing.assert_allclose( | ||
| res_nu.weights, oracle_nu_weights, rtol=1e-10, atol=1e-10 | ||
| ) | ||
|
|
||
|
|
||
| def test_lsr_rankings_invalid_cases(): | ||
| """Verifies that invalid bounds, duplicates, structural types, and broken graphs raise ValueError.""" | ||
| n = 3 | ||
|
|
||
| # Invalid empty rankings | ||
| with pytest.raises(ValueError, match="at least one ranking is required"): | ||
| lsr_rankings([], n) | ||
|
|
||
| # Invalid short ranking | ||
| with pytest.raises(ValueError, match="fewer than 2 items"): | ||
| lsr_rankings([[0]], n) | ||
|
|
||
| # Invalid duplicate items within ranking | ||
| with pytest.raises(ValueError, match="duplicate item"): | ||
| lsr_rankings([[0, 0]], n) | ||
|
seonghobae marked this conversation as resolved.
|
||
|
|
||
| # Negative / out-of-range bounds | ||
| with pytest.raises(ValueError, match=">= n"): | ||
| lsr_rankings([[0, 3]], n) | ||
|
|
||
| # Invalid 'n' (e.g. 0 or 1, which cannot support pairwise graph) | ||
| with pytest.raises(ValueError): | ||
| lsr_rankings([[0, 1]], 1) | ||
|
|
||
| # Disconnected graph at alpha=0 | ||
| with pytest.raises( | ||
| ValueError, match="stationary distribution could not be computed" | ||
| ): | ||
| lsr_rankings([[0, 1]], 3, alpha=0.0) | ||
|
|
||
| # Valid with alpha > 0 | ||
| assert lsr_rankings([[0, 1]], 3, alpha=0.1).weights.shape == (3,) | ||
|
|
||
| # Invalid alpha (negative or non-finite) | ||
| with pytest.raises(ValueError, match="alpha"): | ||
| lsr_rankings([[0, 1], [1, 2], [2, 0]], n, alpha=-1.0) | ||
|
|
||
| with pytest.raises(ValueError, match="alpha"): | ||
| lsr_rankings([[0, 1], [1, 2], [2, 0]], n, alpha=np.nan) | ||
|
|
||
| # Negative item indices | ||
| with pytest.raises(ValueError, match="negative"): | ||
| lsr_rankings([[-1, 1]], n) | ||
|
|
||
| # Non-integral items (float, string, bool) | ||
| with pytest.raises(ValueError, match="non-integer"): | ||
| lsr_rankings([[0.5, 1.5]], n) | ||
|
|
||
| with pytest.raises(ValueError, match="non-integer"): | ||
| lsr_rankings([["a", "b"]], n) | ||
|
|
||
| with pytest.raises(ValueError, match="non-integer"): | ||
| lsr_rankings([[True, False]], n) | ||
|
|
||
| with pytest.raises(ValueError, match="non-integer"): | ||
| lsr_rankings([[np.bool_(True), np.bool_(False)]], n) | ||
|
|
||
| # Invalid n type | ||
| with pytest.raises(ValueError, match="n must be an integer"): | ||
| lsr_rankings([[0, 1]], True) | ||
|
|
||
| with pytest.raises(ValueError, match="n must be an integer"): | ||
| lsr_rankings([[0, 1]], 2.5) | ||
|
|
||
| # Non-finite alpha (inf) | ||
| with pytest.raises(ValueError, match="finite"): | ||
| lsr_rankings([[0, 1], [1, 2], [2, 0]], n, alpha=np.inf) | ||
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.