Skip to content

Commit

Permalink
Delete redudant Analysis class
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCarlberg committed Oct 11, 2016
1 parent 7c3b79d commit f7776c9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 170 deletions.
80 changes: 0 additions & 80 deletions core/src/main/java/edu/wpi/grip/core/metrics/Analysis.java

This file was deleted.

44 changes: 29 additions & 15 deletions core/src/main/java/edu/wpi/grip/core/metrics/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import com.google.common.base.MoreObjects;

import java.util.Collection;

import javax.annotation.concurrent.Immutable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Statistics analysis. Contains:
* <ul>
* <li>Number of samples ({@code n})</li>
* <li>Mean value ({@code mean})</li>
* <li>Standard deviation ({@code s})</li>
* <li>Number of samples</li>
* <li>Sum</li>
* <li>Mean value</li>
* <li>Standard deviation</li>
* </ul>
*/
@Immutable
public final class Statistics {

private final int n;
private final int numSamples;
private final double sum;
private final double mean;
private final double s;
private final double standardDeviation;

/**
* "null" statistics with every value set to zero.
Expand Down Expand Up @@ -49,27 +52,38 @@ public static Statistics of(double... samples) {
return new Statistics(n, sum, mean, s);
}

private Statistics(int n, double sum, double mean, double s) {
this.n = n;
/**
* Calculates the statistics of the given samples.
*
* @param samples the samples to analyze
* @return a statistical analysis of the given samples
*/
public static Statistics of(Collection<? extends Number> samples) {
checkNotNull(samples);
return of(samples.stream().mapToDouble(Number::doubleValue).toArray());
}

private Statistics(int numSamples, double sum, double mean, double standardDeviation) {
this.numSamples = numSamples;
this.sum = sum;
this.mean = mean;
this.s = s;
this.standardDeviation = standardDeviation;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("n", n)
.add("numSamples", numSamples)
.add("mean", mean)
.add("s", s)
.add("standardDeviation", standardDeviation)
.toString();
}

/**
* Gets the number of samples.
*/
public int getN() {
return n;
public int getNumSamples() {
return numSamples;
}

/**
Expand All @@ -90,7 +104,7 @@ public double getMean() {
* Gets the standard deviation in the samples.
*/
public double getStandardDeviation() {
return s;
return standardDeviation;
}

/**
Expand All @@ -105,15 +119,15 @@ public double getStandardDeviation() {
* @return the hotness of the given value.
*/
public double hotness(double value) {
if (n < 2) {
if (numSamples < 2) {
// Hotness doesn't make sense if there's 0 or 1 data points
return 0;
}
if (value <= mean) {
// Avoid negative hotness
return 0;
}
return (value - mean) / s;
return (value - mean) / standardDeviation;
}

}
46 changes: 0 additions & 46 deletions core/src/test/java/edu/wpi/grip/core/metrics/AnalysisTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class StatisticsTest {
@Test
public void testNil() {
Statistics statistics = Statistics.NIL;
assertEquals("n should be 0", 0, statistics.getN(), 0);
assertEquals("n should be 0", 0, statistics.getNumSamples(), 0);
assertEquals("sum should be 0", 0, statistics.getSum(), 0);
assertEquals("mean should be 0", 0, statistics.getMean(), 0);
assertEquals("std dev should be 0", 0, statistics.getStandardDeviation(), 0);
Expand All @@ -20,7 +20,7 @@ public void testNil() {
public void testEmpty() {
double[] values = {};
Statistics statistics = Statistics.of(values);
assertEquals("n should be 0", 0, statistics.getN(), 0);
assertEquals("n should be 0", 0, statistics.getNumSamples(), 0);
assertEquals("sum should be 0", 0, statistics.getSum(), 0);
assertEquals("mean should be 0", 0, statistics.getMean(), 0);
assertEquals("std dev should be 0", 0, statistics.getStandardDeviation(), 0);
Expand All @@ -31,7 +31,7 @@ public void testEmpty() {
public void testSingleDataPoint() {
double[] values = {Math.PI};
Statistics statistics = Statistics.of(values);
assertEquals("n should be 1", 1, statistics.getN(), 0);
assertEquals("n should be 1", 1, statistics.getNumSamples(), 0);
assertEquals("sum should be pi", Math.PI, statistics.getSum(), 0);
assertEquals("mean should be pi", Math.PI, statistics.getMean(), 0);
assertEquals("std dev should be 0", 0, statistics.getStandardDeviation(), 0);
Expand All @@ -42,7 +42,7 @@ public void testSingleDataPoint() {
public void testMultipleDataPoints() {
double[] values = {1, 2, 3, 4, 5};
Statistics statistics = Statistics.of(values);
assertEquals("n should be 5", 5, statistics.getN(), 0);
assertEquals("n should be 5", 5, statistics.getNumSamples(), 0);
assertEquals("sum should be 15", 15, statistics.getSum(), 0);
assertEquals("mean should be 3", 3, statistics.getMean(), 0);
assertEquals("std dev should be sqrt(2)", Math.sqrt(2), statistics.getStandardDeviation(), 0);
Expand Down
Loading

0 comments on commit f7776c9

Please sign in to comment.