Example #1
0
 public Set<Edge> getNonadjacencies() {
   Graph complete = GraphUtils.completeGraph(graph);
   Set<Edge> nonAdjacencies = complete.getEdges();
   Graph undirected = GraphUtils.undirectedGraph(graph);
   nonAdjacencies.removeAll(undirected.getEdges());
   return new HashSet<Edge>(nonAdjacencies);
 }
Example #2
0
  /**
   * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the
   * extending class.
   */
  public void execute() {
    DataModel source = getDataModel();

    if (!(source instanceof DataSet)) {
      throw new IllegalArgumentException("Expecting a rectangular data set.");
    }

    DataSet data = (DataSet) source;

    if (!data.isContinuous()) {
      throw new IllegalArgumentException("Expecting a continuous data set.");
    }

    //        Lingam_old lingam = new Lingam_old();
    //        lingam.setPruningDone(true);
    //        lingam.setAlpha(getParams().getIndTestParams().getAlpha());
    //        GraphWithParameters result = lingam.lingam(data);
    //        Graph graph = result.getGraph();

    Lingam lingam = new Lingam();
    LingamParams params = (LingamParams) getParams();
    lingam.setPruneFactor(params.getPruneFactor());
    Graph graph = lingam.search(data);

    setResultGraph(graph);

    if (getSourceGraph() != null) {
      GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else {
      GraphUtils.circleLayout(graph, 200, 200, 150);
    }
  }
Example #3
0
  // TODO Fix this.
  private List<ScoredGraph> arrangeGraphs() {
    IGesRunner runner = (IGesRunner) getAlgorithmRunner();
    Graph resultGraph = runner.getResultGraph();

    List<ScoredGraph> topGraphs = runner.getTopGraphs();

    if (topGraphs == null) topGraphs = new ArrayList<ScoredGraph>();

    Graph latestWorkbenchGraph = runner.getParams().getSourceGraph();
    Graph sourceGraph = runner.getSourceGraph();

    boolean arrangedAll = false;

    for (ScoredGraph topGraph1 : topGraphs) {
      arrangedAll = GraphUtils.arrangeBySourceGraph(topGraph1.getGraph(), latestWorkbenchGraph);
    }

    if (!arrangedAll) {
      arrangedAll = GraphUtils.arrangeBySourceGraph(resultGraph, sourceGraph);
    }

    if (!arrangedAll) {
      for (ScoredGraph topGraph : topGraphs) {
        GraphUtils.circleLayout(topGraph.getGraph(), 200, 200, 150);
        GraphUtils.circleLayout(resultGraph, 200, 200, 150);
      }
    }

    return topGraphs;
  }
Example #4
0
 protected void doDefaultArrangement(Graph resultGraph) {
   if (getLatestWorkbenchGraph() != null) { // (alreadyLaidOut) {
     GraphUtils.arrangeBySourceGraph(resultGraph, getLatestWorkbenchGraph());
   } else if (getKnowledge().isDefaultToKnowledgeLayout()) {
     SearchGraphUtils.arrangeByKnowledgeTiers(resultGraph, getKnowledge());
     //            alreadyLaidOut = true;
   } else {
     GraphUtils.circleLayout(resultGraph, 200, 200, 150);
     //            alreadyLaidOut = true;
   }
 }
Example #5
0
  JScrollPane workbenchScroll(String resultLabel) {
    Graph resultGraph = resultGraph();

    Graph sourceGraph = algorithmRunner.getSourceGraph();
    SearchParams searchParams = algorithmRunner.getParams();
    Graph latestWorkbenchGraph = null;

    if (searchParams != null) {
      latestWorkbenchGraph = searchParams.getSourceGraph();
    }

    if (latestWorkbenchGraph == null) {
      GraphUtils.arrangeBySourceGraph(resultGraph, sourceGraph);
    } else {
      GraphUtils.arrangeBySourceGraph(resultGraph, latestWorkbenchGraph);
    }

    //        boolean arrangedAll = DataGraphUtils.arrangeBySourceGraph(resultGraph,
    //                latestWorkbenchGraph);
    //
    //        if (!arrangedAll) {
    //            arrangedAll =
    //                    DataGraphUtils.arrangeBySourceGraph(resultGraph, sourceGraph);
    //        }

    //        if (!arrangedAll) {
    //            DataGraphUtils.circleLayout(resultGraph, 200, 200, 150);
    //        }

    this.workbench = new GraphWorkbench(resultGraph);

    graphHistory.clear();
    graphHistory.add(resultGraph);

    this.workbench.setAllowDoubleClickActions(false);
    this.workbench.setAllowNodeEdgeSelection(true);
    this.workbenchScroll = new JScrollPane(workbench);
    //        workbenchScroll.setPreferredSize(new Dimension(450, 450));
    //        workbenchScroll.setBorder(new TitledBorder(resultLabel));

    this.workbench.addMouseListener(
        new MouseAdapter() {
          public void mouseExited(MouseEvent e) {
            storeLatestWorkbenchGraph();
          }
        });

    return workbenchScroll;
  }
Example #6
0
  public Graph orient() {
    Graph skeleton = GraphUtils.undirectedGraph(getPattern());
    Graph graph = new EdgeListGraph(skeleton.getNodes());

    List<Node> nodes = skeleton.getNodes();
    //        Collections.shuffle(nodes);

    if (isR1Done()) {
      ruleR1(skeleton, graph, nodes);
    }

    for (Edge edge : skeleton.getEdges()) {
      if (!graph.isAdjacentTo(edge.getNode1(), edge.getNode2())) {
        graph.addUndirectedEdge(edge.getNode1(), edge.getNode2());
      }
    }

    if (isR2Done()) {
      ruleR2(skeleton, graph);
    }

    if (isMeekDone()) {
      new MeekRules().orientImplied(graph);
    }

    return graph;
  }
Example #7
0
  // Causes a package cycle.
  public void testManualDiscretize2() {
    Graph graph = new Dag(GraphUtils.randomGraph(5, 0, 5, 3, 3, 3, false));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(100, false);

    List<Node> nodes = data.getVariables();

    Discretizer discretizer = new Discretizer(data);
    //        discretizer.setVariablesCopied(true);

    discretizer.equalCounts(nodes.get(0), 3);
    discretizer.equalIntervals(nodes.get(1), 2);
    discretizer.equalCounts(nodes.get(2), 5);
    discretizer.equalIntervals(nodes.get(3), 8);
    discretizer.equalCounts(nodes.get(4), 4);

    DataSet discretized = discretizer.discretize();

    System.out.println(discretized);

    assertEquals(2, maxInColumn(discretized, 0));
    assertEquals(1, maxInColumn(discretized, 1));
    assertEquals(4, maxInColumn(discretized, 2));
    assertEquals(7, maxInColumn(discretized, 3));
    assertEquals(3, maxInColumn(discretized, 4));
  }
  @Test
  public void test7() {
    RandomUtil.getInstance().setSeed(29999483L);

    List<Node> nodes = new ArrayList<>();
    int numVars = 10;

    for (int i = 0; i < numVars; i++) nodes.add(new ContinuousVariable("X" + (i + 1)));

    Graph graph =
        GraphUtils.randomGraphRandomForwardEdges(nodes, 0, numVars, 30, 15, 15, false, true);

    GeneralizedSemPm pm = new GeneralizedSemPm(graph);
    GeneralizedSemIm im = new GeneralizedSemIm(pm);

    print(im);

    DataSet data = im.simulateDataRecursive(1000, false);

    GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
    GeneralizedSemIm estIm = estimator.estimate(pm, data);

    print(estIm);
    print(estimator.getReport());

    double aSquaredStar = estimator.getaSquaredStar();

    assertEquals(0.67, aSquaredStar, 0.01);
  }
Example #9
0
  private void calculateArrowsForward(Node x, Node y, Graph graph) {
    clearArrow(x, y);

    if (!knowledgeEmpty()) {
      if (getKnowledge().isForbidden(x.getName(), y.getName())) {
        return;
      }
    }

    List<Node> naYX = getNaYX(x, y, graph);
    List<Node> t = getTNeighbors(x, y, graph);

    DepthChoiceGenerator gen = new DepthChoiceGenerator(t.size(), t.size());
    int[] choice;

    while ((choice = gen.next()) != null) {
      List<Node> s = GraphUtils.asList(choice, t);

      if (!knowledgeEmpty()) {
        if (!validSetByKnowledge(y, s)) {
          continue;
        }
      }

      double bump = insertEval(x, y, s, naYX, graph);

      if (bump > 0.0) {
        Arrow arrow = new Arrow(bump, x, y, s, naYX);
        sortedArrows.add(arrow);
        addLookupArrow(x, y, arrow);
      }
    }
  }
Example #10
0
 /** @return the list of triples corresponding to <code>getTripleClassificationNames</code>. */
 public List<List<Triple>> getTriplesLists(Node node) {
   List<List<Triple>> triplesList = new ArrayList<>();
   Graph graph = getGraph();
   //        triplesList.add(DataGraphUtils.getDefiniteCollidersFromGraph(node, graph));
   //        triplesList.add(DataGraphUtils.getDefiniteNoncollidersFromGraph(node, graph));
   triplesList.add(GraphUtils.getAmbiguousTriplesFromGraph(node, graph));
   return triplesList;
 }
Example #11
0
  public static void main(String... args) {
    long timestart = System.nanoTime();
    System.out.println("Loading first graph");
    Graph graph1 = GraphUtils.loadGraphTxt(new File("images_graph_10sub_pd40_group1.txt"));
    long timegraph1 = System.nanoTime();
    // System.out.println(graph1);
    System.out.println(
        "Done loading first graph. Elapsed time: " + (timegraph1 - timestart) / 1000000000 + "s");
    System.out.println("Loading second graph");
    Graph graph2 = GraphUtils.loadGraphTxt(new File("images_graph_10sub_pd40_group2.txt"));
    long timegraph2 = System.nanoTime();
    System.out.println(
        "Done loading second graph. Elapsed time: " + (timegraph2 - timegraph1) / 1000000000 + "s");

    // load the location map
    String workingDirectory = System.getProperty("user.dir");
    System.out.println(workingDirectory);
    Path mapPath = Paths.get("erich_coordinates.txt");
    System.out.println(mapPath);
    edu.cmu.tetrad.io.DataReader dataReaderMap = new TabularContinuousDataReader(mapPath, ',');
    try {
      DataSet locationMap = dataReaderMap.readInData();
      long timegraph3 = System.nanoTime();
      System.out.println(
          "Done loading location map. Elapsed time: "
              + (timegraph3 - timegraph2) / 1000000000
              + "s");

      System.out.println("Running Gdistance");
      // Make this either Gdistance or GdistanceVic
      List<Double> distance = GdistanceVic.distances(graph1, graph2, locationMap);
      System.out.println(distance);
      System.out.println(
          "Done running Distance. Elapsed time: "
              + (System.nanoTime() - timegraph3) / 1000000000
              + "s");
      System.out.println(
          "Total elapsed time: " + (System.nanoTime() - timestart) / 1000000000 + "s");

      PrintWriter writer = new PrintWriter("Gdistances.txt", "UTF-8");
      writer.println(distance);
      writer.close();
    } catch (Exception IOException) {
      IOException.printStackTrace();
    }
  }
  /**
   * Transforms a maximally directed pattern (PDAG) represented in graph <code>g</code> into an
   * arbitrary DAG by modifying <code>g</code> itself. Based on the algorithm described in
   * Chickering (2002) "Optimal structure identification with greedy search" Journal of Machine
   * Learning Research. R. Silva, June 2004
   */
  public static void pdagToDag(Graph g) {
    Graph p = new EdgeListGraph(g);
    List<Edge> undirectedEdges = new ArrayList<Edge>();

    for (Edge edge : g.getEdges()) {
      if (edge.getEndpoint1() == Endpoint.TAIL
          && edge.getEndpoint2() == Endpoint.TAIL
          && !undirectedEdges.contains(edge)) {
        undirectedEdges.add(edge);
      }
    }
    g.removeEdges(undirectedEdges);
    List<Node> pNodes = p.getNodes();

    do {
      Node x = null;

      for (Node pNode : pNodes) {
        x = pNode;

        if (p.getChildren(x).size() > 0) {
          continue;
        }

        Set<Node> neighbors = new HashSet<Node>();

        for (Edge edge : p.getEdges()) {
          if (edge.getNode1() == x || edge.getNode2() == x) {
            if (edge.getEndpoint1() == Endpoint.TAIL && edge.getEndpoint2() == Endpoint.TAIL) {
              if (edge.getNode1() == x) {
                neighbors.add(edge.getNode2());
              } else {
                neighbors.add(edge.getNode1());
              }
            }
          }
        }
        if (neighbors.size() > 0) {
          Collection<Node> parents = p.getParents(x);
          Set<Node> all = new HashSet<Node>(neighbors);
          all.addAll(parents);
          if (!GraphUtils.isClique(all, p)) {
            continue;
          }
        }

        for (Node neighbor : neighbors) {
          Node node1 = g.getNode(neighbor.getName());
          Node node2 = g.getNode(x.getName());

          g.addDirectedEdge(node1, node2);
        }
        p.removeNode(x);
        break;
      }
      pNodes.remove(x);
    } while (pNodes.size() > 0);
  }
Example #13
0
  /**
   * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the
   * extending class.
   */
  public void execute() {
    IKnowledge knowledge = getParams().getKnowledge();
    SearchParams searchParams = getParams();

    FciIndTestParams indTestParams = (FciIndTestParams) searchParams.getIndTestParams();

    //            Cfci fciSearch =
    //                    new Cfci(getIndependenceTest(), knowledge);
    //            fciSearch.setMaxIndegree(indTestParams.depth());
    //            Graph graph = fciSearch.search();
    //
    //            if (knowledge.isDefaultToKnowledgeLayout()) {
    //                SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    //            }
    //
    //            setResultGraph(graph);
    Graph graph;

    if (indTestParams.isRFCI_Used()) {
      Rfci fci = new Rfci(getIndependenceTest());
      fci.setKnowledge(knowledge);
      fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed());
      fci.setMaxPathLength(indTestParams.getMaxReachablePathLength());
      fci.setDepth(indTestParams.getDepth());
      graph = fci.search();
    } else {
      Fci fci = new Fci(getIndependenceTest());
      fci.setKnowledge(knowledge);
      fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed());
      fci.setPossibleDsepSearchDone(indTestParams.isPossibleDsepDone());
      fci.setMaxPathLength(indTestParams.getMaxReachablePathLength());
      fci.setDepth(indTestParams.getDepth());
      graph = fci.search();
    }

    if (getSourceGraph() != null) {
      GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
      SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
      GraphUtils.circleLayout(graph, 200, 200, 150);
    }

    setResultGraph(graph);
  }
