Soss.jl icon indicating copy to clipboard operation
Soss.jl copied to clipboard

Ducktype sampleable objects

Open kskyten opened this issue 5 years ago • 8 comments

The is a draft for adding arbitrary simulators here. Would it be possible to just treat everything on the right hand side of a tilde as implementing a rand method and just add custom types? Probably related to #42 .

kskyten avatar Nov 08 '19 19:11 kskyten

I think I'm missing something here - how is what you propose different than the draft?

cscherrer avatar Nov 09 '19 16:11 cscherrer

I would like to do this:

struct MySimulator
  mu
  sigma
end

Base.rand(s::MySimulator) = rand(Normal(s.mu, s.sigma))

m = @model (alpha, beta) begin
  z ~ Normal(alpha, beta)
  y ~ MySimulator(z, 1)
end

kskyten avatar Nov 09 '19 17:11 kskyten

I like it. I think you should currently be able to do something like

m = @model (alpha, beta, sim) begin
  z ~ Normal(alpha, beta)
  y ~ sim(z, 1)
end

rand(m(alpha=a, beta=b, sim=MySimulator))

We'll be able to get rid of the last argument and use the current module scope once we're done with #42

cscherrer avatar Nov 09 '19 17:11 cscherrer

Hi @kskyten ,

Thanks to @thautwarm 's recent update (https://github.com/thautwarm/GeneralizedGenerated.jl/pull/28), we can get this working:

julia> struct MySimulator
         mu
         sigma
       end;

julia> Base.rand(s::MySimulator) = rand(Normal(s.mu, s.sigma))

julia> m = @model (alpha, beta) begin
         z ~ Normal(alpha, beta)
         y ~ MySimulator(z, 1)
       end;

julia> rand(m(alpha=2,beta=4))
(alpha = 2, beta = 4, z = -2.5523934421776824, y = -2.1025024446789806)

Hope to get a PR set up with this soon :)

cscherrer avatar Nov 17 '19 16:11 cscherrer

@kskyten this now works in 0.8!

cscherrer avatar Nov 29 '19 17:11 cscherrer

I used to think I can make MySimulator a module... but it seems unnecessary now😄

thautwarm avatar Nov 29 '19 19:11 thautwarm

Currently, due to the address of performance regression, This feature got lost again: https://github.com/cscherrer/Soss.jl/blob/80a9c2805e5d4e0b4290298b23801b3a198f0a56/src/primitives/rand.jl#L14

we should re-implement of it in recent iterations.

thautwarm avatar Dec 30 '19 06:12 thautwarm

We can allow m.model to store non-models, and do static dispatch on this, certainly we can have this feature again, with no performance loss.

thautwarm avatar Dec 30 '19 06:12 thautwarm