-
Notifications
You must be signed in to change notification settings - Fork 1
test: add a real GRM parameter-recovery test for fast-mlsirm #451
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
11a60b3
fix(frontend): repair the inherited login/admin-panel build break
seonghobae 4bcf6c1
test: add a real GRM parameter-recovery test for fast-mlsirm
seonghobae 25c2be8
Merge remote-tracking branch 'origin/worktree-fix-frontend-build-brea…
seonghobae 65f08d3
docs(test): scope GRM recovery claim to theta
seonghobae e84fa8d
test: match production GRM calibration contract
seonghobae 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
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
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
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,67 @@ | ||
| """Real theta-recovery test for the GRM model period_report.py actually | ||
| fits: simulate polytomous responses from known true item parameters and | ||
| person abilities, fit them with fast_mlsirm.fit_polytomous (the same | ||
| function period_report.py calls, per its own module docstring), and assert | ||
| the recovered EAP thetas are close to the true thetas by RMSE and | ||
| correlation -- not a placeholder or an infra-only smoke test. | ||
|
|
||
| fast_mlsirm ships no polytomous-specific simulator (only MLS2PLMConfig's | ||
| multi-level simulate()), so the GRM response-generation formula is | ||
| implemented directly here: cumulative-logistic category boundaries | ||
| (Samejima, 1969), sampled per person/item from the resulting category | ||
| probabilities. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| from fast_mlsirm import fit_polytomous, score_polytomous, validate_irt_response_matrix | ||
|
|
||
| N_PERSONS = 400 | ||
| N_ITEMS = 12 | ||
| N_CAT = 4 | ||
| SEED = 20260101 | ||
|
|
||
| # Reasonable bounds for a 12-item, 4-category GRM test at this sample size: | ||
| # a real run with these exact parameters/seed measures RMSE ~0.38 and | ||
| # correlation ~0.92, comfortably inside literature-typical recovery for a | ||
| # test this length. The margins below are loose enough to tolerate a minor | ||
| # fast-mlsirm version bump while still catching an actual estimation | ||
| # regression (e.g. RMSE blowing up past ~1 std or correlation collapsing). | ||
| MAX_THETA_RMSE = 0.6 | ||
| MIN_THETA_CORRELATION = 0.75 | ||
|
|
||
|
|
||
| def _grm_category_probs(theta: float, discrimination: float, thresholds: np.ndarray) -> np.ndarray: | ||
| """Samejima (1969) graded-response category probabilities for one | ||
| person/item pair, given known true parameters.""" | ||
| cumulative = np.concatenate(([1.0], 1.0 / (1.0 + np.exp(-(discrimination * theta - thresholds))), [0.0])) | ||
| return -np.diff(cumulative) | ||
|
|
||
|
|
||
| def test_grm_recovers_true_theta_within_expected_rmse() -> None: | ||
| rng = np.random.default_rng(SEED) | ||
| true_theta = rng.normal(0.0, 1.0, N_PERSONS) | ||
| true_discrimination = rng.uniform(0.8, 2.0, N_ITEMS) | ||
| true_thresholds = np.sort(rng.normal(0.0, 1.0, (N_ITEMS, N_CAT - 1)), axis=1) | ||
|
|
||
| responses = np.zeros((N_PERSONS, N_ITEMS)) | ||
| for item in range(N_ITEMS): | ||
| for person in range(N_PERSONS): | ||
| probs = _grm_category_probs(true_theta[person], true_discrimination[item], true_thresholds[item]) | ||
| probs = np.clip(probs, 0.0, None) | ||
| probs = probs / probs.sum() | ||
| responses[person, item] = rng.choice(N_CAT, p=probs) | ||
|
seonghobae marked this conversation as resolved.
|
||
|
|
||
| responses = validate_irt_response_matrix(responses, item_type="polytomous", n_categories=N_CAT) | ||
|
seonghobae marked this conversation as resolved.
|
||
| fit = fit_polytomous(responses, n_cat=N_CAT, model="grm", max_iter=80) | ||
| assert fit.converged | ||
|
|
||
| scored = score_polytomous(responses, fit) | ||
| theta_eap = scored["theta_eap"] | ||
|
|
||
| rmse = float(np.sqrt(np.mean((theta_eap - true_theta) ** 2))) | ||
| correlation = float(np.corrcoef(theta_eap, true_theta)[0, 1]) | ||
|
|
||
| assert rmse < MAX_THETA_RMSE, f"theta recovery RMSE {rmse:.3f} exceeded {MAX_THETA_RMSE}" | ||
| assert correlation > MIN_THETA_CORRELATION, f"theta recovery correlation {correlation:.3f} below {MIN_THETA_CORRELATION}" | ||
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.