Example #14
0
  /**
   * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the
   * extending class.
   */
  public void execute() {
    IKnowledge knowledge = getParams().getKnowledge();
    SearchParams searchParams = getParams();

    FciGesIndTestParams indTestParams = (FciGesIndTestParams) searchParams.getIndTestParams();

    //            Cfci fciSearch =
    //                    new Cfci(getIndependenceTest(), knowledge);
    //            fciSearch.setDepth(indTestParams.depth());
    //            Graph graph = fciSearch.search();
    //
    //            if (knowledge.isDefaultToKnowledgeLayout()) {
    //                SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    //            }
    //
    //            setResultGraph(graph);
    Graph graph;

    TFciGes fci = new TFciGes(getIndependenceTest());
    fci.setKnowledge(knowledge);
    fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed());
    fci.setPossibleDsepSearchDone(indTestParams.isPossibleDsepDone());
    fci.setMaxPathLength(indTestParams.getMaxReachablePathLength());
    fci.setDepth(indTestParams.getDepth());
    fci.setPenaltyDiscount(indTestParams.getPenaltyDiscount());
    fci.setSamplePrior(indTestParams.getSamplePrior());
    fci.setStructurePrior(indTestParams.getStructurePrior());
    fci.setCompleteRuleSetUsed(false);
    fci.setPenaltyDiscount(indTestParams.getPenaltyDiscount());
    fci.setFaithfulnessAssumed(indTestParams.isFaithfulnessAssumed());
    graph = fci.search();

    if (getSourceGraph() != null) {
      GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
      SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
      GraphUtils.circleLayout(graph, 200, 200, 150);
    }

    setResultGraph(graph);
  }
