示例#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;
  }
示例#2
0
  private String reportIfDiscrete(Graph dag, DataSet dataSet) {
    List vars = dataSet.getVariables();
    Map<String, DiscreteVariable> nodesToVars = new HashMap<String, DiscreteVariable>();
    for (int i = 0; i < dataSet.getNumColumns(); i++) {
      DiscreteVariable var = (DiscreteVariable) vars.get(i);
      String name = var.getName();
      Node node = new GraphNode(name);
      nodesToVars.put(node.getName(), var);
    }

    BayesPm bayesPm = new BayesPm(new Dag(dag));
    List<Node> nodes = bayesPm.getDag().getNodes();

    for (Node node : nodes) {
      Node var = nodesToVars.get(node.getName());

      if (var instanceof DiscreteVariable) {
        DiscreteVariable var2 = nodesToVars.get(node.getName());
        int numCategories = var2.getNumCategories();
        List<String> categories = new ArrayList<String>();
        for (int j = 0; j < numCategories; j++) {
          categories.add(var2.getCategory(j));
        }
        bayesPm.setCategories(node, categories);
      }
    }

    BayesProperties properties = new BayesProperties(dataSet, dag);
    properties.setGraph(dag);

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);

    StringBuilder buf = new StringBuilder();
    buf.append("\nP-value = ").append(properties.getLikelihoodRatioP());
    buf.append("\nDf = ").append(properties.getPValueDf());
    buf.append("\nChi square = ").append(nf.format(properties.getPValueChisq()));
    buf.append("\nBIC score = ").append(nf.format(properties.getBic()));
    buf.append("\n\nH0: Completely disconnected graph.");

    return buf.toString();
  }
示例#3
0
  private static int shortestPath(Node n1, Node n2, Graph g) {
    Queue<Node> Q = new ArrayDeque<Node>();
    Map<Node, Node> V = new HashMap<Node, Node>();

    Q.offer(n1);
    V.put(n1, null);

    while (!Q.isEmpty()) {
      Node m = Q.poll();

      if (V.containsKey(n2)) break;

      for (Node p : g.getAdjacentNodes(m)) {
        if (V.containsKey(p)) continue;

        Q.offer(p);
        V.put(p, m);
      }
    }

    int s = 0;

    do {
      s++;
      n2 = V.get(n2);
    } while (n2 != null);

    return s;
  }
示例#4
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));
  }