Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions crates/mlsirm-core/src/fitstats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ fn icc_nodes(
let (free_alpha, uses_space) = model_exec_flags(bank.model_type);
let kind = crate::interaction_kind(bank.model_type);
let n_items = bank.b.len();
let (t_nodes, t_weights) = crate::quadrature::require_gh_rule(q_theta, "quadrature size")?;
let (t_nodes, t_weights) = if uses_space {
crate::quadrature::require_gh_rule(q_theta, "quadrature size")?
} else {
crate::quadrature::require_gh_rule_unidim(q_theta, "quadrature size")?
};
let (x_grid, x_logw) = if uses_space {
let nodes = build_xi_nodes(xi_rule, bank.latent_dim)?;
(nodes.grid, nodes.logw)
Expand Down Expand Up @@ -2978,7 +2982,7 @@ pub fn poly_local_dependence(
let z = n_cat - 1;

// per-item, per-node category probabilities P_i(a | theta_t)
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "quadrature size")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "quadrature size")?;
let qn = nodes.len();
let mut probs = vec![0.0_f64; n_items * qn * n_cat];
for i in 0..n_items {
Expand Down Expand Up @@ -3527,7 +3531,7 @@ pub fn poly_m2(
}

// cumulative-probability tensor S[(i*qn+t)*z + (c-1)] = P(Y_i >= c | theta_t)
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "quadrature size")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "quadrature size")?;
let qn = nodes.len();
let build_cum = |slope: &[f64], cat_params: &[f64]| -> Vec<f64> {
let mut sc = vec![0.0_f64; n_items * qn * z];
Expand Down
10 changes: 5 additions & 5 deletions crates/mlsirm-core/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ pub fn fit_poly_unidim(
}
validate_observed_categories(y, observed, n_cat)?;
let is_obs = |p: usize, i: usize| observed.map_or(true, |o| o[p * n_items + i]);
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let log_w: Vec<f64> = weights.iter().map(|w| w.ln()).collect();
let qn = nodes.len();

Expand Down Expand Up @@ -828,7 +828,7 @@ pub fn fit_nominal(
return Err(format!("item {i} has no observed responses"));
}
}
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let log_w: Vec<f64> = weights.iter().map(|w| w.ln()).collect();
let qn = nodes.len();

Expand Down Expand Up @@ -1401,7 +1401,7 @@ pub fn fit_poly_multigroup(
}
}
let is_obs = |p: usize, i: usize| observed.map_or(true, |o| o[p * n_items + i]);
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let log_w: Vec<f64> = weights.iter().map(|w| w.ln()).collect();
let qn = nodes.len();

Expand Down Expand Up @@ -2209,7 +2209,7 @@ pub fn score_poly_eap(
}
}
let is_obs = |p: usize, i: usize| observed.map_or(true, |o| o[p * n_items + i]);
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let log_w: Vec<f64> = weights.iter().map(|w| w.ln()).collect();
let qn = nodes.len();
let scores: Vec<f64> = (0..n_cat).map(|c| c as f64).collect();
Expand Down Expand Up @@ -2353,7 +2353,7 @@ pub fn poly_s_x2(

let z = n_cat - 1; // highest category score Z
let f_max = n_items * z; // perfect summed score F
let (nodes, weights) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (nodes, weights) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let qn = nodes.len();

// per-item category probabilities at each node: probs[(i*qn + t)*n_cat + zc]
Expand Down
2 changes: 1 addition & 1 deletion crates/mlsirm-core/src/poly_marginal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub fn fit_poly_lsirm(
}
}
}
let (theta, t_w) = crate::quadrature::require_gh_rule(q_theta, "q_theta")?;
let (theta, t_w) = crate::quadrature::require_gh_rule_unidim(q_theta, "q_theta")?;
let t_logw: Vec<f64> = t_w.iter().map(|w| w.ln()).collect();
let (xi_grid, x_logw) = xi_tensor_grid(q_xi, latent_dim)?;
let n_xi = x_logw.len();
Expand Down
125 changes: 124 additions & 1 deletion crates/mlsirm-core/src/quadrature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,112 @@ pub(crate) const GH_WEIGHTS_41: [f64; 41] = [
2.2578639565831077e-30,
];

