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

Permit dot call (like Distributions.Normal) to be used in model definition #237

Merged
merged 3 commits into from
Nov 16, 2024

Conversation

sunxd3
Copy link
Member

@sunxd3 sunxd3 commented Nov 14, 2024

The motivation for the PR is initially to allow the use product_distribution in JuliaBUGS model. By writing

@bugs begin
    x[1:2] ~ Distributions.product_distribution(fill(Normal(), 2))
end

It also enables

julia> foo(x) = x + 1
foo (generic function with 1 method)

julia> model_def = @bugs begin
           a = Main.foo(b)
       end
quote
    a = Main.foo(b)
end

julia> compile(model_def, (;b=2))
BUGSModel (parameters are in transformed (unconstrained) space, with dimension 0):

  Model parameters:

  Variable sizes and types:
    a: type = Int64
    b: type = Int64

i.e., an easier way to introduce external functions into JuliaBUGS.

This can't fully replace @register_primitive yet, because using Main module is relying on Julia runtime behavior and not very intuitive. Examples:

julia> module TestModule
           bar(x) = x + 1
       end
Main.TestModule

julia> model_def = @bugs begin
           a = TestModule.bar(b)
       end
quote
    a = TestModule.bar(b)
end

julia> compile(model_def, (; b = 1))
ERROR: UndefVarError: `TestModule` not defined in `JuliaBUGS`
...

one would need to do

julia> model_def = @bugs begin
           a = Main.TestModule.bar(b)
       end
quote
    a = Main.TestModule.bar(b)
end

also

julia> @testset "t" begin
           foo1(x) = x + 1
           model_def = @bugs begin
               a = Main.foo1(b)
           end
           compile(model_def, (; b= 1))
       end
t: Error During Test at REPL[18]:1
  Got exception outside of a @test
  UndefVarError: `foo1` not defined in `Main`
...

@sunxd3 sunxd3 changed the title Permit dot call (like Distributions.Normal to be used in model definition Permit dot call (like Distributions.Normal) to be used in model definition Nov 14, 2024
@coveralls
Copy link

coveralls commented Nov 14, 2024

Pull Request Test Coverage Report for Build 11836332251

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.08%) to 74.173%

Totals Coverage Status
Change from base Build 11832739597: 0.08%
Covered Lines: 1390
Relevant Lines: 1874

💛 - Coveralls

Copy link
Contributor

github-actions bot commented Nov 14, 2024

BridgeStan not found at location specified by $BRIDGESTAN environment variable, downloading version 2.5.0 to /home/runner/.bridgestan/bridgestan-2.5.0
Done!
Model dogs produces error: ErrorException("log_density() failed with exception: Exception: bernoulli_lpmf: Probability parameter is inf, but must be in the interval [0, 1] (in '/home/runner/work/JuliaBUGS.jl/JuliaBUGS.jl/benchmark/stan/bugs_examples/vol1/dogs/dogs.stan', line 37, column 6 to line 38, column 62)\n")

Model Parameter Count Data Count Stan Density Time (µs) Stan Density Gradient Time (µs) JuliaBUGS Density Time with Graph Walk (µs) JuliaBUGS Density Gradient Time with ReverseDiff.jl(compiled tape) (µs)
rats 65 150 5.4422 8.00833 68.829 89.948
pumps 12 10 0.994276 1.30882 11.301 6.367
dogs 2 720 NA NA 177.276 153.277
seeds 26 21 2.53382 3.137 27.291 19.607
surgical_realistic 14 12 1.25452 1.68539 14.838 8.42267
magnesium 108 96 10.74 12.383 119.914 76.283
salm 22 18 2.42875 3.24056 20.538 12.343
equiv 15 20 2.53291 3.47525 18.675 15.269
dyes 9 30 1.00914 1.33523 11.897 13.5905
stacks 6 21 1.1822 1.78019 22.683 14.427
epil 303 236 34.334 39.9945 278.481 248.61
blockers 47 44 3.23378 3.68688 56.636 30.026
oxford 244 240 16.581 19.055 368.595 172.102
lsat 1006 5000 177.718 215.423 1858.4 1208.69
bones 33 422 73.147 91.05 410.944 179.731
mice 20 65 7.256 9.304 28.5835 38.933
kidney 64 58 10.745 16.34 61.404 70.842
leuk 18 714 20.488 26.721 212.678 201.907
leukfr 40 714 23.614 31.659 225.923 245.866

@sunxd3 sunxd3 merged commit 26ef662 into master Nov 16, 2024
13 checks passed
@sunxd3 sunxd3 deleted the sunxd/dot_call branch November 16, 2024 12:16
naseweisssss pushed a commit that referenced this pull request Nov 17, 2024
…inition (#237)

The motivation for the PR is initially to allow the use
[`product_distribution`](https://juliastats.org/Distributions.jl/stable/multivariate/#Distributions.product_distribution)
in JuliaBUGS model. By writing

```julia
@bugs begin
    x[1:2] ~ Distributions.product_distribution(fill(Normal(), 2))
end
```

It also enables

```julia
julia> foo(x) = x + 1
foo (generic function with 1 method)

julia> model_def = @bugs begin
           a = Main.foo(b)
       end
quote
    a = Main.foo(b)
end

julia> compile(model_def, (;b=2))
BUGSModel (parameters are in transformed (unconstrained) space, with dimension 0):

  Model parameters:

  Variable sizes and types:
    a: type = Int64
    b: type = Int64
```

i.e., an easier way to introduce external functions into JuliaBUGS. 

This can't fully replace `@register_primitive` yet, because using `Main`
module is relying on Julia runtime behavior and not very intuitive.
Examples:

```julia
julia> module TestModule
           bar(x) = x + 1
       end
Main.TestModule

julia> model_def = @bugs begin
           a = TestModule.bar(b)
       end
quote
    a = TestModule.bar(b)
end

julia> compile(model_def, (; b = 1))
ERROR: UndefVarError: `TestModule` not defined in `JuliaBUGS`
...
```

one would need to do 

```julia
julia> model_def = @bugs begin
           a = Main.TestModule.bar(b)
       end
quote
    a = Main.TestModule.bar(b)
end
```

also

```julia
julia> @testset "t" begin
           foo1(x) = x + 1
           model_def = @bugs begin
               a = Main.foo1(b)
           end
           compile(model_def, (; b= 1))
       end
t: Error During Test at REPL[18]:1
  Got exception outside of a @test
  UndefVarError: `foo1` not defined in `Main`
...
```
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.

2 participants