Skip to content

Commit 89c1f0f

Browse files
committed
Merge branch 'dev'
2 parents 265c16b + 772aadd commit 89c1f0f

File tree

333 files changed

+9211
-9058
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

333 files changed

+9211
-9058
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
0.1.6
2+
===========
3+
4+
- fixed #44, #40.
5+
- New class hierarchy for Models:
6+
* the main interface is `Model`,
7+
* the graphical models now implements the `GraphicalModel` interface,
8+
* basic _Directed Acyclic Graph_ is `DAGModel`,
9+
* the `BayesianNetwork` class is now a specialized case of `DAGModel`,
10+
- Added *Join Tree* and *Belief Propagation* algorithms for BayesianFactor-based models.
11+
- Added algorithms for entropy of Bayesian and Credal networks.
12+
- General code cleanup (mainly code style).
13+
- Removed main methods from the library and moved to separate unit test classes.
14+
- Updated tests and removed old experiments.
15+
- Removed examples relative to Adaptive project.
16+
- Updated tutorials.
17+
118
0.1.5
219
===========
320

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ An example of exact inference in a credal network is given below.
1212
```java
1313
import ch.idsia.crema.factor.credal.vertex.VertexFactor;
1414
import ch.idsia.crema.inference.ve.CredalVariableElimination;
15-
import ch.idsia.crema.model.ObservationBuilder;
16-
import ch.idsia.crema.model.Strides;
17-
import ch.idsia.crema.model.graphical.SparseModel;
15+
import ch.idsia.crema.core.ObservationBuilder;
16+
import ch.idsia.crema.core.Strides;
17+
import ch.idsia.crema.model.graphical.GraphicalModel;
1818

1919
public class Starting {
2020
public static void main(String[] args) {
@@ -24,7 +24,7 @@ public class Starting {
2424
/* CN defined with vertex Factor */
2525

2626
// Define the model (with vertex factors)
27-
SparseModel<VertexFactor> model = new SparseModel<>();
27+
GraphicalModel<VertexFactor> model = new DAGModel<>();
2828
int A = model.addVariable(3);
2929
int B = model.addVariable(2);
3030

@@ -37,7 +37,6 @@ public class Starting {
3737

3838
model.setFactor(A,fu);
3939

40-
4140
// Define the credal set of the child
4241
VertexFactor fx = new VertexFactor(model.getDomain(B), model.getDomain(A));
4342
fx.addVertex(new double[]{1., 0.,}, 0);
@@ -69,7 +68,7 @@ Add the following code in the pom.xml of your project:
6968
<dependency>
7069
<groupId>ch.idsia</groupId>
7170
<artifactId>crema</artifactId>
72-
<version>0.1.5</version>
71+
<version>0.1.6</version>
7372
<scope>compile</scope>
7473
</dependency>
7574
</dependencies>

docs/index.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ Crema (CREdal Models Algorithms) is an Java library for inference in credal netw
4949

5050
notes/modeldef
5151
notes/guideinference
52+
notes/bayesianinference
5253
JavaDoc <https://idsia.github.io/crema/javadoc/>
5354

5455

56+
.. toctree::
57+
:includehidden:
58+
:maxdepth: 1
59+
:caption: Tutorials
5560

56-
57-
58-
61+
tutorials/factors.rst
62+
tutorials/domains.rst
63+
tutorials/networks.rst
64+
tutorials/bayesian_inference.rst
5965

6066

6167
.. toctree::

docs/notes/bayesianinference.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Bayesian Inference
2+
==============================
3+
4+
Crema provides useful algorithm for precise inference on Bayesian networks.
5+
6+
Belief Propagation
7+
------------------------------
8+
9+
The ``BeliefPropagation`` inference algorithm works on the ``BayesianFactors`` of a ``BayesainNetwork``.
10+
11+
First instantiate the inference algorithm object using the model. The inference engine will build an internal
12+
``JunctionTree`` that will be used for the following queries. Then remember to call ``fullPropagation()`` to update
13+
the model. This will return the posterior of a variable considered the root of the internal ``JunctionTree``.
14+
15+
.. literalinclude:: ../../examples/docs/BeliefPropagation.java
16+
:language: java
17+
:lines: 42-44
18+
19+
20+
To perform an inference on a variable, as an example if you want the marginal of ``P(A)``, use the ``query()`` method as
21+
in the example below:
22+
23+
.. literalinclude:: ../../examples/docs/BeliefPropagation.java
24+
:language: java
25+
:lines: 55-60
26+
27+
28+
If you want to use evidence, you need to create first a ``TIntIntHashMap`` that will include the state of the various
29+
variables, in the belo case we query for ``P(A | B=0)``:
30+
31+
.. literalinclude:: ../../examples/docs/BeliefPropagation.java
32+
:language: java
33+
:lines: 62-69
34+
35+
36+
Full example:
37+
38+
.. literalinclude:: ../../examples/docs/BeliefPropagation.java
39+
:language: java

docs/notes/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Crema can be easily included at any maven project. For this, add the following c
1616
<dependency>
1717
<groupId>ch.idsia</groupId>
1818
<artifactId>crema</artifactId>
19-
<version>0.1.4</version>
19+
<version>0.1.6</version>
2020
<scope>compile</scope>
2121
</dependency>
2222
</dependencies>

examples/BeliefPropagation.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ch.idsia.crema.inference.bp;
2+
3+
import ch.idsia.crema.factor.bayesian.BayesianFactor;
4+
import ch.idsia.crema.model.graphical.BayesianNetwork;
5+
import gnu.trove.map.hash.TIntIntHashMap;
6+
7+
public class BeliefPropagationTest {
8+
9+
public static void main(String[] args) {
10+
/* Define your Bayesian Network model */
11+
12+
BayesianNetwork model = new BayesianNetwork();
13+
A = model.addVariable(2);
14+
B = model.addVariable(2);
15+
C = model.addVariable(2);
16+
17+
model.addParent(B, A);
18+
model.addParent(C, A);
19+
20+
// define the Bayesian Factors
21+
factors = new BayesianFactor[3];
22+
23+
factors[A] = new BayesianFactor(model.getDomain(A));
24+
factors[B] = new BayesianFactor(model.getDomain(A, B));
25+
factors[C] = new BayesianFactor(model.getDomain(A, C));
26+
27+
factors[A].setData(new int[]{A}, new double[]{.4, .6});
28+
factors[B].setData(new int[]{B, A}, new double[]{.3, .7, .7, .3});
29+
factors[C].setData(new int[]{C, A}, new double[]{.2, .8, .8, .2});
30+
31+
// Assign factors to model
32+
model.setFactors(factors);
33+
34+
/* Instantiate the inference algorithm over BayesianFactors using the model */
35+
36+
BeliefPropagation<BayesianFactor> bp = new BeliefPropagation<>(model);
37+
38+
// perform a full update
39+
BayesianFactor factor = bp.fullPropagation();
40+
41+
// perform the distribution step
42+
bp.distributingEvidence();
43+
44+
// perform the collection step
45+
BayesianFactor factor = bp.collectingEvidence();
46+
47+
/* Simple Inference */
48+
49+
// P(A)
50+
bp.clearEvidence(); // this will clear previous evidence on the model
51+
52+
BayesianFactor pA = bp.query(A);
53+
54+
/* Inference with evidence */
55+
56+
// P(A | B=0)
57+
TIntIntHashMap evidence = new TIntIntHashMap();
58+
evidence.put(B, 0);
59+
bp.setEvidence(evidence); // this will overwrite previous evidence
60+
61+
BayesianFactor pAB0 = bp.query(A);
62+
63+
// P(A | B=0, C=1)
64+
evidence = new TIntIntHashMap();
65+
evidence.put(B, 0);
66+
evidence.put(C, 1);
67+
bp.setEvidence(evidence);
68+
69+
BayesianFactor bp.query(A);
70+
}
71+
}

examples/Constraints.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import ch.idsia.crema.factor.credal.linear.SeparateHalfspaceFactor;
33
import ch.idsia.crema.factor.credal.vertex.VertexFactor;
44
import ch.idsia.crema.model.Strides;
5-
import ch.idsia.crema.model.graphical.SparseModel;
65
import org.apache.commons.math3.optim.linear.Relationship;
76

87
public class Constraints {

examples/CredalInferenceExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import ch.idsia.crema.inference.ve.FactorVariableElimination;
99
import ch.idsia.crema.inference.ve.VariableElimination;
1010
import ch.idsia.crema.model.Strides;
11-
import ch.idsia.crema.model.graphical.SparseModel;
1211
import org.apache.commons.math3.optim.linear.Relationship;
1312

1413
import java.util.Arrays;

examples/LoadFromFile.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import ch.idsia.crema.IO;
22
import ch.idsia.crema.factor.credal.vertex.VertexFactor;
3-
import ch.idsia.crema.model.graphical.SparseModel;
43
import ch.idsia.crema.model.graphical.specialized.BayesianNetwork;
54

65
import java.io.IOException;

examples/PGMpaper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import ch.idsia.crema.inference.ve.CredalVariableElimination;
66
import ch.idsia.crema.model.ObservationBuilder;
77
import ch.idsia.crema.model.Strides;
8-
import ch.idsia.crema.model.graphical.SparseModel;
98
import ch.idsia.crema.user.credal.Vertex;
109

1110
import java.io.IOException;

0 commit comments

Comments
 (0)