/// Look up the embedded rule for `q` nodes; `None` when unsupported.
use std::sync::OnceLock;

// The dense rule is intentionally separate: multidimensional tensor grids keep
// the legacy supported set so q=61 cannot create an accidental 61^D workload.
//
// Golub, G. H., & Welsch, J. H. (1969). Calculation of Gauss quadrature rules.
// Mathematics of Computation, 23(106), 221–230.
// https://doi.org/10.1090/S0025-5718-69-99647-1. Their n-node Gaussian rule is
// exact for weighted polynomials through degree 2n-1. Bock, R. D., & Aitkin,
// M. (1981). Marginal maximum likelihood estimation of item parameters:
// Application of an EM algorithm. Psychometrika, 46(4), 443–459.
// https://doi.org/10.1007/BF02293801. Together these sources support q=61 as a
// denser fixed approximation to the existing unidimensional standard-normal
// expectations used by calibration and diagnostics, not as an adaptive error
// guarantee or a multidimensional tensor-grid rule.

static GH_61: OnceLock<(Vec<f64>, Vec<f64>)> = OnceLock::new();

fn gh_rule_61() -> (&'static [f64], &'static [f64]) {
let (nodes, weights) = GH_61.get_or_init(|| {
// Generated from numpy.polynomial.hermite_e.hermegauss(61), normalized.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let positive = [
0.0,
0.40063821105995046,
0.80153826303172659,
1.2029642303011723,
1.6051847101182257,
2.008475226640472,
2.4131208142506848,
2.8194188542255731,
3.2276822527149105,
3.638243067606806,
4.0514567191649027,
4.4677069572407992,
4.8874118107487146,
5.3110308195370264,
5.7390739549506931,
6.1721127891630712,
6.6107947002797749,
7.055861242232579,
7.5081723365509907,
7.9687387811758628,
8.4387669441217881,
8.9197218412129384,
9.4134189285095022,
9.9221626372550862,
10.448964909729462,
10.997909441340786,
11.574803203521677,
12.188457192776552,
12.853564295912951,
13.598659156615788,
14.498533915900149,
];
let positive_weights = [
0.1598141411777855,
0.14753749310915876,
0.11605765134525153,
0.077742307060467822,
0.044299190141556276,
0.021440700089215427,
0.0087970077780564638,
0.0030522259632903151,
0.00089286291334885349,
0.00021942509449102729,
4.5111525791571688e-05,
7.7204093582151049e-06,
1.0935537165661111e-06,
1.2734244397327087e-07,
1.209624282534318e-08,
9.2880342218154687e-10,
5.7039803251286501e-11,
2.7668880796351234e-12,
1.0446062071534655e-13,
3.015866131841913e-15,
6.5184766419075028e-17,
1.0277368048053688e-18,
1.1444908106729494e-20,
8.6411169244947458e-23,
4.193948417922687e-25,
1.2179530564067127e-27,
1.9132142558327285e-30,
1.3977653698327285e-33,
3.7088060732095731e-37,
2.2364824894989637e-41,
9.3712287678837323e-47,
];
let mut nodes = positive
.iter()
.skip(1)
.rev()
.map(|x| -*x)
.collect::<Vec<_>>();
nodes.extend_from_slice(&positive);
let mut weights = positive_weights
.iter()
.skip(1)
.rev()
.copied()
.collect::<Vec<_>>();
weights.extend_from_slice(&positive_weights);
(nodes, weights)
});
(nodes, weights)
}

