# Documentation: Programming guide

## Interface for Bayesian Belief Networks (BBN)

SPIRIT supports Bayesian belief networks. This chapter assumes a familiarity with the basic concepts of the Bayesian theory. Whenever a knowledge base fulfills a certain Bayesian property, its rules can be converted to Bayesian rules. Then it is possible to perform a Bayesian init to learn those rules.

In order to communicate with other Bayesian belief systems (like HUGIN ), the SPIRIT package includes the BayesBlock class, representing a Bayesian probability table.

### The Bayesian Property

A SPIRIT knowledge base fulfills the Bayesian property, if and only if the following conditions are true:

- it consists only of variables with a well-defined equality operation, i.e. boolean, nominal and number variables
- each rule has the following form:

- its premise is either empty (i.e. a tautology) or a conjunction of literals of different variables,

- its conclusio is a conjunction of literals. - the chain graph property holds (cf. [Meyer 1998])

### Converting a Knowledge Base

It is possible to convert a KBase to a Bayesian network, if the Bayesian property holds.
After calling the method `KBase.convertToBayes`

its rules are replaced by Bayesian rules.
In order to make only a temporary conversion, the knowledge base should be cloned
before the convert command is called.

### The Bayesian Init Method

Whenever a knowledge base fulfills the Bayesian property, it is possible to make use
of a very fast learning method. Calling the method `KBase.bayesianInit`

(assuming uniform distribution, i.e. a previous `KBase.reset`

call) will learn all active rules in a single
traversion of the internal LEG tree.

A typical usage of the Bayesian support method is described in the following code snippet:

`if(kb.isBayesian()) {`

`KBase bayesianKB = kb.clone();`

`bayesianKB.convertToBayes();`

`bayesianKB.reset();`

`bayesianKB.bayesianInit();`

`»Now the converted KBase with the same probabilities is available«`

`}`

### Exporting and Importing Bayesian Networks

SPIRIT provides a very powerful interface class to support communication with Bayesian Belief systems. The BayesBlock class represents a single probability table. Each BayesBlock consists of two disjoint sets of variables:

- the state variables (at least one variable)
- the parent state variables

The BayesBlock object stores a probability table for all configurations of the given variables. Example: Assuming a relationship between the parent states of the variables C and D and two state variables A and B (all of which are boolean) would create a table with 16 entries.

There are two ways of creating BayesBlocks for a Knowledge Base. The first technique
assumes that the KBase is actually Bayesian. In that case it is possible to call the
`KBase.getBayesBlocks`

method. This will create a list of BayesBlock objects representing all active rules
from the KBase. The other way is to create BayesBlocks directly, using the BayesBlock
constructor. Then you can fill the probability table manually. The latter technique is
especially suitable to import Bayesian files with a converter program.

After the creation of BayesBlocks, you can easily query or even change the given
probabilities in the tables. Afterwards it is possible to write them back into the
KBase. The method `KBase.addBayesianRules`

creates a set of rules from a
BayesBlock. Those rules can then be learned into the knowledge base, e.g. with the
bayesianInit method.