Skip to content

Commit

Permalink
AbstractParameter Bounded implemented (#4)
Browse files Browse the repository at this point in the history
* Bounded added

* version updated

* docstring changed to calling signiture

* tests for checking boundaries for Positive and Bounded

Co-authored-by: Letif Mones <[email protected]>
  • Loading branch information
molet and Letif Mones authored Sep 8, 2020
1 parent b21638b commit 5aada90
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 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.1.1"
version = "0.1.2"

[deps]
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
Expand Down
2 changes: 1 addition & 1 deletion src/ParameterHandling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ParameterHandling
using Bijectors
using Compat: only

export flatten, Positive, Fixed, Deferred
export flatten, Positive, Bounded, Fixed, Deferred

include("flatten.jl")
include("parameters.jl")
Expand Down
40 changes: 38 additions & 2 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ value(x::Dict) = Dict(k => value(v) for (k, v) in x)


"""
Positive{T<:Real, V}
Positive(value::Real)
The `value` of a `Positive` is a `Real` number that is constrained to be positive. This is
represented in terms of an `unconstrained_value` and a `transform` that maps any value
Expand All @@ -48,7 +48,43 @@ function flatten(x::Positive)
end

"""
Fixed{T}
Bounded(value::Real, lower_bound::Real, upper_bound::Real)
The `value` of a `Bounded` is a `Real` number that is constrained to be within the interval
(`lower_bound`, `upper_bound`). This is represented in terms of an `unconstrained_value` and
a `transform` that maps any value to reals included by (`lower_bound`, `upper_bound`).
"""
struct Bounded{T<:Real, V<:Bijector, Tε<:Real} <: AbstractParameter
unconstrained_value::T
lower_bound::T
upper_bound::T
transform::V
ε::Tε
end

function Bounded(value::Real, lower_bound::Real, upper_bound::Real)
lb = convert(typeof(value), lower_bound)
ub = convert(typeof(value), upper_bound)
ε = convert(typeof(value), 1e-12)

# Bijectors defines only Logit struct so we use Logistic as the inverse of Logit
return Bounded(value, lb, ub, inv(Bijectors.Logit(lb + ε, ub - ε)), ε)
end

value(x::Bounded) = x.transform(x.unconstrained_value)

function flatten(x::Bounded)
v, unflatten_to_Real = flatten(x.unconstrained_value)

function unflatten_Bounded(v_new::Vector{<:Real})
return Bounded(unflatten_to_Real(v_new), x.lower_bound, x.upper_bound, x.transform, x.ε)
end

return v, unflatten_Bounded
end

"""
Fixed{T}
Represents a parameter whose value is required to stay constant. The `value` of a `Fixed` is
simply its value -- that constantness of the parameter is enforced by returning an empty
Expand Down
17 changes: 16 additions & 1 deletion test/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

@testset "Postive" begin
test_parameter_interface(Positive(5.0))
test_parameter_interface(Positive(5.0))
p = Positive(-10.0)
@test value(p) > p.ε
p = Positive(-Inf)
@test value(p) == p.ε
end

@testset "Bounded" begin
test_parameter_interface(Bounded(-1.0, -0.1, 2.0))
p = Bounded(-10.0, -0.1, 2.0)
@test value(p) > p.lower_bound
p = Bounded(-Inf, -0.1, 2.0)
@test value(p) == p.lower_bound + p.ε
p = Bounded(10.0, -0.1, 2.0)
@test value(p) < p.upper_bound
p = Bounded(Inf, -0.1, 2.0)
@test value(p) == p.upper_bound - p.ε
end

@testset "Fixed" begin
Expand Down

2 comments on commit 5aada90

@molet
Copy link
Contributor Author

@molet molet commented on 5aada90 Sep 9, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/21106

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.1.2 -m "<description of version>" 5aada908548d42754576071910f208f76b0aebfa
git push origin v0.1.2

Please sign in to comment.