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

Ancestral Sampling and Bayes Ball Algorithm #233

Merged
merged 35 commits into from
Nov 21, 2024

Conversation

naseweisssss
Copy link
Collaborator

No description provided.

@coveralls
Copy link

coveralls commented Nov 4, 2024

Pull Request Test Coverage Report for Build 11937874024

Details

  • 0 of 69 (0.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-2.5%) to 71.907%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/experimental/ProbabilisticGraphicalModels/bayesnet.jl 0 69 0.0%
Files with Coverage Reduction New Missed Lines %
src/experimental/ProbabilisticGraphicalModels/bayesnet.jl 2 0.0%
Totals Coverage Status
Change from base Build 11897946581: -2.5%
Covered Lines: 1395
Relevant Lines: 1940

💛 - Coveralls

Copy link
Contributor

github-actions bot commented Nov 4, 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.5424 8.222 66.704 85.56
pumps 12 10 1.02228 1.34027 13.881 6.32425
dogs 2 720 NA NA 174.521 161.272
seeds 26 21 2.55755 3.196 28.163 19.626
surgical_realistic 14 12 1.27196 1.63528 14.848 8.399
magnesium 108 96 10.665 12.258 126.637 74.159
salm 22 18 2.3895 3.08244 21.199 12.5385
equiv 15 20 2.447 3.38 19.016 15.128
dyes 9 30 1.0119 1.34159 11.9475 13.009
stacks 6 21 1.15536 1.69435 22.952 14.577
epil 303 236 32.972 38.953 294.511 239.677
blockers 47 44 3.32737 3.81571 58.089 28.103
oxford 244 240 16.181 18.785 355.364 173.694
lsat 1006 5000 170.499 211.685 1917.05 1175.56
bones 33 422 72.315 90.75 433.76 183.463
mice 20 65 7.32125 9.33733 28.883 37.49
kidney 64 58 11.031 16.48 65.803 77.1295
leuk 18 714 20.819 27.2705 210.744 201.802
leukfr 40 714 24.235 32.03 226.628 257.591

@sunxd3
Copy link
Member

sunxd3 commented Nov 4, 2024

thanks @naseweisssss, could you resolve the formatter's complaints, also maybe we should have a slightly more complicated model for the test? also, it'll be helpful to run the ancestral sampling multiple time and look at the distributions of the variables.

@naseweisssss naseweisssss self-assigned this Nov 4, 2024
naseweisssss and others added 10 commits November 4, 2024 13:43
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
linting

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

JuliaFormatter

[JuliaFormatter] reported by reviewdog 🐶


[JuliaFormatter] reported by reviewdog 🐶


[JuliaFormatter] reported by reviewdog 🐶


[JuliaFormatter] reported by reviewdog 🐶


[JuliaFormatter] reported by reviewdog 🐶


[JuliaFormatter] reported by reviewdog 🐶

Copy link
Member

@sunxd3 sunxd3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review, in general this is very good job! Thanks
I left some comments, but all minor.

A suggestion: next time it'll be better to add some descriptions, it doesn't just help me understand the algorithms, but also explain why you might choose some methods over others. You can also factor a lot of these writings in future reports.

Particularly for implementation of algorithm, a small example would help a lot, giving some correctness proof (sketches are fine) would be great. Doing this kind of writing is something I didn't do very well at, but it is worthwhile to spent some time on.

src/experimental/ProbabilisticGraphicalModels/bayesnet.jl Outdated Show resolved Hide resolved
return true
end

function has_conditioned_descendant(bn::BayesianNetwork, node_id::Int, z_ids::Set{Int})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slight confusion here, why the ball can pass through if the collider child has conditioned descendants? and this could also incur repeated computations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pmc.ncbi.nlm.nih.gov/articles/PMC6089543/figure/F4/
image

I am considering this. Please let me know what do you think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it might incur repeated computations, but its a trade off for ensuring all possible paths are considered. I am thinking of keeping a visited nodes list, but unsure if that will cause conflict

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this, this article is talking about causal effects. But here we are only testing conditional independence. These are related but not entirely identical concepts.

Also I think this is a good example why it is important to communicate why you implement the algorithms in the way you do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i see makes sense

sunxd3 and others added 11 commits November 17, 2024 08:39
…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`
...
```
@sunxd3
Copy link
Member

sunxd3 commented Nov 20, 2024

@naseweisssss thanks a lot for the effort, congrats for the first PR!

@naseweisssss naseweisssss merged commit f07d31b into master Nov 21, 2024
13 checks passed
@naseweisssss naseweisssss deleted the rylin/bayesnet_implementations branch November 21, 2024 12:15
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.

3 participants