Tutorial explaining how to make infereces with the library.
Thus tutorial is using examples from the module Bayes.Examples. Please, refer to this module for documentation about how the example bayesian networks are created or loaded.
inferencesOnStandardNetwork is showing how to use variable elimination
and factor elimination to make inferences.
example is loaded to make its variables and its bayesian network available:
let ([winter,sprinkler,rain,wet,road],exampleG) = example
Then, we compute a prior marginal. Prior means that no evidence is used. A bayesian network is a factorisation of a distribution P(A B C ...). If you want to know the probability of only A, you need to sum out the other variables to eliminate them and get P(A). To compute this prior marginal using variable elimnation, you need to give an elimination order. The complexity of the computation is depending on the elimination order chosen.
For instance, if you want to compute the prior probability of rain, you can write:
priorMarginalexampleG [winter,sprinkler,wet,road] [rain]
Now, if you have observed that the grass is wet and want to take into account thios observation to compute the posterior probability of rain (after observation):
posteriorMarginalexampleG [winter,sprinkler,wet,road] [rain] [wet
If you want to combine several observations:
posteriorMarginalexampleG [winter,sprinkler,wet,road] [rain] [wet
There are several problems with variable elimination:
- You have to specify an elimination order
- If you want to compute another marginal (for instance probability of winter), you have to recompute everything.
But, there exists another category of elimination algorithms based upon factor elimination. They require the creation of an auxiliary data structure : the junction tree.
This tree is then used for computing all marginals (without having to recompute everything). The junction tree is equivalent to giving an elimination order.
So, the previous examples can also be computed with factor elimination. First, the junction tree must created:
let jt =
The junction tree being equivalent to an elimination order, the order chosen will
depend on a cost function. In the previous example, the cost function
is used. Other cost functions may be introduced in a futute version of this library.
Once the junction tree has been computd, it can be used to compute several marginals:
The function is called posterior and will compute posterior only when solme evidence has been introduced into the tree. Otherwise it is computing a prior.
To set evidence, you need to update the junction tree with new evidence:
If you want to compute the posterior for a combination of variables, you have two possibilities : either going back to the variable elimination methods. Or, introduce new nodes in the network to represent the query.
It is easily done through the new
logical function when building the Bayesian graph.
Once you have a node to represent a complex query, you can use it to compute a posterior. For instance, in the rain example, there is a new variable:
variable"rain and slippery road" (t :: Bool)
This variable is representing the assertion : rain True AND slippery road True. This variable can be used to answer different queries, like for instance:
let jt4 =
=:True] jt print "Posterior Marginal : probability of rain and road slippery if grass wet" let m =
posteriorjt4 roadandrain print m -- let jt5 =
=:False] jt print "Posterior Marginal : probability of rain and road slippery if grass wet and srinkler not used" let m =
Inferences with an imported network
There is a slight additional difficulty with imported networks : you need to create new data type to be able to set evidence.
For instance, in the cancer network there is a Coma variable with levels Present or Absent. When imported, those levels are imported as number. But, the evidence API in this library is requiring enumerations.
So, you need to create a
data Coma = Present | Absent deriving(Eq,Enum,Bounded)
and check that
Present is corresponding to the level 0 in the imported network.
Once this datatype is created, you can easily use the cancer network. First we load
the network and import the discrete variables of type
DV from the names of the nodes in the
network (not the label of the nodes).
print "CANCER NETWORK" (varmap,cancer) <-
exampleImportprint cancer let [varA,varB,varC,varE] = fromJust $ mapM (flip Map.lookup varmap) ["A","B","C","E"]
To avoid any errors with the future queries, some imported variables can be transformed into typed variables:
Once the variables are available, you can create the junction tree and start making inferences:
let jtcancer =
nodeComparisonForTriangulationcancer -- mapM_ (x -> putStrLn (show x) >> (print .
posteriorjtcancer $ x)) [varA,varB,varC,varE] -- print "UPDATED EVIDENCE" let jtcancer' =
=:Present] jtcancer mapM_ (x -> putStrLn (show x) >> (print .
posteriorjtcancer' $ x)) [varA,varB,varC,varE]
It is possible to compute the Most Probable Explanation for a set of observation. The syntax is very similar to the posterior computation with variable elimination:
The first list of variables (which should containg the evidence variables) is summed out. The second list of variables is used to maximize the probability. Both lists should contain all variables of the Bayesian network and are defining an elimination order.
The result of the mpe functions is a list of instantiations. The result is easier to read when the type information is
reintroduced. It can be done with the
In this example, all variables are boolean ones.
Soft evidence is more complex to handle since new node have to be added to the graph. And the node factor has to be changed when the node evidence is changed.
Here is how you could do it. First you load an example graph containg a soft evidence node created with
inferencesWithSoftEvidence = do let ((a,seNode),exampleG) =
Then, you create the junction tree as usual and force an hard evidence on the soft evidence node.
nodeComparisonForTriangulationexampleG jt' =
This junction tree cannot be used because the soft evidence node created in
exampleSoftEvidence has a
probability table which is meaningless. You need to update the probability table for a given soft evidence.
You create a new factor for this:
theNewFactor x = fromJust $
seseNode a x -- x % success for the sensor
This new factor, can then be used to do inference with different soft evidences.
print "Sensor 90%" print $ posterior (
changeFactor(theNewFactor 0.9) jt') a -- print "Sensor 50%" print $ posterior (
changeFactor(theNewFactor 0.5) jt') a -- print "Sensor 10%" print $ posterior (
changeFactor(theNewFactor 0.1) jt') a
Tests with the standard network
Tests with the cancer network
Type defined to set the evidence on the Coma variable from the cancer network.