Search in sources :

Example 16 with SemGraph

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

the class GeneralizedSemPmEditor method layoutByKnowledge.

public void layoutByKnowledge() {
    SemGraph _graph = (SemGraph) graphicalEditor().getWorkbench().getGraph();
    _graph.setShowErrorTerms(false);
    graphicalEditor().getWorkbench().layoutByKnowledge();
    _graph.resetErrorPositions();
    // graphicalEditor().getWorkbench().setGraph(_graph);
    errorTerms.setText("Show Error Terms");
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph)

Example 17 with SemGraph

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

the class GeneralizedSemPmEditor method layoutByGraph.

public void layoutByGraph(Graph graph) {
    SemGraph _graph = (SemGraph) graphicalEditor().getWorkbench().getGraph();
    _graph.setShowErrorTerms(false);
    graphicalEditor().getWorkbench().layoutByGraph(graph);
    _graph.resetErrorPositions();
    // Oh no do't you dare do this! You will lose all labels! jdramsey 4/17/10
    // graphicalEditor().getWorkbench().setGraph(_graph);
    errorTerms.setText("Show Error Terms");
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph)

Example 18 with SemGraph

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

the class PatternFitModel method estimatePag.

private SemIm estimatePag(DataSet dataSet, SemPm pm) {
    SemGraph graph = pm.getGraph();
    for (Object o : graph.getNodes()) {
        Node node = (Node) o;
        if (node.getNodeType() == NodeType.LATENT) {
            throw new IllegalArgumentException("Estimation of Bayes IM's " + "with latents is not supported.");
        }
    }
    if (DataUtils.containsMissingValue(dataSet)) {
        throw new IllegalArgumentException("Please remove or impute missing values.");
    }
    try {
        SemOptimizer optimizer = new SemOptimizerRicf();
        SemEstimator estimator = new SemEstimator(dataSet, pm, optimizer);
        return estimator.estimate();
    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
        throw new RuntimeException("Value assignments between Bayes PM " + "and discrete data set do not match.");
    }
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) Node(edu.cmu.tetrad.graph.Node)

Example 19 with SemGraph

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

the class Tsls method estimateCoeffs.