Example #15
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;
  }
  public static Graph bestGuessCycleOrientation(Graph graph, IndependenceTest test) {
    while (true) {
      List<Node> cycle = GraphUtils.directedCycle(graph);

      if (cycle == null) {
        break;
      }

      LinkedList<Node> _cycle = new LinkedList<Node>(cycle);

      Node first = _cycle.getFirst();
      Node last = _cycle.getLast();

      _cycle.addFirst(last);
      _cycle.addLast(first);

      int _j = -1;
      double minP = Double.POSITIVE_INFINITY;

      for (int j = 1; j < _cycle.size() - 1; j++) {
        int i = j - 1;
        int k = j + 1;

        Node x = test.getVariable(_cycle.get(i).getName());
        Node y = test.getVariable(_cycle.get(j).getName());
        Node z = test.getVariable(_cycle.get(k).getName());

        test.isIndependent(x, z, Collections.singletonList(y));

        System.out.println("Testing " + x + " _||_ " + z + " | " + y);

        double p = test.getPValue();

        System.out.println("p = " + p);

        if (p < minP) {
          _j = j;
          minP = p;
        }
      }

      Node x = _cycle.get(_j - 1);
      Node y = _cycle.get(_j);
      Node z = _cycle.get(_j + 1);

      graph.removeEdge(x, y);
      graph.removeEdge(z, y);
      graph.addDirectedEdge(x, y);
      graph.addDirectedEdge(z, y);
    }

    return graph;
  }
