use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class EmBayesProperties method setGraph.
public final void setGraph(Graph graph) {
if (graph == null) {
throw new NullPointerException();
}
List<Node> vars = dataSet.getVariables();
Map<String, DiscreteVariable> nodesToVars = new HashMap<>();
for (int i = 0; i < dataSet.getNumColumns(); i++) {
DiscreteVariable var = (DiscreteVariable) vars.get(i);
String name = var.getName();
Node node = new GraphNode(name);
nodesToVars.put(node.getName(), var);
}
Dag dag = new Dag(graph);
BayesPm bayesPm = new BayesPm(dag);
List<Node> nodes = bayesPm.getDag().getNodes();
for (Node node1 : nodes) {
Node var = nodesToVars.get(node1.getName());
if (var != null) {
DiscreteVariable var2 = (DiscreteVariable) var;
List<String> categories = var2.getCategories();
bayesPm.setCategories(node1, categories);
}
}
this.graph = graph;
this.bayesPm = bayesPm;
this.blankBayesIm = new MlBayesIm(bayesPm);
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class EmBayesProperties method getLikelihoodRatioP.
/**
* Calculates the p-value of the graph with respect to the given data.
*/
public final double getLikelihoodRatioP() {
Graph graph1 = getGraph();
List<Node> nodes = getGraph().getNodes();
// Null hypothesis = no edges.
Graph graph0 = new Dag();
for (Node node : nodes) {
graph0.addNode(node);
}
EmBayesProperties scorer1 = new EmBayesProperties(getDataSet(), graph1);
EmBayesProperties scorer0 = new EmBayesProperties(getDataSet(), graph0);
double l1 = scorer1.logProbDataGivenStructure();
double l0 = scorer0.logProbDataGivenStructure();
System.out.println("l1 = " + l1);
System.out.println("l0 = " + l0);
double chisq = -2.0 * (l0 - l1);
int n1 = scorer1.numNonredundantParams();
int n0 = scorer0.numNonredundantParams();
int df = n1 - n0;
double pValue = (1.0 - ProbUtils.chisqCdf(chisq, df));
// System.out.println("\n*** P Value Calculation ***");
// System.out.println("l1 = " + l1 + " l0 = " + l0 + " l0 - l1 = " + (l0 - l1));
// System.out.println("n1 = " + n1 + " n0 = " + n0 + " n1 - n0 = " + (n1 - n0));
// System.out.println("chisq = " + chisq + " pvalue = " + pValue);
this.pValueDf = df;
this.chisq = chisq;
return pValue;
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class CptInvariantUpdater method setEvidence.
public void setEvidence(Evidence evidence) {
if (evidence == null) {
throw new NullPointerException();
}
if (evidence.isIncompatibleWith(bayesIm)) {
throw new IllegalArgumentException("The variable list for this evidence " + "must be compatible with the variable list of the stored IM.");
}
this.evidence = evidence;
// Create the manipulated Bayes Im.
Graph graph = bayesIm.getBayesPm().getDag();
Dag manipulatedGraph = createManipulatedGraph(graph);
BayesPm manipulatedBayesPm = createManipulatedBayesPm(manipulatedGraph);
this.manipulatedBayesIm = createdManipulatedBayesIm(manipulatedBayesPm);
// Create the updated BayesIm from the manipulated Bayes Im.
Evidence evidence2 = new Evidence(evidence, manipulatedBayesIm);
this.updatedBayesIm = new UpdatedBayesIm(manipulatedBayesIm, evidence2);
// Create the marginal calculator from the updated Bayes Im.
this.cptInvariantMarginalCalculator = new CptInvariantMarginalCalculator(bayesIm, evidence2);
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class CptInvariantUpdater method createManipulatedGraph.
// private Dag createManipulatedGraph(Graph graph) {
// Dag updatedGraph = new Dag(graph);
//
// for (int i = 0; i < bayesIm.getNumNodes(); ++i) {
// if (evidence.isManipulated(i)) {
// Node node = evidence.getNode(i);
// List<Node> parents = updatedGraph.getParents(node);
//
// for (Node parent1 : parents) {
// updatedGraph.removeEdge(node, parent1);
// }
// }
// }
//
// return updatedGraph;
// }
private Dag createManipulatedGraph(Graph graph) {
Dag updatedGraph = new Dag(graph);
// alters graph for manipulated evidenceItems
for (int i = 0; i < evidence.getNumNodes(); ++i) {
if (evidence.isManipulated(i)) {
Node node = updatedGraph.getNode(evidence.getNode(i).getName());
List<Node> parents = updatedGraph.getParents(node);
for (Object parent1 : parents) {
Node parent = (Node) parent1;
updatedGraph.removeEdge(node, parent);
}
}
}
return updatedGraph;
}
use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.
the class ApproximateUpdater method createManipulatedGraph.
private Dag createManipulatedGraph(Graph graph) {
Dag updatedGraph = new Dag(graph);
// alters graph for manipulated evidenceItems
for (int i = 0; i < evidence.getNumNodes(); ++i) {
if (evidence.isManipulated(i)) {
Node node = evidence.getNode(i);
node = updatedGraph.getNode(node.getName());
Collection<Node> parents = updatedGraph.getParents(node);
for (Node parent1 : parents) {
updatedGraph.removeEdge(node, parent1);
}
}
}
return updatedGraph;
}
Aggregations