Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test source with JET #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ julia = "1.3"
[extras]
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ChainRulesTestUtils", "ForwardDiff", "Random", "Test"]
test = ["ChainRulesTestUtils", "ForwardDiff", "JET", "Random", "Test"]
22 changes: 12 additions & 10 deletions src/tvpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Original source available from
# http://www.math.wsu.edu/faculty/genz/software/fort77/tvpack.f

function tvtcdf(nu::Int, h::Vector{Float64}, r::Vector{Float64})
function tvtcdf(nu::Int, h::NTuple{3,Float64}, r::NTuple{3,Float64})
pt = 0.5 * pi
h1 = h[1]
h2 = h[2]
Expand Down Expand Up @@ -128,10 +128,10 @@ end
# # One Dimensional Globally Adaptive Integration Function

function adonet(f::Function, a::Float64, b::Float64, tol::Float64)
ai = Vector{Float64}(100)
bi = Vector{Float64}(100)
ei = Vector{Float64}(100)
fi = Vector{Float64}(100)
ai = Vector{Float64}(undef, 100)
bi = Vector{Float64}(undef, 100)
ei = Vector{Float64}(undef, 100)
fi = Vector{Float64}(undef, 100)
ai[1] = a
bi[1] = b
err = 1.0
Expand Down Expand Up @@ -242,7 +242,9 @@ end

function bvtcdf(nu::Int, dh::Float64, dk::Float64, r::Float64)
if nu < 1
return bvnuppercdf(-dh, -dk, r)
# The source call the commented out line below. Instead we error out
throw(DomainError(nu, "degrees of freedom parameter must be positive"))
# return bvnuppercdf(-dh, -dk, r)
Comment on lines +245 to +247
Copy link
Member

Choose a reason for hiding this comment

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

That is, in tvpack.f this branch is supported? And we error since bvnuppercdf does not exist/was not copied?

Copy link
Member Author

Choose a reason for hiding this comment

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

We do have bvnuppercdf but I think it's weird behavior to call it when the degrees of freedom parameter is non-positive.

Copy link
Member

Choose a reason for hiding this comment

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

I see and I agree, it seems strange to support non-positive degrees. Maybe in tvpack it is just a way to encode the limit case of infinte degrees of freedom?

In any case, shouldn't tcdf be changed as well for consistency? That supports non-positive degrees of freedom as well it seems.

Copy link
Member Author

Choose a reason for hiding this comment

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

It should. I hadn't noticed that. Will push an update.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reading this again. The branch is for nu < 1. I'd read it as < 0. It doesn't makes sense at all to use the normal cdf when the degrees of freedom parameter is between zero and one. Not sure what the idea here was.

elseif 1.0 - r < eps()
return tcdf(nu, min(dh, dk))
elseif r + 1.0 < eps()
Expand All @@ -266,12 +268,12 @@ function bvtcdf(nu::Int, dh::Float64, dk::Float64, r::Float64)
hs = sign(hrk)
ks = sign(krh)
if mod(nu, 2) == 0
bvt = atan2(sqrt(ors), -r) / (2.0pi)
bvt = atan(sqrt(ors), -r) / (2.0pi)
gmph = dh / sqrt(16.0 * (nu + dh^2))
gmpk = dk / sqrt(16.0 * (nu + dk^2))
btnckh = 2.0atan2(sqrt(xnkh), sqrt(1.0 - xnkh)) / pi
btnckh = 2.0atan(sqrt(xnkh), sqrt(1.0 - xnkh)) / pi
btpdkh = 2.0sqrt(xnkh * (1.0 - xnkh)) / pi
btnchk = 2.0atan2(sqrt(xnhk), sqrt(1.0 - xnhk)) / pi
btnchk = 2.0atan(sqrt(xnhk), sqrt(1.0 - xnhk)) / pi
btpdhk = 2.0sqrt(xnhk * (1.0 - xnhk)) / pi
for j = 1:div(nu, 2)
bvt += gmph * (1.0 + ks * btnckh)
Expand All @@ -288,7 +290,7 @@ function bvtcdf(nu::Int, dh::Float64, dk::Float64, r::Float64)
hkrn = dh*dk + r*nu
hkn = dh*dk - nu
hpk = dh + dk
bvt = atan2(-snu * (hkn * qhrk + hpk * hkrn), hkn * hkrn - nu * hpk * qhrk) / (2.0pi)
bvt = atan(-snu * (hkn * qhrk + hpk * hkrn), hkn * hkrn - nu * hpk * qhrk) / (2.0pi)
if bvt < -eps()
bvt += 1.0
end
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ for t in tests
println("* running $fp")
include(fp)
end

if VERSION >= v"1.7"
using JET
@testset "Static analysis" begin
@test isempty(JET.get_reports(report_package("StatsFuns", target_modules=(StatsFuns,))))
Copy link
Member

Choose a reason for hiding this comment

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

One could also use JET.test_package, e.g.,

Suggested change
@test isempty(JET.get_reports(report_package("StatsFuns", target_modules=(StatsFuns,))))
JET.test_package("StatsFuns", target_defined_modules=true)

Copy link
Member

Choose a reason for hiding this comment

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

Or

Suggested change
@test isempty(JET.get_reports(report_package("StatsFuns", target_modules=(StatsFuns,))))
JET.test_package("StatsFuns", target_defined_modules=true)

end
end
11 changes: 11 additions & 0 deletions test/tvpack.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using StatsFuns, Test
using StatsFuns: bvtcdf

@testset "bvncdf: bivariate normal cdf" begin

Expand Down Expand Up @@ -28,3 +29,13 @@ using StatsFuns, Test
@test !isnan(bvncdf(x,y,r))
end
end

@testset "bvtcdf: bivariate T CDF" begin
# Tested against R's mvtnorm::pmvt
@test_throws DomainError bvtcdf(-1, 1.0, 1.0, 0.5)
@test bvtcdf(2, 1.0, 1.0, 1.0) ≈ 0.7886751345948129
@test bvtcdf(2, 1.0, 1.0, -1.0) ≈ 0.5773502691896257
@test bvtcdf(2, 1.0, -1.0, -1.0) == 0.0
@test bvtcdf(2, 1.0, 1.0, 0.5) ≈ 0.6811385938470963
@test bvtcdf(3, 1.0, 1.0, 0.5) ≈ 0.7001771920356403
end