Example #17
0
  public void execute() {
    IKnowledge knowledge = getParams().getKnowledge();
    PcSearchParams searchParams = (PcSearchParams) getParams();

    PcIndTestParams indTestParams = (PcIndTestParams) searchParams.getIndTestParams();

    VcpcAlt VcpcAlt = new VcpcAlt(getIndependenceTest());
    VcpcAlt.setKnowledge(knowledge);
    VcpcAlt.setAggressivelyPreventCycles(this.isAggressivelyPreventCycles());
    VcpcAlt.setDepth(indTestParams.getDepth());
    Graph graph = VcpcAlt.search();

    if (getSourceGraph() != null) {
      GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
      SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
      GraphUtils.circleLayout(graph, 200, 200, 150);
    }

    setResultGraph(graph);
  }
Example #18
0
  private void ruleR1(Graph skeleton, Graph graph, List<Node> nodes) {
    for (Node node : nodes) {
      SortedMap<Double, String> scoreReports = new TreeMap<Double, String>();

      List<Node> adj = skeleton.getAdjacentNodes(node);

      DepthChoiceGenerator gen = new DepthChoiceGenerator(adj.size(), adj.size());
      int[] choice;
      double maxScore = Double.NEGATIVE_INFINITY;
      List<Node> parents = null;

      while ((choice = gen.next()) != null) {
        List<Node> _parents = GraphUtils.asList(choice, adj);

        double score = score(node, _parents);
        scoreReports.put(-score, _parents.toString());

        if (score > maxScore) {
          maxScore = score;
          parents = _parents;
        }
      }

      for (double score : scoreReports.keySet()) {
        TetradLogger.getInstance()
            .log(
                "score",
                "For " + node + " parents = " + scoreReports.get(score) + " score = " + -score);
      }

      TetradLogger.getInstance().log("score", "");

      if (parents == null) {
        continue;
      }

      if (normal(node, parents)) continue;

      for (Node _node : adj) {
        if (parents.contains(_node)) {
          Edge parentEdge = Edges.directedEdge(_node, node);

          if (!graph.containsEdge(parentEdge)) {
            graph.addEdge(parentEdge);
          }
        }
      }
    }
  }
