Probabilistic programming in 30 lines of prolog
1 min read

Probabilistic programming in 30 lines of prolog

Realized prolog’s dynamic assertions can be used to implement simple probabilistic programming with rejection sampling. Can probably be made much prettier, perhaps adding free variables as arguments of output or using clause manipulation. But this works.

What we are doing here is generating X and Y from two random dice rolls and adding them up. Then we want to sample some values of the sum given that rolling X resulted in a 2. We could just set X to 2 and sample Y only, but I’ve tried to do it right allowing, if needed, for X to depend on other random variables.

Try sample_cond(10, X).

Source code.