Skip to content
Snippets Groups Projects
Commit 37a4eebb authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman :boom:
Browse files

Added an additional test to check for asymmetrical relationships in Bayesian Networks.

parent 2eaf1e23
No related branches found
No related tags found
No related merge requests found
/******************************************************************************
* *
* Copyright: (c) Syncleus, Inc. *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by Syncleus, Inc. at www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact Syncleus, Inc. at the information below if you cannot *
* find a license: *
* *
* Syncleus, Inc. *
* 2604 South 12th Street *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann.graphicalmodel.bayesian;
import java.util.Set;
import com.syncleus.dann.graph.DirectedEdge;
import com.syncleus.dann.graph.Graph;
import com.syncleus.dann.graphicalmodel.AbstractGraphicalModelAdjacencyGraph;
import com.syncleus.dann.graphicalmodel.GraphicalModelNode;
public abstract class AbstractBayesianAdjacencyNetwork<N extends GraphicalModelNode, E extends DirectedEdge<N>> extends AbstractGraphicalModelAdjacencyGraph<N, E>
{
protected AbstractBayesianAdjacencyNetwork()
{
super();
}
protected AbstractBayesianAdjacencyNetwork(final Graph<N, E> copyGraph)
{
super(copyGraph.getNodes(), copyGraph.getEdges());
}
protected AbstractBayesianAdjacencyNetwork(final Set<N> nodes, final Set<E> edges)
{
super(nodes, edges);
}
@Override
public double jointProbability()
{
double probabilityProduct = 1.0;
for(final N node : this.getNodes())
probabilityProduct *= node.stateProbability();
return probabilityProduct;
}
}
/******************************************************************************
* *
* Copyright: (c) Syncleus, Inc. *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by Syncleus, Inc. at www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact Syncleus, Inc. at the information below if you cannot *
* find a license: *
* *
* Syncleus, Inc. *
* 2604 South 12th Street *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann.graphicalmodel.markovrandomfield;
import java.util.Set;
import com.syncleus.dann.graph.*;
import com.syncleus.dann.graph.topological.Topography;
import com.syncleus.dann.graphicalmodel.AbstractGraphicalModelAdjacencyGraph;
import com.syncleus.dann.graphicalmodel.GraphicalModelNode;
public abstract class AbstractMarkovRandomFieldAdjacencyGraph<N extends GraphicalModelNode, E extends UndirectedEdge<N>> extends AbstractGraphicalModelAdjacencyGraph<N, E>
{
protected AbstractMarkovRandomFieldAdjacencyGraph()
{
super();
}
protected AbstractMarkovRandomFieldAdjacencyGraph(final Graph<N, E> copyGraph)
{
super(copyGraph.getNodes(), copyGraph.getEdges());
}
protected AbstractMarkovRandomFieldAdjacencyGraph(final Set<N> nodes, final Set<E> edges)
{
super(nodes, edges);
}
@Override
public double jointProbability()
{
//First we want to attempt clique factorization
//if the graph consists of a single clique, then the joint probability is the probability of any single node.
if( Topography.isStronglyConnected(this) )
if(!this.getNodes().isEmpty())
return this.getNodes().iterator().next().stateProbability();
else return 1.0;
//otherwise the joint probability is the product of all the cliques' probabilities within this graph
Set<Graph<N,E>> cliques = Topography.getMaximallyConnectedComponents(this);
double probabilityProduct = 1.0;
for(final Graph<N,E> clique : cliques)
{
probabilityProduct *= clique.getNodes().iterator().next().stateProbability();
}
return probabilityProduct;
}
}
\ No newline at end of file
...@@ -82,4 +82,55 @@ public class TestSimpleBayesianNetwork ...@@ -82,4 +82,55 @@ public class TestSimpleBayesianNetwork
childNode.setState(SimpleEnum.FALSE); childNode.setState(SimpleEnum.FALSE);
Assert.assertTrue("bad state probability (FALSE,FALSE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.25) < 0.0001); Assert.assertTrue("bad state probability (FALSE,FALSE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.25) < 0.0001);
} }
@Test
public void testDependentNodeAsymmetrical()
{
final MutableBayesianAdjacencyNetwork network = new MutableBayesianAdjacencyNetwork();
final GraphicalModelNode<SimpleEnum> parentNode = new SimpleGraphicalModelNode<SimpleEnum>(SimpleEnum.TRUE);
final GraphicalModelNode<SimpleEnum> childNode = new SimpleGraphicalModelNode<SimpleEnum>(SimpleEnum.TRUE);
network.add(parentNode);
network.add(childNode);
final DirectedEdge<GraphicalModelNode> testEdge = new ImmutableDirectedEdge<GraphicalModelNode>(parentNode, childNode);
network.add(testEdge);
parentNode.setState(SimpleEnum.TRUE);
childNode.setState(SimpleEnum.FALSE);
network.learnStates();
network.learnStates();
network.learnStates();
childNode.setState(SimpleEnum.TRUE);
network.learnStates();
parentNode.setState(SimpleEnum.FALSE);
childNode.setState(SimpleEnum.TRUE);
network.learnStates();
network.learnStates();
childNode.setState(SimpleEnum.FALSE);
network.learnStates();
network.learnStates();
final Set<GraphicalModelNode> goals = new HashSet<GraphicalModelNode>();
goals.add(childNode);
final Set<GraphicalModelNode> influences = new HashSet<GraphicalModelNode>();
influences.add(parentNode);
parentNode.setState(SimpleEnum.TRUE);
childNode.setState(SimpleEnum.TRUE);
Assert.assertTrue("bad state probability (TRUE,TRUE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.25) < 0.0001);
parentNode.setState(SimpleEnum.TRUE);
childNode.setState(SimpleEnum.FALSE);
Assert.assertTrue("bad state probability (TRUE,FALSE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.75) < 0.0001);
parentNode.setState(SimpleEnum.FALSE);
childNode.setState(SimpleEnum.TRUE);
Assert.assertTrue("bad state probability (FALSE,TRUE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.5) < 0.0001);
parentNode.setState(SimpleEnum.FALSE);
childNode.setState(SimpleEnum.FALSE);
Assert.assertTrue("bad state probability (FALSE,FALSE)!", Math.abs(network.conditionalProbability(goals, influences) - 0.5) < 0.0001);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment