Package jasima.core.statistics
Class QuantileEstimator
- java.lang.Object
-
- jasima.core.statistics.SummaryStat
-
- jasima.core.statistics.QuantileEstimator
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<QuantileEstimator.Bar>
public class QuantileEstimator extends SummaryStat implements Iterable<QuantileEstimator.Bar>
Implements the extended P2-Algorithm. To calculate histograms, median values or arbitrary quantiles. This class also collects all statistical values collected by
SummaryStat.The method used is based on the following papers:
- Raj Jain, Imrich Chlamtac: The P2 Algorithm for Dynamic Calculation of Quantiles and Histograms Without Storing Observations, ACM 28, 10 (1985)
- Kimmo Raatikainen: Simultaneous estimation of several percentiles, Simulations Councils (1987)
- Author:
- Robin Kreis, 2012-09-07
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQuantileEstimator.BarRepresents one bar of a histogram.
-
Field Summary
Fields Modifier and Type Field Description protected int[]p2_nprotected double[]p2_n_incrementprotected double[]p2_qStores the-
Fields inherited from class jasima.core.statistics.SummaryStat
DEF_ERROR_PROB, lastValue, lastWeight
-
-
Constructor Summary
Constructors Constructor Description QuantileEstimator()Creates a QuantileEstimator and optimizes the marker positions to estimates the quantiles 0.1, 0.5 (the median) and 0.9 well.QuantileEstimator(double... quantiles)Creates a QuantileEstimator and optimizes the marker positions to estimates the given quantiles well.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Resets this object.QuantileEstimatorclone()Clones this object.SummaryStatcombine(SummaryStat other)Combines the data inotherwith this SummaryStat-Object.voidformatForGnuplot(Formatter fmt)Formats a histogram so that a bar graph can be plotted.intgetCellCount()Returns the number of cells of the histogram.StringgetMarkers()Returns all markers and their positions.double[]getQuantileList()Returns a list of quantiles that are estimated well.protected voidinitMarkers()Initializes all markers.Iterator<QuantileEstimator.Bar>iterator()Can be used to receive a list ofQuantileEstimator.Barinstances to create a histogram.protected doublelinPred(int d, int i)protected doublequadPred(int d, int i)doublequantile(double p)Estimates a quantile.voidsetCellCount(int cells)Sets the number of cells of the histogram.voidsetQuantileList(double... quantiles)Sets a list of quantiles to be estimated.StringtoString()QuantileEstimatorvalue(double v, double weight)Adds a value with a given weight.-
Methods inherited from class jasima.core.statistics.SummaryStat
combine, confidenceIntervalLower, confidenceIntervalLower, confidenceIntervalUpper, confidenceIntervalUpper, confIntRangeSingle, lastValue, lastWeight, max, mean, min, numObs, stdDev, sum, summarize, summarize, value, values, varCoeff, variance, variancePopulation, weightSum
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
QuantileEstimator
public QuantileEstimator()
Creates a QuantileEstimator and optimizes the marker positions to estimates the quantiles 0.1, 0.5 (the median) and 0.9 well.
-
QuantileEstimator
public QuantileEstimator(double... quantiles)
Creates a QuantileEstimator and optimizes the marker positions to estimates the given quantiles well.- Parameters:
quantiles- a list of quantiles to be estimated- See Also:
setQuantileList(double...)
-
-
Method Detail
-
setQuantileList
public void setQuantileList(double... quantiles)
Sets a list of quantiles to be estimated. For n quantiles, 2n+3 markers will be created.
-
getQuantileList
public double[] getQuantileList()
Returns a list of quantiles that are estimated well. The returned array will be equal to the one passed to#QuantileEstimator(String, double...)orsetQuantileList(double...), if the markers haven't been modified afterwards.
-
setCellCount
public void setCellCount(int cells)
Sets the number of cells of the histogram. Each cell will usually be plotted as a bar.
-
getCellCount
public int getCellCount()
Returns the number of cells of the histogram. The returned value will be equal to the one passed tosetCellCount(int), if the markers have't been modified afterwards.
-
combine
public SummaryStat combine(SummaryStat other)
Description copied from class:SummaryStatCombines the data inotherwith this SummaryStat-Object. The combined object behaves as if it had also seen the data of "other".- Overrides:
combinein classSummaryStat- Parameters:
other- TheSummaryStatto combine with this object.- Returns:
- Returns
thisto allow easy chaining of calls.
-
clone
public QuantileEstimator clone()
Description copied from class:SummaryStatClones this object. We can use the standard functionality here, as there are only primitive fields.- Overrides:
clonein classSummaryStat- Returns:
- A clone of this
SummaryStat.
-
clear
public void clear()
Description copied from class:SummaryStatResets this object.- Overrides:
clearin classSummaryStat
-
initMarkers
protected void initMarkers()
Initializes all markers. This requiresp2_n_incrementto be set. After this method completes,p2_nandp2_qwill have the right dimensions andp2_nwill be initialized. This method should only be called whenSummaryStat.numObs()would return 0. Otherwise,clear()should be called.
-
value
public QuantileEstimator value(double v, double weight)
Description copied from class:SummaryStatAdds a value with a given weight.- Overrides:
valuein classSummaryStat- Parameters:
v- The value to add.weight- The weight to give to this value. Has to be positive.- Returns:
this, to allow easy chaining of calls.
-
quadPred
protected double quadPred(int d, int i)
-
linPred
protected double linPred(int d, int i)
-
getMarkers
public String getMarkers()
Returns all markers and their positions. Used for testing.- Returns:
- the current markers formatted as a string
-
iterator
public Iterator<QuantileEstimator.Bar> iterator()
Can be used to receive a list ofQuantileEstimator.Barinstances to create a histogram.- Specified by:
iteratorin interfaceIterable<QuantileEstimator.Bar>
-
formatForGnuplot
public void formatForGnuplot(Formatter fmt)
Formats a histogram so that a bar graph can be plotted. Each line of the output will represent one bar and have three columns for the middle X position, height and width of the bar. The area of each bar is the ratio of all values within the X range of the bar. Gnuplot can directly plot a bar graph using the commandplot filename with boxes.- Parameters:
fmt- the formatter to store the output
-
quantile
public double quantile(double p)
Estimates a quantile. If there is no marker for the quantile p, linear interpolation between the two closest markers is performed. If p is NaN, NaN will be returned. If there haven't been enough observations or the markers are not initialized, NaN is returned. Ifp <= 0.0orp >= 1.0, the minimum or maximum will be returned.- Parameters:
p- any number- Returns:
- a number that is estimated to be bigger than 100p percent of all numbers or Double.NaN, if no data is available
-
-