/**
   * Computes the specified quantile elements over the values previously added.
   *
   * @param phis the quantiles for which elements are to be computed. Each phi must be in the
   *     interval (0.0,1.0]. <tt>phis</tt> must be sorted ascending.
   * @return the approximate quantile elements.
   */
  public DoubleArrayList quantileElements(DoubleArrayList phis) {
    if (precomputeEpsilon <= 0.0) return super.quantileElements(phis);

    int quantilesToPrecompute = (int) Utils.epsilonCeiling(1.0 / precomputeEpsilon);
    /*
     * if (phis.size() > quantilesToPrecompute) { // illegal use case! // we
     * compute results, but loose explicit approximation guarantees. return
     * super.quantileElements(phis); }
     */

    // select that quantile from the precomputed set that corresponds to a
    // position closest to phi.
    phis = phis.copy();
    double e = precomputeEpsilon;
    for (int index = phis.size(); --index >= 0; ) {
      double phi = phis.get(index);
      int i = (int) Math.round(((2.0 * phi / e) - 1.0) / 2.0); // finds
      // closest
      i = Math.min(quantilesToPrecompute - 1, Math.max(0, i));
      double augmentedPhi = (e / 2.0) * (1 + 2 * i);
      phis.set(index, augmentedPhi);
    }

    return super.quantileElements(phis);
  }
示例#2
0
  /**
   * Computes the specified quantile elements over the values previously added.
   *
   * @param phis the quantiles for which elements are to be computed. Each phi must be in the
   *     interval [0.0,1.0]. <tt>phis</tt> must be sorted ascending.
   * @return the approximate quantile elements.
   */
  public DoubleArrayList quantileElements(DoubleArrayList phis) {
    /*
    //check parameter
    DoubleArrayList sortedPhiList = phis.copy();
    sortedPhiList.sort();
    if (! phis.equals(sortedPhiList)) {
    	throw new IllegalArgumentException("Phis must be sorted ascending.");
    }
    */

    // System.out.println("starting to augment missing values, if necessary...");

    phis = preProcessPhis(phis);

    long[] triggerPositions = new long[phis.size()];
    long totalSize = this.bufferSet.totalSize();
    for (int i = phis.size(); --i >= 0; ) {
      triggerPositions[i] = Utils.epsilonCeiling(phis.get(i) * totalSize) - 1;
    }

    // System.out.println("triggerPositions="+cern.colt.Arrays.toString(triggerPositions));
    // System.out.println("starting to determine quantiles...");
    // System.out.println(bufferSet);

    DoubleBuffer[] fullBuffers = bufferSet._getFullOrPartialBuffers();
    double[] quantileElements = new double[phis.size()];

    // do the main work: determine values at given positions in sorted sequence
    return new DoubleArrayList(bufferSet.getValuesAtPositions(fullBuffers, triggerPositions));
  }
示例#3
0
 /**
  * Applies a function to each element and aggregates the results. Returns a value <tt>v</tt> such
  * that <tt>v==a(size())</tt> where <tt>a(i) == aggr( a(i-1), f(x(i)) )</tt> and terminators are
  * <tt>a(1) == f(x(0)), a(0)==Double.NaN</tt>.
  *
  * <p><b>Example:</b>
  *
  * <pre>
  * cern.jet.math.Functions F = cern.jet.math.Functions.functions;
  * bin = 0 1 2 3
  *
  * // Sum( x[i]*x[i] )
  * bin.aggregate(F.plus,F.square);
  * --> 14
  * </pre>
  *
  * For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>.
  *
  * @param aggr an aggregation function taking as first argument the current aggregation and as
  *     second argument the transformed current element.
  * @param f a function transforming the current element.
  * @return the aggregated measure.
  * @see cern.jet.math.Functions
  */
 public synchronized double aggregate(
     cern.colt.function.DoubleDoubleFunction aggr, cern.colt.function.DoubleFunction f) {
   int s = size();
   if (s == 0) return Double.NaN;
   double a = f.apply(elements.getQuick(s - 1));
   for (int i = s - 1; --i >= 0; ) {
     a = aggr.apply(a, f.apply(elements.getQuick(i)));
   }
   return a;
 }
示例#4
0
  public void samplesToString(DoubleArrayList theta) {

    int cols = theta.size();
    sampleString = "";
    for (int param = 0; param < (cols - 1); ++param) {
      sampleString = sampleString + Double.toString(theta.getQuick(param)) + ", ";
      // thetaSamples.set(sampleLoc, param, thetaEstNow.get(param));
    }
    sampleString = sampleString + Double.toString(theta.getQuick(cols - 1));
    // System.out.println();
  }
示例#5
0
  /**
   * Fills all keys contained in the receiver into the specified list. Fills the list, starting at
   * index 0. After this call returns the specified list has a new size that equals
   * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by
   * method {@link #forEachKey(DoubleProcedure)}.
   *
   * <p>This method can be used to iterate over the keys of the receiver.
   *
   * @param list the list to be filled, can have any size.
   */
  public void keys(DoubleArrayList list) {
    list.setSize(distinct);
    double[] elements = list.elements();

    double[] tab = table;
    byte[] stat = state;

    int j = 0;
    for (int i = tab.length; i-- > 0; ) {
      if (stat[i] == FULL) elements[j++] = tab[i];
    }
  }
