Skip to content

Conversation

@devmotion
Copy link
Member

@devmotion devmotion commented Mar 7, 2022

No description provided.

julia> @model function demo(x)
μ ~ Normal()
if DynamicPPL.leafcontext(__context__) !== PriorContext()
Copy link
Member

Choose a reason for hiding this comment

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

The leafcontext is not exported and may change. So this example might be fragile. In the future, wt would be better to avoid calling these low-level internal APIs to just get the current (primitive) context. But this should be addressed in separate PRs.

cc @rikhuijzer you might want to take a look at how context works in DynamicPPL.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was not super happy about bringing up leafcontext here - the purpose of the macro is to ensure that users don't have to use internal variable names and functions, so this is really not in line with our goals. Maybe we should just add a warning that it's always accumulated and one has to work with internal unstable fumctionality if that's not OK?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is okay to keep the example - it is useful for the users, but add a (big) warning that this interface is internal, and may change in the future.

A side note: we would like to improve the context design, together with the model macro implementation, in the longer term. Hopefully, these improvements will remove the requirement of using leafcontext.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another alternative (or addition) would be separate macro (or a switch?) if the manual log probability corresponds to an observe statement. (One could also add something for assume statements explicitly, but I guess it is less common since then one also should add a sample to the varinfo - so maybe the observe behaviour should be the default?)

Copy link
Member

Choose a reason for hiding this comment

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

It does not feel critical to have multiple versions of @addlogprob! for now. Most people only use it inside MH or HMC samplers. But there is a need, we can add support for that later.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm mainly concerned about the, in most cases, incorrect results of logprior currently - which is the main reason for this annoying leafcontext approach. I think this could be fixed by identifying it with a custom observe statement instead of an arbitrary modification of the log probability. One could eg. change the macro syntax to

@addlogprob! val [assume=false] [observe=true]

Then it would still be possible to unconditionally modify the log probability but one could also limit it easily to evaluations that include the likelihood or the prior.

Copy link
Member Author

Choose a reason for hiding this comment

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

But in any case this is out of scope for this PR, I think.

@yebai
Copy link
Member

yebai commented Mar 7, 2022

Very nice example, thanks @devmotion!

devmotion and others added 4 commits March 7, 2022 10:41
Co-authored-by: Hong Ge <3279477+yebai@users.noreply.github.com>
@devmotion devmotion mentioned this pull request Mar 7, 2022
@devmotion
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Mar 7, 2022
@rikhuijzer
Copy link
Contributor

Very nice!! 🚀

@bors
Copy link
Contributor

bors bot commented Mar 7, 2022

@bors bors bot changed the title Extend docstring of @addlogprob! [Merged by Bors] - Extend docstring of @addlogprob! Mar 7, 2022
@bors bors bot closed this Mar 7, 2022
@bors bors bot deleted the dw/addlogprob branch March 7, 2022 13:21
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