Spaces:
Sleeping
Sleeping
File size: 4,362 Bytes
a738d20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import numpy as np
import pandas as pd
from config import RASCH_MAX_ITER, RASCH_TOL
class RaschModel:
"""
Modèle de Rasch 1-PL — JMLE from scratch (numpy + scipy uniquement).
"""
def __init__(self, max_iter=RASCH_MAX_ITER, tol=RASCH_TOL):
self.max_iter = max_iter
self.tol = tol
self.theta = None # (n_students,)
self.b = None # (n_items,)
self.converged = False
self.n_iter = 0
@staticmethod
def _logistic(x):
return 1 / (1 + np.exp(-np.clip(x, -30, 30)))
def fit(self, R: np.ndarray):
"""
R : matrice binaire (n_students × n_items).
Algorithme JMLE :
1. Init : theta = logit(r_i / J), b = -logit(s_j / N)
avec correction 0.3 pour scores extrêmes.
2. Alternance mises à jour Newton-Raphson :
theta_i += (r_i - sum_j P_ij) / sum_j P_ij(1-P_ij)
b_j += (sum_i P_ij - s_j) / sum_i P_ij(1-P_ij)
3. Centrage b à chaque itération.
4. Convergence : max(|delta|) < tol
"""
N, J = R.shape
r_i = np.sum(R, axis=1) # scores des étudiants
s_j = np.sum(R, axis=0) # scores des items
# Correction des scores extrêmes (0 ou max)
r_i_adj = np.clip(r_i, 0.3, J - 0.3)
s_j_adj = np.clip(s_j, 0.3, N - 0.3)
# Initialisation via logit
self.theta = np.log(r_i_adj / (J - r_i_adj))
self.b = -np.log(s_j_adj / (N - s_j_adj))
for iter_num in range(self.max_iter):
# Calcul probabilités P_ij
diff = self.theta[:, np.newaxis] - self.b[np.newaxis, :]
P = self._logistic(diff)
W = P * (1 - P) # Variance P(1-P)
# Mise à jour theta
delta_theta = (r_i - np.sum(P, axis=1)) / np.sum(W, axis=1)
self.theta += delta_theta
# Mise à jour b
# diff = self.theta[:, np.newaxis] - self.b[np.newaxis, :]
# P = self._logistic(diff)
# W = P * (1 - P)
delta_b = (np.sum(P, axis=0) - s_j) / np.sum(W, axis=0)
self.b += delta_b
# Centrage de b
mean_b = np.mean(self.b)
self.b -= mean_b
self.theta -= mean_b
# Vérification de la convergence
max_delta = max(np.max(np.abs(delta_theta)), np.max(np.abs(delta_b)))
if max_delta < self.tol:
self.converged = True
self.n_iter = iter_num + 1
break
if not self.converged:
self.n_iter = self.max_iter
def item_fit_stats(self, R: np.ndarray) -> pd.DataFrame:
"""
Infit MNSQ par item.
Flag : <0.7 overfit | >1.3 underfit | sinon acceptable
"""
if self.theta is None or self.b is None:
raise ValueError("Modèle non fitté.")
N, J = R.shape
diff = self.theta[:, np.newaxis] - self.b[np.newaxis, :]
P = self._logistic(diff)
W = P * (1 - P) # Variance P_ij(1-P_ij)
# Résidus standardisés
Z = (R - P) / np.sqrt(W + 1e-10)
# Infit mean-square = sum_i (W_ij * Z_ij^2) / sum_i W_ij
# Puisque Z^2 = (R-P)^2 / W, alors W * Z^2 = (R-P)^2
infit_mnsq = np.sum((R - P)**2, axis=0) / np.sum(W, axis=0)
df = pd.DataFrame({
'item_idx': range(J),
'b': self.b,
'infit_mnsq': infit_mnsq
})
df['fit_status'] = 'acceptable'
df.loc[df['infit_mnsq'] < 0.7, 'fit_status'] = 'overfit'
df.loc[df['infit_mnsq'] > 1.3, 'fit_status'] = 'underfit'
return df
def get_item_difficulties(self) -> np.ndarray:
return self.b
def summary(self) -> dict:
if self.theta is None or self.b is None:
return {"converged": False, "error": "Model not fitted"}
return {
"converged": bool(self.converged),
"n_iter": int(self.n_iter),
"mean_b": float(np.mean(self.b)),
"std_b": float(np.std(self.b)),
"mean_theta": float(np.mean(self.theta)),
"std_theta": float(np.std(self.theta)),
"b_range": [float(np.min(self.b)), float(np.max(self.b))],
}
|