Skip to content

Commit

Permalink
implement shifloNormal
Browse files Browse the repository at this point in the history
  • Loading branch information
bgctw committed Jan 31, 2022
1 parent 972c88b commit 4cc9dee
Show file tree
Hide file tree
Showing 10 changed files with 506 additions and 9 deletions.
7 changes: 0 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,3 @@ StatsBase = "0.33"
StatsFuns = "0.9"
julia = "1.6"

[extras]
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random", "Optim"]
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[deps]
DistributionFits = "45214091-1ed4-4409-9bcf-fdb48a05e921"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Expand Down
8 changes: 8 additions & 0 deletions docs/src/logitnormal.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ end
p # hide
```

A shifted and scaled version of this distribution can be
used as a moother alternative to the Bounded uniform distribution.

```@docs
shifloNormal
```




39 changes: 39 additions & 0 deletions inst/flat.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# determine the steepest part of the most spread flat logitnorm

using Optim
using StatsFuns
using DistributionFits
using StatsPlots

dln = fit_mode_flat(LogitNormal, 0.5)

# Sympy
# x = symbols('x', nonnegative=True, real=True)
# lx = ln(x/(1-x))
# lnormx = 1/(2*sqrt(2*pi)) * 1/(x*(1-x)) * exp(-1/4*lx*lx)
# dlnormx = simplify(diff(lnormx,x))
# ddlnormx = simplify(diff(dlnormx,x))
# ccode(dlnormx)

# second derivative via sympy
pow(b,e) = b^e
ddlogit(x) = 1.0/16.0*sqrt(2)*(24*pow(x,2)12*x*log(x/(x1))24*x+pow(log(x/(x1)),2)+6*log(x/(x1))+6)*exp(1.0/4.0*pow(log(x/(x1)),2))/(sqrt(pi)*pow(x,3)*(pow(x,3)3*pow(x,2)+3*x1))

# first derivative via numpy
dlogit(x) = (1.0/8.0)*sqrt(2)*(4*xlog(x/(x1))2)*exp(1.0/4.0*pow(log(x/(x1)),2))/(sqrt(pi)*pow(x,2)*pow(x1,2))


ansopt2 = optimize(dlogit, 0.9, 1)
ansopt2
xms = ansopt2.minimizer

x = 0.7:0.002:1.0
x = x[1:(end-1)]
plot(x, dlogit.(x))
vline!([xms])

plot(x, pdf.(dln,x))
vline!([xms])



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

# LogitNormal
export fit_mode_flat
export fit_mode_flat, shifloNormal

# dependency inversion: need to define DistributionFits.optimize by user
export AbstractDistributionFitOptimizer, optimize
Expand Down
13 changes: 13 additions & 0 deletions src/univariate/continuous/logitnormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ function of_mode_flat(x, m, logitm = logit(m))
d*d
end

"""
shifloNormal(lower,upper)
Get a Shifted Flat LogitNormal distribution that is most spread
with an extent between lower and upper.
This is a more smooth alternative to the bounded uniform distribution.
"""
function shifloNormal(lower,upper)
lower,upper = promote(lower,upper/1)
dln = LogitNormal{typeof(lower)}(0.0, sqrt(2))
LocationScale(lower, (upper -lower), dln)
end




Expand Down
2 changes: 2 additions & 0 deletions src/univariate/continuous/lognormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@ function fit(::Type{LogNormal}, mean, σstar::AbstractΣstar)
LogNormal(μ, σ)
end



Loading

0 comments on commit 4cc9dee

Please sign in to comment.