public static Graph weightedRandomGraph(int n, int e) { List<Node> nodes = new ArrayList<Node>(); for (int i = 0; i < n; i++) nodes.add(new GraphNode("X" + i)); Graph graph = new EdgeListGraph(nodes); for (int e0 = 0; e0 < e; e0++) { int i1 = weightedRandom(nodes, graph); // int i2 = RandomUtil.getInstance().nextInt(n); int i2 = weightedRandom(nodes, graph); if (!(shortestPath(nodes.get(i1), nodes.get(i2), graph) < 9)) { e0--; continue; } if (i1 == i2) { e0--; continue; } Edge edge = Edges.undirectedEdge(nodes.get(i1), nodes.get(i2)); if (graph.containsEdge(edge)) { e0--; continue; } graph.addEdge(edge); } for (Edge edge : graph.getEdges()) { Node n1 = edge.getNode1(); Node n2 = edge.getNode2(); if (!graph.isAncestorOf(n2, n1)) { graph.removeEdge(edge); graph.addDirectedEdge(n1, n2); } else { graph.removeEdge(edge); graph.addDirectedEdge(n2, n1); } } return graph; }
public void rtest3() { Node x = new GraphNode("X"); Node y = new GraphNode("Y"); Node z = new GraphNode("Z"); Node w = new GraphNode("W"); List<Node> nodes = new ArrayList<Node>(); nodes.add(x); nodes.add(y); nodes.add(z); nodes.add(w); Graph g = new EdgeListGraph(nodes); g.addDirectedEdge(x, y); g.addDirectedEdge(x, z); g.addDirectedEdge(y, w); g.addDirectedEdge(z, w); Graph maxGraph = null; double maxPValue = -1.0; ICovarianceMatrix maxLatentCov = null; Graph mim = DataGraphUtils.randomMim(g, 8, 0, 0, 0, true); // Graph mim = DataGraphUtils.randomSingleFactorModel(5, 5, 8, 0, 0, 0); Graph mimStructure = structure(mim); SemPm pm = new SemPm(mim); System.out.println("\n\nTrue graph:"); System.out.println(mimStructure); SemImInitializationParams params = new SemImInitializationParams(); params.setCoefRange(0.5, 1.5); SemIm im = new SemIm(pm, params); int N = 1000; DataSet data = im.simulateData(N, false); CovarianceMatrix cov = new CovarianceMatrix(data); for (int i = 0; i < 1; i++) { ICovarianceMatrix _cov = DataUtils.reorderColumns(cov); List<List<Node>> partition; FindOneFactorClusters fofc = new FindOneFactorClusters(_cov, TestType.TETRAD_WISHART, .001); fofc.search(); partition = fofc.getClusters(); System.out.println(partition); List<String> latentVarList = reidentifyVariables(mim, data, partition, 2); Mimbuild2 mimbuild = new Mimbuild2(); mimbuild.setAlpha(0.001); // mimbuild.setMinimumSize(5); // To test knowledge. // Knowledge knowledge = new Knowledge2(); // knowledge.setEdgeForbidden("L.Y", "L.W", true); // knowledge.setEdgeRequired("L.Y", "L.Z", true); // mimbuild.setKnowledge(knowledge); Graph mimbuildStructure = mimbuild.search(partition, latentVarList, _cov); double pValue = mimbuild.getpValue(); System.out.println(mimbuildStructure); System.out.println("P = " + pValue); System.out.println("Latent Cov = " + mimbuild.getLatentsCov()); if (pValue > maxPValue) { maxPValue = pValue; maxGraph = new EdgeListGraph(mimbuildStructure); maxLatentCov = mimbuild.getLatentsCov(); } } System.out.println("\n\nTrue graph:"); System.out.println(mimStructure); System.out.println("\nBest graph:"); System.out.println(maxGraph); System.out.println("P = " + maxPValue); System.out.println("Latent Cov = " + maxLatentCov); System.out.println(); }
private void addRequiredEdges(Graph graph) { if (true) return; if (knowledgeEmpty()) return; for (Iterator<KnowledgeEdge> it = getKnowledge().requiredEdgesIterator(); it.hasNext(); ) { KnowledgeEdge next = it.next(); Node nodeA = graph.getNode(next.getFrom()); Node nodeB = graph.getNode(next.getTo()); if (!graph.isAncestorOf(nodeB, nodeA)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeA, nodeB); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeA, nodeB)); } } for (Edge edge : graph.getEdges()) { final String A = edge.getNode1().getName(); final String B = edge.getNode2().getName(); if (knowledge.isForbidden(A, B)) { Node nodeA = edge.getNode1(); Node nodeB = edge.getNode2(); if (nodeA != null && nodeB != null && graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } } else if (knowledge.isForbidden(B, A)) { Node nodeA = edge.getNode2(); Node nodeB = edge.getNode1(); if (nodeA != null && nodeB != null && graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } } } }
/** Do an actual deletion (Definition 13 from Chickering, 2002). */ private void delete(Node x, Node y, List<Node> subset, Graph graph, double bump) { Edge trueEdge = null; if (trueGraph != null) { Node _x = trueGraph.getNode(x.getName()); Node _y = trueGraph.getNode(y.getName()); trueEdge = trueGraph.getEdge(_x, _y); } if (log && verbose) { Edge oldEdge = graph.getEdge(x, y); String label = trueGraph != null && trueEdge != null ? "*" : ""; TetradLogger.getInstance() .log( "deletedEdges", (graph.getNumEdges() - 1) + ". DELETE " + oldEdge + " " + subset + " (" + bump + ") " + label); out.println( (graph.getNumEdges() - 1) + ". DELETE " + oldEdge + " " + subset + " (" + bump + ") " + label); } else { int numEdges = graph.getNumEdges() - 1; if (numEdges % 50 == 0) out.println(numEdges); } graph.removeEdge(x, y); for (Node h : subset) { Edge oldEdge = graph.getEdge(y, h); graph.removeEdge(y, h); graph.addDirectedEdge(y, h); if (log) { TetradLogger.getInstance() .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(y, h)); } if (verbose) { out.println("--- Directing " + oldEdge + " to " + graph.getEdge(y, h)); } if (Edges.isUndirectedEdge(graph.getEdge(x, h))) { if (!graph.isAdjacentTo(x, h)) throw new IllegalArgumentException("Not adjacent: " + x + ", " + h); oldEdge = graph.getEdge(x, h); graph.removeEdge(x, h); graph.addDirectedEdge(x, h); if (log) { TetradLogger.getInstance() .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(x, h)); } if (verbose) { out.println("--- Directing " + oldEdge + " to " + graph.getEdge(x, h)); } } } }
// serial. private void insert(Node x, Node y, List<Node> t, Graph graph, double bump) { if (graph.isAdjacentTo(x, y)) { return; // The initial graph may already have put this edge in the graph. // throw new IllegalArgumentException(x + " and " + y + " are already adjacent in // the graph."); } Edge trueEdge = null; if (trueGraph != null) { Node _x = trueGraph.getNode(x.getName()); Node _y = trueGraph.getNode(y.getName()); trueEdge = trueGraph.getEdge(_x, _y); } graph.addDirectedEdge(x, y); if (log) { String label = trueGraph != null && trueEdge != null ? "*" : ""; TetradLogger.getInstance() .log( "insertedEdges", graph.getNumEdges() + ". INSERT " + graph.getEdge(x, y) + " " + t + " " + bump + " " + label); } else { int numEdges = graph.getNumEdges() - 1; if (verbose) { if (numEdges % 50 == 0) out.println(numEdges); } } if (verbose) { String label = trueGraph != null && trueEdge != null ? "*" : ""; out.println( graph.getNumEdges() + ". INSERT " + graph.getEdge(x, y) + " " + t + " " + bump + " " + label); } else { int numEdges = graph.getNumEdges() - 1; if (verbose) { if (numEdges % 50 == 0) out.println(numEdges); } } for (Node _t : t) { Edge oldEdge = graph.getEdge(_t, y); if (oldEdge == null) throw new IllegalArgumentException("Not adjacent: " + _t + ", " + y); graph.removeEdge(_t, y); graph.addDirectedEdge(_t, y); if (log && verbose) { TetradLogger.getInstance() .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(_t, y)); out.println("--- Directing " + oldEdge + " to " + graph.getEdge(_t, y)); } } }