Search in sources :

Example 6 with SemGraph

use of edu.cmu.tetrad.graph.SemGraph in project tetrad by cmu-phil.

the class DiscreteTetradTest method piHat2.

/**
 * For two factor models.
 */
private double piHat2(int[] indices, int[] v, SemIm semIm) {
    SemGraph graph = semIm.getSemPm().getGraph();
    graph.setShowErrorTerms(true);
    Node etaNode1 = graph.getNode("eta1");
    double varEta1 = semIm.getParamValue(etaNode1, etaNode1);
    double stdEta1 = Math.sqrt(semIm.getParamValue(etaNode1, etaNode1));
    Node etaNode2 = graph.getNode("eta2");
    double coeffEta = semIm.getParamValue(etaNode1, etaNode2);
    Node errorEta2;
    if (graph.getParents(etaNode2).get(0) == etaNode1) {
        errorEta2 = graph.getParents(etaNode2).get(1);
    } else {
        errorEta2 = graph.getParents(etaNode2).get(0);
    }
    double varEta2 = coeffEta * coeffEta * varEta1 + semIm.getParamValue(errorEta2, errorEta2);
    double stdEtaError2 = Math.sqrt(semIm.getParamValue(errorEta2, errorEta2));
    double[] stdE = new double[indices.length];
    double[] varEta = new double[indices.length];
    double[] stdU = new double[indices.length];
    double[] coeff = new double[indices.length];
    for (int i = 0; i < indices.length; i++) {
        Node uNode = graph.getNode("xi" + i);
        Node uParent = null, uError = null;
        for (Node node : graph.getParents(uNode)) {
            Node parent = node;
            if (parent.getNodeType() == NodeType.LATENT) {
                uParent = parent;
            } else {
                uError = parent;
            }
        }
        if (i == 0 || (i == 2 && indices.length == 4) || (i == 3 && indices.length > 4)) {
            coeff[i] = 1.;
        } else {
            coeff[i] = semIm.getParamValue(uParent, uNode);
        }
        if (uParent == etaNode1) {
            varEta[i] = varEta1;
        } else {
            varEta[i] = varEta2;
        }
        stdE[i] = Math.sqrt(semIm.getParamValue(uError, uError));
        stdU[i] = Math.sqrt(coeff[i] * coeff[i] * varEta[i] + semIm.getParamValue(uError, uError));
    }
    double l = 0.;
    for (int t1 = 0; t1 < GHY.length; t1++) {
        for (int t2 = 0; t2 < GHY.length; t2++) {
            double tValue = GHW[t1] * GHW[t2];
            double eta1 = GHY[t1] * stdEta1;
            double eta2 = eta1 * coeffEta + GHY[t2] * stdEtaError2;
            for (int i = 0; i < indices.length; i++) {
                double eta;
                if (indices.length == 4) {
                    if (i < 2) {
                        eta = eta1;
                    } else {
                        eta = eta2;
                    }
                } else {
                    if (i < 3) {
                        eta = eta1;
                    } else {
                        eta = eta2;
                    }
                }
                int numValues = this.values[indices[i]].length;
                if (v[i] == 0) {
                    tValue *= ProbUtils.normalCdf((this.thresholds[indices[i]][0] * stdU[i] - coeff[i] * eta) / stdE[i]);
                } else if (v[i] == numValues - 1) {
                    tValue *= (1. - ProbUtils.normalCdf((this.thresholds[indices[i]][numValues - 2] * stdU[i] - coeff[i] * eta) / stdE[i]));
                } else {
                    tValue *= ProbUtils.normalCdf((this.thresholds[indices[i]][v[i]] * stdU[i] - coeff[i] * eta) / stdE[i]) - ProbUtils.normalCdf((this.thresholds[indices[i]][v[i] - 1] * stdU[i] - coeff[i] * eta) / stdE[i]);
                }
            }
            l += tValue;
        }
    }
    return l;
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) Node(edu.cmu.tetrad.graph.Node)

Example 7 with SemGraph

use of edu.cmu.tetrad.graph.SemGraph in project tetrad by cmu-phil.

the class StandardizedSemImImpliedMatricesPanel method layoutByKnowledge.

/**
 * Lays the workbench graph out using knowledge tiers.
 */
public void layoutByKnowledge() {
    SemGraph _graph = (SemGraph) standardizedSemImGraphicalEditor.getWorkbench().getGraph();
    _graph.setShowErrorTerms(false);
    standardizedSemImGraphicalEditor.getWorkbench().layoutByKnowledge();
    _graph.resetErrorPositions();
    standardizedSemImGraphicalEditor.getWorkbench().setGraph(_graph);
    errorTerms.setText("Show Error Terms");
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph)

Example 8 with SemGraph

use of edu.cmu.tetrad.graph.SemGraph in project tetrad by cmu-phil.

the class StandardizedSemImImpliedMatricesPanel method layoutByGraph.

/**
 * Lays out the graph in the workbench.
 * @param graph The graph whose layout is to be mimicked.
 */
public void layoutByGraph(Graph graph) {
    SemGraph _graph = (SemGraph) standardizedSemImGraphicalEditor.getWorkbench().getGraph();
    _graph.setShowErrorTerms(false);
    standardizedSemImGraphicalEditor.getWorkbench().layoutByGraph(graph);
    _graph.resetErrorPositions();
    // standardizedSemImGraphicalEditor.getWorkbench().setGraph(_graph);
    errorTerms.setText("Show Error Terms");
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph)

Example 9 with SemGraph

use of edu.cmu.tetrad.graph.SemGraph in project tetrad by cmu-phil.

the class SemUpdater method createManipulatedGraph.

/**
 * Alters the graph by removing edges from parents to manipulated variables.
 */
private SemGraph createManipulatedGraph(Graph graph) {
    SemGraph updatedGraph = new SemGraph(graph);
    for (int i = 0; i < evidence.getNumNodes(); ++i) {
        if (evidence.isManipulated(i)) {
            Node node = evidence.getNode(i);
            List<Node> parents = updatedGraph.getParents(node);
            for (Node parent : parents) {
                if (parent.getNodeType() == NodeType.ERROR) {
                    continue;
                }
                updatedGraph.removeEdge(node, parent);
            }
        }
    }
    return updatedGraph;
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) Node(edu.cmu.tetrad.graph.Node)

Example 10 with SemGraph

use of edu.cmu.tetrad.graph.SemGraph in project tetrad by cmu-phil.

the class SemUpdater method getManipulatedSemIm.

public SemIm getManipulatedSemIm() {
    SemGraph graph = getSemIm().getSemPm().getGraph();
    SemGraph manipulatedGraph = createManipulatedGraph(graph);
    return SemIm.retainValues(getSemIm(), manipulatedGraph);
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph)

Aggregations

SemGraph (edu.cmu.tetrad.graph.SemGraph)19 Node (edu.cmu.tetrad.graph.Node)8 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)2 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)2 Algebra (cern.colt.matrix.linalg.Algebra)2 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)2 Endpoint (edu.cmu.tetrad.graph.Endpoint)2 Graph (edu.cmu.tetrad.graph.Graph)2 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)2 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)1 GraphNode (edu.cmu.tetrad.graph.GraphNode)1 TetradVector (edu.cmu.tetrad.util.TetradVector)1 LinkedList (java.util.LinkedList)1 Attribute (nu.xom.Attribute)1 Element (nu.xom.Element)1