From 051481a45fff63f254b3333a70b7a47b0256b608 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 24 Oct 2017 12:15:12 -0400 Subject: [PATCH 1/3] Fix utility functions to branch --- src/modeltools/utility.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/modeltools/utility.jl b/src/modeltools/utility.jl index 3a82a55..06b83e3 100644 --- a/src/modeltools/utility.jl +++ b/src/modeltools/utility.jl @@ -23,9 +23,9 @@ end LogUtility() = LogUtility(1.0) (u::LogUtility)(c::Float64) = - ifelse(c > 1e-10, u.ξ*log(c), u.ξ*(log(1e-10) + 1e10*(c - 1e-10))) + c > 1e-10 ? u.ξ*log(c) : u.ξ*(log(1e-10) + 1e10*(c - 1e-10)) derivative(u::LogUtility, c::Float64) = - ifelse(c > 1e-10, u.ξ / c, u.ξ*1e10) + c > 1e-10 ? u.ξ / c : u.ξ*1e10 """ Type used to evaluate CRRA utility. CRRA utility takes the form @@ -50,11 +50,11 @@ struct CRRAUtility <: AbstractUtility end (u::CRRAUtility)(c::Float64) = - ifelse(c > 1e-10, - u.ξ * (c^(1.0 - u.γ) - 1.0) / (1.0 - u.γ), - u.ξ * ((1e-10^(1.0 - u.γ) - 1.0) / (1.0 - u.γ) + 1e-10^(-u.γ)*(c - 1e-10))) + c > 1e-10 ? + u.ξ * (c^(1.0 - u.γ) - 1.0) / (1.0 - u.γ) : + u.ξ * ((1e-10^(1.0 - u.γ) - 1.0) / (1.0 - u.γ) + 1e-10^(-u.γ)*(c - 1e-10)) derivative(u::CRRAUtility, c::Float64) = - ifelse(c > 1e-10, u.ξ * c^(-u.γ), u.ξ*1e-10^(-u.γ)) + c > 1e-10 ? u.ξ * c^(-u.γ) : u.ξ*1e-10^(-u.γ) # Labor Utility @@ -83,11 +83,11 @@ struct CFEUtility <: AbstractUtility end (u::CFEUtility)(l::Float64) = - ifelse(l > 1e-10, - -u.ξ * l^(1.0 + 1.0/u.ϕ)/(1.0 + 1.0/u.ϕ), - -u.ξ * (1e-10^(1.0 + 1.0/u.ϕ)/(1.0 + 1.0/u.ϕ) + 1e-10^(1.0/u.ϕ) * (l - 1e-10))) + l > 1e-10 ? + -u.ξ * l^(1.0 + 1.0/u.ϕ)/(1.0 + 1.0/u.ϕ) : + -u.ξ * (1e-10^(1.0 + 1.0/u.ϕ)/(1.0 + 1.0/u.ϕ) + 1e-10^(1.0/u.ϕ) * (l - 1e-10)) derivative(u::CFEUtility, l::Float64) = - ifelse(l > 1e-10, -u.ξ * l^(1.0/u.ϕ), -u.ξ * 1e-10^(1.0/u.ϕ)) + l > 1e-10 ? -u.ξ * l^(1.0/u.ϕ) : -u.ξ * 1e-10^(1.0/u.ϕ) """ From 5fce79f293cf26f4ef96f9e3ca447bf7bb71eaa5 Mon Sep 17 00:00:00 2001 From: Chase Coleman Date: Thu, 9 Nov 2017 16:16:31 -0500 Subject: [PATCH 2/3] Add tests for negative values --- test/test_modeltool.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_modeltool.jl b/test/test_modeltool.jl index 9d570d5..d5e9355 100644 --- a/test/test_modeltool.jl +++ b/test/test_modeltool.jl @@ -12,6 +12,7 @@ # Test "extrapolation evaluations" @test isapprox(u(0.5e-10), u(1e-10) + derivative(u, 1e-10)*(0.5e-10 - 1e-10)) + @test u(-0.5) < u(-0.1) # Make sure it doesn't fail with negative values @test isapprox(LogUtility().ξ, 1.0) # Test default constructor @@ -29,6 +30,7 @@ # Test "extrapolation evaluations" @test isapprox(u(0.5e-10), u(1e-10) + derivative(u, 1e-10)*(0.5e-10 - 1e-10)) + @test u(-0.5) < u(-0.1) # Make sure it doesn't fail with negative values @test_throws ErrorException CRRAUtility(1.0) # Test error throwing at γ=1.0 end @@ -44,6 +46,7 @@ # Test "extrapolation evaluations" @test isapprox(v(0.5e-10), v(1e-10) + derivative(v, 1e-10)*(0.5e-10 - 1e-10)) + @test v(-0.5) < v(-0.1) # Make sure it doesn't fail with negative values @test_throws ErrorException CRRAUtility(1.0) # Test error throwing at ϕ=1.0 end @@ -60,6 +63,7 @@ # Test default values @test isapprox(EllipticalUtility().b, 0.5223) @test isapprox(EllipticalUtility().μ, 2.2926) + end end From 7948703ba51f9bea70461ab1253d1075002968e2 Mon Sep 17 00:00:00 2001 From: Chase Coleman Date: Thu, 9 Nov 2017 16:18:42 -0500 Subject: [PATCH 3/3] Make sure the v test is increasing --- test/test_modeltool.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_modeltool.jl b/test/test_modeltool.jl index d5e9355..3997b6e 100644 --- a/test/test_modeltool.jl +++ b/test/test_modeltool.jl @@ -46,7 +46,7 @@ # Test "extrapolation evaluations" @test isapprox(v(0.5e-10), v(1e-10) + derivative(v, 1e-10)*(0.5e-10 - 1e-10)) - @test v(-0.5) < v(-0.1) # Make sure it doesn't fail with negative values + @test v(-0.5) > v(-0.1) # Make sure it doesn't fail with negative values @test_throws ErrorException CRRAUtility(1.0) # Test error throwing at ϕ=1.0 end