private SemIm estimateCoeffs(SemIm semIm) {
    // System.out.print("\n****************\nCalling 2SLS... ");
    SemGraph semGraph = semIm.getSemPm().getGraph();
    // Get list of fixed measurements that will be kept fixed, and the
    // respective latent variables that are their parents.
    // "X" variables are exogenous, while "Y" variables are endogenous.
    List<Node> ly = new LinkedList<>();
    List<Node> lx = new LinkedList<>();
    List<Node> my1 = new LinkedList<>();
    List<Node> mx1 = new LinkedList<>();
    List<Node> observed = new LinkedList<>();
    for (Node nodeA : semGraph.getNodes()) {
        if (nodeA.getNodeType() == NodeType.ERROR) {
            continue;
        }
        if (nodeA.getNodeType() == NodeType.LATENT) {
            if (semGraph.getParents(nodeA).size() == 0) {
                lx.add(nodeA);
            } else {
                ly.add(nodeA);
            }
        } else {
            observed.add(nodeA);
        }
    }
    setFixedNodes(semGraph, mx1, my1);
    // Estimate freeParameters for the latent/latent edges
    for (Node current : ly) {
        if (nodeName != null && !nodeName.equals(current.getName())) {
            continue;
        }
        // Build Z, the matrix containing the data for the fixed measurements
        // associated with the parents of the getModel (endogenous) latent node
        List<Node> endo_parents_m = new LinkedList<>();
        List<Node> exo_parents_m = new LinkedList<>();
        List<Node> endo_parents = new LinkedList<>();
        List<Node> exo_parents = new LinkedList<>();
        Iterator<Node> it_p = semGraph.getParents(current).iterator();
        lNames = new String[lx.size() + ly.size()];
        while (it_p.hasNext()) {
            Node node = it_p.next();
            if (node.getNodeType() == NodeType.ERROR) {
                continue;
            }
            if (lx.contains(node)) {
                int position = lx.indexOf(node);
                exo_parents_m.add(mx1.get(position));
                exo_parents.add(node);
            } else {
                int position = ly.indexOf(node);
                endo_parents_m.add(my1.get(position));
                endo_parents.add(node);
            }
        }
        Object[] endp_a_m = endo_parents_m.toArray();
        Object[] exop_a_m = exo_parents_m.toArray();
        Object[] endp_a = endo_parents.toArray();
        Object[] exop_a = exo_parents.toArray();
        int n = dataSet.getNumRows(), c = endp_a_m.length + exop_a_m.length;
        if (c == 0) {
            continue;
        }
        double[][] Z = new double[n][c];
        int count = 0;
        for (int i = 0; i < endp_a_m.length; i++) {
            Node node = (Node) endp_a_m[i];
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int j = 0; j < n; j++) {
                // Z[j][i] = column_data[j];
                Z[j][i] = dataSet.getDouble(j, colIndex);
            }
            lNames[count++] = (endo_parents.get(i)).getName();
        }
        for (int i = 0; i < exop_a_m.length; i++) {
            Node node = (Node) exop_a_m[i];
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int j = 0; j < n; j++) {
                // Z[j][endp_a_m.length + i] = column_data[j];
                Z[j][endp_a_m.length + i] = dataSet.getDouble(j, colIndex);
            }
            lNames[count++] = exo_parents.get(i).getName();
        }
        // Build V, the matrix containing the data for the nonfixed measurements
        // associated with the parents of the getModel (endogenous) latent node
        endo_parents_m = new LinkedList<>();
        exo_parents_m = new LinkedList<>();
        it_p = semGraph.getParents(current).iterator();
        while (it_p.hasNext()) {
            Node node = it_p.next();
            if (node.getNodeType() == NodeType.ERROR) {
                continue;
            }
            List<Node> other_measures = new LinkedList<>();
            for (Node next : semGraph.getChildren(node)) {
                if (next.getNodeType() == NodeType.MEASURED) {
                    other_measures.add(next);
                }
            }
            if (lx.contains(node)) {
                int position = lx.indexOf(node);
                other_measures.remove(mx1.get(position));
                exo_parents_m.addAll(other_measures);
            } else {
                int position = ly.indexOf(node);
                other_measures.remove(my1.get(position));
                endo_parents_m.addAll(other_measures);
            }
        }
        endp_a_m = endo_parents_m.toArray();
        exop_a_m = exo_parents_m.toArray();
        n = dataSet.getNumRows();
        c = endp_a_m.length + exop_a_m.length;
        double[][] V = new double[n][c];
        if (c == 0) {
            continue;
        }
        for (int i = 0; i < endp_a_m.length; i++) {
            Node node = ((Node) endp_a_m[i]);
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int j = 0; j < n; j++) {
                // V[j][i] = column_data[j];
                V[j][i] = dataSet.getDouble(j, colIndex);
            }
        }
        for (int i = 0; i < exop_a_m.length; i++) {
            Node node = (Node) exop_a_m[i];
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int j = 0; j < n; j++) {
                // V[j][endp_a_m.length + i] = column_data[j];
                V[j][endp_a_m.length + i] = dataSet.getDouble(j, colIndex);
            }
        }
        double[] yi = new double[n];
        if (lx.contains(current)) {
            int position = lx.indexOf(current);
            Node node = mx1.get(position);
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int i = 0; i < n; i++) {
                yi[i] = dataSet.getDouble(i, colIndex);
            }
        } else {
            int position = ly.indexOf(current);
            Node node = my1.get(position);
            String name = node.getName();
            Node variable = dataSet.getVariable(name);
            int colIndex = dataSet.getVariables().indexOf(variable);
            for (int i = 0; i < n; i++) {
                yi[i] = dataSet.getDouble(i, colIndex);
            }
        }
        // Build Z_hat
        double[][] Z_hat = MatrixUtils.product(V, MatrixUtils.product(MatrixUtils.inverse(MatrixUtils.product(MatrixUtils.transpose(V), V)), MatrixUtils.product(MatrixUtils.transpose(V), Z)));
        A_hat = MatrixUtils.product(MatrixUtils.inverse(MatrixUtils.product(MatrixUtils.transpose(Z_hat), Z_hat)), MatrixUtils.product(MatrixUtils.transpose(Z_hat), yi));
        // Set the edge for the fixed measurement
        int position = ly.indexOf(current);
        semIm.setParamValue(current, my1.get(position), 1.);
        // Set the edge for the latents
        for (int i = 0; i < endp_a.length; i++) {
            semIm.setParamValue((Node) endp_a[i], current, A_hat[i]);
        }
        for (int i = 0; i < exop_a.length; i++) {
            semIm.setParamValue((Node) exop_a[i], current, A_hat[endp_a.length + i]);
        }
        if (nodeName != null && nodeName.equals(current.getName())) {
            computeAsymptLatentCovar(yi, A_hat, Z, Z_hat, dataSet.getNumRows());
            break;
        }
    }
    // Set the edges of the fixed measurements of exogenous
    for (Node current : lx) {
        int position = lx.indexOf(current);
        semIm.setParamValue(current, mx1.get(position), 1.);
    }
    for (Node current : observed) {
        if (nodeName != null && !nodeName.equals(current.getName())) {
            continue;
        }
        if (mx1.contains(current) || my1.contains(current)) {
            continue;
        }
        // First, get the parent of this observed
        Node current_latent = null;
        for (Node node : semGraph.getParents(current)) {
            if (node.getNodeType() == NodeType.ERROR) {
                continue;
            }
            current_latent = node;
        }
        Iterator<Node> children = semGraph.getChildren(current_latent).iterator();
        List<Node> other_measures = new LinkedList<>();
        Node fixed_measurement;
        while (children.hasNext()) {
            Node next = children.next();
            if ((next.getNodeType() == NodeType.MEASURED) && next != current) {
                other_measures.add(next);
            }
        }
        if (lx.contains(current_latent)) {
            int position = lx.indexOf(current_latent);
            other_measures.remove(mx1.get(position));
            fixed_measurement = mx1.get(position);
        } else {
            int position = ly.indexOf(current_latent);
            other_measures.remove(my1.get(position));
            fixed_measurement = my1.get(position);
        }
        // Regress other_measures over the fixed measurement x1 (y1) correspondent
        // to the measurement variable that is being evaluated
        int n = dataSet.getNumRows(), c = other_measures.size();
        if (c == 0) {
            continue;
        }
        double[][] Z = new double[n][c];
        for (int i = 0; i < c; i++) {
            Node variable = dataSet.getVariable((other_measures.get(i)).getName());
            int varIndex = dataSet.getVariables().indexOf(variable);
            for (int j = 0; j < n; j++) {
                // Z[j][i] = column_data[j];
                Z[j][i] = dataSet.getDouble(varIndex, j);
            }
        }
        // Build C, the column matrix containing the data for the fixed
        // measurement associated with the only latent parent of the getModel
        // observed node (as assumed by the structure of our measurement model).
        Node variable = dataSet.getVariable(fixed_measurement.getName());
        int colIndex = dataSet.getVariables().indexOf(variable);
        // Column column = dataSet.getColumnObject(variable);
        // double C[] = (double[]) column.getRawData();
        double[] C = new double[dataSet.getNumRows()];
        for (int i = 0; i < dataSet.getNumRows(); i++) {
            C[i] = dataSet.getDouble(colIndex, i);
        }
        // Build V, the matrix containing the data for the other measurements
        // associated with the parents of the (latent) parent of getModel
        // observed node. The only difference with respect to the estimation
        // of the within-latent coefficients is that here we only include
        // the other measurements attached to the parent of the getModel node,
        // assuming that the error term of the getModel node is independent
        // of the error term of the others and that each measurement is
        // taken with respect to only one latent.
        n = dataSet.getNumRows();
        c = other_measures.size();
        double[][] V = new double[n][c];
        for (int i = 0; i < c; i++) {
            Node variable2 = dataSet.getVariable((other_measures.get(i)).getName());
            int var2index = dataSet.getVariables().indexOf(variable2);
            for (int j = 0; j < n; j++) {
                // V[j][i] = column_data[j];
                V[j][i] = dataSet.getDouble(j, var2index);
            }
        }
        double[] yi = new double[n];
        Node variable3 = dataSet.getVariable((current).getName());
        int var3Index = dataSet.getVariables().indexOf(variable3);
        for (int i = 0; i < n; i++) {
            yi[i] = dataSet.getDouble(i, var3Index);
        }
        // Object rawData = dataSet.getColumnObject(variable3).getRawData();
        // System.arraycopy(rawData, 0, yi, 0, n);
        double[] C_hat = MatrixUtils.product(V, MatrixUtils.product(MatrixUtils.inverse(MatrixUtils.product(MatrixUtils.transpose(V), V)), MatrixUtils.product(MatrixUtils.transpose(V), C)));
        double A_hat = MatrixUtils.innerProduct(MatrixUtils.scalarProduct(1. / MatrixUtils.innerProduct(C_hat, C_hat), C_hat), yi);
        // Set the edge for the getModel measurement
        semIm.setParamValue(current_latent, current, A_hat);
    }
    return semIm;
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) Node(edu.cmu.tetrad.graph.Node) LinkedList(java.util.LinkedList)

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