def compute_proposal_probability(self,grammar, t1, t2, resampleProbability=lambdaOne, recurse=True): chosen_node1 , chosen_node2 = least_common_difference(t1,t2) lps = [] if chosen_node1 is None: # any node in the tree could have been copied for node in t1: could_be_source = lambda x: 1.0 * nodes_equal_except_parents(grammar,x,node) * resampleProbability(x) lp_of_choosing_source = (nicelog(t1.sample_node_normalizer(could_be_source) - could_be_source(node)) - nicelog(t1.sample_node_normalizer(resampleProbability))) lp_of_choosing_target = t1.sampling_log_probability(chosen_node1,resampleProbability=resampleProbability) lps += [lp_of_choosing_source + lp_of_choosing_target] else: # we have a specific path up the tree while chosen_node1: could_be_source = lambda x: 1.0 * nodes_equal_except_parents(grammar,x,chosen_node2) * resampleProbability(x) lp_of_choosing_source = nicelog(t1.sample_node_normalizer(could_be_source)) - nicelog(t1.sample_node_normalizer(resampleProbability)) lp_of_choosing_target = t1.sampling_log_probability(chosen_node1,resampleProbability=resampleProbability) lps += [lp_of_choosing_source + lp_of_choosing_target] if recurse: chosen_node1 = chosen_node1.parent chosen_node2 = chosen_node2.parent else: chosen_node1 = None return logsumexp(lps)
def compute_proposal_probability(self, grammar, t1, t2, resampleProbability=lambdaOne, recurse=True): # NOTE: This is not strictly necessary since we don't actually have to sum over trees # if we use an auxiliary variable argument. But this fits nicely with the other proposers # and is not much slower. chosen_node1 , chosen_node2 = least_common_difference(t1,t2) lps = [] if chosen_node1 is None: # any node in the tree could have been regenerated for node in t1: lp_of_choosing_node = t1.sampling_log_probability(node,resampleProbability=resampleProbability) with BVRuleContextManager(grammar, node.parent, recurse_up=True): lp_of_generating_tree = grammar.log_probability(node) lps += [lp_of_choosing_node + lp_of_generating_tree] else: # we have a specific path up the tree while chosen_node1: lp_of_choosing_node = t1.sampling_log_probability(chosen_node1,resampleProbability=resampleProbability) with BVRuleContextManager(grammar, chosen_node2.parent, recurse_up=True): lp_of_generating_tree = grammar.log_probability(chosen_node2) lps += [lp_of_choosing_node + lp_of_generating_tree] if recurse: chosen_node1 = chosen_node1.parent chosen_node2 = chosen_node2.parent else: chosen_node1 = None return logsumexp(lps)
def compute_proposal_probability(self,grammar,t1,t2,resampleProbability=lambdaOne): node_1,node_2 = least_common_difference(t1,t2) if (node_1 and node_2 and any([nodes_are_roughly_equal(arg,node_2) for arg in None2Empty(node_1.args)])): lp_choosing_node_1 = t1.sampling_log_probability(node_1,dp_rp(resampleProbability)) lp_choosing_child = -nicelog(len(list_replicating_children(node_1))) return lp_choosing_node_1 + lp_choosing_child else: # no possible deletion return -Infinity
def compute_proposal_probability(self, grammar, t1, t2, resampleProbability=lambdaOne): node_1, node_2 = least_common_difference(t1, t2) if (node_1 and node_2 and any([ nodes_are_roughly_equal(arg, node_1) for arg in None2Empty(node_2.args) ])): lp_choosing_node_1 = t1.sampling_log_probability( node_1, resampleProbability=lambda t: can_insert_FunctionNode( t, grammar) * resampleProbability(t)) lp_choosing_rule = -nicelog( len( filter(can_insert_GrammarRule, grammar.rules[node_1.returntype]))) lp_choosing_replacement = -nicelog( len( filter( lambda i: node_2.args[i].returntype == node_1. returntype, xrange(len(node_2.args))))) lp_generation = [] for arg in node_2.args: if not (arg.name == node_1.name and arg.returntype == node_1.returntype and arg.args == node_1.args ): # if the nodes are significantly different with BVRuleContextManager(grammar, node_2, recurse_up=True): lp_generation += [grammar.log_probability(arg)] lp_copy_making_node_2 = lp_choosing_rule + lp_choosing_replacement + sum( lp_generation) return lp_choosing_node_1 + lp_copy_making_node_2 else: return -Infinity # the trees cannot be identical if we performed an insertion
def compute_proposal_probability(self, grammar, t1, t2, resampleProbability=lambdaOne): node_1, node_2 = least_common_difference(t1, t2) if (node_1 and node_2 and any([ nodes_are_roughly_equal(arg, node_2) for arg in None2Empty(node_1.args) ])): lp_choosing_node_1 = t1.sampling_log_probability( node_1, dp_rp(resampleProbability)) lp_choosing_child = -nicelog(len( list_replicating_children(node_1))) return lp_choosing_node_1 + lp_choosing_child else: # no possible deletion return -Infinity
def compute_proposal_probability(self, grammar, t1, t2, resampleProbability=lambdaOne, recurse=True): chosen_node1, chosen_node2 = least_common_difference(t1, t2) lps = [] if chosen_node1 is None: # any node in the tree could have been copied for node in t1: could_be_source = lambda x: 1.0 * nodes_equal_except_parents( grammar, x, node) * resampleProbability(x) lp_of_choosing_source = ( nicelog( t1.sample_node_normalizer(could_be_source) - could_be_source(node)) - nicelog(t1.sample_node_normalizer(resampleProbability))) lp_of_choosing_target = t1.sampling_log_probability( chosen_node1, resampleProbability=resampleProbability) lps += [lp_of_choosing_source + lp_of_choosing_target] else: # we have a specific path up the tree while chosen_node1: could_be_source = lambda x: 1.0 * nodes_equal_except_parents( grammar, x, chosen_node2) * resampleProbability(x) lp_of_choosing_source = nicelog( t1.sample_node_normalizer(could_be_source)) - nicelog( t1.sample_node_normalizer(resampleProbability)) lp_of_choosing_target = t1.sampling_log_probability( chosen_node1, resampleProbability=resampleProbability) lps += [lp_of_choosing_source + lp_of_choosing_target] if recurse: chosen_node1 = chosen_node1.parent chosen_node2 = chosen_node2.parent else: chosen_node1 = None return logsumexp(lps)
def compute_proposal_probability(self,grammar,t1,t2,resampleProbability=lambdaOne): node_1,node_2 = least_common_difference(t1,t2) if (node_1 and node_2 and any([nodes_are_roughly_equal(arg,node_1) for arg in None2Empty(node_2.args)])): lp_choosing_node_1 = t1.sampling_log_probability(node_1,resampleProbability=can_insert_FunctionNode) lp_choosing_rule = -nicelog(len(filter(can_insert_GrammarRule, grammar.rules[node_1.returntype]))) lp_choosing_replacement = -nicelog(len(filter( lambda i: node_2.args[i].returntype == node_1.returntype, xrange(len(node_2.args))))) lp_generation = [] for arg in node_2.args: if not (arg.name == node_1.name and arg.returntype == node_1.returntype and arg.args == node_1.args): # if the nodes are significantly different with BVRuleContextManager(grammar, node_2, recurse_up=True): lp_generation += [grammar.log_probability(arg)] lp_copy_making_node_2 = lp_choosing_rule + lp_choosing_replacement + sum(lp_generation) return lp_choosing_node_1 + lp_copy_making_node_2 else: return -Infinity # the trees cannot be identical if we performed an insertion