Added Daniel's Bayes Net code. Converted example code to unit tests. Minor code clean-up.
This commit is contained in:
50
test/dkohl/bayes/example/SprinklerNetExampleTest.java
Normal file
50
test/dkohl/bayes/example/SprinklerNetExampleTest.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package dkohl.bayes.example;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.estimation.MaximumLikelihoodEstimation;
|
||||
import dkohl.bayes.example.builders.EstimateSprinklerNetBuilderTable;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityTable;
|
||||
import dkohl.bayes.statistic.DataSet;
|
||||
|
||||
/**
|
||||
* Parameter estimation for the sprinkler net example
|
||||
*
|
||||
* http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
|
||||
*
|
||||
* @author Daniel Kohlsdorf
|
||||
*/
|
||||
public class SprinklerNetExampleTest {
|
||||
|
||||
@Test
|
||||
public void testSprinklerNetExample() {
|
||||
BayesNet net = EstimateSprinklerNetBuilderTable.sprinkler();
|
||||
DataSet data = EstimateSprinklerNetBuilderTable.dataSet();
|
||||
MaximumLikelihoodEstimation.estimate(data, net,
|
||||
EstimateSprinklerNetBuilderTable.GRASS_WET);
|
||||
|
||||
/**
|
||||
* Output PDF for grass is wet
|
||||
*/
|
||||
ProbabilityTable table = (ProbabilityTable) net.getNodes().get(
|
||||
EstimateSprinklerNetBuilderTable.GRASS_WET);
|
||||
for (String name : table.getNames()) {
|
||||
System.out.print(name + " | ");
|
||||
}
|
||||
System.out.println();
|
||||
for (String key : table.getAssignments().keySet()) {
|
||||
System.out.println(key + " "
|
||||
+ table.getAssignments().get(key).getProbability());
|
||||
if ("false;false;true;".equals(key)) {
|
||||
assertThat(table.getAssignments().get(key).getProbability(), equalTo(0.0));
|
||||
}
|
||||
if ("true;false;true;".equals(key)) {
|
||||
assertThat(table.getAssignments().get(key).getProbability(), equalTo(0.9));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user