Search in sources :

Example 1 with TetradVector

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

the class TestColtDataSet method testPermuteRows.

@Test
public void testPermuteRows() {
    ContinuousVariable x1 = new ContinuousVariable("X1");
    ContinuousVariable x2 = new ContinuousVariable("X2");
    List<Node> nodes = new ArrayList<>();
    nodes.add(x1);
    nodes.add(x2);
    DataSet dataSet = new ColtDataSet(3, nodes);
    RandomUtil randomUtil = RandomUtil.getInstance();
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            dataSet.setDouble(i, j, randomUtil.nextDouble());
        }
    }
    ColtDataSet _dataSet = new ColtDataSet((ColtDataSet) dataSet);
    dataSet.permuteRows();
    I: for (int i = 0; i < dataSet.getNumRows(); i++) {
        TetradVector v = _dataSet.getDoubleData().getRow(i);
        for (int j = 0; j < dataSet.getNumRows(); j++) {
            TetradVector w = dataSet.getDoubleData().getRow(j);
            if (v.equals(w)) {
                continue I;
            }
        }
        fail("Missing row in permutation.");
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) TetradVector(edu.cmu.tetrad.util.TetradVector) RandomUtil(edu.cmu.tetrad.util.RandomUtil) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) ColtDataSet(edu.cmu.tetrad.data.ColtDataSet) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with TetradVector

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

the class IndTestTrekSep method determines.

/**
 * If <code>isDeterminismAllowed()</code>, deters to IndTestFisherZD; otherwise throws
 * UnsupportedOperationException.
 */
public boolean determines(List<Node> z, Node x) throws UnsupportedOperationException {
    int[] parents = new int[z.size()];
    for (int j = 0; j < parents.length; j++) {
        parents[j] = covMatrix.getVariables().indexOf(z.get(j));
    }
    int i = covMatrix.getVariables().indexOf(x);
    TetradMatrix matrix2D = covMatrix.getMatrix();
    double variance = matrix2D.get(i, i);
    if (parents.length > 0) {
        // Regress z onto i, yielding regression coefficients b.
        TetradMatrix Czz = matrix2D.getSelection(parents, parents);
        TetradMatrix inverse;
        try {
            inverse = Czz.inverse();
        } catch (Exception e) {
            return true;
        }
        TetradVector Cyz = matrix2D.getColumn(i);
        Cyz = Cyz.viewSelection(parents);
        TetradVector b = inverse.times(Cyz);
        variance -= Cyz.dotProduct(b);
    }
    return variance < 1e-20;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 3 with TetradVector

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

the class KMeans method squaredError.

/**
 * The squared error of the kth cluster.
 *
 * @param k The index of the cluster in question.
 * @return this squared error.
 */
private double squaredError(int k) {
    double squaredError = 0.0;
    for (int i = 0; i < data.rows(); i++) {
        if (clusters.get(i) == k) {
            TetradVector datum = data.getRow(i);
            TetradVector center = centers.getRow(k);
            squaredError += metric.dissimilarity(datum, center);
        }
    }
    return squaredError;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 4 with TetradVector

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

the class TimeSeriesUtils method sumOfArCoefficients.

public static double sumOfArCoefficients(DataSet timeSeries, int numLags) {
    DataSet timeLags = createLagData(timeSeries, numLags);
    List<Node> regressors = new ArrayList<>();
    for (int i = timeSeries.getNumColumns(); i < timeLags.getNumColumns(); i++) {
        regressors.add(timeLags.getVariable(i));
    }
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    double sum = 0.0;
    int n = 0;
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        RegressionResult result = regression.regress(target, regressors);
        double[] coef = result.getCoef();
        for (int k = 0; k < coef.length; k++) {
            sum += coef[k] * coef[k];
            n++;
        }
        TetradVector residualsColumn = result.getResiduals();
        // residuals.viewColumn(i).assign(residualsColumn);
        residuals.assignColumn(i, residualsColumn);
    }
    return sum / n;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 5 with TetradVector

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

the class TimeSeriesUtils method ar2.

public static DataSet ar2(DataSet timeSeries, int numLags) {
    List<Node> missingVariables = new ArrayList<>();
    for (Node node : timeSeries.getVariables()) {
        int index = timeSeries.getVariables().indexOf(node);
        boolean missing = true;
        for (int i = 0; i < timeSeries.getNumRows(); i++) {
            if (!Double.isNaN(timeSeries.getDouble(i, index))) {
                missing = false;
                break;
            }
        }
        if (missing) {
            missingVariables.add(node);
        }
    }
    DataSet timeLags = createLagData(timeSeries, numLags);
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        int index = timeSeries.getVariables().indexOf(target);
        if (missingVariables.contains(target)) {
            for (int i2 = 0; i2 < residuals.rows(); i2++) {
                residuals.set(i2, index, Double.NaN);
            }
            continue;
        }
        List<Node> regressors = new ArrayList<>();
        for (int i2 = timeSeries.getNumColumns(); i2 < timeLags.getNumColumns(); i2++) {
            int varIndex = i2 % timeSeries.getNumColumns();
            Node var = timeSeries.getVariable(varIndex);
            if (missingVariables.contains(var)) {
                continue;
            }
            regressors.add(timeLags.getVariable(i2));
        }
        RegressionResult result = regression.regress(target, regressors);
        TetradVector residualsColumn = result.getResiduals();
        residuals.assignColumn(i, residualsColumn);
    }
    return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

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