org.apache.commons.math3.stat
Class Frequency

java.lang.Object
  extended by org.apache.commons.math3.stat.Frequency
All Implemented Interfaces:
Serializable

public class Frequency
extends Object
implements Serializable

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

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.

The values are ordered using the default (natural order), unless a Comparator is supplied in the constructor.

Version:
$Id: Frequency.java 1455703 2013-03-12 20:46:23Z tn $
See Also:
Serialized Form

Constructor Summary
Frequency()
          Default constructor.
Frequency(Comparator<?> comparator)
          Constructor allowing values Comparator to be specified.
 
Method Summary
 void addValue(char v)
          Adds 1 to the frequency count for v.
 void addValue(Comparable<?> v)
          Adds 1 to the frequency count for v.
 void addValue(int v)
          Adds 1 to the frequency count for v.
 void addValue(long v)
          Adds 1 to the frequency count for v.
 void clear()
          Clears the frequency table
 Iterator<Map.Entry<Comparable<?>,Long>> entrySetIterator()
          Return an Iterator over the set of keys and values that have been added.
 boolean equals(Object obj)
          
 long getCount(char v)
          Returns the number of values = v.
 long getCount(Comparable<?> v)
          Returns the number of values = v.
 long getCount(int v)
          Returns the number of values = v.
 long getCount(long v)
          Returns the number of values = v.
 long getCumFreq(char v)
          Returns the cumulative frequency of values less than or equal to v.
 long getCumFreq(Comparable<?> v)
          Returns the cumulative frequency of values less than or equal to v.
 long getCumFreq(int v)
          Returns the cumulative frequency of values less than or equal to v.
 long getCumFreq(long v)
          Returns the cumulative frequency of values less than or equal to v.
 double getCumPct(char v)
          Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).
 double getCumPct(Comparable<?> v)
          Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).
 double getCumPct(int v)
          Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).
 double getCumPct(long v)
          Returns the cumulative percentage of values less than or equal to v (as a proportion between 0 and 1).
 double getPct(char v)
          Returns the percentage of values that are equal to v (as a proportion between 0 and 1).
 double getPct(Comparable<?> v)
          Returns the percentage of values that are equal to v (as a proportion between 0 and 1).
 double getPct(int v)
          Returns the percentage of values that are equal to v (as a proportion between 0 and 1).
 double getPct(long v)
          Returns the percentage of values that are equal to v (as a proportion between 0 and 1).
 long getSumFreq()
          Returns the sum of all frequencies.
 int getUniqueCount()
          Returns the number of values in the frequency table.
 int hashCode()
          
 void incrementValue(Comparable<?> v, long increment)
          Increments the frequency count for v.
 void merge(Collection<Frequency> others)
          Merge a Collection of Frequency objects into this instance.
 void merge(Frequency other)
          Merge another Frequency object's counts into this instance.
 String toString()
          Return a string representation of this frequency distribution.
 Iterator<Comparable<?>> valuesIterator()
          Returns an Iterator over the set of values that have been added.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Frequency

public Frequency()
Default constructor.


Frequency

public Frequency(Comparator<?> comparator)
Constructor allowing values Comparator to be specified.

Parameters:
comparator - Comparator used to order values
Method Detail

toString

public String toString()
Return a string representation of this frequency distribution.

Overrides:
toString in class Object
Returns:
a string representation.

addValue

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 - the value to add.
Throws:
MathIllegalArgumentException - if v is not comparable with previous entries

incrementValue

public void incrementValue(Comparable<?> v,
                           long 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.

Parameters:
v - the value to add.
increment - the amount by which the value should be incremented
Throws:
IllegalArgumentException - if v is not comparable with previous entries
Since:
3.1

addValue

public void addValue(int v)
              throws MathIllegalArgumentException
Adds 1 to the frequency count for v.

Parameters:
v - the value to add.
Throws:
MathIllegalArgumentException - if the table contains entries not comparable to Integer

addValue

public void addValue(long v)
              throws MathIllegalArgumentException
Adds 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 MathIllegalArgumentException
Adds 1 to the frequency count for v.

Parameters:
v - the value to add.
Throws:
MathIllegalArgumentException - if the table contains entries not comparable to Char

clear

public void clear()
Clears the frequency table


valuesIterator

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:
values Iterator

entrySetIterator

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:
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(Comparable<?> v)
Returns the number of values = 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 = v.

Parameters:
v - the value to lookup.
Returns:
the frequency of v.

getCount

public long getCount(long v)
Returns the number of values = v.

Parameters:
v - the value to lookup.
Returns:
the frequency of v.

getCount

public long getCount(char v)
Returns the number of values = 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(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.

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(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(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 - 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

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 other Frequency object to be merged
Throws:
NullArgumentException - if other is null
Since:
3.1

merge

public void merge(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 - the other Frequency objects to be merged
Throws:
NullArgumentException - if the collection is null
Since:
3.1

hashCode

public int hashCode()

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object


Copyright © 2003-2013 The Apache Software Foundation. All Rights Reserved.