Top Description Inners Fields Constructors Methods
org.apache.commons.math3.stat

public Class Frequency

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.text.NumberFormat, java.util.ArrayList, .Collection, .Iterator, .Comparator, .List, .Map, .Map.Entry, .SortedMap, .TreeMap, org.apache.commons.math3.exception.MathIllegalArgumentException, .NullArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.MathUtils

Maintains 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 to getCount, 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 addValue to 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 Comparator is supplied in the constructor.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Frequency.NaturalComparator<
the type of the objects compared
T extends Comparable<T>
>

A Comparator that compares comparable objects using the natural order.

Field Summary

Modifier and TypeField and Description
private final SortedMap<Comparable<?>, Long>
freqTable

underlying collection

private static final long
serialVersionUID

Serializable version identifier

Constructor Summary

AccessConstructor and Description
public
Frequency()

Default constructor.

public
Frequency(Comparator<?>
Comparator used to order values
comparator
)

Constructor allowing values Comparator to be specified.

Method Summary

Modifier and TypeMethod and Description
public void
addValue(Comparable<?>
the value to add.
v
)

Adds 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.

public void
addValue(int
the value to add.
v
)

Adds 1 to the frequency count for v.

public void
addValue(long
the value to add.
v
)

Adds 1 to the frequency count for v.

public void
addValue(char
the value to add.
v
)

Adds 1 to the frequency count for v.

public void
clear()

Clears the frequency table

public Iterator<Map.Entry<Comparable<?>, Long>>

Returns:

entry set Iterator
entrySetIterator
()

Return an Iterator over the set of keys and values that have been added.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public long

Returns:

the frequency of v.
getCount
(Comparable<?>
the value to lookup.
v
)

Returns the number of values equal to v. Returns 0 if the value is not comparable.

public long

Returns:

the frequency of v.
getCount
(int
the value to lookup.
v
)

Returns the number of values equal to v.

public long

Returns:

the frequency of v.
getCount
(long
the value to lookup.
v
)

Returns the number of values equal to v.

public long

Returns:

the frequency of v.
getCount
(char
the value to lookup.
v
)

Returns the number of values equal to v.

public long

Returns:

the proportion of values equal to v
getCumFreq
(Comparable<?>
the value to lookup.
v
)

Returns the cumulative frequency of values less than or equal to v.

Returns 0 if v is not comparable to the values set.

public long

Returns:

the proportion of values equal to v
getCumFreq
(int
the value to lookup
v
)

Returns the cumulative frequency of values less than or equal to v.

Returns 0 if v is not comparable to the values set.

public long

Returns:

the proportion of values equal to v
getCumFreq
(long
the value to lookup
v
)

Returns the cumulative frequency of values less than or equal to v.

Returns 0 if v is not comparable to the values set.

public long

Returns:

the proportion of values equal to v
getCumFreq
(char
the value to lookup
v
)

Returns the cumulative frequency of values less than or equal to v.

Returns 0 if v is not comparable to the values set.

public double

Returns:

the proportion of values less than or equal to v
getCumPct
(Comparable<?>
the value to lookup
v
)

Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values less than or equal to v
getCumPct
(int
the value to lookup
v
)

Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values less than or equal to v
getCumPct
(long
the value to lookup
v
)

Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values less than or equal to v
getCumPct
(char
the value to lookup
v
)

Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

public List<Comparable<?>>

Returns:

a list containing the value(s) which appear most often.
getMode
()

Returns the mode value(s) in comparator order.

public double

Returns:

the proportion of values equal to v
getPct
(Comparable<?>
the value to lookup
v
)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values equal to v
getPct
(int
the value to lookup
v
)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values equal to v
getPct
(long
the value to lookup
v
)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

public double

Returns:

the proportion of values equal to v
getPct
(char
the value to lookup
v
)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

public long

Returns:

the total frequency count.
getSumFreq
()

Returns the sum of all frequencies.

public int

Returns:

the number of unique values that have been added to the frequency table.
getUniqueCount
()

