Skip to content
Merged
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ ThickNumbers = "b57aa878-5b76-4266-befc-f8e007760995"

[compat]
SpecialFunctions = "2"
Statistics = "1"
ThickNumbers = "1"
julia = "1"

[extras]
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["HypothesisTests", "Test"]
test = ["HypothesisTests", "Statistics", "Test"]
3 changes: 1 addition & 2 deletions src/GaussianRandomVariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ max(a::GVar, b::Real) = max(b, a)

function Base.exp(a::GVar{<:AbstractFloat})
isempty(a) && return a
return GVar(meanvar(exp, exp, exp, a.center, a.σ)...)
return GVar(exp(a.center + a.σ^2/2), exp(a.center + a.σ^2)*sqrt(1 - exp(-a.σ^2)))
end
Base.exp(a::GVar) = exp(float(a))

function Base.log(a::GVar{<:AbstractFloat})
isempty(a) && return a
Expand Down
27 changes: 17 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
using GaussianRandomVariables
using ThickNumbers
using Statistics
using HypothesisTests
using Test

function testscalar(f, μ, σ; n=1000, pval = 0.001)
x = μ .+ σ .* randn(n)
function testscalar(f, μ, σ; n=1000, pval = 0.001, filter=Returns(true), rtol=nothing)
x = Base.filter(filter, μ .+ σ .* randn(n))
@assert length(x) >= 0.9 * n
y = f.(x)
g = GVar(μ, σ)
fg = f(g)
z = mid(fg) .+ rad(fg) .* randn(n)
return pvalue(EqualVarianceTTest(y, z)) > pval && pvalue(LeveneTest(y, z)) > pval
if rtol === nothing
z = mid(fg) .+ rad(fg) .* randn(n)
return pvalue(EqualVarianceTTest(y, z)) > pval && pvalue(LeveneTest(y, z)) > pval
end
return isapprox(mean(y), mid(fg); rtol) && isapprox(std(y), rad(fg); rtol)
end

ispositive(x) = x > 0

@testset "GaussianRandomVariables.jl" begin
x = 3 ± 1
e = GVar(0, -1) # empty
Expand All @@ -27,14 +34,14 @@ end
@test rad(x - x) ≈ sqrt(2)
@test mid(1/x) ≈ 1/mid(x) + rad(x)^2/mid(x)^3
@test rad(1/x)^2 ≈ rad(x)^2 / mid(x)^4 + 2 * rad(x)^4 / mid(x)^6
@test sqrt(x) ⩪ exp(0.5 * log(x)) rtol=1e-3
@test sqrt(x) ⩪ exp(0.5 * log(x)) rtol=1e-2
@test sqrt(x) ⩪ x^0.5

for μ in (2, 3, 10), σ in (0.25,)
for μ in (2, 3, 10), σ in (0.1, 0.25, 1)
@test testscalar(x -> x^2, μ, σ)
@test testscalar(x -> x^1.8, μ, σ)
@test testscalar(exp, μ, σ)
@test testscalar(log, μ, σ)
@test testscalar(sqrt, μ, σ)
@test testscalar(x -> x^1.8, μ, σ; filter=ispositive)
@test testscalar(exp, μ, σ; rtol=0.03, n=10^6)
μ - 3*σ >= 0 && @test testscalar(log, μ, σ; filter=ispositive)
@test testscalar(sqrt, μ, σ; filter=ispositive)
end
end
Loading