Search in sources :

Example 36 with TetradMatrix

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

the class TestStandardizedSem method test5.

@Test
public void test5() {
    RandomUtil.getInstance().setSeed(582374923L);
    SemGraph graph = new SemGraph();
    graph.setShowErrorTerms(true);
    Node x1 = new ContinuousVariable("X1");
    Node x2 = new ContinuousVariable("X2");
    Node x3 = new ContinuousVariable("X3");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.setShowErrorTerms(true);
    Node ex1 = graph.getExogenous(x1);
    Node ex2 = graph.getExogenous(x2);
    Node ex3 = graph.getExogenous(x3);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    // graph.addDirectedEdge(x1, x2);
    // graph.addBidirectedEdge(ex1, ex2);
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateDataRecursive(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, im.getSemPm());
    SemIm imStandardized = estimator.estimate();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    // sem.setErrorCovariance(ex1, ex2, -.24);
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 37 with TetradMatrix

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

the class TestStandardizedSem method test6.

@Test
public void test6() {
    // RandomUtil.getInstance().setSeed(582374923L);
    SemGraph graph = new SemGraph();
    graph.setShowErrorTerms(true);
    Node x1 = new ContinuousVariable("X1");
    Node x2 = new ContinuousVariable("X2");
    Node x3 = new ContinuousVariable("X3");
    graph.addNode(x1);
    graph.addNode(x2);
    graph.addNode(x3);
    graph.setShowErrorTerms(true);
    Node ex1 = graph.getExogenous(x1);
    Node ex2 = graph.getExogenous(x2);
    Node ex3 = graph.getExogenous(x3);
    graph.addDirectedEdge(x1, x3);
    graph.addDirectedEdge(x2, x3);
    graph.addDirectedEdge(x1, x2);
    graph.addBidirectedEdge(ex1, ex2);
    // List<List<Node>> treks = DataGraphUtils.treksIncludingBidirected(graph, x1, x3);
    // 
    // for (List<Node> trek : treks) {
    // System.out.println(trek);
    // }
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet dataSet = im.simulateDataRecursive(1000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    _dataSet = DataUtils.standardizeData(_dataSet);
    DataSet dataSetStandardized = ColtDataSet.makeData(dataSet.getVariables(), _dataSet);
    SemEstimator estimator = new SemEstimator(dataSetStandardized, im.getSemPm());
    SemIm imStandardized = estimator.estimate();
    StandardizedSemIm sem = new StandardizedSemIm(im);
    assertTrue(isStandardized(sem));
}
Also used : SemPm(edu.cmu.tetrad.sem.SemPm) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SemEstimator(edu.cmu.tetrad.sem.SemEstimator) SemIm(edu.cmu.tetrad.sem.SemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) StandardizedSemIm(edu.cmu.tetrad.sem.StandardizedSemIm) Test(org.junit.Test)

Example 38 with TetradMatrix

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

the class TestStandardizedSem method isStandardized.

private boolean isStandardized(StandardizedSemIm sem) {
    DataSet dataSet = sem.simulateData(5000, false);
    TetradMatrix _dataSet = dataSet.getDoubleData();
    TetradMatrix cov = DataUtils.cov(_dataSet);
    TetradVector means = DataUtils.mean(_dataSet);
    for (int i = 0; i < cov.rows(); i++) {
        if (!(Math.abs(cov.get(i, i) - 1) < .1)) {
            return false;
        }
        if (!(Math.abs(means.get(i)) < .1)) {
            return false;
        }
    }
    return true;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 39 with TetradMatrix

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

the class TestTetradMatrix method test1.

@Test
public void test1() {
    TetradMatrix x = new TetradMatrix(4, 0);
    TetradMatrix xT = x.transpose();
    TetradMatrix xTx = xT.times(x);
    TetradMatrix xTxInv = xTx.inverse();
    assertEquals(0, xTx.trace(), 0.01);
    assertEquals(0, xTxInv.trace(), 0.01);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Test(org.junit.Test)

Example 40 with TetradMatrix

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

the class TestMatrixUtils method testImpiedCovar.

@Test
public void testImpiedCovar() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    TetradMatrix err = im.getErrCovar();
    TetradMatrix coef = im.getEdgeCoef();
    TetradMatrix implied = MatrixUtils.impliedCovar(coef, err);
    assertTrue(MatrixUtils.isPositiveDefinite(implied));
    TetradMatrix corr = MatrixUtils.convertCovToCorr(new TetradMatrix(implied));
    assertTrue(MatrixUtils.isPositiveDefinite(corr));
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) SemPm(edu.cmu.tetrad.sem.SemPm) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Dag(edu.cmu.tetrad.graph.Dag) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

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