QuantEcon / Expectations.jl

Expectation operators for Distributions.jl objects
MIT License
57 stars 17 forks source link

Feature request: Addition of expectations #46

Closed nignatiadis closed 4 years ago

nignatiadis commented 4 years ago

This is a follow-up to #42. I just realized that MixtureExpectation implements addition of expectations:

julia> e1 = expectation(Normal(1.0))
julia> e2 = expectation(Uniform())
julia> e1pe2 = MixtureExpectation([e1,e2],[1.0,1.0])
julia> e1pe2(identity) == e1(identity) + e2(identity)
true

So adding methods for +(e1::Expectation, e2::Expectation) would be both elegant from a mathematical perspective (especially since * is already implemented) and also quite practical/useful!

arnavs commented 4 years ago

Huh. Yeah, you're right. That's a good point.

Will do. Thanks for the good suggestions.

arnavs commented 4 years ago

Done @nignatiadis

arnavs commented 4 years ago

Note that a consequence is that we can add MixtureExpectations and IterableExpectations, e.g.

julia> e1 = expectation(Normal());

julia> e2 = expectation(Uniform());

julia> e3 = expectation(Gamma());

julia> E = e1 + (e2 + e3);

julia> typeof(E.expectations[2])
MixtureExpectation{Tuple{IterableExpectation{Array{Float64,1},Array{Float64,1}},IterableExpectation{Array{Float64,1},Array{Float64,1}}},Array{Float64,1}}

julia> E(x -> x) == e1(identity) + e2(identity) + e3(identity)
true
nignatiadis commented 4 years ago

This is super cool!! Thank you a lot!