Skip to content
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ HypergeometricFunctions = "0.3.10"
InverseFunctions = "0.1"
IrrationalConstants = "0.1, 0.2"
JET = "0.9, 0.10, 0.11"
TestItemRunner = "1"
LogExpFunctions = "0.3.26"
Random = "<0.0.1, 1"
Reexport = "1"
Expand All @@ -46,6 +47,7 @@ InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"

[targets]
test = ["Aqua", "ChainRulesCore", "ChainRulesTestUtils", "ExplicitImports", "ForwardDiff", "InverseFunctions", "JET", "Random", "Test"]
test = ["Aqua", "ChainRulesCore", "ChainRulesTestUtils", "ExplicitImports", "ForwardDiff", "InverseFunctions", "JET", "Random", "Test", "TestItemRunner"]
106 changes: 54 additions & 52 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
using StatsFuns, Test
using ChainRulesCore
using ChainRulesTestUtils
using Random

@testset "chainrules" begin
x = exp(randn())
y = exp(randn())
z = logistic(randn())
test_frule(betalogpdf, x, y, z)
test_rrule(betalogpdf, x, y, z)

x = exp(randn())
y = exp(randn())
z = exp(randn())
test_frule(gammalogpdf, x, y, z)
test_rrule(gammalogpdf, x, y, z)

x = exp(randn())
y = exp(randn())
test_frule(chisqlogpdf, x, y)
test_rrule(chisqlogpdf, x, y)

x = exp(randn())
y = exp(randn())
z = exp(randn())
test_frule(fdistlogpdf, x, y, z)
test_rrule(fdistlogpdf, x, y, z)

x = exp(randn())
y = randn()
test_frule(tdistlogpdf, x, y)
test_rrule(tdistlogpdf, x, y)

x = rand(1:100)
y = logistic(randn())
z = rand(1:x)
test_frule(binomlogpdf, x, y, z)
test_rrule(binomlogpdf, x, y, z)

x = exp(randn())
y = rand(1:100)
test_frule(poislogpdf, x, y)
test_rrule(poislogpdf, x, y)

# test special case λ = 0
_, pb = rrule(poislogpdf, 0.0, 0)
_, x̄1, _ = pb(1)
@test x̄1 == -1
_, pb = rrule(poislogpdf, 0.0, 1)
_, x̄1, _ = pb(1)
@test x̄1 == Inf
@testitem "ChainRules" begin
using StatsFuns, Test
using ChainRulesCore
using ChainRulesTestUtils
using Random

@testset "chainrules" begin
x = exp(randn())
y = exp(randn())
z = logistic(randn())
test_frule(betalogpdf, x, y, z)
test_rrule(betalogpdf, x, y, z)

x = exp(randn())
y = exp(randn())
z = exp(randn())
test_frule(gammalogpdf, x, y, z)
test_rrule(gammalogpdf, x, y, z)

x = exp(randn())
y = exp(randn())
test_frule(chisqlogpdf, x, y)
test_rrule(chisqlogpdf, x, y)

x = exp(randn())
y = exp(randn())
z = exp(randn())
test_frule(fdistlogpdf, x, y, z)
test_rrule(fdistlogpdf, x, y, z)

x = exp(randn())
y = randn()
test_frule(tdistlogpdf, x, y)
test_rrule(tdistlogpdf, x, y)

x = rand(1:100)
y = logistic(randn())
z = rand(1:x)
test_frule(binomlogpdf, x, y, z)
test_rrule(binomlogpdf, x, y, z)

x = exp(randn())
y = rand(1:100)
test_frule(poislogpdf, x, y)
test_rrule(poislogpdf, x, y)

# test special case λ = 0
_, pb = rrule(poislogpdf, 0.0, 0)
_, x̄1, _ = pb(1)
@test x̄1 == -1
_, pb = rrule(poislogpdf, 0.0, 1)
_, x̄1, _ = pb(1)
@test x̄1 == Inf
end
end
74 changes: 37 additions & 37 deletions test/generic.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
using StatsFuns
using ForwardDiff: Dual
using Test

