Skip to content

Commit

Permalink
Adding test for random generation + fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lrnv committed Oct 30, 2023
1 parent 91072c6 commit 5b9127a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "WilliamsonTransforms"
uuid = "48feb556-9bdd-43a2-8e10-96100ec25e22"
authors = ["Oskar Laverny <[email protected]> and contributors"]
version = "0.0.1"
version = "0.0.2"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
6 changes: 4 additions & 2 deletions src/WilliamsonTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions test/testing_the_paper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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

Expand Down

2 comments on commit 5b9127a

@lrnv
Copy link
Owner Author

@lrnv lrnv commented on 5b9127a Oct 30, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

BugFix

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

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

Please sign in to comment.