Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
package de.tutorials;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;
public class RunningStatisticsExample {
public static void main(String[] args) {
Double[] values = { 1., 1., 1., 1., 2., 2., 2., 3., 4., 4., 4., 4., 4., 4., 4., 4., 5., 5. };
Statistics statistics = new Statistics();
for (int i = 0; i < values.length; i++) {
statistics.update(values[i]);
}
System.out.println(statistics);
System.out.println("modus: " + Statistics.modusOf(values));
}
static class Statistics {
double mean;
double standardDeviation;
double variance;
double sumOfSquanres;
double sum;
int count;
private void update(double value) {
count++;
this.sum += value;
this.sumOfSquanres += value * value;
this.mean += (value - mean) / count;
this.standardDeviation = Math.sqrt((count * sumOfSquanres - sum * sum) / (count * (count - 1)));
this.variance = this.standardDeviation * this.standardDeviation;
}
public static <T extends Comparable<T>> T modusOf(T... values) {
Histogram<T> hist = new Histogram<T>(Arrays.asList(values));
//hist.printSorted();
return hist.modus();
}
public double getMean() {
return mean;
}
public void setMean(double mean) {
this.mean = mean;
}
public double getStandardDeviation() {
return standardDeviation;
}
public void setStandardDeviation(double standardDeviation) {
this.standardDeviation = standardDeviation;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("sum: ");
stringBuilder.append(sum);
stringBuilder.append("\n");
stringBuilder.append("sum of x²: ");
stringBuilder.append(sumOfSquanres);
stringBuilder.append("\n");
stringBuilder.append("mean: ");
stringBuilder.append(mean);
stringBuilder.append("\n");
stringBuilder.append("stddev: ");
stringBuilder.append(standardDeviation);
stringBuilder.append("\n");
stringBuilder.append("variance: ");
stringBuilder.append(variance);
stringBuilder.append("\n");
return stringBuilder.toString();
}
static class Histogram<TValue extends Comparable<TValue>> {
Map<TValue, Integer> histogram;
Comparator<Map.Entry<TValue, Integer>> comparator = new Comparator<Map.Entry<TValue, Integer>>() {
public int compare(Entry<TValue, Integer> left, Entry<TValue, Integer> right) {
int result = left.getValue().compareTo(right.getValue());
return result == 0 ? left.getKey().compareTo(right.getKey()) : result;
}
};
public Histogram(List<TValue> comparables) {
this.histogram = new TreeMap<TValue, Integer>();
for (TValue comparable : comparables) {
this.histogram.put(comparable, Collections.frequency(comparables, comparable));
}
}
NavigableSet<Map.Entry<TValue, Integer>> sorted() {
NavigableSet<Map.Entry<TValue, Integer>> set = new TreeSet<Entry<TValue, Integer>>(comparator);
set.addAll(this.histogram.entrySet());
return set;
}
public TValue modus(){
return sorted().last().getKey();
}
public void print() {
System.out.println(histogram);
}
public void printSorted() {
System.out.println(sorted());
}
}
}
}
sum: 55.0
sum of x²: 203.0
mean: 3.055555555555556
stddev: 1.4337208778404378
variance: 2.0555555555555554
modus: 4.0
package math;
import org.apache.commons.math.stat.descriptive.rank.Median;
public class ModalValue {
public static void main(String... args) {
double[] values = { 1., 1., 1., 1., 2., 2., 2., 3., 4., 4., 4., 4., 4.,
4., 4., 4., 5., 5. };
Median tmpMed = new Median();
System.out.println("Median: " + tmpMed.evaluate(values));
}
}