Skip to content

Commit eb6a57d

Browse files
authored
implement and document fit_mean_Σ
additionally filter specific non-relevant warning messages with JET.test_package
1 parent a9c6509 commit eb6a57d

File tree

15 files changed

+279
-15
lines changed

15 files changed

+279
-15
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.3.7-DEV"
66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
88
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
9+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1011
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1112
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
@@ -28,6 +29,7 @@ Requires = "1.2"
2829
StaticArrays = "1.2"
2930
Statistics = "1"
3031
StatsAPI = "1.6"
32+
LinearAlgebra = "1.6"
3133
StatsFuns = "0.9.15, 1"
3234
julia = "1.6"
3335

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ DistributionFits = "45214091-1ed4-4409-9bcf-fdb48a05e921"
33
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
44
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
55
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
6+
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
67
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
78
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ makedocs(;
2929
"LogitNormal" => "logitnormal.md",
3030
"Weibull" => "weibull.md",
3131
"Gamma" => "gamma.md",
32+
"MvLogNormal" => "mvlognormal.md",
3233
],
3334
"Dependencies" => "set_optimize.md",
3435
"API" => "api.md",

docs/src/lognormal.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,29 @@ d = LogNormal(log(2), log(1.2))
2121
true
2222
```
2323

24-
Alternatively the distribution can be specified by its mean and ``\sigma^*`` using type [`AbstractΣstar`](@ref)
24+
Alternatively the distribution can be specified by its mean and either
25+
- Multiplicative standard deviation,``\sigma^*``, using type [`AbstractΣstar`](@ref)
26+
- Standard deviation at log-scale, ``\sigma``, or
27+
- relative error, ``cv``.
2528

2629
```jldoctest; output = false, setup = :(using DistributionFits,Optim)
2730
d = fit(LogNormal, 2, Σstar(1.2))
2831
(mean(d), σstar(d)) == (2, 1.2)
2932
# output
3033
true
3134
```
35+
```jldoctest; output = false, setup = :(using DistributionFits,Optim)
36+
d = fit_mean_Σ(LogNormal, 2, 1.2)
37+
(mean(d), d.σ) == (2, 1.2)
38+
# output
39+
true
40+
```
41+
```jldoctest; output = false, setup = :(using DistributionFits,Optim)
42+
d = fit_mean_relerror(LogNormal, 2, 0.2)
43+
(mean(d), std(d)/mean(d)) .≈ (2, 0.2)
44+
# output
45+
(true, true)
46+
```
3247

3348
## Detailed API
3449

@@ -37,7 +52,7 @@ true
3752
```
3853

3954
```@docs
40-
fit(::Type{LogNormal}, ::T, ::AbstractΣstar) where T<:Real
55+
fit(d::Type{LogNormal}, mean, σstar::AbstractΣstar)
4156
```
4257

