Organize the data to set of bins of equal height

Consider the data in the form data = {{1,10},{1,12},...{}}. How to organize this data in the set

data1 = {{Width1,Height},{Width2,Height},...},


where Width1, Width2, … are widths of bins (from 0 to 10, from 10 to 25 and so on) which correspond to equal heights Height of the data points inside the bins?

• If the data were one dimensional, I'd say use Quantiles, but I guess I have no clue what is being asked. Maybe a small example input and output would help. – Michael E2 Oct 12 '18 at 14:39
• @MichaelE2 : consider that each row corresponds to one event. Then the first column data1 is the width of the bin, while the second column is the number of events in this bin. I am looking for a function in Mathematica that automatically finds the widths of the bins in a way such that the number of events in each bin will be equal. Say for data = {{1,2},{{1,3},{1,4},{1,5},{1,25},{1,90}}, if I set the number of events to Height=2, the data1 will be data1 = {{3-2, 2},{5-4,2},{90-25,2}} – John Taylor Oct 12 '18 at 14:54

You can Quantile to determine the bins:

SeedRandom[1]
data1 = RandomVariate[NormalDistribution[], 100];
n = 10;
binspecs ={ Quantile[data1, Range[0,1,1/n], {{1/2,0},{0,1}}]/. {a_,b__,c_}:>{a-1,b,c+1}};
Histogram[data1, binspecs, "Count"]


You can also Sort and Partition the input data:

bins1 = Partition[Sort[#], Round[Length[#]/#2]] &[data1, 10];
Length /@ bins1


{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}

Alternatively, sort and Split the input data:

bins2 = Module[{t = 0, l = Length[#]/#2},
Split[Sort[#], Or[(t += 1) < l, t = 0] &]] &[data1, 10];
Length /@ bins2


{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}

• ... the bin heigths are almost equal. – kglr Oct 12 '18 at 15:18
• Seems to leave off the largest data point: Histogram[data1, Append[#, Max@binspec + 1] & /@ binspec, "Count"] Need to nudge the last quantile, I guess. – Michael E2 Oct 12 '18 at 15:24
• What I had in mind with my comment to the OP, was Quantile[data1, Range[0, 1, 1/(n - 1)]], which seems to work without having to go through EmpricalDistribution[]. – Michael E2 Oct 12 '18 at 15:27
• @MichaelE2, just saw both your comments. Thank you. – kglr Oct 12 '18 at 15:43
• Thank you! May I ask you how to add to data1 the column of the position of the half-width of the bin? I.e., the column with (bin-position-min + bin-position-max)/2. – John Taylor Oct 13 '18 at 9:05

You can use Histogram to plot histograms.

If you allready know how many points were in one particular bin you could recreate a longer list with values within this one bin, histogram seems to do this counting for you. Your widths (data1[[All,1]]) will need to be changed such that you know the borders of your bins which such be in a list like {{b1,b2,…}}. Call Histogram[points, widths]

Feel free to refer to the Documentation and expand Details and Options for further variants.

Edit: e.g.:

Histogram[{1,2,3, 50,49},{{0,10,25,100}}]