Search code examples
javafrequency

Efficient way to implement frequency table on interval data in Java


I am trying to create a frequency table to eventually create a histogram. The input data is of type Double and should all fall in the interval [-1;1]. I would like to divide this interval into bins with a width of e.g. .02 and count the occurrences of values in each interval, there is no need to store the data.

I tried using Apache Commons Frequency, but that only seems to work with discrete values.

Is there a library that achieves this?


Solution

  • While I haven't been able to find a library that does what I wanted, here's the solution I came up with in case anyone finds this in the future:

    I created a datatype representing an Interval similar to one I found in Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    The actual frequency table is implemented as a TreeMap<Interval, Integer>, since that is able to store the entries in the order of the keys (the Interval implementation needs a Comparator for this to work). Then all I needed to add was method to add observations, which increments the value of the interval in the TreeMap.

    May not be the most efficient way to solve this, but it seems to work for my purposes.