use of edu.cmu.tetrad.sem.Ricf in project tetrad by cmu-phil.
the class TestRicf method test4.
@Test
public void test4() {
List<Node> nodes1 = new ArrayList<>();
for (int i1 = 0; i1 < 5; i1++) {
nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
}
Graph g1 = GraphUtils.randomGraph(nodes1, 0, 5, 0, 0, 0, false);
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph g2 = GraphUtils.randomGraph(nodes, 0, 5, 0, 0, 0, false);
SemPm pm = new SemPm(g1);
SemIm im = new SemIm(pm);
DataSet dataset = im.simulateData(1000, false);
ICovarianceMatrix cov = new CovarianceMatrix(dataset);
new Ricf().ricf(new SemGraph(g1), cov, 0.001);
new Ricf().ricf(new SemGraph(g2), cov, 0.001);
}
use of edu.cmu.tetrad.sem.Ricf in project tetrad by cmu-phil.
the class TestRicf method testRicf1.
/**
* <pre>
* > ## A covariance matrix
*
* > "S" <- structure(c(2.93, -1.7, 0.76, -0.06,
* + -1.7, 1.64, -0.78, 0.1,
* + 0.76, -0.78, 1.66, -0.78,
* + -0.06, 0.1, -0.78, 0.81), .Dim = c(4,4),
* + .Dimnames = list(c("y", "x", "z", "u"), c("y", "x",
* "z", "u")))
*
* > ## The following should give the same fit.
*
* > ## Fit an ancestral graph y -> x <-> z <- u
*
* > fitAncestralGraph(ag1 <- makeAG(dag=DAG(x~y,z~u), bg = UG(~x*z)), S,
* n=100)
* $Shat
* y x z u
* y 2.930000 -1.4344254 0.0000000 0.0000000
* x -1.434425 1.3799680 -0.3430373 0.0000000
* z 0.000000 -0.3430373 1.5943070 -0.7442518
* u 0.000000 0.0000000 -0.7442518 0.8100000
*
* $Lhat
* y x z u
* y 2.93 0 0 0.00
* x 0.00 0 0 0.00
* z 0.00 0 0 0.00
* u 0.00 0 0 0.81
*
* $Bhat
* y x z u
* y 1.000000 0 0 0.0000000
* x 0.489565 1 0 0.0000000
* z 0.000000 0 1 0.9188294
* u 0.000000 0 0 1.0000000
*
* $Ohat
* y x z u
* y 0 0.0000000 0.0000000 0
* x 0 0.6777235 -0.3430373 0
* z 0 -0.3430373 0.9104666 0
* u 0 0.0000000 0.0000000 0
*
* $dev
* [1] 21.57711
*
* $df
* [1] 3
*
* $it
* [1] 4
*
* </pre>
*/
@Test
public void testRicf1() {
String[] varNames = new String[] { "y", "x", "z", "u" };
int numVars = varNames.length;
double[] values = { 2.93, -1.7, 0.76, -0.06, -1.7, 1.64, -0.78, 0.1, 0.76, -0.78, 1.66, -0.78, -0.06, 0.1, -0.78, 0.81 };
TetradMatrix m = matrix(values, numVars, numVars);
ICovarianceMatrix s = new CovarianceMatrix(DataUtils.createContinuousVariables(varNames), m, 30);
Graph mag = new EdgeListGraph();
Node x = new ContinuousVariable("x");
Node y = new ContinuousVariable("y");
Node z = new ContinuousVariable("z");
Node u = new ContinuousVariable("u");
mag.addNode(x);
mag.addNode(y);
mag.addNode(z);
mag.addNode(u);
mag.addDirectedEdge(y, x);
// mag.addDirectedEdge(u, x);
mag.addBidirectedEdge(x, z);
mag.addDirectedEdge(u, z);
// int n = 100;
double tol = 1e-06;
Ricf ricf = new Ricf();
Ricf.RicfResult ricfResult = ricf.ricf(new SemGraph(mag), s, tol);
// Test shat at least.
double[] shatValues = { 2.93, -1.434425, 0, 0, -1.434425, 1.379968, -0.343037, 0, 0, -0.343037, 1.594307, -0.744252, 0, 0, -0.744252, 0.81 };
double norm = normdiff(ricfResult, shatValues, numVars, numVars);
assertTrue(norm < 0.0001);
// sHat should be the same for the bidirected model.
mag.removeEdges(mag.getEdges());
mag.addBidirectedEdge(y, x);
// mag.addDirectedEdge(u, x);
mag.addBidirectedEdge(x, z);
mag.addBidirectedEdge(u, z);
ricf.ricf(new SemGraph(mag), s, tol);
norm = normdiff(ricfResult, shatValues, numVars, numVars);
assertTrue(norm < 0.0001);
}
Aggregations