Example #19
0
  /**
   * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the
   * extending class.
   */
  public void execute() {
    IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2());
    Parameters searchParams = getParams();

    Parameters params = searchParams;

    Graph graph;

    IndependenceTest independenceTest = getIndependenceTest();
    Score score = new ScoredIndTest(independenceTest);

    if (independenceTest instanceof IndTestDSep) {
      final DagToPag dagToPag = new DagToPag(((IndTestDSep) independenceTest).getGraph());
      dagToPag.setCompleteRuleSetUsed(params.getBoolean("completeRuleSetUsed", false));
      graph = dagToPag.convert();
    } else {
      GFci fci = new GFci(independenceTest, score);
      fci.setKnowledge(knowledge);
      fci.setCompleteRuleSetUsed(params.getBoolean("completeRuleSetUsed", false));
      fci.setMaxPathLength(params.getInt("maxReachablePathLength", -1));
      fci.setMaxDegree(params.getInt("maxIndegree"));
      fci.setCompleteRuleSetUsed(false);
      fci.setFaithfulnessAssumed(params.getBoolean("faithfulnessAssumed", true));
      graph = fci.search();
    }

    if (getSourceGraph() != null) {
      GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
      SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
      GraphUtils.circleLayout(graph, 200, 200, 150);
    }

    setResultGraph(graph);
  }
