TuringLang / SSMProblems.jl

Common abstractions for state-space models
http://turinglang.org/SSMProblems.jl/
MIT License
2 stars 2 forks source link

A bit confused regarding the interface #9

Closed torfjelde closed 1 year ago

torfjelde commented 1 year ago

I'm slightly confused as to what the intention of the interface methods are.

Take the transition!! method for example. IIUC this is supposed to be implemented on a per-model basis, no?

But the current code seems to indicate that this method should have the signature

transition!!(rng, step, particle[, cache])

But this is completely lacking any way to dispatch on the target model.

So am I misunderstanding this? Or is the Particle supposed to define all the behavior (including the model)? Or is this simply a typo, and it should indeed be

transition!!(rng, model, step, particle[, cache])

?

yebai commented 1 year ago

Yes, the Particle type fully specifies a model -- each model defines its Particle type and the transition!! method. But it is good to know this is confusing, maybe we can reconsider the signature to clarify this.

torfjelde commented 1 year ago

each model defines its Particle type

How do I define different SMC samplers from this that are applicable to more than a single model type?

I would have imagined something like:

  1. PackageA defines a certain SMC sampler, e.g. PG.
  2. PackageB then defines a few methods, e.g. transition!!, in such a way that it can take advantage of the SMC sampler defined in PackageA.

But if the Particle and the model is coupled, then things become a bit more annoying, no?

FredericWantiez commented 1 year ago

Feel like the naming is a bit unfortunate here, Particle really is the Model as you see it @torfjelde. The actual Particle implementation should be left to the sampler implementation.

Roughly, the way I think this could work: SSMProblems:

abstract type AbstractStateSpaceModel end

function transition!!(::AbstractStateSpaceModel) end
function emission_logdensity(::AbstractStateSpaceModel) end
function isdone(::AbstractSpaceModel) end

PackageA: (SMC implementation)

struct Particle{T<:AbstractStateSpaceModel}
    model::T
    parent::Particle{T} # Whatever datastructure the specialization needs
end

function sample(model::AbstractStateSpaceModel, ::Particle) end

PackageB (Model Implementation)

struct LinearSSM{T} <: AbstractStateSpaceModel
    state::T
end

function transition!!(::LinearSSM) end
...

otherwise you end up having to deal with particle dynamics (copying, memory layout ...) in the model part and that doesn't feel quite right.

yebai commented 1 year ago

Should be fixed by #16