Fit binary logistic regression with Newton's method written as iteratively reweighted least squares (IRLS), with an optional ℓ2 penalty.
Implement irls_logreg(X, y, n_iter, lam=0.0):
X: (N, D) design matrix whose column 0 is the bias (all ones); y: labels in {0,1}.
- Objective (summed, not averaged):
L(w)=−∑n=1N[ynlogμn+(1−yn)log(1−μn)]+λ∑d≥1wd2,μn=σ(w⊤xn)
The bias w0 is not penalized.
- Start from w=0 and perform exactly
n_iter Newton steps, each one solved as a weighted least-squares system with weights sn=μn(1−μn) and working response zn=w⊤xn+(yn−μn)/sn:
(X⊤SX+2λI′)wnew=X⊤Sz
where S=diag(s) and I′ is the identity with its (0,0) entry set to 0.
- Return
(w, history) where history[t] is L(w) after step t+1 (length n_iter).
Use np.linalg.solve; do not form S−1 as a matrix. You may assume the data are not linearly separable when lam == 0.