|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math3.stat.descriptive.DescriptiveStatistics
Stats
class Stats extends DescriptiveStatistics
A Groovy wrapper for Commons-Math DescriptiveStatistcs, combined with numerous convenience methods that return SummaryStatistics.
The summary static methods are used to easily create summary statistics for various collections and iterables. A special class of methods supports efficient creation of statistics for defined ranges of integers (eg: Coverage Depth values). The motivation of it is to be able to calculate the median of coverage depth values efficiently without storing the entire set in memory. See CoverageStats for more information.
The most basic method takes any Iterable and turns it into a SummaryStatistics object:
x = [1,4,5,6] assert Stats.summary(x).mean == 4As an alternative, a second method accepts a closure, which is called repeatedly until an exception occurs such as ArrayIndexOutOfBounds, NoSuchElementException, etc. This allows an inversion of control, and thus, effectively, a streaming model for objects that aren't necessarily iterable:
int i = 0 assert Stats.summary { x[i++] }.mean == 4The Stats class links well with the Matrix class to allow easy and efficient calculation of statistics for matrix columns and rows:
Matrix m = new Matrix(2,2,[1,2,3,4]) assert Stats.from(m[][1]).mean == 3
Constructor Summary | |
Stats()
|
|
Stats(int windowSize)
|
Method Summary | |
---|---|
static Stats
|
from(double[] values)
|
static Stats
|
from(double[] values, Closure c)
A concrete implementation of from(Iterable, Closure) specialised for arrays of double[] values. |
static Stats
|
from(java.lang.Iterable values, Closure c)
A flexible method to generate statistics from any iterable object. |
void
|
leftShift(java.lang.Object value)
Convenience function to add sample value to statistics |
static java.lang.Object
|
mean()
|
static java.lang.Double
|
mean(java.lang.Iterable iterable)
|
static java.lang.Double
|
mean(Iterator i)
|
static java.lang.Double
|
mean(Closure c)
|
static java.lang.Object
|
median()
Convenience method for returning the median of values read from stdin using a default maximum value of 10,000. |
static java.lang.Object
|
median(int max, Closure c)
|
static java.lang.Object
|
median(Closure c)
|
static java.lang.Object
|
percentile(int max)
|
static java.lang.Object
|
percentile()
|
static java.lang.Object
|
percentile(int max, InputStream i)
Return a CoverageStats object by reading lines from the given input stream until no more lines are left. |
static java.lang.Object
|
percentile(int max, Closure c)
Calculate Percentile object by accepting values from the given closure until it either: |
static Stats
|
read(InputStream values = System.in, Closure c = null)
Compute statistcs from values read from the given input stream. |
static SummaryStatistics
|
summary(java.lang.Iterable iterable)
|
static java.lang.Object
|
summary(Iterator i)
|
static SummaryStatistics
|
summary(double[] values)
|
static SummaryStatistics
|
summary(Closure c)
Return a SummaryStatistics object obtained by executing the given closure repeatedly until it either |
Constructor Detail |
---|
Stats()
Stats(int windowSize)
Method Detail |
---|
static Stats from(double[] values)
@CompileStatic static Stats from(double[] values, Closure c)
values
- values to calculate statistics forc
- Closure to filter or transform results
@CompileStatic static Stats from(java.lang.Iterable values, Closure c)
An optional closure can be supplied that has dual functionality:
x = [2,3,4,5,6] assert Stats.from(x) { it % 2 == 0 }.mean == 4 // mean of 2,4,6Alternatively if the value returned is numeric, it is treated as a transformation:
x = [2,3,4,5,6] assert Stats.from(x) { it % 2 }.mean == 1.6 // (3+5)/5Of course, any Iterable could be easily transformed using standard Groovy collection operations to achieve the same effect:
x = [2,3,4,5,6] assert Stats.from(x.collect { it % 2 }).mean == 1.6 // (3+5)/5However the latter requires a complete copy of the transformed data be temporarily created in memory, while the former can potentially stream any number of values in while never consuming anything more than trivial memory overhead.
values
- Iterable object supplying values that can be parsed as numeric
void leftShift(java.lang.Object value)
@CompileStatic static java.lang.Object mean()
static java.lang.Double mean(java.lang.Iterable iterable)
static java.lang.Double mean(Iterator i)
static java.lang.Double mean(Closure c)
static java.lang.Object median()
static java.lang.Object median(int max, Closure c)
static java.lang.Object median(Closure c)
static java.lang.Object percentile(int max)
static java.lang.Object percentile()
@CompileStatic static java.lang.Object percentile(int max, InputStream i)
max
- See #percentile(max,Closure)i
- input stream
@CompileStatic static java.lang.Object percentile(int max, Closure c)
max
- An estimate of the maximum possible value that the percentile
values that are requested will have. If the actual value is
above this level then an exception will be thrown when the
value is requested
static Stats read(InputStream values = System.in, Closure c = null)
cut -f 6 coverage.txt | groovy -e 'println(Stats.from())'If a closure is provided then the caller can transorm the values before they are added. If the closure returns false then the value is not included, which gives the caller the opportunity to filter out values they might not be interested in.
static SummaryStatistics summary(java.lang.Iterable iterable)
static java.lang.Object summary(Iterator i)
@CompileStatic static SummaryStatistics summary(double[] values)
@CompileStatic static SummaryStatistics summary(Closure c)
Groovy Documentation