Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handling the case λmax = 0. #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Lasso.jl
Original file line number Diff line number Diff line change
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 λ."
barankarakus marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ function StatsBase.fit!(path::RegularizationPath{S,T}; verbose::Bool=false, irls
niter = 0
if nλ == 0
i = 0
else
elseif i <= nλ # 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 <= nλ # 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 == nλ || (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 == nλ || (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