Tossing coins

Basic ProbLog: Probabilistic facts and clauses

To illustrate the basics of the ProbLog language, let’s start with a simple example. Suppose we have two coins. The first coin is fair (when tossed, it will land on heads with 50% probability), the second coin is biased (it will land on heads with 60% probability). What is now the probability that, if we toss the coins, both will land on heads?

The ProbLog program for this problem is shown below. Like any ProbLog program, it consists of two parts: a set of probabilistic facts and a set of rules in the form of clauses. The clauses are deterministic rules, as in regular Prolog. In this case, we need one probabilistic fact for each coin being tossed. For instance, 0.6::heads2 indicates that the second coin lands on heads with probability 0.6. In addition we also need a clause that defines what it means for both coins to land on heads: twoHeads :- heads1, heads2.

% Probabilistic facts: 0.5::heads1. 0.6::heads2. % Rules: twoHeads :- heads1, heads2. % Queries: query(heads1). query(heads2). query(twoHeads).

To try this example, press the ‘Evaluate’ button. The output field shows the progress of inference, and when finished also the results (scroll down in the box to see this). Since we have no evidence, the probability of the probabilistic atoms (i.e., atoms occurring in the probabilistic facts) is simply equal to their prior probability: P(heads1)=0.5 and P(heads2)=0.6. The probability of both coins landing on heads is the product of both probabilities: P(twoHeads)=0.3.

Note that in the input field of the above screen (and all others in this tutorial) you can modify the program, evidence and queries as you like, and re-run inference to assess the effect. For instance, paste the following at the bottom of the input field: evidence(twoHeads,false). (including the “.” at the end) and press ‘Evaluate’. We are now asking for the posterior probabilities of the query atoms given the evidence that the atom twoHeads is false. We obtain P(heads1)=0.2857 and P(heads2)=0.42857 (of course P(twoHeads) is now 0.0).

ProbLog syntax documentation

Noisy-or: Multiple rules for the same head

Let’s now consider a variant of the above example in which we are interested not in both coins landing on heads, but in at least one of them landing on heads. This example shows an aspect of the ProbLog language that is useful in many scenarios: multiple rules with the same head lead to a noisy-or effect in ProbLog.

% Probabilistic facts: 0.5::heads1. 0.6::heads2. % Rules: someHeads :- heads1. someHeads :- heads2. % Queries: query(someHeads).

We can calculate the probability of the atom someHeads using the typical noisy-or formula: P(someHeads) = 1 - (1-P(heads1)) (1-P(heads2)) = 1 - (1-0.5) (1-0.6) = 0.8. To verify this result, press ‘Evaluate’.

First-order

The above programs are all propositional. As an example of a first-order ProbLog program, let’s consider a generalization of the above example. Suppose we toss N coins (which are all biased with probability 0.6 for heads) and we are interested in the probability of at least one of them landing on heads. We can model this in ProbLog as shown below. The background information specifies which coins are considered. Before, a probabilistic fact was used for each individual coin, while now we use a non-ground probabilistic fact lands_heads(_). The rule heads(C) :- coin(C), lands_heads(C) states that each coin which lands on heads is heads up, because of the probabilistic fact 0.6::lands_heads(_), this rule will fire with probability 0.6. The rule someHeads :- heads(_) will fire for each coin that is heads up. If we assume that we have N coins, the probability of at least one coin landing on heads is P(someHeads) = 1-(1-0.6)^N. Below we show how to model this in ProbLog in case N=4.

% Probabilistic facts: 0.6::lands_heads(_). % Background information: coin(c1). coin(c2). coin(c3). coin(c4). % Rules: heads(C) :- coin(C), lands_heads(C). someHeads :- heads(_). % Queries: query(someHeads).

By pressing ‘Evaluate’, you can verify that P(someHeads)=0.9744.

Probabilistic clauses

In the previous example, the predicate lands_heads was introduced to ensure that the rule heads(C) :- coin(C) fires with probability 0.6. ProbLog allows for so-called probabilistic clauses which are syntactic sugar for compactly specifying probabilistic rules without the need of introducing auxiliary predicates. The rule can thus be written as a probabilistic clause without the need for lands_head as follows: 0.6::heads(C) :- coin(C). The previous example is rewritten using the probabilistic clause below:

% Probabilistic facts: 0.6::heads(C) :- coin(C). % Background information: coin(c1). coin(c2). coin(c3). coin(c4). % Rules: someHeads :- heads(_). % Queries: query(someHeads).

By pressing ‘Evaluate’, you can verify that P(someHeads)=0.9744.

Probabilistic clauses documentation