Skip to content

Commit 6e47230

Browse files
AsafManelaclaude
andcommitted
Fix DomainError from log(0.0) in Julia 1.13 for Poisson/LogLink models
In Julia 1.13, log(0.0) throws DomainError instead of returning -Inf. During IRLS iterations for Poisson/LogLink models, the linear predictor η can overflow, causing exp(η) = Inf, then y/μ = 0.0 for finite y, and ultimately log(0.0) in xlogy inside GLM's devresid. Mirror GLM.jl's approach: wrap updateμ! and deviance() in try-catch blocks that catch DomainError and set dev = typemax(T), triggering the existing step-halving mechanism to recover with a smaller step size. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c900991 commit 6e47230

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/coordinate_descent.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,15 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false, irls
704704
niter += cdfit!(newcoef, update!(cd, newcoef, scratchmu, wrkwt), curλ, criterion)
705705
b0 = intercept(newcoef, cd)
706706

707-
# Update GLM and get deviance
708-
updateμ!(r, linpred!(scratchmu, cd, newcoef, b0))
709-
710-
# Compute Elastic Net objective
707+
# Update GLM and get deviance; catch DomainError (e.g. log(0.0) in
708+
# Julia 1.13+ when exp(η) overflows) and set dev=Inf to trigger step-halving
711709
objold = obj
712-
dev = deviance(r)
710+
try
711+
updateμ!(r, linpred!(scratchmu, cd, newcoef, b0))
712+
dev = deviance(r)
713+
catch e
714+
isa(e, DomainError) ? (dev = typemax(T)) : rethrow(e)
715+
end
713716
obj = dev/2 + curλ*P(α, newcoef, ω)
714717

715718
if obj > objold + length(scratchmu)*eps(objold)
@@ -729,8 +732,12 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false, irls
729732
newcoef.coef[icoef] = oldcoefval+f*(coefdiff.coef[icoef])
730733
end
731734
b0 = oldb0+f*b0diff
732-
updateμ!(r, linpred!(scratchmu, cd, newcoef, b0))
733-
dev = deviance(r)
735+
try
736+
updateμ!(r, linpred!(scratchmu, cd, newcoef, b0))
737+
dev = deviance(r)
738+
catch e
739+
isa(e, DomainError) ? (dev = typemax(T)) : rethrow(e)
740+
end
734741
obj = dev/2 + curλ*P(α, newcoef, ω)
735742
end
736743
end

0 commit comments

Comments
 (0)