1+
2+ using LogarithmicNumbers
3+ using LogExpFunctions
4+
15export SuperpositionMeasure
26
37@doc raw """
4- struct SuperpositionMeasure{X, NT} <: AbstractMeasure
8+ struct SuperpositionMeasure{NT} <: AbstractMeasure
59 components :: NT
610 end
711Superposition of measures is analogous to mixture distributions, but (because
@@ -57,16 +61,14 @@ function Base.:+(μ::AbstractMeasure, ν::AbstractMeasure)
5761 superpose (μ, ν)
5862end
5963
60- using LogarithmicNumbers
61-
6264oneplus (x:: ULogarithmic ) = exp (ULogarithmic, log1pexp (x. log))
6365
6466@inline function density_def (s:: SuperpositionMeasure{Tuple{A,B}} , x) where {A,B}
6567 (μ, ν) = s. components
6668
6769 insupport (μ, x) || return exp (ULogarithmic, logdensity_def (ν, x))
6870 insupport (ν, x) || return exp (ULogarithmic, logdensity_def (μ, x))
69-
71+
7072 α = basemeasure (μ)
7173 β = basemeasure (ν)
7274 dμ_dα = exp (ULogarithmic, logdensity_def (μ, x))
@@ -76,13 +78,24 @@ oneplus(x::ULogarithmic) = exp(ULogarithmic, log1pexp(x.log))
7678 return dμ_dα / oneplus (dβ_dα) + dν_dβ / oneplus (dα_dβ)
7779end
7880
79- using LogExpFunctions
81+ function density_def (s:: SuperpositionMeasure , x)
82+ T = typeof (s)
83+ msg = """
84+ Not implemented: There is no method
85+ density_def(::$T , x)
86+ """
87+ error (msg)
88+ end
8089
81- @inline function logdensity_def (μ:: T , ν:: T , x:: Any ) where T<: (SuperpositionMeasure{Tuple{A, B}} where {A, B})
90+ @inline function logdensity_def (
91+ μ:: T ,
92+ ν:: T ,
93+ x,
94+ ) where {T<: (SuperpositionMeasure{Tuple{A,B}} where {A,B}) }
8295 if μ === ν
8396 return zero (return_type (logdensity_def, (μ, x)))
8497 else
85- return logdensity_def (μ,x) - logdensity_def (ν, x)
98+ return logdensity_def (μ, x) - logdensity_def (ν, x)
8699 end
87100end
88101
94107 return logaddexp (logdensity_rel (μ, β, x), logdensity_rel (ν, β, x))
95108end
96109
97- @inline function logdensity_def (s:: SuperpositionMeasure{Tuple{A,B}} , β:: SuperpositionMeasure , x) where {A,B}
110+ @inline function logdensity_def (
111+ s:: SuperpositionMeasure{Tuple{A,B}} ,
112+ β:: SuperpositionMeasure ,
113+ x,
114+ ) where {A,B}
98115 (μ, ν) = s. components
99116 insupport (μ, x) == true || return logdensity_rel (ν, β, x)
100117 insupport (ν, x) == true || return logdensity_rel (μ, β, x)
107124
108125@inline logdensity_def (s:: SuperpositionMeasure , x) = log (density_def (s, x))
109126
110- basemeasure (μ:: SuperpositionMeasure ) = superpose (map (basemeasure, μ. components)... )
127+ function basemeasure (μ:: SuperpositionMeasure{Tuple{A,B}} ) where {A,B}
128+ superpose (map (basemeasure, μ. components)... )
129+ end
130+ basemeasure (μ:: SuperpositionMeasure ) = superpose (map (basemeasure, μ. components))
111131
112132# TODO : Fix `rand` method (this one is wrong)
113133# function Base.rand(μ::SuperpositionMeasure{X,N}) where {X,N}
@@ -118,4 +138,4 @@ basemeasure(μ::SuperpositionMeasure) = superpose(map(basemeasure, μ.components
118138 any (d. components) do c
119139 dynamic (insupport (c, x))
120140 end
121- end
141+ end
0 commit comments