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

Add ConvergenceException from StatsBase #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions src/statisticalmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,41 @@ function adjr2(model::StatisticalModel, variant::Symbol)
end

const adjr² = adjr2

"""
ConvergenceException(iterations::Int, lastchange::Real=NaN, tolerance::Real=NaN,
message::String="")

The fitting procedure failed to converge in `iterations` number of iterations. Typically
this is because the `lastchange` between the objective in the final and penultimate
iterations was greater than the specified `tolerance`. Further information can be provided
by `message`.
"""
struct ConvergenceException{T<:Real} <: Exception
iterations::Int
lastchange::T
tolerance::T
message::String

function ConvergenceException(iterations, lastchange=NaN, tolerance=NaN, message="")
if tolerance > lastchange
throw(ArgumentError("can't construct `ConvergenceException` with change " *
"less than tolerance; got $lastchange and $tolerance"))
end
T = promote_type(typeof(lastchange), typeof(tolerance))
return new{T}(iterations, lastchange, tolerance, message)
end
end

function Base.showerror(io::IO, ce::ConvergenceException)
print(io, "failure to converge after ", ce.iterations, " iterations")
if !isnan(ce.lastchange)
print(io, "; last change between iterations (", ce.lastchange, ") was greater ",
"than tolerance (", ce.tolerance, ")")
end
print(io, '.')
if !isempty(ce.message)
print(io, ' ', ce.message)
end
return nothing
end
17 changes: 15 additions & 2 deletions test/statisticalmodel.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module TestStatisticalModel

using Test, StatsAPI
using StatsAPI: StatisticalModel, stderror, aic, aicc, bic, r2, r², adjr2, adjr²
using StatsAPI: ConvergenceException, StatisticalModel, stderror, aic, aicc, bic,
r2, r², adjr2, adjr²

struct MyStatisticalModel <: StatisticalModel
end
Expand Down Expand Up @@ -36,4 +37,16 @@ StatsAPI.nobs(::MyStatisticalModel) = 100
@test adjr2 === adjr²
end

end # module TestStatisticalModel
@testset "ConvergenceException" begin
fail = "failure to converge after 10 iterations"
chgtol = "last change between iterations (0.2) was greater than tolerance (0.1)"
msg = "Try changing maxiter."
@test sprint(showerror, ConvergenceException(10)) == "$fail."
@test sprint(showerror, ConvergenceException(10, 0.2, 0.1)) == "$fail; $chgtol."
@test sprint(showerror, ConvergenceException(10, 0.2, 0.1, msg)) == "$fail; $chgtol. $msg"
err = @test_throws ArgumentError ConvergenceException(10, 0.1, 0.2)
@test err.value.msg == string("can't construct `ConvergenceException` with change ",
"less than tolerance; got 0.1 and 0.2")
end

end # module TestStatisticalModel