Search in sources :

Example 41 with TetradVector

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

the class SemBicScoreDeterministic method getMinimalLinearlyDependentSet.

private int[] getMinimalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
    double small = getDeterminismThreshold();
    List<Node> _parents = new ArrayList<>();
    for (int p : parents) _parents.add(variables.get(p));
    DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
    int[] choice;
    while ((choice = gen.next()) != null) {
        int[] sel = new int[choice.length];
        List<Node> _sel = new ArrayList<>();
        for (int m = 0; m < choice.length; m++) {
            sel[m] = parents[m];
            _sel.add(variables.get(sel[m]));
        }
        TetradMatrix m = cov.getSelection(sel, sel);
        double s2 = getCovariances().getValue(i, i);
        TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
        TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
        s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
        if (s2 <= small) {
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return sel;
        }
        try {
            m.inverse();
        } catch (Exception e2) {
            // forbidden.add(sel[0]);
            out.println("### Linear dependence among variables: " + _sel);
            out.println("### Removing " + _sel.get(0));
            return sel;
        }
    }
    return new int[0];
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException)

Example 42 with TetradVector

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

the class SemBicScoreDeterministic method localScore.

/**
 * Calculates the sample likelihood and BIC score for i given its parents in a simple SEM model
 */
public double localScore(int i, int... parents) {
    for (int p : parents) if (forbidden.contains(p))
        return Double.NaN;
    double small = getDeterminismThreshold();
    double s2 = getCovariances().getValue(i, i);
    int p = parents.length;
    TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
    TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
    try {
        s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
    } catch (SingularMatrixException e) {
        s2 = 0;
    }
    // System.out.println(s2);
    int n = getSampleSize();
    int k = 2 * p + 1;
    if (s2 < small) {
        s2 = 0;
    }
    if (s2 == 0) {
        printDeterminism(i, parents);
        return Double.NaN;
    }
    return -(n) * log(s2) - getPenaltyDiscount() * k * log(n);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 43 with TetradVector

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

the class ShortTriangularMatrix method becomeCorrelationMatrix.

public void becomeCorrelationMatrix(DataSet dataSet) {
    for (int i = 0; i < dataSet.getNumColumns(); i++) matrix[i][i] = 10000;
    TetradMatrix doubleData = dataSet.getDoubleData();
    TetradVector[] views = new TetradVector[dataSet.getNumColumns()];
    for (int i = 0; i < views.length; i++) views[i] = doubleData.getColumn(i);
    for (int i = 0; i < dataSet.getNumColumns() - 1; i++) for (int j = i + 1; j < dataSet.getNumColumns(); j++) matrix[j][i] = StatUtils.compressedCorrelation(views[i], views[j]);
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 44 with TetradVector

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

the class KMeans method toString.

/**
 * @return a string representation of the cluster result.
 */
public String toString() {
    NumberFormat n1 = NumberFormatUtil.getInstance().getNumberFormat();
    TetradVector counts = countClusterSizes();
    double totalSquaredError = totalSquaredError();
    StringBuilder buf = new StringBuilder();
    buf.append("Cluster Result (").append(clusters.size()).append(" cases, ").append(centers.columns()).append(" feature(s), ").append(centers.rows()).append(" clusters)");
    for (int k = 0; k < centers.rows(); k++) {
        buf.append("\n\tCluster #").append(k + 1).append(": n = ").append(counts.get(k));
        buf.append(" Squared Error = ").append(n1.format(squaredError(k)));
    }
    buf.append("\n\tTotal Squared Error = ").append(n1.format(totalSquaredError));
    return buf.toString();
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) NumberFormat(java.text.NumberFormat)

Example 45 with TetradVector

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

the class KMeans method reassignPoints.

// ==========================PRIVATE METHODS=========================//
private int reassignPoints() {
    int numChanged = 0;
    for (int i = 0; i < data.rows(); i++) {
        TetradVector datum = data.getRow(i);
        double minDissimilarity = Double.POSITIVE_INFINITY;
        int cluster = -1;
        for (int k = 0; k < centers.rows(); k++) {
            TetradVector center = centers.getRow(k);
            double dissimilarity = getMetric().dissimilarity(datum, center);
            if (dissimilarity < minDissimilarity) {
                minDissimilarity = dissimilarity;
                cluster = k;
            }
        }
        if (cluster != clusters.get(i)) {
            clusters.set(i, cluster);
            numChanged++;
        }
    }
    // System.out.println("Moved " + numChanged + " points.");
    return numChanged;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector)

Aggregations

TetradVector (edu.cmu.tetrad.util.TetradVector)54 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)44 ArrayList (java.util.ArrayList)19 Node (edu.cmu.tetrad.graph.Node)11 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)9 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 DoubleArrayList (cern.colt.list.DoubleArrayList)5 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)5 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)5 List (java.util.List)4 Test (org.junit.Test)4 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)3 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)2 RandomUtil (edu.cmu.tetrad.util.RandomUtil)2 Vector (java.util.Vector)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)1 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)1