Search in sources :

Example 1 with JunctionTreeClique

use of org.drools.beliefs.bayes.JunctionTreeClique in project drools by kiegroup.

the class SprinkerTest method testInitialize.

@Test
public void testInitialize() {
    JunctionTreeClique jtNode = jTree.getRoot();
    // cloud, rain sprinkler
    assertArray(new double[] { 0.2, 0.05, 0.2, 0.05, 0.09, 0.36, 0.01, 0.04 }, scaleDouble(3, jtNode.getPotentials()));
    // wetGrass
    jtNode = jTree.getRoot().getChildren().get(0).getChild();
    assertArray(new double[] { 1.0, 0.0, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99 }, scaleDouble(3, jtNode.getPotentials()));
}
Also used : JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) Test(org.junit.Test)

Example 2 with JunctionTreeClique

use of org.drools.beliefs.bayes.JunctionTreeClique in project drools by kiegroup.

the class SprinkerTest method testNoEvidence.

@Test
public void testNoEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 0.7, 0.3 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 0.353, 0.647 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 3 with JunctionTreeClique

use of org.drools.beliefs.bayes.JunctionTreeClique in project drools by kiegroup.

the class SprinkerTest method testGrassWetEvidence.

@Test
public void testGrassWetEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("WetGrass", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.639, 0.361 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.881, 0.119 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 0.938, 0.062 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 4 with JunctionTreeClique

use of org.drools.beliefs.bayes.JunctionTreeClique in project drools by kiegroup.

the class SprinkerTest method marginalize.

public static void marginalize(BayesVariableState varState, CliqueState cliqueState) {
    JunctionTreeClique jtNode = cliqueState.getJunctionTreeClique();
    new Marginalizer(jtNode.getValues().toArray(new BayesVariable[jtNode.getValues().size()]), cliqueState.getPotentials(), varState.getVariable(), varState.getDistribution());
    System.out.print(varState.getVariable().getName() + " ");
    for (double d : varState.getDistribution()) {
        System.out.print(d);
        System.out.print(" ");
    }
    System.out.println(" ");
}
Also used : Marginalizer(org.drools.beliefs.bayes.Marginalizer) BayesVariable(org.drools.beliefs.bayes.BayesVariable) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique)

Example 5 with JunctionTreeClique

use of org.drools.beliefs.bayes.JunctionTreeClique in project drools by kiegroup.

the class SprinkerTest method testSprinklerEvidence.

@Test
public void testSprinklerEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("Sprinkler", new double[] { 1.0, 0.0 });
    bayesInstance.setLikelyhood("Cloudy", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.8, 0.2 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 0.82, 0.18 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Aggregations

JunctionTreeClique (org.drools.beliefs.bayes.JunctionTreeClique)5 Test (org.junit.Test)4 BayesInstance (org.drools.beliefs.bayes.BayesInstance)3 JunctionTree (org.drools.beliefs.bayes.JunctionTree)3 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)3 BayesVariable (org.drools.beliefs.bayes.BayesVariable)1 Marginalizer (org.drools.beliefs.bayes.Marginalizer)1