Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix and test call to fit_median_quantile #28

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
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;
Loading