Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Commit

Permalink
Added tests for hyperparameter module
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Thatcher committed May 26, 2017
1 parent e2c6c30 commit 8b8fe16
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/HyperParameters/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ theta{T<:AbstractFloat,_<:Bound}(I::Interval{T,_,OpenBound{T}}, x::T) = log(I.b.
theta{T<:AbstractFloat}(I::Interval{T}, x::T) = x

function invtheta{T<:AbstractFloat}(I::Interval{T,OpenBound{T},OpenBound{T}}, x::T)
y = (I.b.value - I.a.value)*x + I.a.value
exp(y)/(one(T)+exp(y))
y = exp(x)/(one(T)+exp(x))
(I.b.value - I.a.value)*y + I.a.value
end
invtheta{T<:AbstractFloat,_<:Bound}(I::Interval{T,OpenBound{T},_}, x::T) = exp(x) + I.a.value
invtheta{T<:AbstractFloat,_<:Bound}(I::Interval{T,_,OpenBound{T}}, x::T) = I.b.value - exp(x)
Expand Down
77 changes: 72 additions & 5 deletions test/hyperparameter.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
MODHP = MLKernels.HyperParameters

info("Testing ", MOD.OpenBound)
for T in (FloatingPointTypes..., IntegerTypes...)
if T <: Integer
Expand Down Expand Up @@ -33,6 +35,9 @@ for T in (FloatingPointTypes..., IntegerTypes...)
@test checkvalue(B, -one(T)) == false
@test checkvalue(B, zero(T)) == false
@test checkvalue(B, one(T)) == true

@test string(B) == string("OpenBound(", zero(T), ")")
@test show(DevNull, B) == nothing
end
end

Expand Down Expand Up @@ -68,6 +73,9 @@ for T in (FloatingPointTypes..., IntegerTypes...)
@test checkvalue(B, -one(T)) == false
@test checkvalue(B, zero(T)) == true
@test checkvalue(B, one(T)) == true

@test string(B) == string("ClosedBound(", zero(T), ")")
@test show(DevNull, B) == nothing
end

info("Testing ", MOD.NullBound)
Expand All @@ -81,6 +89,18 @@ for T in (FloatingPointTypes..., IntegerTypes...)
@test eltype(B_u) == U
end

B = MOD.NullBound(T)

@test checkvalue(-one(T), B) == true
@test checkvalue(zero(T), B) == true
@test checkvalue(one(T), B) == true

@test checkvalue(B, -one(T)) == true
@test checkvalue(B, zero(T)) == true
@test checkvalue(B, one(T)) == true

@test string(B) == string("NullBound(", T, ")")
@test show(DevNull, B) == nothing
end

info("Testing ", MOD.Interval)
Expand All @@ -91,10 +111,15 @@ for T in FloatingPointTypes

a = MOD.ClosedBound(one(T))
b = MOD.ClosedBound(one(T))
B = MOD.Interval(a,b)
@test B.a == a
@test B.b == b
@test eltype(B) == T
I = MOD.Interval(a,b)
@test I.a == a
@test I.b == b
@test eltype(I) == T

I = MOD.interval(nothing, nothing)
@test I.a == NullBound(Float64)
@test I.b == NullBound(Float64)
@test eltype(I) == Float64

for a in (NullBound(T), ClosedBound(-one(T)), OpenBound(-one(T)))
for b in (NullBound(T), ClosedBound(one(T)), OpenBound(one(T)))
Expand All @@ -103,13 +128,55 @@ for T in FloatingPointTypes
@test I.b == b
@test eltype(I) == T

if typeof(a) <: NullBound
if typeof(b) <: NullBound
@test I == MOD.interval(T)
@test string(I) == string("interval(", T, ")")
else
@test I == MOD.interval(nothing, b)
@test string(I) == string("interval(nothing,", string(b), ")")
end
else
if typeof(b) <: NullBound
@test I == MOD.interval(a,nothing)
@test string(I) == string("interval(", string(a), ",nothing)")
else
@test I == MOD.interval(a, b)
@test string(I) == string("interval(", string(a), ",", string(b), ")")
end
end

@test MOD.checkvalue(I, convert(T,-2)) == (typeof(a) <: NullBound ? true : false)
@test MOD.checkvalue(I, -one(T)) == (typeof(a) <: OpenBound ? false : true)
@test MOD.checkvalue(I, zero(T)) == true
@test MOD.checkvalue(I, one(T)) == (typeof(b) <: OpenBound ? false : true)
@test MOD.checkvalue(I, convert(T,2)) == (typeof(b) <: NullBound ? true : false)
end
end

a = convert(T,7.6)
b = convert(T,23)
c = convert(T,13)

I = MODHP.Interval(ClosedBound(a), ClosedBound(b))
@test_approx_eq MODHP.theta(I,c) c
@test_approx_eq MODHP.invtheta(I,MODHP.theta(I,c)) c

I = MODHP.Interval(ClosedBound(a), OpenBound(b))
@test_approx_eq MODHP.theta(I,c) log(b-c)
@test_approx_eq MODHP.invtheta(I,MODHP.theta(I,c)) c

I = MODHP.Interval(OpenBound(a), ClosedBound(b))
@test_approx_eq MODHP.theta(I,c) log(c-a)
@test_approx_eq MODHP.invtheta(I,MODHP.theta(I,c)) c

I = MODHP.Interval(OpenBound(a), OpenBound(b))
v = (c-a)/(b-a)
@test_approx_eq MODHP.theta(I,c) log(v/(1-v))
@test_approx_eq MODHP.invtheta(I,MODHP.theta(I,c)) c

@test show(DevNull, MOD.interval(MOD.ClosedBound(one(T)), MOD.ClosedBound(one(T)))) == nothing

end

info("Testing ", MOD.interval)
Expand Down Expand Up @@ -152,5 +219,5 @@ for T in (FloatingPointTypes..., IntegerTypes...)
@test getindex(P.value) == zero(T)
@test getvalue(P) == zero(T)

show(DevNull, P)
@test show(DevNull, P) == nothing
end

0 comments on commit 8b8fe16

Please sign in to comment.