Example #1
0
  private Graph condense(Graph mimStructure, Graph mimbuildStructure) {
    //        System.out.println("Uncondensed: " + mimbuildStructure);

    Map<Node, Node> substitutions = new HashMap<Node, Node>();

    for (Node node : mimbuildStructure.getNodes()) {
      for (Node _node : mimStructure.getNodes()) {
        if (node.getName().startsWith(_node.getName())) {
          substitutions.put(node, _node);
          break;
        }

        substitutions.put(node, node);
      }
    }

    HashSet<Node> nodes = new HashSet<Node>(substitutions.values());
    Graph graph = new EdgeListGraph(new ArrayList<Node>(nodes));

    for (Edge edge : mimbuildStructure.getEdges()) {
      Node node1 = substitutions.get(edge.getNode1());
      Node node2 = substitutions.get(edge.getNode2());

      if (node1 == node2) continue;

      if (graph.isAdjacentTo(node1, node2)) continue;

      graph.addEdge(new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2()));
    }

    //        System.out.println("Condensed: " + graph);

    return graph;
  }
Example #2
0
  // Cannot be done if the graph changes.
  public void setInitialGraph(Graph initialGraph) {
    initialGraph = GraphUtils.replaceNodes(initialGraph, variables);

    out.println("Initial graph variables: " + initialGraph.getNodes());
    out.println("Data set variables: " + variables);

    if (!new HashSet<Node>(initialGraph.getNodes()).equals(new HashSet<Node>(variables))) {
      throw new IllegalArgumentException("Variables aren't the same.");
    }

    this.initialGraph = initialGraph;
  }
Example #3
0
  private Graph changeLatentNames(Graph full, Clusters measurements, List<String> latentVarList) {
    Graph g2 = null;

    try {
      g2 = (Graph) new MarshalledObject(full).get();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

    for (int i = 0; i < measurements.getNumClusters(); i++) {
      List<String> d = measurements.getCluster(i);
      String latentName = latentVarList.get(i);

      for (Node node : full.getNodes()) {
        if (!(node.getNodeType() == NodeType.LATENT)) {
          continue;
        }

        List<Node> _children = full.getChildren(node);

        _children.removeAll(ReidentifyVariables.getLatents(full));

        List<String> childNames = getNames(_children);

        if (new HashSet<String>(childNames).equals(new HashSet<String>(d))) {
          g2.getNode(node.getName()).setName(latentName);
        }
      }
    }

    return g2;
  }
Example #4
0
  // ===========================SCORING METHODS===================//
  public double scoreDag(Graph graph) {
    Graph dag = new EdgeListGraphSingleConnections(graph);
    buildIndexing(graph);

    double score = 0.0;

    for (Node y : dag.getNodes()) {
      Set<Node> parents = new HashSet<Node>(dag.getParents(y));
      int nextIndex = -1;
      for (int i = 0; i < getVariables().size(); i++) {
        nextIndex = hashIndices.get(variables.get(i));
      }
      int parentIndices[] = new int[parents.size()];
      Iterator<Node> pi = parents.iterator();
      int count = 0;
      while (pi.hasNext()) {
        Node nextParent = pi.next();
        parentIndices[count++] = hashIndices.get(nextParent);
      }

      if (this.isDiscrete()) {
        score += localDiscreteScore(nextIndex, parentIndices);
      } else {
        score += localSemScore(nextIndex, parentIndices);
      }
    }
    return score;
  }
Example #5
0
  private Graph structure(Graph mim) {
    List<Node> latents = new ArrayList<Node>();

    for (Node node : mim.getNodes()) {
      if (node.getNodeType() == NodeType.LATENT) {
        latents.add(node);
      }
    }

    return mim.subgraph(latents);
  }
Example #6
0
  private Graph restrictToEmpiricalLatents(Graph mimStructure, Graph mimbuildStructure) {
    Graph _mim = new EdgeListGraph(mimStructure);

    for (Node node : mimbuildStructure.getNodes()) {
      if (!mimbuildStructure.containsNode(node)) {
        _mim.removeNode(node);
      }
    }

    return _mim;
  }
Example #7
0
  private static double characteristicPathLength(Graph g) {
    List<Node> nodes = g.getNodes();
    int total = 0;
    int count = 0;

    for (int i = 0; i < nodes.size(); i++) {
      for (int j = i; j < nodes.size(); j++) {
        int shortest = shortestPath(nodes.get(i), nodes.get(j), g);
        total += shortest;
        count++;
      }
    }

    return total / (double) count;
  }
Example #8
0
  public void testAlternativeGraphs() {

    //        UniformGraphGenerator gen = new UniformGraphGenerator(UniformGraphGenerator.ANY_DAG);
    //        gen.setNumNodes(100);
    //        gen.setMaxEdges(200);
    //        gen.setMaxDegree(30);
    //        gen.setMaxInDegree(30);
    //        gen.setMaxOutDegree(30);
    ////        gen.setNumIterations(3000000);
    //        gen.setResamplingDegree(10);
    //
    //        gen.generate();
    //
    //        Graph graph = gen.getDag();

    Graph graph = weightedRandomGraph(250, 400);

    List<Integer> degreeCounts = new ArrayList<Integer>();
    Map<Integer, Integer> degreeCount = new HashMap<Integer, Integer>();

    for (Node node : graph.getNodes()) {
      int degree = graph.getNumEdges(node);
      degreeCounts.add(degree);

      if (degreeCount.get(degree) == null) {
        degreeCount.put(degree, 0);
      }

      degreeCount.put(degree, degreeCount.get(degree) + 1);
    }

    Collections.sort(degreeCounts);
    System.out.println(degreeCounts);
    List<Integer> _degrees = new ArrayList<Integer>(degreeCount.keySet());
    Collections.sort(_degrees);

    for (int i : _degrees) {
      int j = degreeCount.get(i);
      //            System.out.println(i + " " + j);
      System.out.println(log(i + 1) + " " + log(j));
    }

    System.out.println("\nCPL = " + characteristicPathLength(graph));

    Graph erGraph = erdosRenyiGraph(200, 200);
    System.out.println("\n ER CPL = " + characteristicPathLength(erGraph));
  }
Example #9
0
  /**
   * Greedy equivalence search: Start from the empty graph, add edges till model is significant.
   * Then start deleting edges till a minimum is achieved.
   *
   * @return the resulting Pattern.
   */
  public Graph search() {

    Graph graph;

    if (initialGraph == null) {
      graph = new EdgeListGraphSingleConnections(getVariables());
    } else {
      graph = new EdgeListGraphSingleConnections(initialGraph);
    }

    fireGraphChange(graph);
    buildIndexing(graph);
    addRequiredEdges(graph);

    topGraphs.clear();

    storeGraph(graph);

    List<Node> nodes = graph.getNodes();

    long start = System.currentTimeMillis();
    score = 0.0;

    // Do forward search.
    fes(graph, nodes);

    // Do backward search.
    bes(graph);

    long endTime = System.currentTimeMillis();
    this.elapsedTime = endTime - start;
    this.logger.log("graph", "\nReturning this graph: " + graph);

    this.logger.log("info", "Elapsed time = " + (elapsedTime) / 1000. + " s");
    this.logger.flush();

    return graph;
  }
Example #10
0
 private void buildIndexing(Graph graph) {
   this.hashIndices = new HashMap<Node, Integer>();
   for (Node node : graph.getNodes()) {
     this.hashIndices.put(node, variables.indexOf(node));
   }
 }