Skip to content

Unify model construction workflow #383

@yebai

Description

@yebai

Currently, model definition in JuliaBUGS is inconsistent:

  • Models defined with @bugs or @bugs_str require an explicit call to compile(...).
  • Models defined with @model are directly callable and return a BUGSModel.

This inconsistency can be confusing and complicates the implementation.

# @bugs macro
model1 = @bugs begin 
   x[1:2] ~ dmnorm(mu[:], sigma[:, :]) 
   x[3] ~ dnorm(0, 1) 
   y = x[1] + x[3] 
end

# @bugs_str macro
model2 = @bugs"""
  model {
     x[1:2] ~ dmnorm(mu[:], sigma[:, :]) 
     x[3] ~ dnorm(0, 1) 
     y = x[1] + x[3] 
  }
"""

# @model macro
@model function model3((;x), mu, sigma)
   x[1:2] ~ dmnorm(mu[:], sigma[:, :]) 
   x[3] ~ dnorm(0, 1) 
   y = x[1] + x[3] 
end

Usage:

# Requires compile(...)
compile(model1, (;mu=[0,0], sigma=[1 0; 0 1]))
compile(model2, (;mu=[0,0], sigma=[1 0; 0 1]))

# Directly callable
model3((;), [0,0], [1 0; 0 1])

Unify the API so that all models—whether defined via @bugs, @bugs_str, or @model—return a callable function of the form

model(data::NamedTuple) -> BUGSModel 

That is, let @bugs and @bugs_str return an anonymous function instead of a static object.

# unified behaviour
m1 = model1((;mu=[0,0], sigma=[1 0; 0 1])) # --> `compile(model1, (;mu=[0,0], sigma=[1 0; 0 1]))`
m2 = model2((;mu=[0,0], sigma=[1 0; 0 1])) # --> `compile(model2, (;mu=[0,0], sigma=[1 0; 0 1]))`

# The current `@model` generated constructor would still work. 
# But, this is not available for models defined via `@bugs` and `@bugs_str` 
m3 = model3((;), [0,0], [1 0; 0 1])

# all models can be initialised via
initialize!(::BUGSModel, initial_params::NamedTuple)

Then, we can remove compile from the export list and keep it only as an internal utility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions