Search code examples
statisticsjulianormal-distribution

How to specify the weights in a MixtureModel in Julia Distributions.jl?


In Distributions.jl we can specify the priors of a mixture model. But we cannot specify the weights. For example, if I want to make a mixture like this:

pdf(Normal(2, 3), x)*w1.+pdf(Normal(5, 10), x)*w2

I cannot really specify the weights. And the priors are required to add up to 1 for obv reasons. So, is there a way to specify the weights in MixtureModel? Something like:

MixtureModel(Normal[
        Normal(2, 3),
        Normal(5, 10)
        ], **weights=[w1, w2]**)

Thanks


Solution

  • This is covered in the Distributions.jl documentation on mixture model constructors — you want the prior argument. See

    https://juliastats.org/Distributions.jl/v0.14/mixture.html#Constructors-1

    Here's a quick plot of their first example. The [0.2, 0.5, 0.3] are the weights:

    julia> using Distributions, Plots
    
    julia> d = MixtureModel(Normal[
              Normal(-2.0, 1.2),
              Normal(0.0, 1.0),
              Normal(3.0, 2.5)], [0.2, 0.5, 0.3])
    MixtureModel{Normal}(K = 3)
    components[1] (prior = 0.2000): Normal{Float64}(μ=-2.0, σ=1.2)
    components[2] (prior = 0.5000): Normal{Float64}(μ=0.0, σ=1.0)
    components[3] (prior = 0.3000): Normal{Float64}(μ=3.0, σ=2.5)
    
    
    julia> x = -10:0.1:10
    -10.0:0.1:10.0
    
    julia> plot(x, pdf.(d, x), legend=nothing, xlabel="x", ylabel="pdf")
    

    Which produces

    enter image description here