From 5b9127aead7220a5602693fc32104ccd0647e8d5 Mon Sep 17 00:00:00 2001 From: Oskar Laverny Date: Mon, 30 Oct 2023 18:52:29 +0100 Subject: [PATCH] Adding test for random generation + fixed bug --- Project.toml | 2 +- src/WilliamsonTransforms.jl | 6 ++++-- test/testing_the_paper.jl | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index a9f55fd..0b0b0c1 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "WilliamsonTransforms" uuid = "48feb556-9bdd-43a2-8e10-96100ec25e22" authors = ["Oskar Laverny and contributors"] -version = "0.0.1" +version = "0.0.2" [deps] Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" diff --git a/src/WilliamsonTransforms.jl b/src/WilliamsonTransforms.jl index 2d24014..ee6ca60 100644 --- a/src/WilliamsonTransforms.jl +++ b/src/WilliamsonTransforms.jl @@ -101,7 +101,9 @@ function Distributions.cdf(d::𝒲₋₁, x::Real) c_Ο• = taylor(d.Ο•, x, d.d, typeof(x)) c_Ο•[end] = max(c_Ο•[end], 0) for k in 0:(d.d-1) - rez += (-1)^k * x^k * c_Ο•[k+1] + if c_Ο•[k+1] != 0 # We need c_Ο• = 0 to dominate x = Inf + rez += (-1)^k * x^k * c_Ο•[k+1] + end end return 1-rez end @@ -112,6 +114,6 @@ function Distributions.logpdf(d::𝒲₋₁, x::Real) end function Distributions.rand(rng::Distributions.AbstractRNG, d::𝒲₋₁) u = rand(rng) - Roots.find_zero(x -> (Distributions.cdf(d,x) - u), (0, Inf)) + Roots.find_zero(x -> (Distributions.cdf(d,x) - u), (0.0, Inf)) end end diff --git a/test/testing_the_paper.jl b/test/testing_the_paper.jl index 1fdb39b..523c700 100644 --- a/test/testing_the_paper.jl +++ b/test/testing_the_paper.jl @@ -5,6 +5,8 @@ Ο•(x, d) = max((1-x)^(d-1),zero(x)) Xhat = 𝒲₋₁(x -> Ο•(x,d),d) Ο•hat = 𝒲(X,d) + + rand(Xhat,10) @test maximum(abs.([cdf(X,x) - cdf(Xhat,x) for x in 0:0.01:10*d])) <= sqrt(eps(Float64)) @test maximum(abs.([Ο•(x, d) - Ο•hat(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) @@ -17,6 +19,8 @@ end Ο•(x) = exp(-x) Xhat = 𝒲₋₁(Ο•,d) Ο•hat = 𝒲(X,d) + + rand(Xhat,10) @test maximum(abs.([cdf(X,x) - cdf(Xhat,x) for x in 0:0.01:3*d])) <= sqrt(eps(Float64)) @test maximum(abs.([Ο•(x) - Ο•hat(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) @@ -65,6 +69,7 @@ end (2, -1.0) ) Xhat = 𝒲₋₁(x -> Ο•(x,ΞΈ),d) + rand(Xhat,10) @test maximum(abs.([F(x,ΞΈ,d) - cdf(Xhat,x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) end