In this article, we will discuss “Diagram to Histogram” also known as “Interval Finding”. While dealing with statistical data, diagrams are represented with single points(or the corresponding numbers) like the stars, which is supposed to be a histogram with a certain width as shown in the below figure.
Analyzing the Problem:
In this problem statement, the assumption is that all intervals stick seamlessly together i.e., there are no gaps and no overlaps. The right edge of a bin is identical to the left edge of the following bin. Given N points, thus N bins, the task is to find (N + 1) bin edges. Every given point is located in the exact center of its X interval. This gives N equations for (N + 1) unknown quantities, so the system is underdetermined. There are two suggestions:
- The bins shall be as uniform. Mathematically, the variance of their bin widths shall be minimized.
- One supplies directly one fixed value for a bin edge, only the others are derived from that.
Here are few assumptions:
- Let xi be the coordinates of the point (their y values are completely irrelevant for us).
- wi the respective bin widths.
- ei the coordinates of the edges of the bin.
- Assume that the xi is sorted in ascending order.
Below is the image to illustrate the above concepts:
From the above representation, two simple relations that can be easily verified are:
The above formula simplifies the calculations especially for even N and is the reason why different results are obtained for odd and even values of N. The latter is directly the recursion formula that needs to fill in the array e(the points on the X-axis of the histogram).
How to minimize the Variance?
The idea is similar to all the minimization problems i.e., check for the derivative 0. The problem is to reduce its formula to only one unknown variable that we can then work with it to find the minimum value.
- The variance is given by:
- The value of mean value in the above formula is given by:
- Derive the above equation with respect to an arbitrary quantity z as:
- Applying iteratively the first equation derived above by replacing wi with (z = e0). For Example:
- Putting all the above values obtained to find the value of e0:
When N is odd, then
When N is even, then
- Alternatively, using the value of z = eN, gives a more simple formula as:
When N is odd, then
When N is even, then
Approach: The idea to solve the given problem is to iterate two nested loops, one for find the value of e0 or eN according to the derived formula and another loop for finding the elements of the array e. Therefore, the time complexity will be O(N) for all variants. Both loops work recursively and the second loop find the elements of the array e from the previous according to the second equation given above.
- The rounding effect of integer divisions is used to handle both cases of odd and even values of N.
- The C functions will work in C++ as well if the keyword register is omitted in the below implementation.
- The program will require a C compiler of the standard C99 and a C++ compiler of C++14 respectively.
Below is the implementation of the above approach:
Example for n = 6: x 20.500 79.000 160.500 217.000 267.000 332.500 e_orig 4.000 37.000 121.000 200.000 234.000 300.000 365.000 e_recN 3.583 37.417 120.583 200.417 233.583 300.417 364.583 e_rec0 3.583 37.417 120.583 200.417 233.583 300.417 364.583 e_fix 5.400 35.600 122.400 198.600 235.400 298.600 366.400
Caveats and Prospects:
- Both the methods i.e., the minimum variance and fixed edge occasionally fail in such a way that one or more histogram bins with negative width is obtained, i.e., there are some ei > e(i + 1) seemingly not properly sorted. This always happens when any random X values is taken as input.
- Try fixing the bin with the “most negative” width of all, hoping that all others will then look reasonable too.
- This brings us to the prospects because the two methods illustrated above are by far not the only possibilities to formulate an extra condition. Instead of “possibly equal” bin widths, assume a tendency like linear increase of the wi or an absolute minimum of all wi.