Thomas Mikaelsen
Thomas Mikaelsen

Reputation: 1

Dot-notation not working with MixtureExpectation?

This is my first post so I apologize for any formatting issues.

I'm trying to calculate the expected value of a collection of numbers in Julia, given a probability distribution that is the mixture of two Beta distributions. Using the following code gives the error seen below

using Distribution, Expectations, Statistics
d = MixtureModel([Beta(1,1),Beta(3,1.6)],[0.5,0.5])
E = expectation(d)
E*rand(32,1)
MethodError: no method matching *(::MixtureExpectation{Vector{IterableExpectation{Vector{Float64}, Vector{Float64}}}, Vector{Float64}}, ::Matrix{Float64})

If I use just a single Beta distribution, the above syntax works fine:

d = Beta(1,1)
E = expectation(d)
E*rand(32,1)

Out = 0.503

And if I use function notation in the expectation, I can calculate expectations of functions using the Mixture model as well.

d = MixtureModel([Beta(1,1),Beta(3,1.6)],[0.5,0.5])
E = expectation(d)
E(x -> x^2)

It just seems to not work when using the dot-notation shown above.

Upvotes: 0

Views: 40

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42194

Single distribution yields IterableExpectation that allows multiplication over an array, while mixture distribution yields MixtureExpectation that allows multiplications only over a scalar. You can run typeof(E) to check the type in your code.

julia> methodswith(IterableExpectation)
[1] *(r::Real, e::IterableExpectation) in Expectations at C:\JuliaPkg\Julia-1.8.0\packages\Expectations\hZ5Gh\src\iterable.jl:53
[2] *(e::IterableExpectation, h::AbstractArray) in Expectations at C:\JuliaPkg\Julia-1.8.0\packages\Expectations\hZ5Gh\src\iterable.jl:44
...
julia> methodswith(MixtureExpectation)
[1] *(r::Real, e::MixtureExpectation) in Expectations at C:\JuliaPkg\Julia-1.8.0\packages\Expectations\hZ5Gh\src\mixturemodels.jl:15
...

Upvotes: 0

Related Questions