예제 #1
0
파일: Lofs.java 프로젝트: jdramsey/tetrad
  private double pValue(Node node, List<Node> parents) {
    List<Double> _residuals = new ArrayList<Double>();

    Node _target = node;
    List<Node> _regressors = parents;
    Node target = getVariable(variables, _target.getName());
    List<Node> regressors = new ArrayList<Node>();

    for (Node _regressor : _regressors) {
      Node variable = getVariable(variables, _regressor.getName());
      regressors.add(variable);
    }

    DATASET:
    for (int m = 0; m < dataSets.size(); m++) {
      RegressionResult result = regressions.get(m).regress(target, regressors);
      TetradVector residualsSingleDataset = result.getResiduals();

      for (int h = 0; h < residualsSingleDataset.size(); h++) {
        if (Double.isNaN(residualsSingleDataset.get(h))) {
          continue DATASET;
        }
      }

      DoubleArrayList _residualsSingleDataset =
          new DoubleArrayList(residualsSingleDataset.toArray());

      double mean = Descriptive.mean(_residualsSingleDataset);
      double std =
          Descriptive.standardDeviation(
              Descriptive.variance(
                  _residualsSingleDataset.size(),
                  Descriptive.sum(_residualsSingleDataset),
                  Descriptive.sumOfSquares(_residualsSingleDataset)));

      for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
        //                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) /
        // std);
        if (isMeanCenterResiduals()) {
          _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean));
        }
        //                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2)));
      }

      for (int k = 0; k < _residualsSingleDataset.size(); k++) {
        _residuals.add(_residualsSingleDataset.get(k));
      }
    }

    double[] _f = new double[_residuals.size()];

    for (int k = 0; k < _residuals.size(); k++) {
      _f[k] = _residuals.get(k);
    }

    return new AndersonDarlingTest(_f).getP();
  }
예제 #2
0
  /** assertion: isBasicParametersValid == false */
  protected void updateIncrementalStats() {
    // prepare arguments
    double[] arguments = new double[4];
    arguments[0] = this.min;
    arguments[1] = this.max;
    arguments[2] = this.sum;
    arguments[3] = this.sum_xx;

    Descriptive.incrementalUpdate(this.elements, this.size, this.elements.size() - 1, arguments);

    // store the new parameters back
    this.min = arguments[0];
    this.max = arguments[1];
    this.sum = arguments[2];
    this.sum_xx = arguments[3];

    this.isIncrementalStatValid = true;
    this.size =
        this.elements.size(); // next time we don't need to redo the stuff we have just done...
  }
예제 #3
0
파일: Lofs.java 프로젝트: jdramsey/tetrad
  private double andersonDarlingPASquareStarB(Node node, List<Node> parents) {
    List<Double> _residuals = new ArrayList<Double>();

    Node _target = node;
    List<Node> _regressors = parents;
    Node target = getVariable(variables, _target.getName());
    List<Node> regressors = new ArrayList<Node>();

    for (Node _regressor : _regressors) {
      Node variable = getVariable(variables, _regressor.getName());
      regressors.add(variable);
    }

    double sum = 0.0;

    DATASET:
    for (int m = 0; m < dataSets.size(); m++) {
      RegressionResult result = regressions.get(m).regress(target, regressors);
      TetradVector residualsSingleDataset = result.getResiduals();

      for (int h = 0; h < residualsSingleDataset.size(); h++) {
        if (Double.isNaN(residualsSingleDataset.get(h))) {
          continue DATASET;
        }
      }

      DoubleArrayList _residualsSingleDataset =
          new DoubleArrayList(residualsSingleDataset.toArray());

      double mean = Descriptive.mean(_residualsSingleDataset);
      double std =
          Descriptive.standardDeviation(
              Descriptive.variance(
                  _residualsSingleDataset.size(),
                  Descriptive.sum(_residualsSingleDataset),
                  Descriptive.sumOfSquares(_residualsSingleDataset)));

      // By centering the individual residual columns, all moments of the mixture become weighted
      // averages of the moments
      // of the individual columns.
      // http://en.wikipedia.org/wiki/Mixture_distribution#Finite_and_countable_mixtures
      for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
        //                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) /
        // std);
        //                _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2)) / std);
        if (isMeanCenterResiduals()) {
          _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean));
        }
      }

      double[] _f = new double[_residuals.size()];

      for (int k = 0; k < _residuals.size(); k++) {
        _f[k] = _residuals.get(k);
      }

      sum += new AndersonDarlingTest(_f).getASquaredStar();
    }

    return sum / dataSets.size();
  }
