use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class RowSummingExactUpdater method setEvidence.
public final void setEvidence(Evidence evidence) {
if (evidence == null) {
throw new NullPointerException();
}
if (evidence.isIncompatibleWith(bayesIm)) {
throw new IllegalArgumentException("The variable list for the " + "given bayesIm must be compatible with the variable list " + "for this evidence.");
}
this.evidence = evidence;
Graph graph = bayesIm.getBayesPm().getDag();
Dag manipulatedGraph = createManipulatedGraph(graph);
BayesPm manipulatedPm = createUpdatedBayesPm(manipulatedGraph);
this.manipulatedBayesIm = createdUpdatedBayesIm(manipulatedPm);
for (int i = 0; i < evidence.getNumNodes(); i++) {
if (evidence.isManipulated(i)) {
for (int j = 0; j < evidence.getNumCategories(i); j++) {
if (evidence.getProposition().isAllowed(i, j)) {
manipulatedBayesIm.setProbability(i, 0, j, 1.0);
} else {
manipulatedBayesIm.setProbability(i, 0, j, 0.0);
}
}
}
}
this.bayesImProbs = new BayesImProbs(manipulatedBayesIm);
this.updatedBayesIm = null;
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class XdslXmlParser method buildIM.
private BayesIm buildIM(Element element0, Map<String, String> displayNames) {
Elements elements = element0.getChildElements();
for (int i = 0; i < elements.size(); i++) {
if (!"cpt".equals(elements.get(i).getQualifiedName())) {
throw new IllegalArgumentException("Expecting cpt element.");
}
}
Dag dag = new Dag();
// Get the nodes.
for (int i = 0; i < elements.size(); i++) {
Element cpt = elements.get(i);
String name = cpt.getAttribute(0).getValue();
if (displayNames == null) {
dag.addNode(new GraphNode(name));
} else {
dag.addNode(new GraphNode(displayNames.get(name)));
}
}
// Get the edges.
for (int i = 0; i < elements.size(); i++) {
Element cpt = elements.get(i);
Elements cptElements = cpt.getChildElements();
for (int j = 0; j < cptElements.size(); j++) {
Element cptElement = cptElements.get(j);
if (cptElement.getQualifiedName().equals("parents")) {
String list = cptElement.getValue();
String[] parentNames = list.split(" ");
for (String name : parentNames) {
if (displayNames == null) {
edu.cmu.tetrad.graph.Node parent = dag.getNode(name);
edu.cmu.tetrad.graph.Node child = dag.getNode(cpt.getAttribute(0).getValue());
dag.addDirectedEdge(parent, child);
} else {
edu.cmu.tetrad.graph.Node parent = dag.getNode(displayNames.get(name));
edu.cmu.tetrad.graph.Node child = dag.getNode(displayNames.get(cpt.getAttribute(0).getValue()));
dag.addDirectedEdge(parent, child);
}
}
}
}
String name;
if (displayNames == null) {
name = cpt.getAttribute(0).getValue();
} else {
name = displayNames.get(cpt.getAttribute(0).getValue());
}
dag.addNode(new GraphNode(name));
}
// PM
BayesPm pm = new BayesPm(dag);
for (int i = 0; i < elements.size(); i++) {
Element cpt = elements.get(i);
String varName = cpt.getAttribute(0).getValue();
Node node;
if (displayNames == null) {
node = dag.getNode(varName);
} else {
node = dag.getNode(displayNames.get(varName));
}
Elements cptElements = cpt.getChildElements();
List<String> stateNames = new ArrayList<>();
for (int j = 0; j < cptElements.size(); j++) {
Element cptElement = cptElements.get(j);
if (cptElement.getQualifiedName().equals("state")) {
Attribute attribute = cptElement.getAttribute(0);
String stateName = attribute.getValue();
stateNames.add(stateName);
}
}
pm.setCategories(node, stateNames);
}
// IM
BayesIm im = new MlBayesIm(pm);
for (int nodeIndex = 0; nodeIndex < elements.size(); nodeIndex++) {
Element cpt = elements.get(nodeIndex);
Elements cptElements = cpt.getChildElements();
for (int j = 0; j < cptElements.size(); j++) {
Element cptElement = cptElements.get(j);
if (cptElement.getQualifiedName().equals("probabilities")) {
String list = cptElement.getValue();
String[] probsStrings = list.split(" ");
List<Double> probs = new ArrayList<>();
for (String probString : probsStrings) {
probs.add(Double.parseDouble(probString));
}
int count = -1;
for (int row = 0; row < im.getNumRows(nodeIndex); row++) {
for (int col = 0; col < im.getNumColumns(nodeIndex); col++) {
im.setProbability(nodeIndex, row, col, probs.get(++count));
}
}
}
}
}
return im;
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class TestDiscretizer method testManualDiscretize2.
@Test
public void testManualDiscretize2() {
List<Node> nodes1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes1.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 3, 3, 3, false));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet data = im.simulateData(100, false);
List<Node> nodes = data.getVariables();
Discretizer discretizer = new Discretizer(data);
discretizer.equalCounts(nodes.get(0), 3);
discretizer.equalIntervals(nodes.get(1), 2);
discretizer.equalCounts(nodes.get(2), 5);
discretizer.equalIntervals(nodes.get(3), 8);
discretizer.equalCounts(nodes.get(4), 4);
DataSet discretized = discretizer.discretize();
assertEquals(2, maxInColumn(discretized, 0));
assertEquals(1, maxInColumn(discretized, 1));
assertEquals(4, maxInColumn(discretized, 2));
assertEquals(7, maxInColumn(discretized, 3));
assertEquals(3, maxInColumn(discretized, 4));
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class TestDiscretizer method testManualDiscretize3.
@Test
public void testManualDiscretize3() {
List<Node> nodes1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes1.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes1, 0, 5, 3, 3, 3, false));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet data = im.simulateData(100, false);
List<Node> nodes = data.getVariables();
Discretizer discretizer = new Discretizer(data);
discretizer.setVariablesCopied(true);
discretizer.setVariablesCopied(true);
discretizer.equalCounts(nodes.get(0), 3);
DataSet discretized = discretizer.discretize();
assertTrue(discretized.getVariable(0) instanceof DiscreteVariable);
assertTrue(discretized.getVariable(1) instanceof ContinuousVariable);
assertTrue(discretized.getVariable(2) instanceof ContinuousVariable);
assertTrue(discretized.getVariable(3) instanceof ContinuousVariable);
assertTrue(discretized.getVariable(4) instanceof ContinuousVariable);
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class TestEvidence method sampleBayesIm2.
private static BayesIm sampleBayesIm2() {
Node a = new GraphNode("a");
Node b = new GraphNode("b");
Node c = new GraphNode("c");
Dag graph;
graph = new Dag();
graph.addNode(a);
graph.addNode(b);
graph.addNode(c);
graph.addDirectedEdge(a, b);
graph.addDirectedEdge(a, c);
graph.addDirectedEdge(b, c);
BayesPm bayesPm = new BayesPm(graph);
bayesPm.setNumCategories(b, 3);
BayesIm bayesIm1 = new MlBayesIm(bayesPm);
bayesIm1.setProbability(0, 0, 0, .3);
bayesIm1.setProbability(0, 0, 1, .7);
bayesIm1.setProbability(1, 0, 0, .3);
bayesIm1.setProbability(1, 0, 1, .4);
bayesIm1.setProbability(1, 0, 2, .3);
bayesIm1.setProbability(1, 1, 0, .6);
bayesIm1.setProbability(1, 1, 1, .1);
bayesIm1.setProbability(1, 1, 2, .3);
bayesIm1.setProbability(2, 0, 0, .9);
bayesIm1.setProbability(2, 0, 1, .1);
bayesIm1.setProbability(2, 1, 0, .1);
bayesIm1.setProbability(2, 1, 1, .9);
bayesIm1.setProbability(2, 2, 0, .5);
bayesIm1.setProbability(2, 2, 1, .5);
bayesIm1.setProbability(2, 3, 0, .2);
bayesIm1.setProbability(2, 3, 1, .8);
bayesIm1.setProbability(2, 4, 0, .6);
bayesIm1.setProbability(2, 4, 1, .4);
bayesIm1.setProbability(2, 5, 0, .7);
bayesIm1.setProbability(2, 5, 1, .3);
return bayesIm1;
}
Aggregations