Skip to content

Commit

Permalink
fix and test call to fit_median_quantile (#28)
Browse files Browse the repository at this point in the history
* fix and test call to fit_median_quantile

* exclude JET test prior to version 1.9.2
  • Loading branch information
bgctw authored Oct 25, 2023
1 parent db76e41 commit 4912bd0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/fitstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 6 additions & 4 deletions src/univariate/continuous/logitnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

12 changes: 12 additions & 0 deletions test/univariate/continuous/lognormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 4912bd0

Please sign in to comment.