예제 #4
0
파일: Lofs.java 프로젝트: jdramsey/tetrad
  private double localScoreB(Node node, List<Node> parents) {

    double score = 0.0;
    double maxScore = Double.NEGATIVE_INFINITY;

    Node _target = node;
    List<Node> _regressors = parents;
    Node target = getVariable(variables, _target.getName());
    List<Node> regressors = new ArrayList<Node>();

    for (Node _regressor : _regressors) {
      Node variable = getVariable(variables, _regressor.getName());
      regressors.add(variable);
    }

    DATASET:
    for (int m = 0; m < dataSets.size(); m++) {
      RegressionResult result = regressions.get(m).regress(target, regressors);
      TetradVector residualsSingleDataset = result.getResiduals();
      DoubleArrayList _residualsSingleDataset =
          new DoubleArrayList(residualsSingleDataset.toArray());

      for (int h = 0; h < residualsSingleDataset.size(); h++) {
        if (Double.isNaN(residualsSingleDataset.get(h))) {
          continue DATASET;
        }
      }

      double mean = Descriptive.mean(_residualsSingleDataset);
      double std =
          Descriptive.standardDeviation(
              Descriptive.variance(
                  _residualsSingleDataset.size(),
                  Descriptive.sum(_residualsSingleDataset),
                  Descriptive.sumOfSquares(_residualsSingleDataset)));

      for (int i2 = 0; i2 < _residualsSingleDataset.size(); i2++) {
        _residualsSingleDataset.set(i2, (_residualsSingleDataset.get(i2) - mean) / std);
      }

      double[] _f = new double[_residualsSingleDataset.size()];

      for (int k = 0; k < _residualsSingleDataset.size(); k++) {
        _f[k] = _residualsSingleDataset.get(k);
      }

      DoubleArrayList f = new DoubleArrayList(_f);

      for (int k = 0; k < f.size(); k++) {
        f.set(k, Math.abs(f.get(k)));
      }

      double _mean = Descriptive.mean(f);
      double diff = _mean - Math.sqrt(2.0 / Math.PI);
      score += diff * diff;

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

    double avg = score / dataSets.size();

    return avg;
  }
예제 #5
0
 protected void updateSumOfLogarithms() {
   this.sumOfLogarithms = Descriptive.sumOfLogarithms(this.elements, 0, size() - 1);
   this.isSumOfLogarithmsValid = true;
 }
예제 #6
0
 /** assertion: isBasicParametersValid == false */
 protected void updateSumOfInversions() {
   this.sumOfInversions = Descriptive.sumOfInversions(this.elements, 0, size() - 1);
   this.isSumOfInversionsValid = true;
 }
예제 #7
0
 /**
  * Returns the trimmed mean. That is the mean of the data <i>if</i> the <tt>s</tt> smallest and
  * <tt>l</tt> largest elements <i>would</i> be removed from the receiver (they are not removed).
  *
  * @param s the number of smallest elements to trim away (<tt>s >= 0</tt>).
  * @param l the number of largest elements to trim away (<tt>l >= 0</tt>).
  * @return the trimmed mean.
  */
 public synchronized double trimmedMean(int s, int l) {
   // no caching for this parameter.
   return Descriptive.trimmedMean(sortedElements_unsafe(), mean(), s, l);
 }
예제 #8
0
 /**
  * Modifies the receiver to be standardized. Changes each element <tt>x[i]</tt> as follows:
  * <tt>x[i] = (x[i]-mean)/standardDeviation</tt>.
  */
 public synchronized void standardize(double mean, double standardDeviation) {
   Descriptive.standardize(this.elements, mean, standardDeviation);
   clearAllMeasures();
   invalidateAll();
   this.size = 0;
 }
예제 #9
0
  /**
   * Returns the <tt>k-th</tt> order sum of powers, which is <tt>Sum( x[i]<sup>k</sup> )</tt>.
   *
   * @param k the order of the powers.
   * @return the sum of powers.
   */
  public synchronized double sumOfPowers(int k) {
    // no chaching for this measure
    if (k >= -1 && k <= 2) return super.sumOfPowers(k);

    return Descriptive.sumOfPowers(this.elements, k);
  }
예제 #10
0
 /**
  * Returns the exact quantiles of the specified percentages.
  *
  * @param percentages the percentages for which quantiles are to be computed. Each percentage must
  *     be in the interval <tt>(0.0,1.0]</tt>. <tt>percentages</tt> must be sorted ascending.
  * @return the exact quantiles.
  */
 public DoubleArrayList quantiles(DoubleArrayList percentages) {
   return Descriptive.quantiles(sortedElements_unsafe(), percentages);
 }
예제 #11
0
 /**
  * Returns exactly how many percent of the elements contained in the receiver are <tt>&lt;=
  * element</tt>. Does linear interpolation if the element is not contained but lies in between two
  * contained elements.
  *
  * @param element the element to search for.
  * @return the exact percentage <tt>phi</tt> of elements <tt>&lt;= element</tt> (<tt>0.0 &lt;= phi
  *     &lt;= 1.0)</tt>.
  */
 public synchronized double quantileInverse(double element) {
   return Descriptive.quantileInverse(sortedElements_unsafe(), element);
 }
예제 #12
0
 /**
  * Returns the exact <tt>phi-</tt>quantile; that is, the smallest contained element <tt>elem</tt>
  * for which holds that <tt>phi</tt> percent of elements are less than <tt>elem</tt>.
  *
  * @param phi must satisfy <tt>0 &lt; phi &lt; 1</tt>.
  */
 public synchronized double quantile(double phi) {
   return Descriptive.quantile(sortedElements_unsafe(), phi);
 }
예제 #13
0
 /**
  * Returns the moment of <tt>k</tt>-th order with value <tt>c</tt>, which is <tt>Sum(
  * (x[i]-c)<sup>k</sup> ) / size()</tt>.
  *
  * @param k the order; any number - can be less than zero, zero or greater than zero.
  * @param c any number.
  */
 public synchronized double moment(int k, double c) {
   // currently no caching for this parameter
   return Descriptive.moment(this.elements, k, c);
 }
예제 #14
0
 /**
  * Computes the frequency (number of occurances, count) of each distinct element. After this call
  * returns both <tt>distinctElements</tt> and <tt>frequencies</tt> have a new size (which is equal
  * for both), which is the number of distinct elements currently contained.
  *
  * <p>Distinct elements are filled into <tt>distinctElements</tt>, starting at index 0. The
  * frequency of each distinct element is filled into <tt>frequencies</tt>, starting at index 0.
  * Further, both <tt>distinctElements</tt> and <tt>frequencies</tt> are sorted ascending by
  * "element" (in sync, of course). As a result, the smallest distinct element (and its frequency)
  * can be found at index 0, the second smallest distinct element (and its frequency) at index 1,
  * ..., the largest distinct element (and its frequency) at index
  * <tt>distinctElements.size()-1</tt>.
  *
  * <p><b>Example:</b> <br>
  * <tt>elements = (8,7,6,6,7) --> distinctElements = (6,7,8), frequencies = (2,2,1)</tt>
  *
  * @param distinctElements a list to be filled with the distinct elements; can have any size.
  * @param frequencies a list to be filled with the frequencies; can have any size; set this
  *     parameter to <tt>null</tt> to ignore it.
  */
 public synchronized void frequencies(DoubleArrayList distinctElements, IntArrayList frequencies) {
   Descriptive.frequencies(sortedElements_unsafe(), distinctElements, frequencies);
 }