Skip to content

Commit

Permalink
Merge pull request #15 from invenia/rf/positive-type-consistency
Browse files Browse the repository at this point in the history
Make default error tolerance on positive relative to the value type
  • Loading branch information
rofinn authored Jan 12, 2021
2 parents 0faff9b + 4700043 commit 89c3fa0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ParameterHandling"
uuid = "2412ca09-6db7-441c-8e3a-88d5709968c5"
authors = ["Invenia Technical Computing Corporation"]
version = "0.2.2"
version = "0.3.0"

[deps]
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
Expand Down
11 changes: 6 additions & 5 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ value(x::NamedTuple) = map(value, x)
value(x::Dict) = Dict(k => value(v) for (k, v) in x)

"""
positive(val::Real, transform::Bijector=Bijectors.Exp(), ε::Real = 1e-12)
positive(val::T, transform::Bijector=Bijectors.Exp(), ε=sqrt(eps(T))) where T<:Real
Returns a `Postive`.
The `value` of a `Positive` is a `Real` number that is constrained to be positive.
This is represented in terms of an a `transform` that maps an `unconstrained_value` to the
positive reals.
Satisfies `val ≈ transform(unconstrained_value)`
"""
function positive(val::Real, transform::Bijector=Bijectors.Exp(), ε::Real = 1e-12)
if val <= 0
throw(ArgumentError("Value, $val, is not positive."))
end
function positive(
val::T, transform::Bijector=Bijectors.Exp(), ε=sqrt(eps(T)),
) where T<:Real
val > 0 || throw(ArgumentError("Value ($val) is not positive."))
val > ε || throw(ArgumentError("Value ($val) is too small, relative to ε ()."))
unconstrained_value = inv(transform)(val - ε)
return Positive(unconstrained_value, transform, convert(typeof(unconstrained_value), ε))
end
Expand Down
6 changes: 5 additions & 1 deletion test/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ pdiagmat(args...) = PDiagMat(args...)
@testset "parameters" begin

@testset "postive" begin
@testset "$val" for val in [5.0, 1e-11, 1e-12]
@testset "$val" for val in [5.0, 0.001f0, 1.0e-7]
p = positive(val)
test_parameter_interface(p)
@test value(p) val
@test typeof(value(p)) === typeof(val)
end

# Test edge cases around the size of the value relative to the error tol.
@test_throws ArgumentError positive(-0.1)
@test_throws ArgumentError positive(1e-11)
@test value(positive(1e-11, Bijectors.Exp(), 1e-12)) 1e-11
end

@testset "bounded" begin
Expand Down

2 comments on commit 89c3fa0

@rofinn
Copy link
Contributor Author

@rofinn rofinn commented on 89c3fa0 Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/27875

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 89c3fa0f774c10ab1dacdcbb14174757008ac0fd
git push origin v0.3.0

Please sign in to comment.