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

prob and logprob macros for simple model queries #997

Merged
merged 19 commits into from
Dec 25, 2019
Merged

Conversation

mohamed82008
Copy link
Member

@mohamed82008 mohamed82008 commented Nov 26, 2019

This PR is based on #965. In this PR, I implemented a similar syntax to the one proposed in #989. Here is an example:

using Turing

@model demo(y, ::Type{T} = Float64) where {T} = begin
	x = Vector{T}(undef, 10)
	x .~ Normal()
	y .~ Normal.(x, 1.0)
end
chain = sample(demo(rand(10)), HMC(0.64, 1), 1000)

# log likelihood
logprob"y = rand(10) | model = demo, x = rand(10)" 

# all log likelihoods, one for each sample in chain
logprob"y = rand(10) | chain = chain"

# log prior
logprob"x = rand(10) | model = demo" 

The posterior and joint probabilities are next.

Note the use of a string macro which is handled just like a normal macro except there is no syntax highlighting. The main difference under the hood is that this one doesn't go through the Julia parser to parse the input expression. The precedence of | in the Julia parser is quite high compared to how we want it here, so I chose not to go against the flow and just used a string macro.

@mohamed82008
Copy link
Member Author

Just some dispatch bugs left in Inference, then I will add tests.

@mohamed82008
Copy link
Member Author

mohamed82008 commented Dec 16, 2019

Here are the implemented and remaining features using the above model as an example:

  • Query the prior using prob"x = rand(3) | model = demo"
  • Query the likelihood using prob"y = rand(3) | x = rand(3), model = demo"
  • Query the likelihood given every sample in the chain using prob"y = rand(3) | chain = chain, model = demo"
  • Query the joint probability using prob"x = rand(3), y = rand(3) | model = demo"

In a future PR, I will implement:

  • Create a prior probability distribution using prob"x | model = demo"
  • Create the likelihood probability distribution using prob"y | x = rand(3), model = demo"
  • Create a joint probability distribution using prob"x, y | model = demo"
  • Create the posterior predictive distribution of y using prob"y | chain = chain, model = demo"

@mohamed82008
Copy link
Member Author

Because this PR is already quite large, I will leave the syntax for creating a distribution from the model for another PR. For now, I will just finish the joint probability querying and update the docs, then this PR will be ready for review.

@yebai
Copy link
Member

yebai commented Dec 17, 2019

Because this PR is already quite large, I will leave the syntax for creating a distribution from the model for another PR. For now, I will just finish the joint probability querying and update the docs, then this PR will be ready for review.

Sounds good.

@mohamed82008
Copy link
Member Author

Just docs left and maybe a couple more tests.

Copy link
Member

@trappmartin trappmartin left a comment

Choose a reason for hiding this comment

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

This look really cool. I left a few minor remarks.

src/Turing.jl Outdated Show resolved Hide resolved
src/Turing.jl Show resolved Hide resolved
src/Turing.jl Show resolved Hide resolved
src/core/compiler.jl Show resolved Hide resolved
src/core/compiler.jl Show resolved Hide resolved
src/core/compiler.jl Show resolved Hide resolved
src/core/compiler.jl Show resolved Hide resolved
src/core/compiler.jl Outdated Show resolved Hide resolved
src/core/prob_macro.jl Show resolved Hide resolved
src/core/probmacro.jl Outdated Show resolved Hide resolved
@mohamed82008
Copy link
Member Author

Thanks @trappmartin for your review. I will add the missing docstrings and comments while updating the docs, shortly.

@mohamed82008
Copy link
Member Author

DistributionsAD-Zygote bug

@mohamed82008
Copy link
Member Author

Docs, docs and more docs now.

Copy link
Member

@trappmartin trappmartin left a comment

Choose a reason for hiding this comment

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

See comments for tests.

test/core/prob_macro.jl Show resolved Hide resolved
test/core/prob_macro.jl Show resolved Hide resolved
test/core/prob_macro.jl Show resolved Hide resolved
test/core/prob_macro.jl Show resolved Hide resolved
@trappmartin
Copy link
Member

This PR feels like a Christmas present. 🎁

@trappmartin
Copy link
Member

I’m happy to approve once we have the issues which discuss the special cases.

Copy link
Member

@cpfiffer cpfiffer left a comment

Choose a reason for hiding this comment

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

I've got no issues -- looks like @trappmartin covered pretty much everything. This is a spectacular PR -- I'm very pleased with the stuff we'll be able to do.

@mohamed82008
Copy link
Member Author

Some spacings are off, and the docs need an update. Then a rebase and we should be ready for a merge. Should be hopefully done by tomorrow. Would be cool to merge this on Christmas day!

@mohamed82008 mohamed82008 changed the title [WIP] prob and logprob macros for simple model queries prob and logprob macros for simple model queries Dec 24, 2019
@mohamed82008
Copy link
Member Author

If tests pass and there are no further comments, this is ready for merge.

@mohamed82008
Copy link
Member Author

The error is due to a Pkg bug. The only workarounds I know are to either limit our MCMCChains compat to pre-1.0 or to get a new release for CmdStan that supports MCMCChains 1.0.

@mohamed82008
Copy link
Member Author

StanJulia/CmdStan.jl#76

@mohamed82008
Copy link
Member Author

We can also just drop CmdStan tests for now, or merge anyways and hope for a new CmdStan release soon.

@cpfiffer
Copy link
Member

We could also drop the MCMCChains compat to 0.4, not much changed between 0.4 and 1.0. Then we can keep the CmdStan tests in.

@mohamed82008
Copy link
Member Author

We can do that but that won't fix the issue. There is no version of CmdStan that supports MCMCChains 1.0 yet. My understanding is that Pkg is struggling to downgrade MCMCChains from 1.0 to the perfectly compatible 0.4 because of a bug. So we have to either stop Pkg from thinking MCMCChains 1.0 is valid to begin with (by limiting our MCMCChains compat in Turing to pre-1.0) or provide a version of CmdStan that supports MCMCChains 1.0 and hence Pkg doesn't need to downgrade anything. This is my best understanding of the dependency error we have.

@mohamed82008
Copy link
Member Author

We could also drop the MCMCChains compat to 0.4

Oh I think I misread your comment. If you mean to only support MCMCChains 0.4 then yes this should fix the error IIUC.

@mohamed82008
Copy link
Member Author

Pkg error resolved, tests are running.

@trappmartin
Copy link
Member

Looks good so far

@mohamed82008
Copy link
Member Author

As always @goedman comes and saves the day, thanks Rob!

@trappmartin
Copy link
Member

Looks like this can be merged.

@yebai yebai merged commit 5db142b into master Dec 25, 2019
@delete-merged-branch delete-merged-branch bot deleted the mt/prob_macro branch December 25, 2019 12:17
@yebai
Copy link
Member

yebai commented Dec 25, 2019

Thanks, @mohamed82008!

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.

None yet

5 participants