示例#6
0
 /** Returns a String representation of the receiver. */
 public synchronized String toString() {
   StringBuffer buf = new StringBuffer(super.toString());
   DoubleArrayList distinctElements = new DoubleArrayList();
   IntArrayList frequencies = new IntArrayList();
   frequencies(distinctElements, frequencies);
   if (distinctElements.size() < 100) { // don't cause unintended floods
     buf.append("Distinct elements: " + distinctElements + "\n");
     buf.append("Frequencies: " + frequencies + "\n");
   } else {
     buf.append("Distinct elements & frequencies not printed (too many).");
   }
   return buf.toString();
 }
示例#7
0
  /**
   * Fills all pairs satisfying a given condition into the specified lists. Fills into the lists,
   * starting at index 0. After this call returns the specified lists both have a new size, the
   * number of pairs satisfying the condition. Iteration order is guaranteed to be <i>identical</i>
   * to the order used by method {@link #forEachKey(DoubleProcedure)}.
   *
   * <p><b>Example:</b> <br>
   *
   * <pre>
   * DoubleIntProcedure condition = new DoubleIntProcedure() { // match even values only
   * public boolean apply(double key, int value) { return value%2==0; }
   * }
   * keys = (8,7,6), values = (1,2,2) --> keyList = (6,8), valueList = (2,1)</tt>
   * </pre>
   *
   * @param condition the condition to be matched. Takes the current key as first and the current
   *     value as second argument.
   * @param keyList the list to be filled with keys, can have any size.
   * @param valueList the list to be filled with values, can have any size.
   */
  public void pairsMatching(
      final DoubleIntProcedure condition,
      final DoubleArrayList keyList,
      final IntArrayList valueList) {
    keyList.clear();
    valueList.clear();

    for (int i = table.length; i-- > 0; ) {
      if (state[i] == FULL && condition.apply(table[i], values[i])) {
        keyList.add(table[i]);
        valueList.add(values[i]);
      }
    }
  }
示例#8
0
 /**
  * Returns the number of elements contained in the receiver.
  *
  * @returns the number of elements contained in the receiver.
  */
 public synchronized int size() {
   return elements.size();
   // Never ever use "this.size" as it would be intuitive!
   // This class abuses "this.size". "this.size" DOES NOT REFLECT the number of elements contained
   // in the receiver!
   // Instead, "this.size" reflects the number of elements incremental stats computation has
   // already processed.
 }
示例#9
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();
  }
示例#10
0
  /**
   * Adds the part of the specified list between indexes <tt>from</tt> (inclusive) and <tt>to</tt>
   * (inclusive) to the receiver.
   *
   * @param values the list of which elements shall be added.
   * @param from the index of the first element to be added (inclusive).
   * @param to the index of the last element to be added (inclusive).
   */
  public void addAllOfFromTo(DoubleArrayList values, int from, int to) {
    /*
    // the obvious version, but we can do quicker...
    double[] theValues = values.elements();
    int theSize=values.size();
    for (int i=0; i<theSize; ) add(theValues[i++]);
    */

    double[] valuesToAdd = values.elements();
    int k = this.bufferSet.k();
    int bufferSize = k;
    double[] bufferValues = null;
    if (currentBufferToFill != null) {
      bufferValues = currentBufferToFill.values.elements();
      bufferSize = currentBufferToFill.size();
    }

    for (int i = from - 1; ++i <= to; ) {
      if (sampleNextElement()) {
        if (bufferSize == k) { // full
          if (bufferSet._getFirstEmptyBuffer() == null) collapse();
          newBuffer();
          if (!currentBufferToFill.isAllocated) currentBufferToFill.allocate();
          currentBufferToFill.isSorted = false;
          bufferValues = currentBufferToFill.values.elements();
          bufferSize = 0;
        }

        bufferValues[bufferSize++] = valuesToAdd[i];
        if (bufferSize == k) { // full
          currentBufferToFill.values.setSize(bufferSize);
          currentBufferToFill = null;
        }
      }
    }
    if (this.currentBufferToFill != null) {
      this.currentBufferToFill.values.setSize(bufferSize);
    }

    this.totalElementsFilled += to - from + 1;
  }
示例#11
0
 /**
  * Adds all values of the specified list to the receiver.
  *
  * @param values the list of which all values shall be added.
  */
 public void addAllOf(DoubleArrayList values) {
   addAllOfFromTo(values, 0, values.size() - 1);
 }
示例#12
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();
  }
示例#13
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;
  }
示例#14
0
 /**
  * Adds the specified element to the receiver.
  *
  * @param element element to be appended.
  */
 public synchronized void add(double element) {
   elements.add(element);
   invalidateAll();
 }
示例#15
0
 /**
  * Removes the <tt>s</tt> smallest and <tt>l</tt> largest elements from the receiver. The
  * receivers size will be reduced by <tt>s + l</tt> elements.
  *
  * @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>).
  */
 public synchronized void trim(int s, int l) {
   DoubleArrayList elems = sortedElements();
   clear();
   addAllOfFromTo(elems, s, elems.size() - 1 - l);
 }