Skip to content

Turing-like @model macro for specifying BUGS models #281

@yebai

Description

@yebai

Logistic Regression with Random Effects using current syntax

model_def = JuliaBUGS.@bugs begin
    for i in 1:N
        r[i] ~ dbin(p[i], n[i])
        b[i] ~ dnorm(0.0, tau)
        p[i] = logistic(alpha0 + alpha1 * x1[i] + alpha2 * x2[i] + alpha12 * x1[i] * x2[i] + b[i])
    end
    alpha0 ~ dnorm(0.0, 1.0E-6)
    alpha1 ~ dnorm(0.0, 1.0E-6)
    alpha2 ~ dnorm(0.0, 1.0E-6)
    alpha12 ~ dnorm(0.0, 1.0E-6)
    tau ~ dgamma(0.001, 0.001)
    sigma = 1 / sqrt(tau)
end

Desired Turing-like modelling syntax

JuliaBUGS.@parameters struct Tp; r; b; alpha0; alpha1; alpha2; alpha12; tau; end

JuliaBUGS.@model function model((;r, b, alpha0, alpha1, alpha2, alpha12, tau)::Tp, x1, x2, N, n)
    for i in 1:N
        r[i] ~ dbin(p[i], n[i])
        b[i] ~ dnorm(0.0, tau)
        p[i] = logistic(alpha0 + alpha1 * x1[i] + alpha2 * x2[i] + alpha12 * x1[i] * x2[i] + b[i])
    end
    alpha0 ~ dnorm(0.0, 1.0E-6)
    alpha1 ~ dnorm(0.0, 1.0E-6)
    alpha2 ~ dnorm(0.0, 1.0E-6)
    alpha12 ~ dnorm(0.0, 1.0E-6)
    tau ~ dgamma(0.001, 0.001)
    sigma = 1 / sqrt(tau)
end

Note that LHS variables of tilde are passed as the first argument via destructuring syntax ((;r, b, alpha0, alpha1, alpha2, alpha12, tau), ...) while constants and other variables are passed as (..., x1, x2, N, n).

Then, we could do

posterior = model(Tp(), x1, x2, N, n) | data
AbstractMCMC.sample(posterior, 
                        NUTS(0.8),
                        n_samples;
                        chain_type = Chains,
                        n_adapts = n_adapts,
                        init_params = initializations,
                        discard_initial = n_adapts
)

instead of compile(model_def, data, initializations). Similarly, one could use all these other model operations like (condition / )fix / predict / to_distribution.

Sub-issues

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions