Using truncation in MixtureModel

Can we also use truncation here?

MixtureModel(Normal[
   Normal(-2.0, 1.2),
   Normal(0.0, 1.0),
   Normal(3.0, 2.5)])

I was trying with some truncated distributions and it gives error. These distributions are obtained from fitting Normal distribution to a vector and then truncate them.

Error:

ERROR: MethodError: Cannot `convert` an object of type 
  Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64} to an object of type 
  Normal

Closest candidates are:
  convert(::Type{Normal}, ::NormalCanon)
   @ Distributions ~/.julia/packages/Distributions/Ufrz2/src/univariate/continuous/normalcanon.jl:34
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:64

Stacktrace:
 [1] setindex!(A::Vector{Normal}, x::Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}, i1::Int64)
   @ Base ./array.jl:969
 [2] getindex(::Type{Normal}, ::Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}, ::Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}, ::Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}, ::Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}, ::Vararg{Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}})
   @ Base ./array.jl:403
 [3] top-level scope
   @ ~/PhD/Program_Julia/code/fitting_dist.jl:25

Haven’t tried it, but maybe truncating each component distribution and then creating the mixture model from the truncated distributions is possible? (So, reverse order of the steps you are doing)

Works just fine without coercing the vector of components to have element type Normal :slight_smile:

julia> comps = truncated.([
          Normal(-2.0, 1.2),
             Normal(0.0, 1.0),
                Normal(3.0, 2.5)], upper=2.0, lower=-2.0)
3-element Vector{Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}}:
 Truncated(Normal{Float64}(μ=-2.0, σ=1.2); lower=-2.0, upper=2.0)
 Truncated(Normal{Float64}(μ=0.0, σ=1.0); lower=-2.0, upper=2.0)
 Truncated(Normal{Float64}(μ=3.0, σ=2.5); lower=-2.0, upper=2.0)

julia> MixtureModel(comps)
MixtureModel{Truncated{Normal{Float64}, Continuous, Float64, Float64, Float64}}(K = 3)
components[1] (prior = 0.3333): Truncated(Normal{Float64}(μ=-2.0, σ=1.2); lower=-2.0, upper=2.0)
components[2] (prior = 0.3333): Truncated(Normal{Float64}(μ=0.0, σ=1.0); lower=-2.0, upper=2.0)
components[3] (prior = 0.3333): Truncated(Normal{Float64}(μ=3.0, σ=2.5); lower=-2.0, upper=2.0)
2 Likes