Returns the number of values in the frequency table.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public void
incrementValue(Comparable<?>
the value to add.
v
,
long
the amount by which the value should be incremented
increment
)

Increments 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.

public void
incrementValue(int
the value to add.
v
,
long
the amount by which the value should be incremented
increment
)

Increments 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.

public void
incrementValue(long
the value to add.
v
,
long
the amount by which the value should be incremented
increment
)

Increments 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.

public void
incrementValue(char
the value to add.
v
,
long
the amount by which the value should be incremented
increment
)

Increments 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.

public void
merge(final Frequency
the other Frequency object to be merged
other
)

Merge another Frequency object's counts into this instance.

public void
merge(final Collection<Frequency>
the other Frequency objects to be merged
others
)

Merge a Collection of Frequency objects into this instance.

public String

Returns:

a string representation.
toString
()

Overrides java.lang.Object.toString.

Return a string representation of this frequency distribution.

public Iterator<Comparable<?>>

Returns:

values Iterator
valuesIterator
()

Returns an Iterator over the set of values that have been added.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

freqTableback to summary
private final SortedMap<Comparable<?>, Long> freqTable

underlying collection

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

Constructor Detail

Frequencyback to summary
public Frequency()

Default constructor.

Frequencyback to summary
public Frequency(Comparator<?> comparator)

Constructor allowing values Comparator to be specified.

Parameters
comparator:Comparator<?>

Comparator used to order values

Annotations
@SuppressWarnings:unchecked

Method Detail

addValueback to summary
public void addValue(Comparable<?> v) throws MathIllegalArgumentException

Adds 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:Comparable<?>

the value to add.

Exceptions
MathIllegalArgumentException:
if v is not comparable with previous entries
addValueback to summary
public void addValue(int v) throws MathIllegalArgumentException

Adds 1 to the frequency count for v.

Parameters
v:int

the value to add.

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Long
addValueback to summary
public void addValue(long v) throws MathIllegalArgumentException

Adds 1 to the frequency count for v.

Parameters
v:long

the value to add.

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Long
addValueback to summary
public void addValue(char v) throws MathIllegalArgumentException

Adds 1 to the frequency count for v.

Parameters
v:char

the value to add.

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Char
clearback to summary
public void clear()

Clears the frequency table

entrySetIteratorback to summary
public Iterator<Map.Entry<Comparable<?>, 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:Iterator<Map.Entry<Comparable<?>, Long>>

entry set Iterator

Since
3.1
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getCountback to summary
public long getCount(Comparable<?> v)

Returns the number of values equal to v. Returns 0 if the value is not comparable.

Parameters
v:Comparable<?>

the value to lookup.

Returns:long

the frequency of v.

getCountback to summary
public long getCount(int v)

Returns the number of values equal to v.

Parameters
v:int

the value to lookup.

Returns:long

the frequency of v.

getCountback to summary
public long getCount(long v)

Returns the number of values equal to v.

Parameters
v:long

the value to lookup.

Returns:long

the frequency of v.

getCountback to summary
public long getCount(char v)

Returns the number of values equal to v.

Parameters
v:char

the value to lookup.

Returns:long

the frequency of v.

getCumFreqback to summary
public long getCumFreq(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:Comparable<?>

the value to lookup.

Returns:long

the proportion of values equal to v

Annotations
@SuppressWarnings:rawtypes, unchecked
getCumFreqback to summary
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:int

the value to lookup

Returns:long

the proportion of values equal to v

getCumFreqback to summary
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:long

the value to lookup

Returns:long

the proportion of values equal to v

getCumFreqback to summary
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:char

the value to lookup

Returns:long

the proportion of values equal to v

getCumPctback to summary
public double getCumPct(Comparable<?> v)

Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).

Returns Double.NaN if 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:Comparable<?>

the value to lookup

Returns:double

the proportion of values less than or equal to v

getCumPctback to summary
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:int

the value to lookup

Returns:double

the proportion of values less than or equal to v

getCumPctback to summary
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:long

the value to lookup

Returns:double

the proportion of values less than or equal to v

getCumPctback to summary
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:char

