ProbLog broadly follows Prolog syntax, up to a few exceptions. You may reasonably assume that anything you can do in Prolog, you can do in ProbLog as well. For a detailed list of Prolog functions available in ProbLog, see here.
Installing details can be found on the ProbLog website and there is also an interactive interface online.
ProbLog is a fairly recent programming paradigm, which came out of machine learning research. This means that sometimes, the implementation or the documentation may be lacking. This is the price to pay for working with cutting-edge technology :)
First steps in ProbLog
In ProbLog, fully grounded atoms (i.e. without any variables) are associated with a probability (i.e. a number between 0 and 1). Our first example will focus on weather and pupils in primary school.
Single-pupil
First, let’s consider a single pupil in primary school, Alice. Alice can be happy or not happy. We will model this using the atom happy. There is of course some randomness to Alice’s mood, and we’d like to compute the probability that Alice is happy, given some amount of evidence.Let’s start with arity-0 predicates. As we saw in class, .2::raining. means that "it is raining with probability 20%".
Atoms (1)
Write a clause to express "Alice is happy with probability 50%" using the atom alice_is_happy.
Whether Alice is happy also depends on whether there is a school test and whether the class is fun.
Atoms (2)
Write a clause to express "There is a school test with probability 5%" using the atom school_test.
Atoms (3)
Write a clause to express "The class is fun with probability 10%" using the atom fun_class.
Now let’s add rules that express the relationship between these atoms. As a reminder,
.7::a :- b. means that if b, then a with probability .7
.7::\+a :- b. means that if b, then \+ a with probability .7
\+ a means "not a"
Rules (4)
Write a rule to express "Alice is happy if the class was fun, with probability 30%" using the atoms defined previously.
Rules (5)
Write a rule to express "Alice is not happy if it’s raining, with probability 70%" using the atoms defined previously, including raining, used in the introduction of this section.
Rules (6)
Write a rule to express "Alice is not happy if there is a test, with probability 90%" using the atoms defined previously.
Is Alice happy? (1)
Taking all the rules defined until now, what is the probability that Alice is happy, knowing that the class was fun?
Alice, Bob, Charlie, Dorothy and Emily
Alice goes to school with other kids. Some of them are friends, some aren’t. Whether Alice’s friends are happy has an influence on whether she is happy. Below is some Prolog that encodes who is friends with who:
friend(alice, charlie). % Alice is friends with Charlie friend(charlie, bob). friend(dorothy, emily). friend(X,Y) :- Y @< X, friend(Y,X). % symmetry of friendship friend(X,Y) :- X\=Y, friend(X,Z), friend(Z,Y). % transitivity of friendship
Since there are multiple pupils, we need to update our previous predicates. The predicate happy now takes 1 argument, the pupil. happy(P) is whether pupil P is happy.
Pupils (1)
Write a rule to express "A pupil P is happy if one of their friends Q is happy, with probability 80%". You may test whether the syntax of your rule is correct by running it through the ProbLog interpreter.
Is Alice happy? (2)
Taking all the rules defined until now (and dropping any evidence about the class being fun or not), what is the probability that Alice is happy, knowing that there is a school test, that Bob is happy, and Charlie is not happy? Don’t forget to adapt the happy predicate in the rules previously defined to take 1 argument.
Successive samples
Flipping coins
As a warmup, write a prolog predicate flips(+PastFlips,-NewFlips) where PastFlips is a list containing heads and tails, and NewFlips is the same as PastFlips but with a new element added at the beginning. You should assume access to a predicate coin/1, defined by two clauses: coin(heads). coin(tails).
After submitting your answer, read the expected answer carefully, as it contains important information for the following questions.
Alice and Bob like marbles. They also like probabilities. Alice proposes the following gamble to Bob:
Bob puts one marble on the table as a stake.
Alice flips coin: if it’s heads, Bob wins and Alice gives him as many marbles as are on stake (in this case 1). If it’s tails, Bob loses. But Bob can try to win his marbles back:
The stake is doubled, and is now 2. If Bob can afford it, he can place 2 marbles on the table as a stake.
Alice flips another coin: if it’s heads, Bob wins and Alice gives him as many marbles as are on stake (in this case 1). If it’s tails, Bob loses. But Bob can try to win his marbles back...
The stake is doubled at every step.
Marbles
Write a ProbLog program that can compute the outcome of the game. Formally, write a predicate game(Win) that simulates a game, where Win is equal to the amount of marbles that Bob has won. Assume that Bob starts with 15 marbles. Win can be positive or negative. To help you write this program: you should write a predicate next(+Marbles,+Stake,+Flips,-Result,-Sequence) where Marbles is the number of marbles Bob has, Stake is the current value of the stake, Flips is the sequence of flips so far, Result is the number of marbles Bob has at the end of the game, and Sequence is the sequence of flips at the end of the game. Result and Sequence serve to store values at the end of the game, like we did in the second lab session with the actions of the monkey. next will call to the code provided to you in the answer of the previous question.
Biased coin
In the previous example, because the coin is fair, the expectation of Win is equal to 0. What is Bob’s expected Win if the coin is biased and lands on tails with probability .6?
As a reminder, if Bob can win xi marbles with probability pi, Bob’s expected win is \(\sum \limits_i x_i p_i\).