pub(crate) fn gh_rule(q: usize) -> Option<(&'static [f64], &'static [f64])> {
match q {
7 => Some((&GH_NODES_7, &GH_WEIGHTS_7)),
Expand All @@ -291,6 +396,14 @@ pub(crate) fn gh_rule(q: usize) -> Option<(&'static [f64], &'static [f64])> {
}
}

pub(crate) fn gh_rule_unidim(q: usize) -> Option<(&'static [f64], &'static [f64])> {
if q == 61 {
Some(gh_rule_61())
} else {
gh_rule(q)
}
}

/// Resolve an embedded rule with a consistent public-validation error.
pub(crate) fn require_gh_rule(
q: usize,
Expand All @@ -302,6 +415,16 @@ pub(crate) fn require_gh_rule(
}
}

pub(crate) fn require_gh_rule_unidim(
q: usize,
name: &str,
) -> Result<(&'static [f64], &'static [f64]), String> {
match gh_rule_unidim(q) {
Some(rule) => Ok(rule),
None => Err(format!("unsupported {name} {q}")),
}
}

pub(crate) const SUPPORTED_Q: [usize; 6] = [7, 11, 15, 21, 31, 41];

#[cfg(test)]
Expand Down
42 changes: 30 additions & 12 deletions python/fast_mlsirm/polytomous.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
MAX_POLY_QUADRATURE_POINTS = 4_096
MAX_POLY_BOOTSTRAP_REPLICATES = 10_000
MAX_POLY_CAT_ITEMS = 10_000
_SUPPORTED_FIT_QUADRATURE_POINTS = (7, 11, 15, 21, 31, 41)
_SUPPORTED_FIT_QUADRATURE_POINTS = (7, 11, 15, 21, 31, 41, 61)
_SUPPORTED_XI_QUADRATURE_POINTS = (7, 11, 15, 21, 31, 41)
_NUMPY_INTEGER_SCALAR_TYPES = (
np.int8,
np.int16,
Expand Down Expand Up @@ -81,8 +82,8 @@ def _bounded_integer(value, name: str, lower: int, upper: int) -> int:


def _quadrature_points(value) -> int:
"""Validate and return the Gauss-Hermite quadrature node count ``q_theta``."""
return _bounded_integer(value, "q_theta", 1, MAX_POLY_QUADRATURE_POINTS)
"""Backward-compatible alias for the supported unidimensional rule set."""
return _fit_quadrature_points(value)


def _fit_quadrature_points(value) -> int:
Expand All @@ -93,9 +94,23 @@ def _fit_quadrature_points(value) -> int:
elif _is_exact_type(value_type, _NUMPY_INTEGER_SCALAR_TYPES):
validated = int(value)
else:
raise ValueError("q_theta must be one of 7, 11, 15, 21, 31, 41")
raise ValueError("q_theta must be one of 7, 11, 15, 21, 31, 41, 61")
if validated not in _SUPPORTED_FIT_QUADRATURE_POINTS:
raise ValueError("q_theta must be one of 7, 11, 15, 21, 31, 41")
raise ValueError("q_theta must be one of 7, 11, 15, 21, 31, 41, 61")
return validated


def _fit_xi_quadrature_points(value) -> int:
"""Validate the bounded tensor-grid node count for latent-space ``xi``."""
value_type = type(value)
if value_type is int:
validated = value
elif _is_exact_type(value_type, _NUMPY_INTEGER_SCALAR_TYPES):
validated = int(value)
else:
raise ValueError("q_xi must be one of 7, 11, 15, 21, 31, 41")
if validated not in _SUPPORTED_XI_QUADRATURE_POINTS:
raise ValueError("q_xi must be one of 7, 11, 15, 21, 31, 41")
return validated


Expand Down Expand Up @@ -539,9 +554,12 @@ def fit_lsirm_polytomous(
raise ValueError("latent_dim must be an integer in 1..3") from exc
try:
validated_q_theta = _fit_quadrature_points(q_theta)
validated_q_xi = _fit_quadrature_points(q_xi)
validated_q_xi = _fit_xi_quadrature_points(q_xi)
except ValueError as exc:
raise ValueError("q_theta/q_xi must be one of 7, 11, 15, 21, 31, 41") from exc
raise ValueError(
"q_theta must be one of 7, 11, 15, 21, 31, 41, 61; "
"q_xi must be one of 7, 11, 15, 21, 31, 41"
) from exc
validated_max_iter = _bounded_integer(max_iter, "max_iter", 1, MAX_MAX_ITER)
validated_tol = _positive_real(tol, "tol")

Expand Down Expand Up @@ -671,7 +689,7 @@ def item_fit_polytomous(
"""
n_items = fit.slope.shape[0]
n_cat = fit.cat_params.shape[1] + 1
q_theta = _quadrature_points(q_theta)
q_theta = _fit_quadrature_points(q_theta)
if not np.isfinite(min_expected) or min_expected <= 0:
raise ValueError("min_expected must be positive")
y_int, observed = _poly_int_and_mask(responses, n_cat)
Expand Down Expand Up @@ -732,7 +750,7 @@ def m2_polytomous(
categorical data analysis. *Multivariate Behavioral Research,
49*(4), 305-328. https://doi.org/10.1080/00273171.2014.911075
"""
q_theta = _quadrature_points(q_theta)
q_theta = _fit_quadrature_points(q_theta)
if hasattr(fit, "converged") and not bool(fit.converged):
reason = getattr(fit, "termination_reason", "unknown")
n_iter = getattr(fit, "n_iter", "unknown")
Expand Down Expand Up @@ -801,7 +819,7 @@ def local_dependence_polytomous(
"""
n_items = fit.slope.shape[0]
n_cat = fit.cat_params.shape[1] + 1
q_theta = _quadrature_points(q_theta)
q_theta = _fit_quadrature_points(q_theta)
y_int, observed = _poly_int_and_mask(responses, n_cat)
if y_int.shape[1] != n_items:
raise ValueError("responses column count must match the fitted item count")
Expand Down Expand Up @@ -960,7 +978,7 @@ def person_fit_polytomous(
"""
n_items = fit.slope.shape[0]
n_cat = fit.cat_params.shape[1] + 1
q_theta = _quadrature_points(q_theta)
q_theta = _fit_quadrature_points(q_theta)
if not np.isfinite(prior_mean):
raise ValueError("prior_mean must be finite")
if not np.isfinite(prior_sd) or prior_sd <= 0:
Expand Down Expand Up @@ -1030,7 +1048,7 @@ def cat_simulate_polytomous(
tt = np.asarray(true_theta, dtype=np.float64).ravel()
if tt.size == 0 or not np.all(np.isfinite(tt)):
raise ValueError("true_theta must be a non-empty finite 1-D array")
q_theta = _quadrature_points(q_theta)
q_theta = _fit_quadrature_points(q_theta)
min_items = _bounded_integer(min_items, "min_items", 1, MAX_POLY_CAT_ITEMS)
max_items = _bounded_integer(max_items, "max_items", min_items, MAX_POLY_CAT_ITEMS)
effective_max_items = min(max_items, n_items)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/quadrature_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,29 @@ fn required_rule_covers_success_and_error_contracts() {
require_gh_rule(8, "quadrature size").unwrap_err(),
"unsupported quadrature size 8"
);
assert!(crate::quadrature::gh_rule(61).is_none());
}

#[test]
fn dense_unidimensional_rule_is_normalized_and_symmetric() {
let (nodes, weights) = crate::quadrature::gh_rule_unidim(61).unwrap();
assert_eq!(nodes.len(), 61);
assert_eq!(weights.len(), 61);
assert!((weights.iter().sum::<f64>() - 1.0).abs() < 1e-14);
let second = nodes
.iter()
.zip(weights)
.map(|(node, weight)| weight * node.powi(2))
.sum::<f64>();
let fourth = nodes
.iter()
.zip(weights)
.map(|(node, weight)| weight * node.powi(4))
.sum::<f64>();
assert!((second - 1.0).abs() < 1e-14);
assert!((fourth - 3.0).abs() < 1e-12);
for i in 0..61 {
assert!((nodes[i] + nodes[60 - i]).abs() < 1e-14);
assert!((weights[i] - weights[60 - i]).abs() < 1e-14);
}
}
Loading