|
1 | 1 | using StatsFuns |
2 | 2 | using StatsFuns: RFunctions |
3 | 3 | using ForwardDiff: Dual |
| 4 | +using Test |
| 5 | + |
| 6 | +include("utils.jl") |
4 | 7 |
|
5 | 8 | function check_rmath(fname, statsfun, rmathfun, params, aname, a, isprob, rtol) |
6 | 9 | v = @inferred(rmathfun(params..., a)) |
7 | 10 | rv = @inferred(statsfun(params..., Dual(a))).value |
| 11 | + @test v isa float(Base.promote_typeof(params..., a)) |
| 12 | + @test rv isa float(Base.promote_typeof(params..., a)) |
8 | 13 | if isprob |
9 | | - rd = abs(v / rv - 1.0) |
10 | | - if rd > rtol |
11 | | - error("$fname deviates too much from Rmath at " * |
12 | | - "params = $params, $aname = $a:\n" * |
13 | | - " v = $v (rv = $rv)\n |v/rv - 1| = $rd > $rtol.") |
14 | | - end |
| 14 | + @test v ≈ rv rtol=rtol nans=true |
15 | 15 | else |
16 | | - τ = (1.0 + abs(rv)) * rtol |
17 | | - ad = abs(v - rv) |
18 | | - if ad > τ |
19 | | - error("$fname deviates too much from Rmath at " * |
20 | | - "params = $params, $aname = $a:\n" * |
21 | | - " v = $v (rv = $rv)\n |v - rv| = $ad > $τ.") |
22 | | - end |
| 16 | + @test v ≈ rv atol=rtol rtol=rtol nans=true |
23 | 17 | end |
24 | 18 | end |
25 | 19 |
|
26 | | -function genericcomp(basename, params, X::AbstractArray, rtol=100eps(float(one(eltype(X))))) |
| 20 | +function genericcomp(basename, params, X::AbstractArray, rtol=_default_rtol(params, X)) |
27 | 21 | pdf = string(basename, "pdf") |
28 | 22 | logpdf = string(basename, "logpdf") |
29 | 23 | stats_pdf = eval(Symbol(pdf)) |
30 | 24 | stats_logpdf = eval(Symbol(logpdf)) |
31 | 25 | rmath_pdf = eval(Meta.parse(string("RFunctions.", pdf))) |
32 | 26 | rmath_logpdf = eval(Meta.parse(string("RFunctions.", logpdf))) |
33 | | - for i = 1:length(X) |
34 | | - x = X[i] |
| 27 | + |
| 28 | + @testset "pdf with params=$params, x=$x" for x in X |
35 | 29 | check_rmath(pdf, stats_pdf, rmath_pdf, params, "x", x, true, rtol) |
| 30 | + end |
| 31 | + |
| 32 | + @testset "logpdf with params=$params, x=$x" for x in X |
36 | 33 | check_rmath(logpdf, stats_logpdf, rmath_logpdf, params, "x", x, false, rtol) |
37 | 34 | end |
38 | 35 | end |
|
121 | 118 |
|
122 | 119 | genericcomp_tests("pois", [ |
123 | 120 | ((1.0,), 0:30), |
124 | | - ((10.0,), 0:42) |
| 121 | + ((10.0,), 0:37), |
| 122 | + ((10.0,), 39:42), |
125 | 123 | ]) |
| 124 | + # Requires slightly larger tolerance: #157 |
| 125 | + genericcomp("pois", (10.0,), 38:38, 2.5e-14) |
126 | 126 |
|
127 | 127 | genericcomp_tests("tdist", [ |
128 | 128 | ((1,), -5.0:0.1:5.0), |
|
0 commit comments