Example #20
0
  /**
   * @return the estimated conditional probability for the given assertion conditional on the given
   *     condition.
   */
  public double getConditionalProb(Proposition assertion, Proposition condition) {
    if (assertion.getVariableSource() != condition.getVariableSource()) {
      throw new IllegalArgumentException(
          "Assertion and condition must be " + "for the same Bayes IM.");
    }

    List<Node> assertionVars = assertion.getVariableSource().getVariables();
    List<Node> dataVars = dataSet.getVariables();

    assertionVars = GraphUtils.replaceNodes(assertionVars, dataVars);

    if (!new HashSet<Node>(assertionVars).equals(new HashSet<Node>(dataVars))) {
      throw new IllegalArgumentException(
          "Assertion variable and data variables"
              + " are either different or in a different order: "
              + "\n\tAssertion vars: "
              + assertionVars
              + "\n\tData vars: "
              + dataVars);
    }

    int[] point = new int[dims.length];
    int count1 = 0;
    int count2 = 0;
    this.missingValueCaseFound = false;

    point:
    for (int i = 0; i < numRows; i++) {
      for (int j = 0; j < dims.length; j++) {
        point[j] = dataSet.getInt(i, j);

        if (point[j] == DiscreteVariable.MISSING_VALUE) {
          continue point;
        }
      }

      if (condition.isPermissibleCombination(point)) {
        count1++;

        if (assertion.isPermissibleCombination(point)) {
          count2++;
        }
      }
    }

    return count2 / (double) count1;
  }
Example #21
0
  /////////////////////////////////////////////////////////////////////////////
  // set the sepSet of x and y to the minimal such subset of the given sepSet
  // and remove the edge <x, y> if background knowledge allows
  /////////////////////////////////////////////////////////////////////////////
  private void setMinSepSet(List<Node> sepSet, Node x, Node y) {
    // It is assumed that BK has been considered before calling this method
    // (for example, setting independent1 and independent2 in ruleR0_RFCI)
    /*
          // background knowledge requires this edge
    if (knowledge.noEdgeRequired(x.getNode(), y.getNode()))
    {
    	return;
    }
     */

    List<Node> empty = Collections.emptyList();
    boolean indep;

    try {
      indep = independenceTest.isIndependent(x, y, empty);
    } catch (Exception e) {
      indep = false;
    }

    if (indep) {
      getSepsets().set(x, y, empty);
      return;
    }

    int sepSetSize = sepSet.size();
    for (int i = 1; i <= sepSetSize; i++) {
      ChoiceGenerator cg = new ChoiceGenerator(sepSetSize, i);
      int[] combination;

      while ((combination = cg.next()) != null) {
        List<Node> condSet = GraphUtils.asList(combination, sepSet);

        try {
          indep = independenceTest.isIndependent(x, y, condSet);
        } catch (Exception e) {
          indep = false;
        }

        if (indep) {
          getSepsets().set(x, y, condSet);
          return;
        }
      }
    }
  }
  public static List<Dag> getAllDagsInUndirectedGraph(Graph graph) {
    Graph undirected = GraphUtils.undirectedGraph(graph);

    DagIterator iterator = new DagIterator(undirected);
    List<Dag> dags = new ArrayList<Dag>();

    while (iterator.hasNext()) {
      Graph _graph = iterator.next();

      try {
        Dag dag = new Dag(_graph);
        dags.add(dag);
      } catch (IllegalArgumentException e) {
        //
      }
    }

    return dags;
  }
