Search in sources :

Example 81 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class KamadaKawaiLayout method optimize.

private void optimize(double deltaCutoff) {
    NumberFormat nf = NumberFormatUtil.getInstance().getNumberFormat();
    double initialMaxDelta = -1.;
    double maxDelta;
    int jump = 100;
    TetradMatrix a = new TetradMatrix(2, 2);
    TetradMatrix b = new TetradMatrix(2, 1);
    int oldM = -1;
    do {
        if (monitor.isCanceled()) {
            return;
        }
        int[] m = new int[1];
        maxDelta = maxDelta(m);
        if (initialMaxDelta == -1) {
            initialMaxDelta = maxDelta;
        }
        if (m[0] == oldM) {
            p[m[0]][0] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
            p[m[0]][1] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
            continue;
        }
        oldM = m[0];
        int progress = (int) (99.0 - 98.0 * maxDelta / (0.5 * initialMaxDelta));
        if (progress < 1) {
            progress = 1;
        }
        if (progress > 99) {
            progress = 99;
        }
        getMonitor().setProgress(progress);
        getMonitor().setNote("Energy = " + nf.format(maxDelta));
        if (m[0] == -1) {
            throw new IllegalStateException();
        }
        double oldDelta = Double.NaN;
        double delta;
        while ((delta = delta(m[0])) > deltaCutoff) {
            Thread.yield();
            if (monitor.isCanceled()) {
                return;
            }
            if (Math.abs(delta - oldDelta) < 0.001) {
                p[m[0]][0] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
                p[m[0]][1] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
                continue;
            }
            double h = 1.e-2;
            double partialXX = secondPartial(m[0], 0, 0, h);
            double partialXY = secondPartial(m[0], 0, 1, h);
            double partialX = firstPartial(m[0], 0, h);
            double partialYY = secondPartial(m[0], 1, 1, h);
            double partialY = firstPartial(m[0], 1, h);
            a.set(0, 0, partialXX);
            a.set(0, 1, partialXY);
            a.set(1, 0, partialXY);
            a.set(1, 1, partialYY);
            b.set(0, 0, -partialX);
            b.set(1, 0, -partialY);
            TetradMatrix c;
            try {
                c = TetradAlgebra.solve(a, b);
            } catch (Exception e) {
                p[m[0]][0] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
                p[m[0]][1] += RandomUtil.getInstance().nextInt(2 * jump) - jump;
                continue;
            }
            double dx = c.get(0, 0);
            double dy = c.get(1, 0);
            p[m[0]][0] += dx;
            p[m[0]][1] += dy;
            oldDelta = delta;
        }
    } while (maxDelta > deltaCutoff);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) NumberFormat(java.text.NumberFormat)

Example 82 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class EdgeWeightComparison method getDisplayString.

public String getDisplayString() {
    String displayString = "";
    SemIm ref = reference;
    TetradMatrix referenceMatrix = ref.getEdgeCoef();
    TetradMatrix targetMatrix = target.getEdgeCoef();
    if (targetMatrix.columns() != referenceMatrix.columns() || targetMatrix.rows() != referenceMatrix.rows())
        return "The SEM IM's you selected don't have the same number of variables!  No comparison is possible here.";
    double score = 0;
    for (int i = 0; i < ref.getEdgeCoef().rows(); i++) {
        for (int j = 0; j < ref.getEdgeCoef().columns(); j++) {
            score += (targetMatrix.get(i, j) - referenceMatrix.get(i, j)) * (targetMatrix.get(i, j) - referenceMatrix.get(i, j));
        }
    }
    displayString += "Scheines Score: " + score + "\n\n";
    displayString += "(Calculated by summing the squared differences\n of each corresponding edge weight.)";
    return displayString;
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 83 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TimeSeriesData method serializableInstance.

/**
 * Generates a simple exemplar of this class to test serialization.
 */
public static TimeSeriesData serializableInstance() {
    List<String> varNames = new ArrayList<>();
    varNames.add("X");
    varNames.add("Y");
    return new TimeSeriesData(new TetradMatrix(2, 2), varNames);
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 84 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method removeZeroRowsAndCols.

private TetradMatrix removeZeroRowsAndCols(TetradMatrix w, List<Node> variables) {
    TetradMatrix _W = w.copy();
    List<Node> _variables = new ArrayList<>(variables);
    List<Integer> remove = new ArrayList<>();
    ROW: for (int i = 0; i < _W.rows(); i++) {
        TetradVector row = _W.getRow(i);
        for (int j = 0; j < row.size(); j++) {
            if (row.get(j) != 0)
                continue ROW;
        }
        remove.add(i);
    }
    COLUMN: for (int i = 0; i < _W.rows(); i++) {
        TetradVector col = _W.getColumn(i);
        for (int j = 0; j < col.size(); j++) {
            if (col.get(j) != 0)
                continue COLUMN;
        }
        if (!remove.contains((i))) {
            remove.add(i);
        }
    }
    int[] rows = new int[_W.rows() - remove.size()];
    int count = -1;
    for (int k = 0; k < w.rows(); k++) {
        if (remove.contains(k)) {
            variables.remove(_variables.get(k));
        } else {
            if (!remove.contains(k))
                rows[++count] = k;
        }
    }
    w = w.getSelection(rows, rows);
    return w;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 85 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class Ling method permuteRows.

private static TetradMatrix permuteRows(TetradMatrix mat, List<Integer> permutation) {
    TetradMatrix permutedMat = mat.like();
    for (int j = 0; j < mat.rows(); j++) {
        TetradVector row = mat.getRow(j);
        permutedMat.assignRow(permutation.get(j), row);
    }
    return permutedMat;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5