/**
   * Computes that value x such that P(abs(N(0,1) > x) < alpha. Note that this is a two sided test
   * of the null hypothesis that the Fisher's Z value, which is distributed as N(0,1) is not equal
   * to 0.0.
   */
  private double cutoffGaussian(double alpha) {
    double upperTail = 1.0 - alpha / 2.0;
    double epsilon = 1e-14;

    // Find an upper bound.
    double lowerBound = -1.0;
    double upperBound = 0.0;

    while (ProbUtils.normalCdf(upperBound) < upperTail) {
      lowerBound += 1.0;
      upperBound += 1.0;
    }

    while (upperBound >= lowerBound + epsilon) {
      double midPoint = lowerBound + (upperBound - lowerBound) / 2.0;

      if (ProbUtils.normalCdf(midPoint) <= upperTail) {
        lowerBound = midPoint;
      } else {
        upperBound = midPoint;
      }
    }

    return lowerBound;
  }
Пример #2
0
 public double getEdgePValue(String source) {
   if (asymptLCovar == null) {
     return 0.;
   }
   for (int i = 0; i < lNames.length; i++) {
     if (lNames[i].equals(source)) {
       double z = Math.abs(A_hat[i] / Math.sqrt(asymptLCovar[i][i]));
       System.out.println("Asymptotic Z = " + z);
       return 2.0 * (1.0 - edu.cmu.tetrad.util.ProbUtils.normalCdf(z));
     }
   }
   return 0.;
 }
Пример #3
0
  /** @return the p value for the most recent test. */
  @Override
  public double getPValue() {
    double cdf = ProbUtils.chisqCdf(this.chisq, this.df);
    double p = 1.0 - cdf;

    String s = "";

    for (int i = 0; i < storedSextads.length; i++) {
      s += storedSextads[i] + " ";
    }

    s += "value = " + nf.format(storedValue) + " p = " + nf.format(p);

    TetradLogger.getInstance().log("sextadPValues", s);

    return p;
  }
  private double[] dependencePvalsLogit(Node x, Node y, List<Node> z) {
    if (!variablesPerNode.containsKey(x)) {
      throw new IllegalArgumentException("Unrecogized node: " + x);
    }

    if (!variablesPerNode.containsKey(y)) {
      throw new IllegalArgumentException("Unrecogized node: " + y);
    }

    for (Node node : z) {
      if (!variablesPerNode.containsKey(node)) {
        throw new IllegalArgumentException("Unrecogized node: " + node);
      }
    }

    List<Double> pValues = new ArrayList<Double>();

    int[] _rows = getNonMissingRows(x, y, z);
    logisticRegression.setRows(_rows);

    List<Node> yzDumList = new ArrayList<>();
    List<Node> yzList = new ArrayList<>();
    yzList.add(y);
    yzList.addAll(z);
    // List<Node> zList = new ArrayList<>();

    yzDumList.addAll(variablesPerNode.get(y));
    for (Node _z : z) {
      yzDumList.addAll(variablesPerNode.get(_z));
      // zList.addAll(variablesPerNode.get(_z));
    }

    // double[][] coeffsDep = new double[variablesPerNode.get(x).size()][];
    // DoubleMatrix2D coeffsNull = DoubleFactory2D.dense.make(zList.size(),
    // variablesPerNode.get(x).size());
    // DoubleMatrix2D coeffsDep = DoubleFactory2D.dense.make(yzDumList.size()+1,
    // variablesPerNode.get(x).size());
    double[] sumLnP = new double[yzList.size()];
    for (int i = 0; i < sumLnP.length; i++) sumLnP[i] = 0.0;

    for (int i = 0; i < variablesPerNode.get(x).size(); i++) {
      Node _x = variablesPerNode.get(x).get(i);

      LogisticRegression.Result result1 =
          logisticRegression.regress((DiscreteVariable) _x, yzDumList);

      int n = originalData.getNumRows();
      int k = yzDumList.size();

      // skip intercept at index 0
      int coefIndex = 1;
      for (int j = 0; j < yzList.size(); j++) {
        for (int dum = 0; dum < variablesPerNode.get(yzList.get(j)).size(); dum++) {

          double wald = Math.abs(result1.getCoefs()[coefIndex] / result1.getStdErrs()[coefIndex]);
          // double val = (1.0 - new
          // NormalDistribution(0,1).cumulativeProbability(wald))*2;//two-tailed test
          // double val = 1-result1.getProbs()[i+1];

          // this is exactly the same test as the linear case
          double val = (1.0 - ProbUtils.tCdf(wald, n - k)) * 2;
          // System.out.println(_x.getName() + "\t" + yzDumList.get(coefIndex-1).getName() + "\t" +
          // val + "\t" + (n-k));
          // if(val <= 0) System.out.println("Zero p-val t-test: p " + val + " stat " + wald + " k "
          // + k + " n " + n);
          sumLnP[j] += Math.log(val);
          coefIndex++;
        }
      }
    }

    double[] pVec = new double[sumLnP.length];
    for (int i = 0; i < pVec.length; i++) {
      if (sumLnP[i] == Double.NEGATIVE_INFINITY) pVec[i] = 0.0;
      else {
        int df = 2 * variablesPerNode.get(x).size() * variablesPerNode.get(yzList.get(i)).size();
        pVec[i] = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(-2 * sumLnP[i]);
      }
    }

    return pVec;
  }
Пример #5
0
  private String compileReport() {
    StringBuilder builder = new StringBuilder();

    builder.append("Datset\tFrom\tTo\tType\tValue\tSE\tT\tP");

    java.util.List<SemEstimator> estimators = wrapper.getMultipleResultList();

    for (int i = 0; i < estimators.size(); i++) {
      SemEstimator estimator = estimators.get(i);

      SemIm estSem = estimator.getEstimatedSem();
      String dataName = estimator.getDataSet().getName();

      for (Parameter parameter : estSem.getFreeParameters()) {
        builder.append("\n");
        builder.append(dataName + "\t");
        builder.append(parameter.getNodeA() + "\t");
        builder.append(parameter.getNodeB() + "\t");
        builder.append(typeString(parameter) + "\t");
        builder.append(asString(paramValue(estSem, parameter)) + "\t");
        /*
         Maximum number of free parameters for which statistics will be
         calculated. (Calculating standard errors is high complexity.) Set this to
         zero to turn  off statistics calculations (which can be problematic
         sometimes).
        */
        int maxFreeParamsForStatistics = 200;
        builder.append(
            asString(estSem.getStandardError(parameter, maxFreeParamsForStatistics)) + "\t");
        builder.append(asString(estSem.getTValue(parameter, maxFreeParamsForStatistics)) + "\t");
        builder.append(asString(estSem.getPValue(parameter, maxFreeParamsForStatistics)) + "\t");
      }

      List<Node> nodes = estSem.getVariableNodes();

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

        int n = estSem.getSampleSize();
        int df = n - 1;
        double mean = estSem.getMean(node);
        double stdDev = estSem.getMeanStdDev(node);
        double stdErr = stdDev / Math.sqrt(n);

        double tValue = mean / stdErr;
        double p = 2.0 * (1.0 - ProbUtils.tCdf(Math.abs(tValue), df));

        builder.append("\n");
        builder.append(dataName + "\t");
        builder.append(nodes.get(j) + "\t");
        builder.append(nodes.get(j) + "\t");
        builder.append("Mean" + "\t");
        builder.append(asString(mean) + "\t");
        builder.append(asString(stdErr) + "\t");
        builder.append(asString(tValue) + "\t");
        builder.append(asString(p) + "\t");
      }
    }

    return builder.toString();
  }