|
| 1 | +using StatsFuns |
| 2 | + |
1 | 3 | @testset "Rician" begin |
2 | 4 |
|
3 | 5 | d1 = Rician(0.0, 10.0) |
|
9 | 11 | @test eltype(d1) === Float64 |
10 | 12 | @test rand(d1) isa Float64 |
11 | 13 |
|
| 14 | + # Reference: WolframAlpha |
| 15 | + @test @inferred(mean(d1))::Float64 ≈ 5 * sqrt(2 * π) |
| 16 | + @test @inferred(std(d1))::Float64 ≈ sqrt(200 - 50 * π) |
| 17 | + @test @inferred(var(d1))::Float64 ≈ 200 - 50 * π |
| 18 | + @test @inferred(mode(d1))::Float64 ≈ 10 |
| 19 | + ps = 0:0.1:1 |
| 20 | + @test all(splat(isapprox), zip(quantile.(d1, ps), @. sqrt(-200 * log1p(-ps)))) |
| 21 | + @test all(splat(isapprox), zip(cquantile.(d1, ps), @. sqrt(-200 * log(ps)))) |
| 22 | + @test all(splat(isapprox), zip(invlogcdf.(d1, log.(ps)), @. sqrt(-200 * log1p(-ps)))) |
| 23 | + @test all(splat(isapprox), zip(invlogccdf.(d1, log.(ps)), @. sqrt(-200 * log(ps)))) |
| 24 | + xs = 0:0.5:30 # 99th percentile is approx 30 |
| 25 | + @test all(splat(isapprox), zip(pdf.(d1, xs), map(x -> x / 100 * exp(-x^2/200), xs))) |
| 26 | + @test all(splat(isapprox), zip(logpdf.(d1, xs), map(x -> log(x / 100) - x^2/200, xs))) |
| 27 | + @test all(splat(isapprox), zip(cdf.(d1, xs), @. 1 - exp(-xs^2/200))) |
| 28 | + @test all(splat(isapprox), zip(logcdf.(d1, xs), @. log1mexp(-xs^2/200))) |
| 29 | + @test all(splat(isapprox), zip(ccdf.(d1, xs), @. exp(-xs^2/200))) |
| 30 | + @test all(splat(isapprox), zip(logccdf.(d1, xs), @. -xs^2/200)) |
| 31 | + |
12 | 32 | d2 = Rayleigh(10.0) |
13 | 33 | @test mean(d1) ≈ mean(d2) |
14 | 34 | @test var(d1) ≈ var(d2) |
|
23 | 43 | x = Base.Fix1(quantile, d1).([0.25, 0.45, 0.60, 0.80, 0.90]) |
24 | 44 | @test all(Base.Fix1(cdf, d1).(x) .≈ [0.25, 0.45, 0.60, 0.80, 0.90]) |
25 | 45 |
|
26 | | - x = rand(Rician(5.0, 5.0), 100000) |
| 46 | + # Reference: WolframAlpha |
| 47 | + @test mean(d1) ≈ 15.4857246055 |
| 48 | + @test std(d1) ≈ 7.75837182934 |
| 49 | + @test var(d1) ≈ 60.1923334423 |
| 50 | + @test median(d1) ≈ 14.7547909179 |
| 51 | + @test all(xy -> isapprox(xy...; rtol=1e-5), zip(quantile.(d1, [0.1, 0.25, 0.5, 0.75, 0.9]), [5.86808, 9.62923, 14.7548, 20.5189, 26.0195])) |
| 52 | + |
| 53 | + d1 = Rician(5.0, 5.0) |
| 54 | + # Reference: WolframAlpha |
| 55 | + @test mean(d1) ≈ 7.7428623027557 |
| 56 | + @test std(d1) ≈ 3.8791859146687 |
| 57 | + @test var(d1) ≈ 15.0480833605606 |
| 58 | + @test median(d1) ≈ 7.37739545894 |
| 59 | + @test all(xy -> isapprox(xy...; rtol=1e-5), zip(quantile.(d1, [0.1, 0.25, 0.5, 0.75, 0.9]), [2.93404, 4.81462, 7.3774, 10.2595, 13.0097])) |
| 60 | + |
| 61 | + x = rand(d1, 100000) |
27 | 62 | d1 = fit(Rician, x) |
28 | 63 | @test d1 isa Rician{Float64} |
29 | 64 | @test params(d1)[1] ≈ 5.0 atol=0.2 |
|
37 | 72 | @test partype(d1) === Float32 |
38 | 73 | @test eltype(d1) === Float64 |
39 | 74 | @test rand(d1) isa Float64 |
| 75 | + d1_f64 = Rician(10.0, 10.0) |
| 76 | + @test @inferred(mean(d1))::Float32 ≈ Float32(mean(d1_f64)) |
| 77 | + @test @inferred(std(d1))::Float32 ≈ Float32(std(d1_f64)) |
| 78 | + @test @inferred(var(d1))::Float32 ≈ Float32(var(d1_f64)) |
| 79 | + @test @inferred(median(d1))::Float32 ≈ Float32(median(d1_f64)) |
40 | 80 |
|
41 | 81 | d1 = Rician() |
42 | 82 | @test d1 isa Rician{Float64} |
|
94 | 134 | @inferred logcdf(d1, 1//2) |
95 | 135 | @inferred logcdf(d1, Inf) |
96 | 136 |
|
| 137 | + # issue #2035 |
| 138 | + # reference values computed with WolframAlpha |
| 139 | + d = Rician(1.0, 0.12) |
| 140 | + @test mean(d) ≈ 1.00722650704 |
| 141 | + @test std(d) ≈ 0.119560710568 |
| 142 | + @test var(d) ≈ 0.0142947635114 |
| 143 | + @test median(d) ≈ 1.00719144719 |
| 144 | + @test pdf(d, 0.5) ≈ 0.000400758853946 |
| 145 | + @test pdf(d, 1) ≈ 3.33055235263 |
| 146 | + @test pdf(d, 1.5) ≈ 0.000692437774661 |
| 147 | + @test pdf(d, 2) ≈ 3.91711741719e-15 |
| 148 | + @test logpdf(d, 0.5) ≈ -7.82215067328 |
| 149 | + @test logpdf(d, 1) ≈ 1.2031381619 |
| 150 | + @test logpdf(d, 1.5) ≈ -7.27529218003 |
| 151 | + @test logpdf(d, 2) ≈ -33.1734203644 |
97 | 152 | end |
0 commit comments