Frequency

A mutable accumulator that counts the frequency of discrete values.

Maintains exact counts for each distinct value of type T, and provides cumulative counts, proportions, and mode queries. This is the discrete counterpart of histogram binning — it counts exact matches rather than grouping values into intervals.

Analogous to Apache Commons Math Frequency.

Example:

val freq = Frequency<String>()
freq.addAll(listOf("a", "a", "b", "b", "b", "c"))
freq.count("b") // 3
freq.proportion("b") // 0.5
freq.cumulativeCount("b") // 5 (a=2 + b=3)
freq.mode // setOf("b")

Type Parameters

T

the type of observed values; must be Comparable for cumulative queries and sorted output.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

All observed values with their counts, sorted by value in ascending order.

Link copied to clipboard
val mode: Set<T>

The set of values with the highest observed frequency (the statistical mode).

Link copied to clipboard

The total number of observations added so far.

Link copied to clipboard

The number of distinct values observed.

Link copied to clipboard
val values: List<T>

All distinct observed values, sorted in ascending order.

Functions

Link copied to clipboard
fun add(value: T)

Adds a single observation of value (increments its count by 1).

fun add(value: T, count: Long)

Adds count observations of value.

Link copied to clipboard
fun addAll(values: Iterable<T>)

Adds a single observation for each element in values.

Link copied to clipboard
fun clear()

Resets the accumulator to its initial empty state.

Link copied to clipboard
fun count(value: T): Long

Returns the number of times value has been observed, or 0 if it has never been observed.

Link copied to clipboard
fun cumulativeCount(value: T): Long

Returns the cumulative count for value: the sum of counts for all observed values less than or equal to value.

Link copied to clipboard

Returns the cumulative proportion for value (cumulativeCount / totalCount).

Link copied to clipboard
fun proportion(value: T): Double

Returns the proportion of observations equal to value (count / totalCount).