コード例 #1
1
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  private MaxMinAssocResult maxMinAssoc(Node t, List<Node> pc, List<Node> indepOfT) {
    Node f = null;
    List<Node> maxAssocSet = null;
    double maxAssoc = 0.0;

    for (Node v : variables) {
      if (t == v) continue;
      if (pc.contains(v)) continue;

      if (indepOfT.contains(v)) {
        continue;
      }

      List<Node> minAssoc = minAssoc(v, t, pc);
      double assoc = association(v, t, minAssoc);

      // If v is conditionally independent of t, don't consider it
      // again. Note if this code is right, then we have to use the
      // association test as an independence test... ugh.
      if (assoc < 1.0 - independenceTest.getAlpha()) {
        indepOfT.add(v);
      }

      if (assoc > maxAssoc) {
        maxAssocSet = minAssoc;
        maxAssoc = assoc;
        f = v;
      }
    }

    return new MaxMinAssocResult(f, maxAssocSet);
  }
コード例 #2
0
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  private void backwardsConditioning(List<Node> pc, Node target) {
    for (Node x : new LinkedList<>(pc)) {
      List<Node> _pc = new LinkedList<>(pc);
      _pc.remove(x);
      _pc.remove(target);

      List<Node> minAssoc = minAssoc(x, target, _pc);

      numIndTests++;

      if (independenceTest.isIndependent(x, target, minAssoc)) {
        pc.remove(x);
      }
    }
  }
コード例 #3
0
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  /**
   * Constructs.
   *
   * @param test The independence test used in the search.
   * @param depth The maximum number of variables conditioned on.
   * @param symmetric True if the symmetric algorithm is to be used.
   */
  public Mmmb(IndependenceTest test, int depth, boolean symmetric) {
    if (test == null) {
      throw new NullPointerException();
    }

    if (depth < -1) {
      throw new IllegalArgumentException();
    }

    this.independenceTest = test;
    this.variables = test.getVariables();
    this.depth = depth;
    this.symmetric = symmetric;

    pc = new HashMap<>();
    trimmed = new HashSet<>();
  }
コード例 #4
0
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  private List<Node> mmpc(Node t) {
    List<Node> pc = new LinkedList<>();
    boolean pcIncreased = true;

    // First optimization: Don't consider adding again variables that have
    // already been found independent of t.
    List<Node> indepOfT = new LinkedList<>();

    // Phase 1
    while (pcIncreased) {
      pcIncreased = false;

      MaxMinAssocResult ret = maxMinAssoc(t, pc, indepOfT);
      Node f = ret.getNode();
      List<Node> assocSet = ret.getAssocSet();

      if (f == null) {
        break;
      }

      numIndTests++;

      if (!independenceTest.isIndependent(f, t, assocSet)) {
        pcIncreased = true;
        pc.add(f);
      }
    }

    // Phase 2.
    backwardsConditioning(pc, t);

    TetradLogger.getInstance().log("details", "PC(" + t + ") = " + pc);
    //        System.out.println("PC(" + t + ") = " + pc);

    return pc;
  }
コード例 #5
0
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  private double association(Node x, Node target, List<Node> s) {
    numIndTests++;

    independenceTest.isIndependent(x, target, s);
    return 1.0 - independenceTest.getPValue();
  }
コード例 #6
0
ファイル: Mmmb.java プロジェクト: bd2kccd/r-causal
  private List<Node> mmmb(Node t) {
    // MB <- {}
    Set<Node> mb = new HashSet<>();

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

    for (Node node : getPc(t)) {
      List<Node> f = getPc(node);
      this.pc.put(node, f);
      _pcpc.addAll(f);
    }

    List<Node> pcpc = new LinkedList<>(_pcpc);

    Set<Node> currentMb = new HashSet<>(getPc(t));
    currentMb.addAll(pcpc);
    currentMb.remove(t);

    HashSet<Node> diff = new HashSet<>(currentMb);
    diff.removeAll(getPc(t));
    diff.remove(t);

    // for each x in PCPC \ PC
    for (Node x : diff) {
      List<Node> s = null;

      // Find an S such PC such that x _||_ t | S
      DepthChoiceGenerator generator = new DepthChoiceGenerator(pcpc.size(), depth);
      int[] choice;

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

        for (int index : choice) {
          _s.add(pcpc.get(index));
        }

        numIndTests++;
        if (independenceTest.isIndependent(t, x, _s)) {
          s = _s;
          break;
        }
      }

      if (s == null) {
        System.out.println("S not found.");
        //                mb.add(x);
        continue;
      }

      // y_set <- {y in PC(t) : x in PC(y)}
      Set<Node> ySet = new HashSet<>();
      for (Node y : getPc(t)) {
        if (this.pc.get(y).contains(x)) {
          ySet.add(y);
        }
      }

      //  For each y in y_set
      for (Node y : ySet) {
        if (x == y) continue;

        List<Node> _s = new LinkedList<>(s);
        _s.add(y);

        // If x NOT _||_ t | S U {y}
        numIndTests++;
        if (!independenceTest.isIndependent(t, x, _s)) {
          mb.add(x);
          break;
        }
      }
    }

    mb.addAll(getPc(t));
    return new LinkedList<>(mb);
  }