Example #23
0
  /** Tests to see if d separation facts are symmetric. */
  public void testDSeparation2() {
    EdgeListGraphSingleConnections graph =
        new EdgeListGraphSingleConnections(
            new Dag(GraphUtils.randomGraph(7, 0, 14, 30, 15, 15, true)));

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

    int depth = -1;

    for (int i = 0; i < nodes.size(); i++) {
      for (int j = i; j < nodes.size(); j++) {
        Node x = nodes.get(i);
        Node y = nodes.get(j);

        List<Node> theRest = new ArrayList<Node>(nodes);
        //                theRest.remove(x);
        //                theRest.remove(y);

        DepthChoiceGenerator gen = new DepthChoiceGenerator(theRest.size(), depth);
        int[] choice;

        while ((choice = gen.next()) != null) {
          List<Node> z = new LinkedList<Node>();

          for (int k = 0; k < choice.length; k++) {
            z.add(theRest.get(choice[k]));
          }

          boolean dConnectedTo = graph.isDConnectedTo(x, y, z);
          boolean dConnectedTo1 = graph.isDConnectedTo(y, x, z);

          if (dConnectedTo != dConnectedTo1) {
            System.out.println(x + " d connected to " + y + " given " + z);
            System.out.println(graph);
            System.out.println("dconnectedto = " + dConnectedTo);
            System.out.println("dconnecteto1 = " + dConnectedTo1);
            fail();
          }
        }
      }
    }
  }
Example #24
0
  // Invalid if then nodes or graph changes.
  private void calculateArrowsBackward(Node x, Node y, Graph graph) {
    if (x == y) {
      return;
    }

    if (!graph.isAdjacentTo(x, y)) {
      return;
    }

    if (!knowledgeEmpty()) {
      if (!getKnowledge().noEdgeRequired(x.getName(), y.getName())) {
        return;
      }
    }

    List<Node> naYX = getNaYX(x, y, graph);

    clearArrow(x, y);

    List<Node> _naYX = new ArrayList<Node>(naYX);
    DepthChoiceGenerator gen = new DepthChoiceGenerator(_naYX.size(), _naYX.size());
    int[] choice;

    while ((choice = gen.next()) != null) {
      List<Node> H = GraphUtils.asList(choice, _naYX);

      if (!knowledgeEmpty()) {
        if (!validSetByKnowledge(y, H)) {
          continue;
        }
      }

      double bump = deleteEval(x, y, H, naYX, graph);

      if (bump > 0.0) {
        Arrow arrow = new Arrow(bump, x, y, H, naYX);
        sortedArrows.add(arrow);
        addLookupArrow(x, y, arrow);
      }
    }
  }
  /**
   * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the
   * extending class.
   */
  public void execute() {
    DataModel source = getDataModel();

    if (!(source instanceof DataSet)) {
      throw new IllegalArgumentException("Expecting a rectangular data set.");
    }

    DataSet data = (DataSet) source;

    if (!data.isContinuous()) {
      throw new IllegalArgumentException("Expecting a continuous data set.");
    }

    Lingam lingam = new Lingam();
    lingam.setAlpha(getParams().getIndTestParams().getAlpha());
    lingam.setPruningDone(true);
    lingam.setAlpha(getParams().getIndTestParams().getAlpha());
    GraphWithParameters result = lingam.lingam(data);
    setResultGraph(result.getGraph());
    GraphUtils.arrangeBySourceGraph(getResultGraph(), getSourceGraph());
  }
Example #26
0
  /** Tests to see if d separation facts are symmetric. */
  public void testDSeparation() {
    EdgeListGraphSingleConnections graph =
        new EdgeListGraphSingleConnections(
            new Dag(GraphUtils.randomGraph(7, 0, 7, 30, 15, 15, true)));
    System.out.println(graph);

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

    int depth = -1;

    for (int i = 0; i < nodes.size(); i++) {
      for (int j = i + 1; j < nodes.size(); j++) {
        Node x = nodes.get(i);
        Node y = nodes.get(j);

        List<Node> theRest = new ArrayList<Node>(nodes);
        theRest.remove(x);
        theRest.remove(y);

        DepthChoiceGenerator gen = new DepthChoiceGenerator(theRest.size(), depth);
        int[] choice;

        while ((choice = gen.next()) != null) {
          List<Node> z = new LinkedList<Node>();

          for (int k = 0; k < choice.length; k++) {
            z.add(theRest.get(choice[k]));
          }

          if (graph.isDSeparatedFrom(x, y, z) != graph.isDSeparatedFrom(y, x, z)) {
            fail(
                SearchLogUtils.independenceFact(x, y, z)
                    + " should have same d-sep result as "
                    + SearchLogUtils.independenceFact(y, x, z));
          }
        }
      }
    }
  }