the value to lookup

Returns:double

the proportion of values less than or equal to v

getModeback to summary
public List<Comparable<?>> getMode()

Returns the mode value(s) in comparator order.

Returns:List<Comparable<?>>

a list containing the value(s) which appear most often.

Since
3.3
getPctback to summary
public double getPct(Comparable<?> v)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

Returns Double.NaN if 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:Comparable<?>

the value to lookup

Returns:double

the proportion of values equal to v

getPctback to summary
public double getPct(int v)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

Parameters
v:int

the value to lookup

Returns:double

the proportion of values equal to v

getPctback to summary
public double getPct(long v)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

Parameters
v:long

the value to lookup

Returns:double

the proportion of values equal to v

getPctback to summary
public double getPct(char v)

Returns the percentage of values that are equal to v (as a proportion between 0 and 1).

Parameters
v:char

the value to lookup

Returns:double

the proportion of values equal to v

getSumFreqback to summary
public long getSumFreq()

Returns the sum of all frequencies.

Returns:long

the total frequency count.

getUniqueCountback to summary
public int getUniqueCount()

Returns the number of values in the frequency table.

Returns:int

the number of unique values that have been added to the frequency table.

See Also
valuesIterator()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
incrementValueback to summary
public void incrementValue(Comparable<?> v, long increment) throws MathIllegalArgumentException

Increments 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:Comparable<?>

the value to add.

increment:long

the amount by which the value should be incremented

Exceptions
MathIllegalArgumentException:
if v is not comparable with previous entries
Since
3.1
incrementValueback to summary
public void incrementValue(int v, long increment) throws MathIllegalArgumentException

Increments 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:int

the value to add.

increment:long

the amount by which the value should be incremented

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Long
Since
3.3
incrementValueback to summary
public void incrementValue(long v, long increment) throws MathIllegalArgumentException

Increments 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:long

the value to add.

increment:long

the amount by which the value should be incremented

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Long
Since
3.3
incrementValueback to summary
public void incrementValue(char v, long increment) throws MathIllegalArgumentException

Increments 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:char

the value to add.

increment:long

the amount by which the value should be incremented

Exceptions
MathIllegalArgumentException:
if the table contains entries not comparable to Char
Since
3.3
mergeback to summary
public void merge(final 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:Frequency

the other Frequency object to be merged

Exceptions
NullArgumentException:
if other is null
Since
3.1
mergeback to summary
public void merge(final Collection<Frequency> others) throws NullArgumentException

Merge a Collection of Frequency objects 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:Collection<Frequency>

the other Frequency objects to be merged

Exceptions
NullArgumentException:
if the collection is null
Since
3.1
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return a string representation of this frequency distribution.

Returns:String

a string representation.

Annotations
@Override
valuesIteratorback to summary
public Iterator<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:Iterator<Comparable<?>>

values Iterator

org.apache.commons.math3.stat back to summary

private Class Frequency.NaturalComparator<T extends Comparable<T>>

extends Object
implements Comparator<Comparable<T extends Comparable<T>>>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Comparator
Type Parameters
<T>
the type of the objects compared

A Comparator that compares comparable objects using the natural order. Copied from Commons Collections ComparableComparator.

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Serializable version identifier

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

result of comparison
compare
(Comparable<T>
the first object
o1
,
Comparable<T>
the second object
o2
)

Implements java.util.Comparator.compare.

Compare the two Comparable arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier

Constructor Detail

NaturalComparatorback to summary
private NaturalComparator()

Method Detail

compareback to summary
public int compare(Comparable<T> o1, Comparable<T> o2)

Implements java.util.Comparator.compare.

Compare the two Comparable arguments. This method is equivalent to:

((Comparable)o1).compareTo(o2)
Parameters
o1:Comparable<T>

the first object

o2:Comparable<T>

the second object

Returns:int

result of comparison

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
when o1 is null, or when ((Comparable)o1).compareTo(o2) does
ClassCastException:
when o1 is not a Comparable, or when ((Comparable)o1).compareTo(o2) does