use of edu.cmu.tetrad.search.Fges in project tetrad by cmu-phil.
the class TestFges method explore1.
// private OutputStream out =
// @Test
public void explore1() {
RandomUtil.getInstance().setSeed(1450184147770L);
int numVars = 10;
double edgesPerNode = 1.0;
int numCases = 1000;
double penaltyDiscount = 2.0;
final int numEdges = (int) (numVars * edgesPerNode);
List<Node> vars = new ArrayList<>();
for (int i = 0; i < numVars; i++) {
vars.add(new ContinuousVariable("X" + i));
}
Graph dag = GraphUtils.randomGraphRandomForwardEdges(vars, 0, numEdges, 30, 15, 15, false, true);
// printDegreeDistribution(dag, System.out);
int[] causalOrdering = new int[vars.size()];
for (int i = 0; i < vars.size(); i++) {
causalOrdering[i] = i;
}
LargeScaleSimulation simulator = new LargeScaleSimulation(dag, vars, causalOrdering);
simulator.setOut(out);
DataSet data = simulator.simulateDataFisher(numCases);
// ICovarianceMatrix cov = new CovarianceMatrix(data);
ICovarianceMatrix cov = new CovarianceMatrixOnTheFly(data);
SemBicScore score = new SemBicScore(cov);
score.setPenaltyDiscount(penaltyDiscount);
Fges fges = new Fges(score);
fges.setVerbose(false);
fges.setNumPatternsToStore(0);
fges.setOut(out);
fges.setFaithfulnessAssumed(true);
// fges.setMaxIndegree(1);
fges.setCycleBound(5);
Graph estPattern = fges.search();
// printDegreeDistribution(estPattern, out);
final Graph truePattern = SearchGraphUtils.patternForDag(dag);
int[][] counts = SearchGraphUtils.graphComparison(estPattern, truePattern, null);
int[][] expectedCounts = { { 2, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 8, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
for (int i = 0; i < counts.length; i++) {
assertTrue(Arrays.equals(counts[i], expectedCounts[i]));
}
//
// System.out.println(MatrixUtils.toString(expectedCounts));
// System.out.println(MatrixUtils.toString(counts));
}
use of edu.cmu.tetrad.search.Fges in project tetrad by cmu-phil.
the class TestFges method testExplore5.
@Test
public void testExplore5() {
Graph graph = GraphConverter.convert("A-->B,A-->C,A-->D,A->E,B-->F,C-->F,D-->F,E-->F");
Fges fges = new Fges(new GraphScore(graph));
fges.setFaithfulnessAssumed(false);
Graph pattern = fges.search();
assertEquals(SearchGraphUtils.patternForDag(graph), pattern);
}
use of edu.cmu.tetrad.search.Fges in project tetrad by cmu-phil.
the class TestFges method searchSemFges.
private Graph searchSemFges(DataSet Dk, double penalty) {
Dk = DataUtils.convertNumericalDiscreteToContinuous(Dk);
SemBicScore score = new SemBicScore(new CovarianceMatrixOnTheFly(Dk));
score.setPenaltyDiscount(penalty);
Fges fges = new Fges(score);
return fges.search();
}
use of edu.cmu.tetrad.search.Fges in project tetrad by cmu-phil.
the class TestFges method checkWithKnowledge.
/**
* Presents the input graph to FCI and checks to make sure the output of FCI is equivalent to the given output
* graph.
*/
private void checkWithKnowledge(String inputGraph, String answerGraph, IKnowledge knowledge) {
// Set up graph and node objects.
Graph input = GraphConverter.convert(inputGraph);
// Set up search.
Fges fges = new Fges(new GraphScore(input));
// Set up search.
fges.setKnowledge(knowledge);
// Run search
Graph result = fges.search();
// Build comparison graph.
Graph answer = GraphConverter.convert(answerGraph);
// Graph answer = new PC(new IndTestDSep(input)).search();
// System.out.println("Input = " + input);
// System.out.println("Knowledge = " + knowledge);
// System.out.println("Answer = " + answer);
// System.out.println("Result graph = " + result);
// Do test.
assertEquals(answer, result);
}
use of edu.cmu.tetrad.search.Fges in project tetrad by cmu-phil.
the class TestFges method testFromGraphSimpleFgesMb.
@Test
public void testFromGraphSimpleFgesMb() {
// This may fail if faithfulness is assumed but should pass if not.
Node x1 = new GraphNode("X1");
Node x2 = new GraphNode("X2");
Node x3 = new GraphNode("X3");
Node x4 = new GraphNode("X4");
Graph dag = new EdgeListGraph();
dag.addNode(x1);
dag.addNode(x2);
dag.addNode(x3);
dag.addNode(x4);
dag.addDirectedEdge(x1, x2);
dag.addDirectedEdge(x1, x3);
dag.addDirectedEdge(x4, x2);
dag.addDirectedEdge(x4, x3);
GraphScore fgesScore = new GraphScore(dag);
Fges fges = new Fges(fgesScore);
Graph pattern1 = fges.search();
Set<Node> mb = new HashSet<>();
mb.add(x1);
mb.addAll(pattern1.getAdjacentNodes(x1));
for (Node child : pattern1.getChildren(x1)) {
mb.addAll(pattern1.getParents(child));
}
Graph mb1 = pattern1.subgraph(new ArrayList<>(mb));
FgesMb fgesMb = new FgesMb(fgesScore);
Graph mb2 = fgesMb.search(x1);
assertEquals(mb1, mb2);
}
Aggregations