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

The following examples show how to use org.apache.commons.math3.stat.descriptive.DescriptiveStatistics#getN() . 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: 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 2
Source File: CourseGradeStatistics.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Calculates the average GPA for the course
 *
 * This is only applicable if Grade Points is the grading schema in use. However this is not currently checked.
 *
 * @return String average GPA
 */
private String getAverageGPA(final DescriptiveStatistics stats) {

	if (!StringUtils.equals(this.gradingSchemaName, "Grade Points")) {
		return null;
	}

	if (this.bottomPercents == null || this.bottomPercents.isEmpty()) {
		// cannot display averageGpa without bottomPercents
		return null;
	}

	if (stats.getN() == 0) {
		return "-";
	}

	// get all of the non null mapped grades
	// mapped grades will be null if the student doesn't have a course grade yet.
	final List<String> mappedGrades = this.courseGradeMap.values().stream().filter(c -> c.getMappedGrade() != null)
			.map(c -> (c.getMappedGrade())).collect(Collectors.toList());
	Double averageGPA = 0.0;
	for (final String mappedGrade : mappedGrades) {
		final Double grade = this.bottomPercents.get(mappedGrade);
		if (grade != null) {
			averageGPA += grade;
		} else {
			log.debug("Grade skipped when calculating course average GPA: {}. Calculated value will be incorrect.", mappedGrade);
		}
	}
	averageGPA /= mappedGrades.size();

	return String.format("%.2f", averageGPA);
}
 
Example 3
Source File: GenotypeSummaries.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    if ( ! vc.hasGenotypes() ) {
        return Collections.emptyMap();
    }

    final Map<String,Object> returnMap = new LinkedHashMap<>();
    returnMap.put(GATKVCFConstants.NOCALL_CHROM_KEY, vc.getNoCallCount());

    final DescriptiveStatistics stats = new DescriptiveStatistics();
    for( final Genotype g : vc.getGenotypes() ) {
        if( g.hasGQ() ) {
            stats.addValue(g.getGQ());
        }
    }
    if( stats.getN() > 0L ) {
        returnMap.put(GATKVCFConstants.GQ_MEAN_KEY, String.format("%.2f", stats.getMean()));
        if( stats.getN() > 1L ) {
            returnMap.put(GATKVCFConstants.GQ_STDEV_KEY, String.format("%.2f", stats.getStandardDeviation()));
        }
    }

    return returnMap;
}
 
Example 4
Source File: PhiAccrualFailureDetector.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Compute phi for the specified node id.
 *
 * @return phi value
 */
public double phi() {
  long latestHeartbeat = history.latestHeartbeatTime();
  DescriptiveStatistics samples = history.samples();
  if (samples.getN() < minSamples) {
    return 0.0;
  }
  return computePhi(samples, latestHeartbeat, System.currentTimeMillis());
}
 
Example 5
Source File: AbstractClientConnection.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the timeout for the next request.
 *
 * @param type the message type
 * @return the computed timeout for the next request
 */
private long computeTimeoutMillis(String type) {
  DescriptiveStatistics samples = replySamples.get(type);
  if (samples == null || samples.getN() < MIN_SAMPLES) {
    return MAX_TIMEOUT_MILLIS;
  }
  return Math.min(Math.max((int) samples.getMax() * TIMEOUT_FACTOR, MIN_TIMEOUT_MILLIS), MAX_TIMEOUT_MILLIS);
}
 
Example 6
Source File: CourseGradeStatistics.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Calculates the average GPA for the course
 *
 * This is only applicable if Grade Points is the grading schema in use. However this is not currently checked.
 *
 * @return String average GPA
 */
private String getAverageGPA(final DescriptiveStatistics stats) {

	if (!StringUtils.equals(this.gradingSchemaName, "Grade Points")) {
		return null;
	}

	if (this.bottomPercents == null || this.bottomPercents.isEmpty()) {
		// cannot display averageGpa without bottomPercents
		return null;
	}

	if (stats.getN() == 0) {
		return "-";
	}

	// get all of the non null mapped grades
	// mapped grades will be null if the student doesn't have a course grade yet.
	final List<String> mappedGrades = this.courseGradeMap.values().stream().filter(c -> c.getMappedGrade() != null)
			.map(c -> (c.getMappedGrade())).collect(Collectors.toList());
	Double averageGPA = 0.0;
	for (final String mappedGrade : mappedGrades) {
		final Double grade = this.bottomPercents.get(mappedGrade);
		if (grade != null) {
			averageGPA += grade;
		} else {
			log.debug("Grade skipped when calculating course average GPA: {}. Calculated value will be incorrect.", mappedGrade);
		}
	}
	averageGPA /= mappedGrades.size();

	return String.format("%.2f", averageGPA);
}
 
Example 7
Source File: MovingMedianEvaluator.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);
  Percentile percentile = new Percentile();
  for(Object value : values){
    slider.addValue(((Number)value).doubleValue());
    if(slider.getN() >= window){
      double median = percentile.evaluate(slider.getValues(), 50);
      moving.add(median);
    }
  }

  return moving;
}
 
Example 8
Source File: MovingAverageEvaluator.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){
      moving.add(slider.getMean());
    }      
  }
  
  return moving;
}
 
Example 9
Source File: FixedCommitmentPolicy.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public A getAction(final N node, final Map<A, N> actionsWithSuccessors) throws ActionPredictionFailedException {

	/* determine number of visits of the child with least visits */
	Entry<A, N> actionAndChildWithLeastVisits = null;
	Entry<A, N> actionAndChildWithBestVisit = null;
	int numOfVisitsOfThatChild = Integer.MAX_VALUE;
	double bestChildScore = Double.MAX_VALUE;
	for (Entry<A, N> child : actionsWithSuccessors.entrySet()) {
		DescriptiveStatistics observations = this.observationsPerNode.computeIfAbsent(child.getValue(), n -> new DescriptiveStatistics());
		int numOfVisitsOfThisChild = (int)observations.getN();
		if (numOfVisitsOfThisChild < numOfVisitsOfThatChild) {
			actionAndChildWithLeastVisits = child;
			numOfVisitsOfThatChild = numOfVisitsOfThisChild;
		}
		double bestScoreOfThisChild = this.metric.applyAsDouble(observations);
		if (bestScoreOfThisChild < bestChildScore) {
			bestChildScore = bestScoreOfThisChild;
			actionAndChildWithBestVisit = child;
		}
	}

	/* now decide */
	Objects.requireNonNull(actionAndChildWithLeastVisits);
	Objects.requireNonNull(actionAndChildWithBestVisit);
	if (numOfVisitsOfThatChild < this.k) {
		return actionAndChildWithLeastVisits.getKey();
	} else {
		return actionAndChildWithBestVisit.getKey();
	}
}
 
Example 10
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;
}
 
Example 11
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the mean for the set
 *
 * @return String mean
 */
protected final String getMean(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMean()) : "-";
}
 
Example 12
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the median for the set
 *
 * @return String median
 */
protected final String getMedian(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getPercentile(50)) : "-";
}
 
Example 13
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the min for the set
 *
 * @return String min
 */
protected final String getMin(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMin()) : "-";
}
 
Example 14
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the max for the set
 *
 * @return String max
 */
protected final String getMax(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMax()) : "-";
}
 
Example 15
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the standard deviation for the set
 *
 * @return String standard deviation
 */
protected final String getStandardDeviation(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getStandardDeviation()) : "-";
}
 
Example 16
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the mean for the set
 *
 * @return String mean
 */
protected final String getMean(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMean()) : "-";
}
 
Example 17
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the median for the set
 *
 * @return String median
 */
protected final String getMedian(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getPercentile(50)) : "-";
}
 
Example 18
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the min for the set
 *
 * @return String min
 */
protected final String getMin(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMin()) : "-";
}
 
Example 19
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the max for the set
 *
 * @return String max
 */
protected final String getMax(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getMax()) : "-";
}
 
Example 20
Source File: BaseStatistics.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Calculates the standard deviation for the set
 *
 * @return String standard deviation
 */
protected final String getStandardDeviation(final DescriptiveStatistics stats) {
	return stats.getN() > 0 ? String.format("%.2f", stats.getStandardDeviation()) : "-";
}