JuliaMath / MeasureBase.jl

Other
26 stars 4 forks source link

Monadic iteration for measures? #55

Open oschulz opened 2 years ago

oschulz commented 2 years ago

By defining

Base.iterate(m::AbstractMeasure) = rand(m), nothing
Base.iterate(m::AbstractMeasure, ::Nothing) = rand(m), nothing
Base.Iterators.IteratorSize(::Type{<:AbstractMeasure}) = Base.IsInfinite()

we could make measures monadic in respect to Julia's iteration interface:

julia> using Base.Iterators, MeasureTheory
julia> take((x^2 for x in Normal()), 10) |> collect
10-element Vector{Float64}:
[...]

This would play well semantically with a syntax f.(μ) for pushforward measures (#54).

x^2 for x in Normal would become semantically equivalent to x ~ Normal(); return x^2 in a model.

cscherrer commented 2 years ago

Thanks @oschulz . We've talked a little bit about ideas in this direction, and I appreciate your creating these issues to make things easier to track.

Similar to likelihoods, I think pushforwards need to be set up in a way that allows simplification in cases with closed form solutions. The problem with sampling is that it "forgets" about any structure of a given measure and takes a very specific concrete approach.

I like the idea of being able to map a measure to an infinite stream of samples (maybe similar to Chain), but I think it's important to have a method to take us from the world of "measure expressions" to the world of infinite streams.