Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: aliases and keyword distribution specification in the model macro #18

Merged
merged 9 commits into from
Aug 5, 2022

Conversation

bvdmitri
Copy link
Member

@bvdmitri bvdmitri commented Aug 3, 2022

This PR adds aliases feature to model specification (@model macro):

See ReactiveBayes/ReactiveMP.jl#1 and #17

In the first version the following aliases are available:

a || b: alias for OR(a, b) node
a && b: alias for AND(a, b) node
a -> b: alias for IMPLY(a, b) node
¬a or !a: alias for NOT(a) node
a + b + c: alias for (a + b) + c
Normal(μ|m|mean = ..., σ²|τ⁻¹|v|var|variance = ...) alias for NormalMeanVariance(..., ...) node
Normal(μ|m|mean = ..., τ|σ⁻²|w|p|prec|precision = ...) alias for NormalMeanVariance(..., ...) node
MvNormal(μ|m|mean = ..., Σ|V|Λ⁻¹|cov|covariance = ...) alias for MvNormalMeanCovariance(..., ...) node
MvNormal(μ|m|mean = ..., Λ|W|Σ⁻¹|prec|precision = ...) alias for MvNormalMeanPrecision(..., ...) node

Example

@model function mymodel()
    
    
    x1 ~ MvNormal= zeros(2), Σ⁻¹ = diageye(2))
    x2 ~ MvNormal= zeros(2), Λ = diageye(2))
    x3 ~ MvNormal(mean = zeros(2), W = diageye(2))
    x4 ~ MvNormal= zeros(2), prec = diageye(2))
    x5 ~ MvNormal(m = zeros(2), precision = diageye(2))
    
    y1 ~ MvNormal(mean = zeros(2), Σ = diageye(2))
    y2 ~ MvNormal(m = zeros(2), Λ⁻¹ = diageye(2))
    y3 ~ MvNormal= zeros(2), V = diageye(2))
    y4 ~ MvNormal(mean = zeros(2), cov = diageye(2))
    y5 ~ MvNormal(mean = zeros(2), covariance = diageye(2))

    x ~ x1 + x2 + x3 + x4 + x5
    y ~ y1 + y2 + y3 + y4 + y5
    
    d = datavar(Vector{Float64})
    d ~ MvNormal= x + y, covariance = diageye(2))
end

@model function mycoolmodel()
    a ~ Bernoulli(0.5)
    b ~ Bernoulli(0.5)
    c ~ Bernoulli(0.5)
    
    y = datavar(Float64)
    
    o ~ ¬a || b && !c
    o ~ Bernoulli(y)
    
end

TODO:

  • add Gamma distributions aliases

Update:

  • a * b * c: alias for (a * b) * c
  • Gamma(α|a|shape = ..., θ|β⁻¹|scale = ...) alias for GammaShapeScale(..., ...) node.
  • Gamma(α|a|shape = ..., β|θ⁻¹|rate = ...) alias for GammaShapeRate(..., ...) node.

@bartvanerp
Copy link
Member

Looks great!
Perhaps we could also add γ for the univariate precision parameter, and and for and and or (or the other way around)?

@bvdmitri
Copy link
Member Author

bvdmitri commented Aug 4, 2022

Thanks @bartvanerp ! I will add γ.

Initially this PR implemented and for or and and respectively. However, it turned out, that Julia does not have any precedence priority for those. In contrast, && and || have the correct precedence (&& exectues first then ||). I decided not to use and because it might be confusing for a user and may potentially create wrong models. We could add only if we explicitly state that these do not have precedence and a user must use brackets. But that is not that natural as && and ||.

@bvdmitri
Copy link
Member Author

bvdmitri commented Aug 4, 2022

@wmkouw This feature also enables this

y ~ x1 + x2 + x3 + x4

But only static sum can be unrolled at a parse time, e.g. this is still not possible

y ~ ∑(x) # not possible
# or 
y ~ +(x...) # not possible

@wmkouw
Copy link
Member

wmkouw commented Aug 11, 2022

I am happy with these aliases: I think they really add to the user-friendliness of RMP. But the feature to add multiple variables at once, i.e.,

y ~ x1 + x2 + x3 + x4

does not solve the issue with unnecessary loops being constructed.

Perhaps I just need a custom node that implements a linear combination. Something along the lines of:

y ~ lincomb(A, x1, x2, ...)

which implements y = A'x. I'll work on a PR for RMP.

@wouterwln wouterwln deleted the dev-node-specification-aliases branch April 18, 2023 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants