Java Code Examples for org.apache.commons.math3.stat.descriptive.DescriptiveStatistics#getMean()

The following examples show how to use org.apache.commons.math3.stat.descriptive.DescriptiveStatistics#getMean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 2
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 3
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 4
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the series, so in the end it is having a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 5
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the series, so in the end it is having a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 6
Source File: StatUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.
 *
 * @param sample Sample to normalize.
 * @return normalized (standardized) sample.
 * @since 2.2
 */
public static double[] normalize(final double[] sample) {
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // Add the data from the series to stats
    for (int i = 0; i < sample.length; i++) {
        stats.addValue(sample[i]);
    }

    // Compute mean and standard deviation
    double mean = stats.getMean();
    double standardDeviation = stats.getStandardDeviation();

    // initialize the standardizedSample, which has the same length as the sample
    double[] standardizedSample = new double[sample.length];

    for (int i = 0; i < sample.length; i++) {
        // z = (x- mean)/standardDeviation
        standardizedSample[i] = (sample[i] - mean) / standardDeviation;
    }
    return standardizedSample;
}
 
Example 7
Source File: Spectrum.java    From cineast with MIT License 6 votes vote down vote up
/**
 * Find local maxima in the spectrum and returns the indices of those maxima as integer
 * array.
 *
 * @param threshold Threshold for search. Values bellow that threshold won't be considered.
 * @return Array containing indices (zero-based) of local maxima.
 */
public List<Pair<Float, Double>> findLocalMaxima(double threshold, boolean significant) {
    List<Pair<Float,Double>> peaks = new ArrayList<>();
    for (int i=1;i<this.spectrum.length-1;i++) {
        if (this.spectrum[i] < threshold) {
          continue;
        }
        if (spectrum[i] > Math.max(spectrum[i+1], spectrum[i-1])) {
          peaks.add(this.get(i));
        }
    }

    if (significant) {
        DescriptiveStatistics statistics = new DescriptiveStatistics();
        for (Pair<Float, Double> peak : peaks) {
            statistics.addValue(peak.second);
        }
        final double mean = statistics.getMean();
        final double stddev = statistics.getStandardDeviation();
        peaks.removeIf(p -> p.second < (mean + stddev * 2));
    }

    return peaks;
}
 
Example 8
Source File: ExecuteOutlierErrors.java    From BART with MIT License 6 votes vote down vote up
private String printStat(DescriptiveStatistics stats) {
    double mean = stats.getMean();
    double std = stats.getStandardDeviation();
    double median = stats.getPercentile(50);
    double q1 = stats.getPercentile(25);
    double q3 = stats.getPercentile(75);
    double iqr = q3 - q1;
    double trimmedMean = (q1 + q3 + 2 * median) / 4;
    double skewness = stats.getSkewness();

    StringBuilder sb = new StringBuilder();
    sb.append(" *** Distribution Analysis ***").append("\n")
            .append("\tMean= ").append(mean).append("\n")
            .append("\tStd= ").append(std).append("\n")
            .append("\tMedian= ").append(median).append("\n")
            .append("\tQ1= ").append(q1).append("\tQ3=").append(q3).append("\tIQR=").append(iqr).append("\n")
            .append("\tTrimmed Mean= ").append(trimmedMean).append("\n")
            .append("\tSkewness= ").append(skewness).append("\n");
    return sb.toString();
}
 
Example 9
Source File: ColumnarStructureX.java    From mmtf-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns z-scores for B-factors (normalized B-factors).
 * 
 * Critical z-score values: Confidence level Tail Area z critical 
 *                          90%              0.05      +-1.645 
 *                          95%              0.025     +-1.96 
 *                          99%              0.005     +-2.576
 * 
 * @return
 */
public float[] getNormalizedbFactors() {
    if (normalizedbFactors == null) {
        normalizedbFactors = new float[getNumAtoms()];

        float[] bFactors = getbFactors();
        String[] types = getEntityTypes();

        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (int i = 0; i < getNumAtoms(); i++) {
            if (! (types[i].equals("WAT"))) {
                stats.addValue(bFactors[i]);
            }
        }
        double mean = stats.getMean();
        double stddev = stats.getStandardDeviation();

        if (stddev > EPSILON) {
            for (int i = 0; i < getNumAtoms(); i++) {
                normalizedbFactors[i] = (float) ((bFactors[i] - mean) / stddev);
            }
        } else {
            Arrays.fill(normalizedbFactors, Float.MAX_VALUE);
        }
    }

    return normalizedbFactors;
}
 
Example 10
Source File: AbstractServerSideTest.java    From lightning with MIT License 5 votes vote down vote up
private void calculateActualResult(PerfMonEntries entries) {
    DescriptiveStatistics ds = new DescriptiveStatistics();
    entries.asStream()
            .map(e -> e.getValue())
            .forEach(ds::addValue);
    actualResult = (int) ds.getMean();
}
 
Example 11
Source File: CorrelationTechniquesReducer.java    From data-polygamy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private double[] normalize(double[] array) {
    DescriptiveStatistics stats = new DescriptiveStatistics(array);
    double mean = stats.getMean();
    double stdDev = stats.getStandardDeviation();
    for (int i = 0; i < array.length; i++) {
        array[i] = (array[i] - mean)/stdDev;
    }
    return array;
}
 
