From 4912bd030ded173fb0267785e9a84e0f507df2d8 Mon Sep 17 00:00:00 2001 From: Thomas Wutzler Date: Wed, 25 Oct 2023 17:04:33 +0200 Subject: [PATCH] fix and test call to fit_median_quantile (#28) * fix and test call to fit_median_quantile * exclude JET test prior to version 1.9.2 --- src/fitstats.jl | 1 + src/univariate/continuous/logitnormal.jl | 10 ++++++---- test/Project.toml | 1 + test/runtests.jl | 14 ++++++++++++++ test/univariate/continuous/lognormal.jl | 12 ++++++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/fitstats.jl b/src/fitstats.jl index 5b05c60..e237fcd 100644 --- a/src/fitstats.jl +++ b/src/fitstats.jl @@ -203,6 +203,7 @@ macro qs_cf95(q0_025, q0_975) end # The non-macro versions return percentile whose type matches that of the argument +qp(q,p) = QuantilePoint(q, p) qp_ll(q0_025::T) where {T} = QuantilePoint(q0_025, T(0.025)) qp_l(q0_05::T) where {T} = QuantilePoint(q0_05, T(0.05)) qp_m(median::T) where {T} = QuantilePoint(median, T(0.5)) diff --git a/src/univariate/continuous/logitnormal.jl b/src/univariate/continuous/logitnormal.jl index b97f146..3cd38cc 100644 --- a/src/univariate/continuous/logitnormal.jl +++ b/src/univariate/continuous/logitnormal.jl @@ -36,12 +36,12 @@ function fit_mode_quantile(::Type{LogitNormal}, fit_mode_quantile(LogitNormal{T}, mode, qp) end function fit_mode_quantile(::Type{LogitNormal{T}}, mode::Real, qp::QuantilePoint) where {T} - matchModeUpper(T(mode), qp.q, Val(40); perc = qp.p) + matchModeUpper(T(mode), qp, Val(40)) end -function matchModeUpper(mode::T, upper, ::Val{nTry} - ; perc::Real = 0.99) where {nTry, T <: Real} - mode == 0.5 && return matchMedianUpper(LogitNormal, 0.5, upper; perc = perc) +function matchModeUpper(mode::T, qp::QuantilePoint, ::Val{nTry}) where {nTry, T <: Real} + # for symmetric - same as fitting median + mode == 0.5 && return fit_median_quantile(LogitNormal, mode, qp) # for given mu we can compute sigma by mode and upper quantile # hence univariate search for mu # we now that mu is in (\code{logit(mode)},0) for \code{mode < 0.5} and in @@ -50,6 +50,8 @@ function matchModeUpper(mode::T, upper, ::Val{nTry} # hence, first get near the global minimum by a evaluating the cost at a # grid that is spaced narrower at the edge # + upper = qp.q + perc = qp.p logitMode = logit(mode) logitUpper = logit(upper) upperMu = abs(logitMode) - eps() diff --git a/test/Project.toml b/test/Project.toml index da290b9..670df11 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,5 +1,6 @@ [deps] Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" Optim = "429524aa-4258-5aef-a3af-852621145aeb" QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc" diff --git a/test/runtests.jl b/test/runtests.jl index 1c3aebf..5511cff 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,12 @@ using Test using Random: Random using LoggingExtras +#using Aqua; Aqua.test_all(DistributionFits) # ambiguities from other packages +#using JET; JET.report_package(DistributionFits) # +#invalid possible error due to quantile may accept/return an Array (we pass a scalar) +#only report problems in this module: +#using JET; JET.report_package(DistributionFits; target_modules=(@__MODULE__,)) # + @testset "optimize error" begin @test_throws Exception DistributionFits.optimize(x -> x * x, -1, 1) end @@ -37,3 +43,11 @@ include("univariate/test_univariate.jl") println("Potentially stale exports: ") display(Test.detect_ambiguities(DistributionFits)) println() + +using JET: JET +@testset "JET" begin + @static if VERSION ≥ v"1.9.2" + JET.test_package(DistributionFits; target_modules=(@__MODULE__,)) # + end +end; + diff --git a/test/univariate/continuous/lognormal.jl b/test/univariate/continuous/lognormal.jl index 972d7e3..7700af0 100644 --- a/test/univariate/continuous/lognormal.jl +++ b/test/univariate/continuous/lognormal.jl @@ -37,3 +37,15 @@ end; # # plot(d); plot!(dfit32) end; + +@testset "fit_mode_quantile_0.5" begin + # special for LogitNormal mode + d = fit_mode_quantile(LogNormal, 0.5, @qp(0.9, 0.95)) + @test mode(d) ≈ 0.5 + @test quantile(d, 0.95) ≈ 0.9 + # + dfit32 = fit_mode_quantile(LogNormal, 0.5f0, @qp(0.9, 0.95)) + @test partype(dfit32) === Float32 + @test mode(d) ≈ 0.5 + @test quantile(d, 0.95) ≈ 0.9 +end;