Beispiel #1
0
  /**
   * Calculates the sample likelihood and BIC score for i given its parents in a simple SEM model.
   */
  private double localSemScore(int i, int[] parents) {
    try {
      ICovarianceMatrix cov = getCovMatrix();
      double varianceY = cov.getValue(i, i);
      double residualVariance = varianceY;
      int n = sampleSize();
      int p = parents.length;
      int k = (p * (p + 1)) / 2 + p;
      //            int k = (p + 1) * (p + 1);
      //            int k = p + 1;
      TetradMatrix covxx = cov.getSelection(parents, parents);
      TetradMatrix covxxInv = covxx.inverse();
      TetradVector covxy = cov.getSelection(parents, new int[] {i}).getColumn(0);
      TetradVector b = covxxInv.times(covxy);
      residualVariance -= covxy.dotProduct(b);

      if (residualVariance <= 0 && verbose) {
        out.println(
            "Nonpositive residual varianceY: resVar / varianceY = "
                + (residualVariance / varianceY));
        return Double.NaN;
      }

      double c = getPenaltyDiscount();

      //            return -n * log(residualVariance) - 2 * k; //AIC
      return -n * Math.log(residualVariance) - c * k * Math.log(n);
      //            return -n * log(residualVariance) - c * k * (log(n) - log(2 * PI));
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
      //            throwMinimalLinearDependentSet(parents, cov);
    }
  }
Beispiel #2
0
  /**
   * Constructs a test using the given covariance matrix. Fourth moment statistics are not
   * caculated; it is assumed that the data are distributed as multivariate Gaussian.
   */
  public DeltaSextadTest(ICovarianceMatrix cov) {
    if (cov == null) {
      throw new NullPointerException();
    }

    this.cov = cov;
    this.N = cov.getSampleSize();
    this.numVars = cov.getVariables().size();
    this.variables = cov.getVariables();

    this.variablesHash = new HashMap<Node, Integer>();

    for (int i = 0; i < variables.size(); i++) {
      variablesHash.put(variables.get(i), i);
    }
  }
 public FindOneFactorClustersWithCausalIndicators(
     ICovarianceMatrix cov, TestType testType, double alpha) {
   this.variables = cov.getVariables();
   this.test = new ContinuousTetradTest(cov, testType, alpha);
   this.indTest = new IndTestFisherZ(cov, alpha);
   this.alpha = alpha;
   this.testType = testType;
   deltaTest = new DeltaTetradTest(cov);
   this.dataModel = cov;
   this.cov = cov;
 }
Beispiel #4
0
  /**
   * If using a covariance matrix or a correlation matrix, just returns the lookups. Otherwise
   * calculates the covariance.
   */
  private double r(Node _node1, Node _node2) {
    int i = variablesHash.get(_node1);
    int j = variablesHash.get(_node2);

    if (cov != null) {
      return cov.getValue(i, j);
    } else {
      double[] arr1 = data[i];
      double[] arr2 = data[j];
      return sxy(arr1, arr2, arr1.length);
    }
  }
Beispiel #5
0
  // Prints a smallest subset of parents that causes a singular matrix exception.
  private void printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
    List<Node> _parents = new ArrayList<>();
    for (int p : parents) _parents.add(variables.get(p));

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

    while ((choice = gen.next()) != null) {
      int[] sel = new int[choice.length];
      List<Node> _sel = new ArrayList<>();
      for (int m = 0; m < choice.length; m++) {
        sel[m] = parents[m];
        _sel.add(variables.get(sel[m]));
      }

      TetradMatrix m = cov.getSelection(sel, sel);

      try {
        m.inverse();
      } catch (Exception e2) {
        out.println("### Linear dependence among variables: " + _sel);
      }
    }
  }
Beispiel #6
0
 private void setCovMatrix(ICovarianceMatrix covarianceMatrix) {
   this.covariances = covarianceMatrix;
   this.variables = covarianceMatrix.getVariables();
   this.sampleSize = covarianceMatrix.getSampleSize();
 }
Beispiel #7
0
 private TetradVector getSelection2(ICovarianceMatrix cov, int[] rows, int k) {
   return cov.getSelection(rows, new int[] {k}).getColumn(0);
 }
Beispiel #8
0
 private TetradMatrix getSelection1(ICovarianceMatrix cov, int[] rows) {
   return cov.getSelection(rows, rows);
 }