Example 12
Source File: AvroJacksonBenchmarks.java    From schema-evolution-samples with Apache License 2.0 5 votes vote down vote up
private Double average(List<Double> values){
	Collections.sort(values,(o1, o2) -> o1.compareTo(o2));
	List<Double> filtered = values.subList((int)(0.2*values.size()),(int)(0.8*values.size()));
	DescriptiveStatistics dStats = new DescriptiveStatistics(filtered.stream().mapToDouble(Double::doubleValue).toArray());

	return dStats.getMean();
}
 
Example 13
Source File: MovingMADEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object doWork(Object first, Object second) throws IOException{
  if(null == first){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
  }
  if(null == second){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
  }
  if(!(first instanceof List<?>)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a List",toExpression(constructingFactory), first.getClass().getSimpleName()));
  }
  if(!(second instanceof Number)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName()));
  }

  List<?> values = (List<?>)first;
  int window = ((Number)second).intValue();

  List<Number> moving = new ArrayList<>();
  DescriptiveStatistics slider = new DescriptiveStatistics(window);
  for(Object value : values){
    slider.addValue(((Number)value).doubleValue());
    if(slider.getN() >= window){
      double[] doubles = slider.getValues();
      double mean = slider.getMean();
      double total = 0;
      for(double d : doubles) {
        total+=Math.abs(d-mean);
      }
      moving.add(total/window);
    }
  }

  return moving;
}
 
Example 14
Source File: AllPairsTable.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public double getAverageSeparability(final Collection<String> classes) {
	DescriptiveStatistics stats = new DescriptiveStatistics();
	for (Collection<String> pair : SetUtil.getAllPossibleSubsetsWithSize(classes, 2)) {
		Iterator<String> i = pair.iterator();
		String a = i.next();
		String b = i.next();
		stats.addValue(this.getSeparability(a, b));
	}
	return stats.getMean();
}
 
Example 15
Source File: ChoquisticRelevanceLoss.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public double loss(final List<? extends int[]> expected, final List<? extends IMultiLabelClassification> actual) {
	this.checkConsistency(expected, actual);
	DescriptiveStatistics stats = new DescriptiveStatistics();
	for (int i = 0; i < expected.size(); i++) {
		stats.addValue(this.instanceLoss(expected.get(i), actual.get(i).getPrediction()));
	}
	return stats.getMean();
}
 
Example 16
Source File: OWARelevanceLoss.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public double loss(final List<? extends int[]> expected, final List<? extends IMultiLabelClassification> actual) {
	this.checkConsistency(expected, actual);
	DescriptiveStatistics stats = new DescriptiveStatistics();
	for (int i = 0; i < expected.size(); i++) {
		stats.addValue(this.instanceLoss(expected.get(i), actual.get(i).getPrediction()));
	}
	return stats.getMean();
}
 
Example 17
Source File: PLNetDyadRanker.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Computes the average error on a set of dyad rankings in terms on the negative
 * log likelihood (NLL).
 *
 * @param drTest
 *            Test data on which the error should be computed given as a
 *            {@link List} of {@link IDyadRankingInstance}
 * @return Average error on the given test data
 */
private double computeAvgError(final List<INDArray> drTest) {
	DescriptiveStatistics stats = new DescriptiveStatistics();
	for (INDArray dyadRankingInstance : drTest) {
		INDArray outputs = this.plNet.output(dyadRankingInstance);
		outputs = outputs.transpose();
		double score = PLNetLoss.computeLoss(outputs).getDouble(0);
		stats.addValue(score);
	}
	return stats.getMean();
}
 
Example 18
Source File: PhiAccrualFailureDetector.java    From onos with Apache License 2.0 5 votes vote down vote up
private double computePhi(DescriptiveStatistics samples, long tLast, long tNow) {
    long elapsedTime = tNow - tLast;
    double meanMillis = samples.getMean();
    double y = (elapsedTime - meanMillis) / Math.max(samples.getStandardDeviation(), minStandardDeviationMillis);
    double e = Math.exp(-y * (1.5976 + 0.070566 * y * y));
    if (elapsedTime > meanMillis) {
        return -Math.log10(e / (1.0 + e));
    } else {
        return -Math.log10(1.0 - 1.0 / (1.0 + e));
    }
}
 
Example 19
Source File: RespTimeAvgTest.java    From lightning with MIT License 4 votes vote down vote up
@Override
protected int calculateNumericResult(DescriptiveStatistics ds) {
    return (int) ds.getMean();
}
 
Example 20
Source File: PhiAccrualFailureDetector.java    From atomix with Apache License 2.0 3 votes vote down vote up
/**
 * Computes the phi value from the given samples.
 * <p>
 * The original phi value in Hayashibara's paper is calculated based on a normal distribution.
 * Here, we calculate it based on an exponential distribution.
 *
 * @param samples       the samples from which to compute phi
 * @param lastHeartbeat the last heartbeat
 * @param currentTime   the current time
 * @return phi
 */
private double computePhi(DescriptiveStatistics samples, long lastHeartbeat, long currentTime) {
  long size = samples.getN();
  long t = currentTime - lastHeartbeat;
  return (size > 0)
      ? phiFactor * t / samples.getMean()
      : 100;
}