Skip to content

Commit

Permalink
Correctly handling the case λmax = 0.
Browse files Browse the repository at this point in the history
Two changes:
1) Change to computeλ to ensure λmax = 0 leads to an output of [0] and 
not [NaN, ..., NaN].
2) Change to fit! to ensure the case where autoλ = true and λmax = 0 is 
handled correctly (rather than throwing an error).
  • Loading branch information
barankarakus committed Sep 6, 2020
1 parent 81ec2a4 commit 3ccd5f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/Lasso.jl
Expand Up @@ -209,6 +209,10 @@ const MAX_DEV_FRAC = 0.999
# Compute automatic λ values based on λmax and λminratio
function computeλ(λmax, λminratio, α, nλ)
λmax /= α
if λmax == 0
@info "The penalized coefficients equal zero for all values of the regularisation parameter λ."
return [λmax]
end
logλmax = log(λmax)
exp.(range(logλmax, stop=logλmax + log(λminratio), length=nλ))
end
Expand Down
40 changes: 22 additions & 18 deletions src/coordinate_descent.jl
Expand Up @@ -685,7 +685,7 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false, irls
niter = 0
if== 0
i = 0
else
elseif i <=# need this check because it is possible that autoλ is true and nλ is 1
while true # outer loop
obj = convert(T, Inf)
last_dev_ratio = dev_ratio
Expand Down Expand Up @@ -776,6 +776,7 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false, irls
end
end

i = min(i, nλ)
path.λ = path.λ[1:i]
path.pct_dev = pct_dev[1:i]
path.coefs = coefs[:, 1:i]
Expand Down Expand Up @@ -819,29 +820,32 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false,
i = 1
end

while true # outer loop
last_dev_ratio = dev_ratio
curλ = λ[i]
if i <=# need this check because it is possible that autoλ is true and nλ is 1
while true # outer loop
last_dev_ratio = dev_ratio
curλ = λ[i]

# Run coordinate descent
niter += cdfit!(newcoef, cd, curλ, criterion)
# Run coordinate descent
niter += cdfit!(newcoef, cd, curλ, criterion)

dev_ratio = cd.dev/nulldev
pct_dev[i] = 1 - dev_ratio
addcoefs!(coefs, newcoef, i)
b0s[i] = intercept(newcoef, cd)
dev_ratio = cd.dev/nulldev
pct_dev[i] = 1 - dev_ratio
addcoefs!(coefs, newcoef, i)
b0s[i] = intercept(newcoef, cd)

# Test whether we should continue
if i ==|| (stopearly && autoλ && (last_dev_ratio - dev_ratio < MIN_DEV_FRAC_DIFF ||
pct_dev[i] > MAX_DEV_FRAC))
break
end
# Test whether we should continue
if i ==|| (stopearly && autoλ && (last_dev_ratio - dev_ratio < MIN_DEV_FRAC_DIFF ||
pct_dev[i] > MAX_DEV_FRAC))
break
end

verbose && println("$i: λ=$curλ, pct_dev=$(pct_dev[i])")
poststep(path, cd, i, newcoef)
i += 1
verbose && println("$i: λ=$curλ, pct_dev=$(pct_dev[i])")
poststep(path, cd, i, newcoef)
i += 1
end
end

i = min(i, nλ)
path.λ = path.λ[1:i]
path.pct_dev = pct_dev[1:i]
path.coefs = coefs[:, 1:i]
Expand Down

0 comments on commit 3ccd5f0

Please sign in to comment.