4358
```@docs

docs/src/mvlognormal.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```@meta
2+
CurrentModule = DistributionFits
3+
```
4+
5+
# Multivariate LogNormal distribution
6+
7+
Can be fitted to a given mean, provided the Covariance of the underlying
8+
normal distribution.
9+
10+
```@docs
11+
fit_mean_Σ(::Type{MvLogNormal}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T1 <:Real,T2 <:Real}
12+
```
13+
14+
```jldoctest; output = false, setup = :(using DistributionFits)
15+
Σ = hcat([0.6,0.02],[0.02,0.7])
16+
μ = [1.2,1.3]
17+
d = MvLogNormal(μ, Σ)
18+
d2 = fit_mean_Σ(MvLogNormal, mean(d), Σ)
19+
isapprox(d2, d, rtol = 1e6)
20+
# output
21+
true
22+
```

src/DistributionFits.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using Reexport
55

66
using FillArrays, StaticArrays
77
using StatsFuns: logit, logistic, normcdf
8+
using LinearAlgebra
89
#using Infiltrator
910

1011
if !isdefined(Base, :get_extension)
@@ -26,7 +27,8 @@ export
2627
@qs_cf90, @qs_cf95,
2728
qp, qp_ll, qp_l, qp_m, qp_u, qp_uu,
2829
qs_cf90, qs_cf95,
29-
fit_mean_relerror
30+
fit_mean_relerror,
31+
fit_mean_Σ
3032

3133
# document but do not export - need to qualify by 'DistributionFits.'
3234
# export
@@ -53,5 +55,6 @@ end
5355
# fitting distributions to stats
5456
include("fitstats.jl")
5557
include("univariates.jl")
58+
include("multivariates.jl")
5659

5760
end

src/multivariate/mvlognormal.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
fit_mean_Σ(::Type{<:Distribution}, mean, Σ)
3+
4+
Fit a Distribution to mean and uncertainty quantificator Σ.
5+
6+
The meaning of `Σ` depends on the type of distribution:
7+
- `MvLogNormal`: the Covariancematrix of the associated normal distribution
8+
- `LogNormal`: the scale parameter, i.e. the standard deviation at log-scale, `σ`
9+
"""
10+
function fit_mean_Σ(::Type{MvLogNormal}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T1 <:Real,T2 <:Real}
11+
_T = promote_type(T1, T2)
12+
fit_mean_Σ(MvLogNormal{_T}, mean, Σ)
13+
end
14+
function fit_mean_Σ(::Type{MvLogNormal{T}}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T, T1 <:Real,T2 <:Real}
15+
meanT = T1 == T ? mean : begin
16+
meanT = similar(mean, T)
17+
meanT .= mean
18+
end
19+
ΣT = T2 == T ? Σ : begin
20+
ΣT = similar(Σ, T)
21+
ΣT .= Σ
22+
end
23+
σ2 = diag(ΣT)
24+
μ = log.(meanT) .- σ2 ./ 2
25+
MvLogNormal(μ, ΣT)
26+
end

src/multivariates.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
##### Specific distributions #####
2+
3+
for fname in [
4+
# "dirichlet.jl",
5+
# "multinomial.jl",
6+
# "dirichletmultinomial.jl",
7+
# "jointorderstatistics.jl",
8+
# "mvnormal.jl",
9+
# "mvnormalcanon.jl",
10+
# "mvlogitnormal.jl",
11+
"mvlognormal.jl",
12+
# "mvtdist.jl",
13+
# "vonmisesfisher.jl"
14+
]
15+
include(joinpath("multivariate", fname))
16+
end

src/univariate/continuous/lognormal.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,20 @@ true
101101
σstar(d::LogNormal) = exp(params(d)[2])
102102

103103
"""
104-
fit(D, mean, σstar)
104+
fit(D, mean, σstar::AbstractΣstar)
105+
fit_mean_Σ(D, mean, σ::Real)
105106
106107
Fit a statistical distribution of type `D` to mean and multiplicative
107-
standard deviation.
108+
standard deviation, `σstar`, or scale parameter at log-scale: `σ`.
108109
109110
# Arguments
110111
- `D`: The type of distribution to fit
111112
- `mean`: The moments of the distribution
112113
- `σstar::AbstractΣstar`: The multiplicative standard deviation
114+
- `σ`: The standard-deviation parameter at log-scale
113115
114-
See also [`σstar`](@ref), [`AbstractΣstar`](@ref).
116+
The first version uses type [`AbstractΣstar`](@ref) to distinguish from
117+
other methods of function fit.
115118
116119
# Examples
117120
```jldoctest fm1; output = false, setup = :(using DistributionFits)
@@ -121,17 +124,23 @@ d = fit(LogNormal, 2, Σstar(1.1));
121124
true
122125
```
123126
"""
124-
function fit(::Type{LogNormal}, mean::T, σstar::AbstractΣstar) where {T <: Real}
125-
_T = promote_type(T, eltype(σstar))
126-
fit(LogNormal{_T}, mean, σstar)
127+
function fit(d::Type{LogNormal}, mean, σstar::AbstractΣstar)
128+
fit_mean_Σ(d, mean, log(σstar()))
127129
end
128-
129-
function fit(::Type{LogNormal{T}}, mean::Real, σstar::AbstractΣstar) where {T}
130-
σ = log(σstar())
130+
function fit(d::Type{LogNormal{T}}, mean::Real, σstar::AbstractΣstar) where {T}
131+
fit_mean_Σ(d, mean, log(σstar()))
132+
end
133+
function fit_mean_Σ(::Type{LogNormal}, mean::T1, σ::T2) where {T1 <: Real,T2 <: Real}
134+
_T = promote_type(T1, T2)
135+
fit_mean_Σ(LogNormal{_T}, mean, σ)
136+
end
137+
function fit_mean_Σ(::Type{LogNormal{T}}, mean::Real, σ::Real) where {T}
138+
#σ = log(σstar())
131139
μ = log(mean) - σ * σ / 2
132140
LogNormal(T(μ), T(σ))
133141
end
134142

143+
135144
"""
136145
fit_mean_relerror(D, mean, relerror)
137146

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
44
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
5+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
56
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
67
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
8+
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
79
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
810
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
911
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"

0 commit comments

Comments
 (0)