Last change
on this file since 161 was 127, checked in by Wouter Pasman, 6 years ago |
#41 ROLL BACK of rev.126 . So this version is equal to rev. 125
|
File size:
1.3 KB
|
Rev | Line | |
---|
[127] | 1 | package agents.ai2014.group11;
|
---|
| 2 |
|
---|
| 3 | import java.util.Arrays;
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * Class to use for basic statistics.
|
---|
| 7 | *
|
---|
| 8 | * Source:
|
---|
| 9 | * http://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java
|
---|
| 10 | */
|
---|
| 11 | public class Statistics {
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Get the mean of dataset
|
---|
| 15 | * @param data
|
---|
| 16 | * @return
|
---|
| 17 | */
|
---|
| 18 | public static double getMean(double[] data) {
|
---|
| 19 | double sum = 0.0;
|
---|
| 20 | for (double a : data)
|
---|
| 21 | sum += a;
|
---|
| 22 | return sum / data.length;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Get the variance of dataset
|
---|
| 27 | * @param data
|
---|
| 28 | * @return
|
---|
| 29 | */
|
---|
| 30 | public static double getVariance(double[] data) {
|
---|
| 31 | double mean = getMean(data);
|
---|
| 32 | double temp = 0;
|
---|
| 33 | for (double a : data)
|
---|
| 34 | temp += (mean - a) * (mean - a);
|
---|
| 35 | return temp / data.length;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Get the standard deviation of dataset
|
---|
| 40 | * @param data
|
---|
| 41 | * @return
|
---|
| 42 | */
|
---|
| 43 | public static double getStdDev(double[] data) {
|
---|
| 44 | return Math.sqrt(getVariance(data));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Get the median of dataset
|
---|
| 49 | * @param data
|
---|
| 50 | * @return
|
---|
| 51 | */
|
---|
| 52 | public static double median(double[] data) {
|
---|
| 53 | double[] b = new double[data.length];
|
---|
| 54 | System.arraycopy(data, 0, b, 0, b.length);
|
---|
| 55 | Arrays.sort(b);
|
---|
| 56 |
|
---|
| 57 | if (data.length % 2 == 0) {
|
---|
| 58 | return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
|
---|
| 59 | } else {
|
---|
| 60 | return b[b.length / 2];
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.