Class PSquarePercentile
- java.lang.Object
-
- org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic
-
- org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic
-
- org.apache.commons.math3.stat.descriptive.rank.PSquarePercentile
-
- All Implemented Interfaces:
java.io.Serializable
,StorelessUnivariateStatistic
,UnivariateStatistic
,MathArrays.Function
public class PSquarePercentile extends AbstractStorelessUnivariateStatistic implements StorelessUnivariateStatistic, java.io.Serializable
AStorelessUnivariateStatistic
estimating percentiles using theP2 Algorithm as explained by Raj Jain and Imrich Chlamtac in P2 Algorithm for Dynamic Calculation of Quantiles and Histogram Without Storing Observations. Note: This implementation is not synchronized and produces an approximate result. For small samples, where data can be stored and processed in memory,
Percentile
should be used.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PSquarePercentile.FixedCapacityList<E>
A simple fixed capacity list that has an upper bound to growth.private static class
PSquarePercentile.Marker
The class modeling the attributes of the marker of the P-square algorithmprivate static class
PSquarePercentile.Markers
Markers is an encapsulation of the five markers/buckets as indicated in the original works.protected static interface
PSquarePercentile.PSquareMarkers
An interface that encapsulates abstractions of the P-square algorithm markers as is explained in the original works.
-
Field Summary
Fields Modifier and Type Field Description private long
countOfObservations
Counter to count the values/observations accepted into this data setprivate static java.text.DecimalFormat
DECIMAL_FORMAT
A decimal formatter for print convenienceprivate static double
DEFAULT_QUANTILE_DESIRED
A Default quantile needed in case if user prefers to use default no argument constructor.private java.util.List<java.lang.Double>
initialFive
Initial list of 5 numbers corresponding to 5 markers.private double
lastObservation
lastObservation is the last observation value/input sample.private PSquarePercentile.PSquareMarkers
markers
Markers is the marker collection object which comes to effect only after 5 values are insertedprivate static int
PSQUARE_CONSTANT
The maximum array size used for psquare algorithmprivate double
pValue
Computed p value (i,e percentile value of data set hither to received)private double
quantile
The quantile needed should be in range of 0-1.private static long
serialVersionUID
Serial ID
-
Constructor Summary
Constructors Constructor Description PSquarePercentile()
Default constructor that assumes adefault quantile
neededPSquarePercentile(double p)
Constructs a PSquarePercentile with the specific percentile value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the internal state of the StatisticStorelessUnivariateStatistic
copy()
Returns a copy of the statistic with the same internal state.boolean
equals(java.lang.Object o)
Returns true iffo
is aPSquarePercentile
returning the same values as this forgetResult()
andgetN()
and also having equal markerslong
getN()
Returns the number of values that have been added.double
getResult()
Returns the current value of the Statistic.int
hashCode()
Returns hash code based on getResult() and getN()void
increment(double observation)
Updates the internal state of the statistic to reflect the addition of the new value.private double
maximum()
private double
minimum()
static PSquarePercentile.PSquareMarkers
newMarkers(java.util.List<java.lang.Double> initialFive, double p)
A creation method to build Markersdouble
quantile()
Returns the quantile estimated by this statistic in the range [0.0-1.0]java.lang.String
toString()
Returns a string containing the last observation, the current estimate of the quantile and all markers.-
Methods inherited from class org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic
evaluate, evaluate, incrementAll, incrementAll
-
Methods inherited from class org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic
evaluate, getData, getDataRef, setData, setData, test, test, test, test
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic
incrementAll, incrementAll
-
Methods inherited from interface org.apache.commons.math3.stat.descriptive.UnivariateStatistic
evaluate, evaluate
-
-
-
-
Field Detail
-
PSQUARE_CONSTANT
private static final int PSQUARE_CONSTANT
The maximum array size used for psquare algorithm- See Also:
- Constant Field Values
-
DEFAULT_QUANTILE_DESIRED
private static final double DEFAULT_QUANTILE_DESIRED
A Default quantile needed in case if user prefers to use default no argument constructor.- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
Serial ID- See Also:
- Constant Field Values
-
DECIMAL_FORMAT
private static final java.text.DecimalFormat DECIMAL_FORMAT
A decimal formatter for print convenience
-
initialFive
private final java.util.List<java.lang.Double> initialFive
Initial list of 5 numbers corresponding to 5 markers. NOTE:watch out for the add methods that are overloaded
-
quantile
private final double quantile
The quantile needed should be in range of 0-1. The constructorPSquarePercentile(double)
ensures that passed in percentile is divided by 100.
-
lastObservation
private transient double lastObservation
lastObservation is the last observation value/input sample. No need to serialize
-
markers
private PSquarePercentile.PSquareMarkers markers
Markers is the marker collection object which comes to effect only after 5 values are inserted
-
pValue
private double pValue
Computed p value (i,e percentile value of data set hither to received)
-
countOfObservations
private long countOfObservations
Counter to count the values/observations accepted into this data set
-
-
Constructor Detail
-
PSquarePercentile
public PSquarePercentile(double p)
Constructs a PSquarePercentile with the specific percentile value.- Parameters:
p
- the percentile- Throws:
OutOfRangeException
- if p is not greater than 0 and less than or equal to 100
-
PSquarePercentile
PSquarePercentile()
Default constructor that assumes adefault quantile
needed
-
-
Method Detail
-
hashCode
public int hashCode()
Returns hash code based on getResult() and getN()- Overrides:
hashCode
in classAbstractStorelessUnivariateStatistic
- Returns:
- hash code
-
equals
public boolean equals(java.lang.Object o)
Returns true iffo
is aPSquarePercentile
returning the same values as this forgetResult()
andgetN()
and also having equal markers- Overrides:
equals
in classAbstractStorelessUnivariateStatistic
- Parameters:
o
- object to compare- Returns:
- true if
o
is aPSquarePercentile
with equivalent internal state
-
increment
public void increment(double observation)
Updates the internal state of the statistic to reflect the addition of the new value.The internal state updated due to the new value in this context is basically of the marker positions and computation of the approximate quantile.- Specified by:
increment
in interfaceStorelessUnivariateStatistic
- Specified by:
increment
in classAbstractStorelessUnivariateStatistic
- Parameters:
observation
- the observation currently being added.
-
toString
public java.lang.String toString()
Returns a string containing the last observation, the current estimate of the quantile and all markers.- Overrides:
toString
in classjava.lang.Object
- Returns:
- string representation of state data
-
getN
public long getN()
Returns the number of values that have been added.- Specified by:
getN
in interfaceStorelessUnivariateStatistic
- Returns:
- the number of values.
-
copy
public StorelessUnivariateStatistic copy()
Returns a copy of the statistic with the same internal state.- Specified by:
copy
in interfaceStorelessUnivariateStatistic
- Specified by:
copy
in interfaceUnivariateStatistic
- Specified by:
copy
in classAbstractStorelessUnivariateStatistic
- Returns:
- a copy of the statistic
-
quantile
public double quantile()
Returns the quantile estimated by this statistic in the range [0.0-1.0]- Returns:
- quantile estimated by
getResult()
-
clear
public void clear()
Clears the internal state of the Statistic. This basically clears all the markers, the initialFive list and sets countOfObservations to 0.- Specified by:
clear
in interfaceStorelessUnivariateStatistic
- Specified by:
clear
in classAbstractStorelessUnivariateStatistic
-
getResult
public double getResult()
Returns the current value of the Statistic.- Specified by:
getResult
in interfaceStorelessUnivariateStatistic
- Specified by:
getResult
in classAbstractStorelessUnivariateStatistic
- Returns:
- value of the statistic,
Double.NaN
if it has been cleared or just instantiated.
-
maximum
private double maximum()
- Returns:
- maximum in the data set added to this statistic
-
minimum
private double minimum()
- Returns:
- minimum in the data set added to this statistic
-
newMarkers
public static PSquarePercentile.PSquareMarkers newMarkers(java.util.List<java.lang.Double> initialFive, double p)
A creation method to build Markers- Parameters:
initialFive
- list of initial five elementsp
- the quantile desired- Returns:
- an instance of PSquareMarkers
-
-