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));
}
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));
}
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;
}
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);
}
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));
}
Aggregations