-
Notifications
You must be signed in to change notification settings - Fork 1
test: add a real GPCM parameter-recovery test for fast-mlsirm #452
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 c7b596a
test: add a real GPCM parameter-recovery test for fast-mlsirm
seonghobae 6d8d33b
Merge remote-tracking branch 'origin/worktree-fix-frontend-build-brea…
seonghobae 576d64f
fix(docs): scope gap-baseline testing note to this PR's GPCM test
seonghobae 09e0ec0
test(psychometrics): match production GPCM fit bound
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,69 @@ | ||
| """Real theta-recovery test for the GPCM model period_report.py's | ||
| production code can also fit: simulate polytomous responses from known | ||
| true item parameters and person abilities using the generalized partial | ||
| credit model (Muraki, 1993), fit them with fast_mlsirm.fit_polytomous(..., | ||
| model="gpcm") -- the same function/model option period_report.py's | ||
| "pick the model with fixed_item_calibration_diagnostics" step can select | ||
| -- and assert the recovered EAP thetas are close to the true thetas by | ||
| RMSE and correlation. | ||
|
|
||
| fast_mlsirm ships no polytomous-specific simulator, so the GPCM | ||
| category-probability formula is implemented directly here, matching the | ||
| library's own documented parameterization (PolytomousFit's docstring: | ||
| "GPCM additive category intercepts"): cumulative step logits | ||
| z_k = sum_{v=1}^{k} a*(theta - b_v), z_0 = 0, softmax over z. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| from fast_mlsirm import fit_polytomous, score_polytomous | ||
|
|
||
| N_PERSONS = 400 | ||
| N_ITEMS = 12 | ||
| N_CAT = 4 | ||
| SEED = 20260101 | ||
|
|
||
| # A real run with these exact parameters/seed measures RMSE ~0.30 and | ||
| # correlation ~0.95 -- stronger recovery than the GRM test's ~0.38/~0.92 | ||
| # at the same sample size, consistent with GPCM's additive (vs. GRM's | ||
| # cumulative) category structure being easier to identify here. The | ||
| # margins below stay loose enough to tolerate a minor fast-mlsirm version | ||
| # bump while still catching an actual estimation regression. | ||
| MAX_THETA_RMSE = 0.55 | ||
| MIN_THETA_CORRELATION = 0.8 | ||
|
seonghobae marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _gpcm_category_probs(theta: float, discrimination: float, steps: np.ndarray) -> np.ndarray: | ||
| """Muraki (1993) generalized partial credit model category | ||
| probabilities for one person/item pair, given known true parameters.""" | ||
| cumulative_steps = np.cumsum(discrimination * (theta - steps)) | ||
| z = np.concatenate(([0.0], cumulative_steps)) | ||
| z = z - z.max() | ||
| unnormalized = np.exp(z) | ||
| return unnormalized / unnormalized.sum() | ||
|
|
||
|
|
||
| def test_gpcm_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_steps = rng.normal(0.0, 1.0, (N_ITEMS, N_CAT - 1)) | ||
|
|
||
| responses = np.zeros((N_PERSONS, N_ITEMS)) | ||
| for item in range(N_ITEMS): | ||
| for person in range(N_PERSONS): | ||
| probs = _gpcm_category_probs(true_theta[person], true_discrimination[item], true_steps[item]) | ||
| responses[person, item] = rng.choice(N_CAT, p=probs) | ||
|
seonghobae marked this conversation as resolved.
|
||
|
|
||
| fit = fit_polytomous(responses, n_cat=N_CAT, model="gpcm", 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.