Skip to content

Commit

Permalink
Fix determination of nobs in lambda_min_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDunnNZ committed Sep 27, 2024
1 parent 4167f0c commit d33f97e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/CoxNet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64}, family::CoxPH;
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
dfmax::Int=size(X, 2)+1, pmax::Int=min(dfmax*2+20, size(X, 2)),
nlambda::Int=100,
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
intercept::Bool=true, maxit::Int=1000000)
@validate_and_init
Expand Down
4 changes: 2 additions & 2 deletions src/GLMNet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64},
penalty_factor::Vector{Float64}=ones(size(X, 2)),
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
dfmax::Int=size(X, 2), pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
intercept::Bool=true, maxit::Int=1000000, algorithm::Symbol=:newtonraphson)
@validate_and_init
Expand Down Expand Up @@ -385,7 +385,7 @@ function glmnet!(X::SparseMatrixCSC{Float64,Int32}, y::Matrix{Float64},
penalty_factor::Vector{Float64}=ones(size(X, 2)),
constraints::Array{Float64, 2}=[x for x in (-Inf, Inf), y in 1:size(X, 2)],
dfmax::Int=size(X, 2), pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
intercept::Bool=true, maxit::Int=1000000, algorithm::Symbol=:newtonraphson)
@validate_and_init
Expand Down
2 changes: 1 addition & 1 deletion src/Multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function glmnet!(X::Matrix{Float64}, y::Matrix{Float64},
penalty_factor::Vector{Float64}=ones(size(X, 2)),
constraints::Array{Float64, 2}=[a for a in (-Inf, Inf), y in 1:size(X, 2)],
dfmax::Int=size(X, 2)+1, pmax::Int=min(dfmax*2+20, size(X, 2)), nlambda::Int=100,
lambda_min_ratio::Real=(length(y) < size(X, 2) ? 1e-2 : 1e-4),
lambda_min_ratio::Real=(size(y, 1) < size(X, 2) ? 1e-2 : 1e-4),
lambda::Vector{Float64}=Float64[], tol::Real=1e-7, standardize::Bool=true,
intercept::Bool=true, maxit::Int=1000000, grouped_multinomial::Bool=false,
algorithm::Symbol=:newtonraphson)
Expand Down
39 changes: 38 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ cv4 = glmnetcv(X, y; rng=MersenneTwister(1))
# Test previously ambiguous definitions
glmnet(float.(X), y)
glmnetcv(float.(X), y)

# Check for issue #72
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], y[1:nobs], nfolds=2)
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], y[1:nobs], nfolds=2)
end


Expand Down Expand Up @@ -355,6 +361,12 @@ cv = glmnetcv(X, yl, Binomial(); folds=[1,1,1,1,2,2,2,3,3,3])
show(IOBuffer(), cv)
show(IOBuffer(), cv.path)

# Check for issue #72
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], yl[1:nobs, :], Binomial(), nfolds=2)
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], yl[1:nobs, :], Binomial(), nfolds=2)

# Passing RNG makes cv deterministic
cv1 = glmnetcv(X, yl, Binomial())
cv2 = glmnetcv(X, yl, Binomial())
Expand Down Expand Up @@ -532,6 +544,12 @@ cv = glmnetcv(X, y, Poisson(); folds=[1,1,1,1,2,2,2,3,3,3])
show(IOBuffer(), cv)
show(IOBuffer(), cv.path)

# Check for issue #72
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], y[1:nobs], Poisson(), nfolds=2)
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], y[1:nobs], Poisson(), nfolds=2)

# Passing RNG makes cv deterministic
cv1 = glmnetcv(X, y, Poisson())
cv2 = glmnetcv(X, y, Poisson())
Expand Down Expand Up @@ -602,6 +620,12 @@ coxcv = glmnetcv(dat[:,3:size(dat,2)], dat[:,1], dat[:,2], lambda = cox_lambda,
show(IOBuffer(), coxcv)
show(IOBuffer(), coxcv.path)

# Check for issue #72
X = dat[:,3:size(dat,2)]
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], dat[1:nobs,1], dat[1:nobs,2], nfolds=2)
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], dat[1:nobs,1], dat[1:nobs,2], nfolds=2)

# Passing RNG makes cv deterministic
cv1 = glmnetcv(dat[:,3:size(dat,2)], dat[:,1], dat[:,2])
Expand Down Expand Up @@ -670,6 +694,13 @@ iris_cv2 = glmnetcv(iris_x, iris_yy, Multinomial(), folds = multi_folds)
# Make sure show works
show(IOBuffer(), iris_cv)
show(IOBuffer(), iris_cv.path)

# Check for issue #72
X = [iris_x iris_x]
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], iris_yy[1:nobs, :], Multinomial(), nfolds=2)
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], iris_yy[1:nobs, :], Multinomial(), nfolds=2)
end

@testset "MvNormal" begin
Expand Down Expand Up @@ -725,4 +756,10 @@ end
@test path.lambda R_lambda
@test path.a0 R_a0

end
# Check for issue #72
nobs = size(X, 2) - 1
glmnetcv(X[1:nobs, :], Y[1:nobs, :], MvNormal())
nobs = size(X, 2) + 1
glmnetcv(X[1:nobs, :], Y[1:nobs, :], MvNormal())

end

0 comments on commit d33f97e

Please sign in to comment.