Reputation: 289
I want to plotting mixtures of two 1d Gaussian distributions with Julia. I am not sure what is the best way to do it. I am trying to use Distributions.jl
and in specific to both
d1 = Normal(0.0, 1.0)
and d2 = Normal(1.0, 1.8)
MixtureModel(Normal[ Normal(-2.0, 1.2), Normal(0.0, 1.0), Normal(3.0, 2.5)], [0.1, 0.6, 0.3])
Now, for the 1st attempt I do not know how to define the weights. My questions therefore is about how to proceed on to simply generate and draw samples of this mixture?
I would like to plot them and also use these samples in order to perform parameter estimation.
Upvotes: 0
Views: 1042
Reputation: 13800
I'm not sure I fully understand the question - why are you defining d1
and d2
?
To answer your bold question: just use rand()
to draw from your mixture distribution:
julia> using Distributions
julia> mm = MixtureModel([Normal(-2.0, 1.2), Normal(), Normal(3.0, 2.5)], [0.1, 0.6, 0.3])
MixtureModel{Normal{Float64}}(K = 3)
components[1] (prior = 0.1000): Normal{Float64}(μ=-2.0, σ=1.2)
components[2] (prior = 0.6000): Normal{Float64}(μ=0.0, σ=1.0)
components[3] (prior = 0.3000): Normal{Float64}(μ=3.0, σ=2.5)
julia> rand(mm)
1.882130062980293
Note that here I have used Normal()
instead of Normal(0.0, 1.0)
, as Normal()
already returns the standard normal distribution.
To plot:
julia> using Plots
julia> histogram(rand(mm, 100_000), normalize = true, xlabel = "Value", ylabel = "Frequency", label = "Mixture model")
Upvotes: 2