include("utils.jl")

function check_dualx(statsfun, params, a, isprob, rtol)
v = @inferred(statsfun(params..., a))
rv = @inferred(statsfun(params..., Dual(a))).value
@test v isa float(Base.promote_typeof(params..., a))
@test rv isa float(Base.promote_typeof(params..., a))
return if isprob
@test v ≈ rv rtol = rtol nans = true
else
@test v ≈ rv atol = rtol rtol = rtol nans = true
@testitem "Generic" setup=[Utils] begin
using StatsFuns
using ForwardDiff: Dual
using Test

_default_rtol = Utils._default_rtol

function check_dualx(statsfun, params, a, isprob, rtol)
v = @inferred(statsfun(params..., a))
rv = @inferred(statsfun(params..., Dual(a))).value
@test v isa float(Base.promote_typeof(params..., a))
@test rv isa float(Base.promote_typeof(params..., a))
return if isprob
@test v ≈ rv rtol = rtol nans = true
else
@test v ≈ rv atol = rtol rtol = rtol nans = true
end
end
end

function genericcomp(basename::String, params, X::AbstractArray, rtol = _default_rtol(params, X))
if isdefined(@__MODULE__, Symbol(basename, :pdf))
stats_pdf = getproperty(@__MODULE__, Symbol(basename, :pdf))
@testset "pdf with params=$params, x=$x" for x in X
check_dualx(stats_pdf, params, x, true, rtol)
function genericcomp(basename::String, params, X::AbstractArray, rtol = _default_rtol(params, X))
if isdefined(@__MODULE__, Symbol(basename, :pdf))
stats_pdf = getproperty(@__MODULE__, Symbol(basename, :pdf))
@testset "pdf with params=$params, x=$x" for x in X
check_dualx(stats_pdf, params, x, true, rtol)
end
end
end

if isdefined(@__MODULE__, Symbol(basename, :logpdf))
stats_logpdf = getproperty(@__MODULE__, Symbol(basename, :logpdf))
@testset "logpdf with params=$params, x=$x" for x in X
check_dualx(stats_logpdf, params, x, false, rtol)
if isdefined(@__MODULE__, Symbol(basename, :logpdf))
stats_logpdf = getproperty(@__MODULE__, Symbol(basename, :logpdf))
@testset "logpdf with params=$params, x=$x" for x in X
check_dualx(stats_logpdf, params, x, false, rtol)
end
end
end

return nothing
end
return nothing
end

function genericcomp_tests(basename::String, configs)
println("\ttesting $basename ...")
for (params, data) in configs
genericcomp(basename, params, data)
function genericcomp_tests(basename::String, configs)
println("\ttesting $basename ...")
for (params, data) in configs
genericcomp(basename, params, data)
end
return
end
return
end

### Test cases
### Test cases

@testset "Generic" begin
genericcomp_tests(
"beta", [
((1.0, 1.0), 0.01:0.01:0.99),
Expand Down
20 changes: 11 additions & 9 deletions test/inverse.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using StatsFuns
using Test
using InverseFunctions
@testitem "Inverse" begin
using StatsFuns
using Test
using InverseFunctions

@testset "inverse" begin
x = 0.7
InverseFunctions.test_inverse(norminvcdf, x)
InverseFunctions.test_inverse(norminvccdf, x)
InverseFunctions.test_inverse(norminvlogcdf, log(x))
InverseFunctions.test_inverse(norminvlogccdf, log(x))
@testset "inverse" begin
x = 0.7
InverseFunctions.test_inverse(norminvcdf, x)
InverseFunctions.test_inverse(norminvccdf, x)
InverseFunctions.test_inverse(norminvlogcdf, log(x))
InverseFunctions.test_inverse(norminvlogccdf, log(x))
end
end
Loading
Loading