Skip to content

Fix the SEM criterion and link fit - #11

Merged
adimajo merged 5 commits into
masterfrom
fix/sem-algorithm
Nov 1, 2026
Merged

adimajo merged 5 commits into
masterfrom
fix/sem-algorithm

Conversation

@adimajo

@adimajo adimajo commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Five fixes in the SEM fit (glmdisc/_fitSEM.py), one commit each:

  • The five independent if blocks of _calculate_criterion are one if/elif chain.
  • The BIC penalty uses the number of training rows, not the whole sample: the log-likelihood is computed on the training rows.
  • With validation=True, the aic/bic likelihood is now computed on the validation rows. It was logged as a validation likelihood but computed on the training rows, so the best iteration was picked on training data. This changes which iteration is kept for the default bic + validation setting.
  • The link p(e|x) of a continuous feature no longer crashes when all complete training rows carry one label: a constant link (_SingleClassLink, a LogisticRegression subclass so best_link keeps working in discretize) is used instead.
  • The tail of y_p, p * (2y - 1) - y + 1, is a named helper, _likelihood_of_labels, shared by the continuous and categorical branches.

np.in1d was already np.isin on master (#10).

Each fix has a test that fails on the previous code. Full suite: 98 passed.

The five independent ifs are one if/elif chain, so exactly one branch
sets the performance and the Gini branch computes it once, on the
validation or training rows.
The log-likelihood is computed on the training rows, so the penalty
log(n) k must use their number, not the size of the whole sample.
When every complete training row of a feature carries the same label,
the logistic regression cannot be fitted and the chain crashed. Use a
constant link with probability one for that label instead.
p * (2y - 1) - y + 1 is p(y=1) where y=1 and 1 - p elsewhere. Say so
with np.where in one helper shared by the continuous and categorical
branches.
With validation=True the aic and bic criteria logged a validation
likelihood but computed it on the training rows, so the best iteration
was picked on training data. Use the validation rows.
@adimajo
adimajo merged commit abe762c into master Nov 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant