Class Frequency
- java.lang.Object
-
- org.apache.commons.math3.stat.Frequency
-
- All Implemented Interfaces:
java.io.Serializable
public class Frequency extends java.lang.Object implements java.io.SerializableMaintains a frequency distribution.Accepts int, long, char or Comparable values. New values added must be comparable to those that have been added, otherwise the add method will throw an IllegalArgumentException.
Integer values (int, long, Integer, Long) are not distinguished by type -- i.e.
addValue(Long.valueOf(2)), addValue(2), addValue(2l)all have the same effect (similarly for arguments togetCount,etc.).NOTE: byte and short values will be implicitly converted to int values by the compiler, thus there are no explicit overloaded methods for these primitive types.
char values are converted by
addValueto Character instances. As such, these values are not comparable to integral values, so attempts to combine integral types with chars in a frequency distribution will fail.Float is not coerced to Double. Since they are not Comparable with each other the user must do any necessary coercion. Float.NaN and Double.NaN are not treated specially; they may occur in input and will occur in output if appropriate.
The values are ordered using the default (natural order), unless a
Comparatoris supplied in the constructor.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFrequency.NaturalComparator<T extends java.lang.Comparable<T>>A Comparator that compares comparable objects using the natural order.
-
Field Summary
Fields Modifier and Type Field Description private java.util.SortedMap<java.lang.Comparable<?>,java.lang.Long>freqTableunderlying collectionprivate static longserialVersionUIDSerializable version identifier
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddValue(char v)Adds 1 to the frequency count for v.voidaddValue(int v)Adds 1 to the frequency count for v.voidaddValue(long v)Adds 1 to the frequency count for v.voidaddValue(java.lang.Comparable<?> v)Adds 1 to the frequency count for v.voidclear()Clears the frequency tablejava.util.Iterator<java.util.Map.Entry<java.lang.Comparable<?>,java.lang.Long>>entrySetIterator()Return an Iterator over the set of keys and values that have been added.booleanequals(java.lang.Object obj)longgetCount(char v)Returns the number of values equal to v.longgetCount(int v)Returns the number of values equal to v.longgetCount(long v)Returns the number of values equal to v.longgetCount(java.lang.Comparable<?> v)Returns the number of values equal to v.longgetCumFreq(char v)Returns the cumulative frequency of values less than or equal to v.longgetCumFreq(int v)Returns the cumulative frequency of values less than or equal to v.longgetCumFreq(long v)Returns the cumulative frequency of values less than or equal to v.longgetCumFreq(java.lang.Comparable<?> v)Returns the cumulative frequency of values less than or equal to v.doublegetCumPct(char v)Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).doublegetCumPct(int v)Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).doublegetCumPct(long v)Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).doublegetCumPct(java.lang.Comparable<?> v)Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).java.util.List<java.lang.Comparable<?>>getMode()Returns the mode value(s) in comparator order.doublegetPct(char v)Returns the percentage of values that are equal to v (as a proportion between 0 and 1).doublegetPct(int v)Returns the percentage of values that are equal to v (as a proportion between 0 and 1).doublegetPct(long v)Returns the percentage of values that are equal to v (as a proportion between 0 and 1).doublegetPct(java.lang.Comparable<?> v)Returns the percentage of values that are equal to v (as a proportion between 0 and 1).longgetSumFreq()Returns the sum of all frequencies.intgetUniqueCount()Returns the number of values in the frequency table.inthashCode()voidincrementValue(char v, long increment)Increments the frequency count for v.voidincrementValue(int v, long increment)Increments the frequency count for v.voidincrementValue(long v, long increment)Increments the frequency count for v.voidincrementValue(java.lang.Comparable<?> v, long increment)Increments the frequency count for v.voidmerge(java.util.Collection<Frequency> others)Merge aCollectionofFrequencyobjects into this instance.voidmerge(Frequency other)Merge another Frequency object's counts into this instance.java.lang.StringtoString()Return a string representation of this frequency distribution.java.util.Iterator<java.lang.Comparable<?>>valuesIterator()Returns an Iterator over the set of values that have been added.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serializable version identifier- See Also:
- Constant Field Values
-
freqTable
private final java.util.SortedMap<java.lang.Comparable<?>,java.lang.Long> freqTable
underlying collection
-
-
Method Detail
-
toString
public java.lang.String toString()
Return a string representation of this frequency distribution.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation.
-
addValue
public void addValue(java.lang.Comparable<?> v) throws MathIllegalArgumentExceptionAdds 1 to the frequency count for v.If other objects have already been added to this Frequency, v must be comparable to those that have already been added.
- Parameters:
v- the value to add.- Throws:
MathIllegalArgumentException- ifvis not comparable with previous entries
-
addValue
public void addValue(int v) throws MathIllegalArgumentExceptionAdds 1 to the frequency count for v.- Parameters:
v- the value to add.- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Long
-
addValue
public void addValue(long v) throws MathIllegalArgumentExceptionAdds 1 to the frequency count for v.- Parameters:
v- the value to add.- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Long
-
addValue
public void addValue(char v) throws MathIllegalArgumentExceptionAdds 1 to the frequency count for v.- Parameters:
v- the value to add.- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Char
-
incrementValue
public void incrementValue(java.lang.Comparable<?> v, long increment) throws MathIllegalArgumentExceptionIncrements the frequency count for v.If other objects have already been added to this Frequency, v must be comparable to those that have already been added.
- Parameters:
v- the value to add.increment- the amount by which the value should be incremented- Throws:
MathIllegalArgumentException- ifvis not comparable with previous entries- Since:
- 3.1
-
incrementValue
public void incrementValue(int v, long increment) throws MathIllegalArgumentExceptionIncrements the frequency count for v.If other objects have already been added to this Frequency, v must be comparable to those that have already been added.
- Parameters:
v- the value to add.increment- the amount by which the value should be incremented- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Long- Since:
- 3.3
-
incrementValue
public void incrementValue(long v, long increment) throws MathIllegalArgumentExceptionIncrements the frequency count for v.If other objects have already been added to this Frequency, v must be comparable to those that have already been added.
- Parameters:
v- the value to add.increment- the amount by which the value should be incremented- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Long- Since:
- 3.3
-
incrementValue
public void incrementValue(char v, long increment) throws MathIllegalArgumentExceptionIncrements the frequency count for v.If other objects have already been added to this Frequency, v must be comparable to those that have already been added.
- Parameters:
v- the value to add.increment- the amount by which the value should be incremented- Throws:
MathIllegalArgumentException- if the table contains entries not comparable to Char- Since:
- 3.3
-
clear
public void clear()
Clears the frequency table
-
valuesIterator
public java.util.Iterator<java.lang.Comparable<?>> valuesIterator()
Returns an Iterator over the set of values that have been added.If added values are integral (i.e., integers, longs, Integers, or Longs), they are converted to Longs when they are added, so the objects returned by the Iterator will in this case be Longs.
- Returns:
- values Iterator
-
entrySetIterator
public java.util.Iterator<java.util.Map.Entry<java.lang.Comparable<?>,java.lang.Long>> entrySetIterator()
Return an Iterator over the set of keys and values that have been added. Using the entry set to iterate is more efficient in the case where you need to access respective counts as well as values, since it doesn't require a "get" for every key...the value is provided in the Map.Entry.If added values are integral (i.e., integers, longs, Integers, or Longs), they are converted to Longs when they are added, so the values of the map entries returned by the Iterator will in this case be Longs.
- Returns:
- entry set Iterator
- Since:
- 3.1
-
getSumFreq
public long getSumFreq()
Returns the sum of all frequencies.- Returns:
- the total frequency count.
-
getCount
public long getCount(java.lang.Comparable<?> v)
Returns the number of values equal to v. Returns 0 if the value is not comparable.- Parameters:
v- the value to lookup.- Returns:
- the frequency of v.
-
getCount
public long getCount(int v)
Returns the number of values equal to v.- Parameters:
v- the value to lookup.- Returns:
- the frequency of v.
-
getCount
public long getCount(long v)
Returns the number of values equal to v.- Parameters:
v- the value to lookup.- Returns:
- the frequency of v.
-
getCount
public long getCount(char v)
Returns the number of values equal to v.- Parameters:
v- the value to lookup.- Returns:
- the frequency of v.
-
getUniqueCount
public int getUniqueCount()
Returns the number of values in the frequency table.- Returns:
- the number of unique values that have been added to the frequency table.
- See Also:
valuesIterator()
-
getPct
public double getPct(java.lang.Comparable<?> v)
Returns the percentage of values that are equal to v (as a proportion between 0 and 1).Returns
Double.NaNif no values have been added. Returns 0 if at least one value has been added, but v is not comparable to the values set.- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getPct
public double getPct(int v)
Returns the percentage of values that are equal to v (as a proportion between 0 and 1).- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getPct
public double getPct(long v)
Returns the percentage of values that are equal to v (as a proportion between 0 and 1).- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getPct
public double getPct(char v)
Returns the percentage of values that are equal to v (as a proportion between 0 and 1).- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getCumFreq
public long getCumFreq(java.lang.Comparable<?> v)
Returns the cumulative frequency of values less than or equal to v.Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup.- Returns:
- the proportion of values equal to v
-
getCumFreq
public long getCumFreq(int v)
Returns the cumulative frequency of values less than or equal to v.Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getCumFreq
public long getCumFreq(long v)
Returns the cumulative frequency of values less than or equal to v.Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getCumFreq
public long getCumFreq(char v)
Returns the cumulative frequency of values less than or equal to v.Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values equal to v
-
getCumPct
public double getCumPct(java.lang.Comparable<?> v)
Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).Returns
Double.NaNif no values have been added. Returns 0 if at least one value has been added, but v is not comparable to the values set.- Parameters:
v- the value to lookup- Returns:
- the proportion of values less than or equal to v
-
getCumPct
public double getCumPct(int v)
Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values less than or equal to v
-
getCumPct
public double getCumPct(long v)
Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values less than or equal to v
-
getCumPct
public double getCumPct(char v)
Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).Returns 0 if v is not comparable to the values set.
- Parameters:
v- the value to lookup- Returns:
- the proportion of values less than or equal to v
-
getMode
public java.util.List<java.lang.Comparable<?>> getMode()
Returns the mode value(s) in comparator order.- Returns:
- a list containing the value(s) which appear most often.
- Since:
- 3.3
-
merge
public void merge(Frequency other) throws NullArgumentException
Merge another Frequency object's counts into this instance. This Frequency's counts will be incremented (or set when not already set) by the counts represented by other.- Parameters:
other- the otherFrequencyobject to be merged- Throws:
NullArgumentException- ifotheris null- Since:
- 3.1
-
merge
public void merge(java.util.Collection<Frequency> others) throws NullArgumentException
Merge aCollectionofFrequencyobjects into this instance. This Frequency's counts will be incremented (or set when not already set) by the counts represented by each of the others.- Parameters:
others- the otherFrequencyobjects to be merged- Throws:
NullArgumentException- if the collection is null- Since:
- 3.1
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
-