Search in sources :

Example 21 with Dag

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);
}
Also used : HashMap(java.util.HashMap) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable)

Example 22 with Dag

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;
}
Also used : Graph(edu.cmu.tetrad.graph.Graph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) Dag(edu.cmu.tetrad.graph.Dag)

Example 23 with Dag

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);
}
Also used : Graph(edu.cmu.tetrad.graph.Graph) Dag(edu.cmu.tetrad.graph.Dag)

Example 24 with Dag

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;
}
Also used : Node(edu.cmu.tetrad.graph.Node) Dag(edu.cmu.tetrad.graph.Dag)

Example 25 with Dag

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;
}
Also used : Node(edu.cmu.tetrad.graph.Node) Dag(edu.cmu.tetrad.graph.Dag)

Aggregations

Dag (edu.cmu.tetrad.graph.Dag)53 Node (edu.cmu.tetrad.graph.Node)41 Graph (edu.cmu.tetrad.graph.Graph)21 GraphNode (edu.cmu.tetrad.graph.GraphNode)21 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)12 SemIm (edu.cmu.tetrad.sem.SemIm)10 SemPm (edu.cmu.tetrad.sem.SemPm)10 DataSet (edu.cmu.tetrad.data.DataSet)7 BayesPm (edu.cmu.tetrad.bayes.BayesPm)6 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)6 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)6 BayesIm (edu.cmu.tetrad.bayes.BayesIm)5 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)3 FruchtermanReingoldLayout (edu.cmu.tetrad.graph.FruchtermanReingoldLayout)2 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)2 LinkedList (java.util.LinkedList)2 StoredCellProbs (edu.cmu.tetrad.bayes.StoredCellProbs)1 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)1