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

Metropolis Hastings with All or Nothing for Likelihood #54

Open
wants to merge 5 commits into
base: metro_hastings
Choose a base branch
from

Conversation

jingnongqu
Copy link
Collaborator

No description provided.

Copy link
Collaborator

@shanest shanest left a comment

Choose a reason for hiding this comment

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

Awesome; great work, Jingnong!

I left a few focused / detail-oriented comments in the code. One high-level thing I'd like to discuss is: should we make mh_sample more modular / separated out? One thing I can imagine: grammar becomes a folder/package, and then we have a module for inference that defines the algorithms (mainly MH for now).

Similarly: I wonder whether the sampling step and the acceptance step should be factored out and passed in as arguments, instead of "hard-coded" in the same mega-method. What do you think?


indefinites_grammar = Grammar.from_yaml("indefinites/grammar.yml")
#indefinites_grammar = Grammar.from_module("indefinites.grammar_functions")
print(indefinites_grammar.parse("and(not(K+), or(N-, not(SE-)))").hm_sample(indefinites_grammar, [(universe.referents[2], True)]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's important for the indefinites example to not modify this file directly. The value indefinites_grammar is used in other scripts in the example.

Can you make an example demo-ing the MH sampling elsewhere (either its own file here, or a new sub-folder)?

src/ultk/language/grammar.py Show resolved Hide resolved
src/ultk/language/grammar.py Show resolved Hide resolved
src/ultk/language/grammar.py Show resolved Hide resolved
src/ultk/language/grammar.py Show resolved Hide resolved
if random.random() < mh_accept:
return(new_tree)

def prior(self, grammar: "Grammar") -> float:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Conceptually, I think I'd vote for prior belonging to Grammar and taking an expression as input (i.e. the grammar is the thing that assigns probabilities to expressions), but am open to being convinced otherwise

@@ -258,6 +343,9 @@ def add_rule(self, rule: Rule):
)
self._rules_by_name[rule.name] = rule

def probability(self, rule: Rule) -> float:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably worth exploring whether we can use cache or cached_property for this, so that it only gets computed once; see https://docs.python.org/3/library/functools.html

src/ultk/language/grammar.py Show resolved Hide resolved
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