Created
October 5, 2012 22:42
-
-
Save Pet3ris/3842888 to your computer and use it in GitHub Desktop.
rejection sampling conditioner in prolog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Two dices are thrown and the results added up. | |
% We sample when the sum is conditioned on the first dice landing on 2. | |
output(S) :- | |
randvar(a, dice(X), X), | |
dice(Y), | |
S is X + Y. | |
dice(X) :- | |
X is random(6) + 1. | |
:- dynamic randvar/3. | |
randvar(_, Goal, _) :- | |
call(Goal). | |
sample_cond(N, Xs) :- | |
asserta(( | |
randvar(a, Goal, X) :- | |
!, | |
call(Goal), | |
X = 2)), | |
sample_subcond(N, Xs). | |
sample_subcond(0, []). | |
sample_subcond(N, [X | Xs]) :- | |
repeat, | |
output(X), | |
K is N - 1, | |
sample_subcond(K, Xs). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment