Search in sources :

Example 51 with TetradVector

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

the class TestGeneralizedSem method test5.

@Test
public void test5() {
    RandomUtil.getInstance().setSeed(29999483L);
    List<Node> nodes = new ArrayList<>();
    for (int i1 = 0; i1 < 5; i1++) {
        nodes.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    semIm.simulateDataReducedForm(1000, false);
    GeneralizedSemPm pm = new GeneralizedSemPm(semPm);
    GeneralizedSemIm im = new GeneralizedSemIm(pm, semIm);
    TetradVector e = new TetradVector(5);
    for (int i = 0; i < e.size(); i++) {
        e.set(i, RandomUtil.getInstance().nextNormal(0, 1));
    }
    TetradVector record1 = semIm.simulateOneRecord(e);
    TetradVector record2 = im.simulateOneRecord(e);
    print("XXX1" + e);
    print("XXX2" + record1);
    print("XXX3" + record2);
    for (int i = 0; i < record1.size(); i++) {
        assertEquals(record1.get(i), record2.get(i), 1e-10);
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) TetradVector(edu.cmu.tetrad.util.TetradVector) Test(org.junit.Test)

Example 52 with TetradVector

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

the class TestStandardizedSem method test1.

// Test the code that standardizes a data set.
@Test
public void test1() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    SemGraph graph = new SemGraph(new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false)));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateData(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    DataUtils.cov(_dataSet);
    DataUtils.mean(_dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, pm);
    SemIm imStandardized = estimator.estimate();
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    new TetradVector(imStandardized.getMeans());
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    imStandardized.getEdgeCoef();
    imStandardized.getErrCovar();
    assertTrue(isStandardized(sem));
}
Also used : ArrayList(java.util.ArrayList) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) TetradVector(edu.cmu.tetrad.util.TetradVector) SemPm(edu.cmu.tetrad.sem.SemPm) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 53 with TetradVector

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

the class IndTestDirichletScore 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);
    double variance = covMatrix.getValue(i, i);
    if (parents.length > 0) {
        // Regress z onto i, yielding regression coefficients b.
        TetradMatrix Czz = covMatrix.getSelection(parents, parents);
        TetradMatrix inverse;
        try {
            inverse = Czz.inverse();
        } catch (Exception e) {
            return true;
        }
        TetradVector Cyz = covMatrix.getSelection(parents, new int[] { i }).getColumn(0);
        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 54 with TetradVector

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

the class TestBoxDataSet 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 BoxDataSet(new DoubleDataBox(3, nodes.size()), 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());
        }
    }
    BoxDataSet _dataSet = new BoxDataSet((BoxDataSet) 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 : TetradVector(edu.cmu.tetrad.util.TetradVector) RandomUtil(edu.cmu.tetrad.util.RandomUtil) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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