Skip to content
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
4 changes: 4 additions & 0 deletions docs/src/logitnormal.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ p # hide
A shifted and scaled version of this distribution can be
used as a moother alternative to the Bounded uniform distribution.

When no information on mode or peakedness is required, prefer the option
without mode, because those does not require optimization.

```@docs
shifloNormal
shifloNormal_mode
```


Expand Down
2 changes: 1 addition & 1 deletion docs/src/partype.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ true

If the parametric type is omitted, default Float64 is assumed, or inferred from
other parameters of the fitting function.
Since quantiles and median are rather sample-like measures than paraemter-like
Since quantiles and median are rather sample-like measures than parameter-like
measures, they do not influence the inferred parameter type.


Expand Down
2 changes: 1 addition & 1 deletion src/DistributionFits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export
export AbstractΣstar, Σstar, σstar, ScaledLogNormal

# LogitNormal
export fit_mode_flat, shifloNormal
export fit_mode_flat, shifloNormal, shifloNormal_mode

# dependency inversion: need to define DistributionFits.optimize by user
export AbstractDistributionFitOptimizer, optimize
Expand Down
25 changes: 22 additions & 3 deletions src/univariate/continuous/logitnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ true
"""
function fit_mode_flat(::Type{LogitNormal},
mode::T,
::Val{nTry} = Val(40);
ntry::Val{nTry} = Val(40);
peakedness = 1) where {T <: Real, nTry}
fit_mode_flat(LogitNormal{T}, mode, Val(nTry); peakedness)
fit_mode_flat(LogitNormal{T}, mode, ntry; peakedness)
end
function fit_mode_flat(::Type{LogitNormal{T}},
mode,
::Val{nTry} = Val(40);
ntry::Val{nTry} = Val(40);
peakedness = 1) where {T <: Real, nTry}
mode == 0.5 && return (LogitNormal{T}(zero(T), sqrt(2) / peakedness))
is_right = mode > 0.5
Expand Down Expand Up @@ -158,3 +158,22 @@ function shifloNormal(lower, upper)
#dln * (upper-lower) + lower
Distributions.AffineDistribution{T}(lower, (upper - lower), dln)
end

"""
shifloNormal_mode(lower,upper,mode; peakedness=1.1)

Shifted LogitNormal where mode and peakedness are specified.
Note, that this requires optimization, while shifloNormal without mode
is a closed form solution.
"""
function shifloNormal_mode(lower, upper, mode,
ntry::Val{nTry} = Val(40);
peakedness = 1.0) where {nTry}
lower, upper, mode = promote(lower, upper, mode / 1)
T = typeof(lower)
mode1 = (mode-lower)/(upper-lower)
dln = fit_mode_flat(LogitNormal{T}, mode1, ntry; peakedness)
#dln * (upper-lower) + lower
Distributions.AffineDistribution{T}(lower, (upper - lower), dln)
end

21 changes: 21 additions & 0 deletions test/univariate/continuous/logitnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,24 @@ end;
#plot(d)
#dln = LogitNormal(0f0,sqrt(2f0)); plot!(dln)
end;

@testset "shifloNormal_mode Float32" begin
d32 = shifloNormal_mode(1.0f0, 3.0f0, 1.5f0)
@test partype(d32) == Float32
@test mean(d32) isa Float32
# wait until fix in AffineDistribution is merged to Distributions.jl
# @test rand(d32) isa eltype(d32)
m = mode(d32)
@test m isa Float32
@test isapprox(m, 1.5, atol = 0.1)
dmin = minimum(d32)
@test dmin == 1.0
@test maximum(d32) == 3.0
@test scale(d32) == 3 - 1
@test scale(d32) isa Float32
@test location(d32) == 1.0
@test location(d32) isa Float32
#using StatsPlots
#plot(d32)
#dln = LogitNormal(0f0,sqrt(2f0)); plot!(dln)
end;
Loading