Skip to content

Commit 638e6c3

Browse files
authored
implment fit_mean_Σ for Normal and MvNormal (#35)
1 parent eb6a57d commit 638e6c3

File tree

10 files changed

+68
-14
lines changed

10 files changed

+68
-14
lines changed

docs/src/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ fit(::Type{D}, ::QuantilePoint, ::QuantilePoint) where {D<:Distribution}
8484
```@docs
8585
fit(::Type{D}, ::Any, ::QuantilePoint, ::Val{stats} = Val(:mean)) where {D<:Distribution, stats}
8686
```
87+
88+
## Fit to mean and uncertainty parameter
89+
For bayesian inversion it is often required to specify a distribution given
90+
the expected value (the predction of the population value) and a description of
91+
uncertainty of an observation.
92+
93+
```@docs
94+
fit_mean_Σ
95+
```
96+
97+
8798
## Currently supported distributions
8899
Univariate continuous
89100
- Normal

docs/src/mvlognormal.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ CurrentModule = DistributionFits
77
Can be fitted to a given mean, provided the Covariance of the underlying
88
normal distribution.
99

10-
```@docs
11-
fit_mean_Σ(::Type{MvLogNormal}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T1 <:Real,T2 <:Real}
12-
```
13-
1410
```jldoctest; output = false, setup = :(using DistributionFits)
1511
Σ = hcat([0.6,0.02],[0.02,0.7])
1612
μ = [1.2,1.3]

src/fitstats.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,15 @@ end
295295
# function fit_mean_quantile(d::Type{D}, mean::Real, qp::QuantilePoint) where D<:Distribution
296296
# error("fit_mean_quantile not yet implemented for Distribution of type: $D")
297297
# end
298+
299+
"""
300+
fit_mean_Σ(::Type{<:Distribution}, mean, Σ)
301+
302+
Fit a Distribution to mean and uncertainty quantificator Σ.
303+
304+
The meaning of `Σ` depends on the type of distribution:
305+
- `MvLogNormal`, `MvNormal`: the Covariancematrix of the associated normal distribution
306+
- `LogNormal`, `Normal`: the scale parameter, i.e. the standard deviation at log-scale, `σ`
307+
"""
308+
function fit_mean_Σ end
309+

src/multivariate/mvlognormal.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
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-
"""
101
function fit_mean_Σ(::Type{MvLogNormal}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T1 <:Real,T2 <:Real}
112
_T = promote_type(T1, T2)
123
fit_mean_Σ(MvLogNormal{_T}, mean, Σ)

src/multivariate/mvnormal.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function fit_mean_Σ(::Type{MvNormal}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T1 <:Real,T2 <:Real}
2+
_T = promote_type(T1, T2)
3+
fit_mean_Σ(MvNormal{_T}, mean, Σ)
4+
end
5+
function fit_mean_Σ(::Type{MvNormal{T}}, mean::AbstractVector{T1}, Σ::AbstractMatrix{T2}) where {T, T1 <:Real,T2 <:Real}
6+
meanT = T1 == T ? mean : begin
7+
meanT = similar(mean, T)
8+
meanT .= mean
9+
end
10+
ΣT = T2 == T ? Σ : begin
11+
ΣT = similar(Σ, T)
12+
ΣT .= Σ
13+
end
14+
MvNormal(meanT, ΣT)
15+
end

src/multivariates.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ for fname in [
55
# "multinomial.jl",
66
# "dirichletmultinomial.jl",
77
# "jointorderstatistics.jl",
8-
# "mvnormal.jl",
8+
"mvnormal.jl",
99
# "mvnormalcanon.jl",
1010
# "mvlogitnormal.jl",
1111
"mvlognormal.jl",

src/univariate/continuous/normal.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ end
3535
function fit_mode_quantile(D::Type{Normal{T}}, mode::Real, qp::QuantilePoint) where {T}
3636
fit(D, QuantilePoint(mode, 0.5), qp)
3737
end
38+
39+
function fit_mean_Σ(::Type{Normal}, mean::T1, σ::T2) where {T1 <: Real, T2 <: Real}
40+
_T = promote_type(T1,T2)
41+
fit_mean_Σ(Normal{_T}, mean, σ)
42+
end
43+
function fit_mean_Σ(D::Type{Normal{T}}, mean::Real, σ::Real) where {T}
44+
Normal{T}(mean, σ)
45+
end
46+

test/multivariate/mvnormal.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using DistributionFits
2+
using Test
3+
4+
@testset "fit_mean_Σ" begin
5+
m = [3.0f0, 4.0f0]
6+
Σ = hcat([2.0f0,0.2],[0.2, 2.0f0])
7+
d = fit_mean_Σ(MvNormal, m, Σ)
8+
@test mean(d) == m
9+
@test cov(d) == Σ
10+
end;
11+

test/multivariate/test_multivariate.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ end
123123

124124
const tests = [
125125
"mvlognormal",
126+
"mvnormal",
126127
]
127128
#tests = ["mvlognormal"]
128129

test/univariate/continuous/normal.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ end;
88
d = Normal(3.0f0, 2.0f0)
99
test_univariate_fits(d)
1010
end;
11+
12+
@testset "fit_mean_Σ" begin
13+
m = 3.0f0
14+
σ = 2.0f0
15+
d = fit_mean_Σ(Normal, m, σ)
16+
@test mean(d) == m
17+
@test scale(d) == σ
18+
end;

0 commit comments

Comments
 (0)