Example #27
0
  public void rtestDSeparation4() {
    Graph graph = new Dag(GraphUtils.randomGraph(100, 20, 100, 5, 5, 5, false));

    long start, stop;
    int depth = -1;

    IndependenceTest test = new IndTestDSep(graph);

    Rfci fci = new Rfci(test);
    Fas fas = new Fas(test);
    start = System.currentTimeMillis();
    fci.setDepth(depth);
    fci.setVerbose(true);
    fci.search(fas, fas.getNodes());
    stop = System.currentTimeMillis();

    System.out.println("DSEP RFCI");
    System.out.println("# dsep checks = " + fas.getNumIndependenceTests());
    System.out.println("Elapsed " + (stop - start));
    System.out.println("Per " + fas.getNumIndependenceTests() / (double) (stop - start));

    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(1000, false);
    IndependenceTest test2 = new IndTestFisherZ(data, 0.001);

    Rfci fci3 = new Rfci(test2);
    Fas fas2 = new Fas(test2);
    start = System.currentTimeMillis();
    fci3.setDepth(depth);
    fci3.search(fas2, fas2.getNodes());
    stop = System.currentTimeMillis();

    System.out.println("FISHER Z RFCI");
    System.out.println("# indep checks = " + fas.getNumIndependenceTests());
    System.out.println("Elapsed " + (stop - start));
    System.out.println("Per " + fas.getNumIndependenceTests() / (double) (stop - start));
  }
Example #28
0
  public Graph search(List<Node> nodes) {
    long startTime = System.currentTimeMillis();
    localScoreCache.clear();

    if (!dataSet().getVariables().containsAll(nodes)) {
      throw new IllegalArgumentException("All of the nodes must be in " + "the supplied data set.");
    }

    Graph graph;

    if (initialGraph == null) {
      graph = new EdgeListGraphSingleConnections(nodes);
    } else {
      initialGraph = GraphUtils.replaceNodes(initialGraph, variables);
      graph = new EdgeListGraphSingleConnections(initialGraph);
    }

    topGraphs.clear();

    buildIndexing(graph);
    addRequiredEdges(graph);
    score = 0.0;

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

    // Do backward search.
    bes(graph);

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

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

    return graph;
  }
  @Test
  public void test6() {
    RandomUtil.getInstance().setSeed(29999483L);

    int numVars = 5;

    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < numVars; i++) nodes.add(new ContinuousVariable("X" + (i + 1)));

    Graph graph =
        GraphUtils.randomGraphRandomForwardEdges(nodes, 0, numVars, 30, 15, 15, false, true);

    SemPm spm = new SemPm(graph);

    SemImInitializationParams params = new SemImInitializationParams();
    params.setCoefRange(0.5, 1.5);
    params.setVarRange(1, 3);

    SemIm sim = new SemIm(spm, params);

    GeneralizedSemPm pm = new GeneralizedSemPm(spm);
    GeneralizedSemIm im = new GeneralizedSemIm(pm, sim);

    DataSet data = im.simulateData(1000, false);

    print(im);

    GeneralizedSemEstimator estimator = new GeneralizedSemEstimator();
    GeneralizedSemIm estIm = estimator.estimate(pm, data);

    print(estIm);
    print(estimator.getReport());

    double aSquaredStar = estimator.getaSquaredStar();

    assertEquals(0.59, aSquaredStar, 0.01);
  }
Example #30
0
  public void testManualDiscretize3() {
    Graph graph = new Dag(GraphUtils.randomGraph(5, 0, 5, 3, 3, 3, false));
    SemPm pm = new SemPm(graph);
    SemIm im = new SemIm(pm);
    DataSet data = im.simulateData(100, false);

    List<Node> nodes = data.getVariables();

    Discretizer discretizer = new Discretizer(data);
    discretizer.setVariablesCopied(true);

    discretizer.setVariablesCopied(true);
    discretizer.equalCounts(nodes.get(0), 3);

    DataSet discretized = discretizer.discretize();

    System.out.println(discretized);

    assertTrue(discretized.getVariable(0) instanceof DiscreteVariable);
    assertTrue(discretized.getVariable(1) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(2) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(3) instanceof ContinuousVariable);
    assertTrue(discretized.getVariable(4) instanceof ContinuousVariable);
  }