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?

  • $\begingroup$ 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. $\endgroup$ – Michael E2 Oct 12 '18 at 14:39
  • $\begingroup$ @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}} $\endgroup$ – John Taylor Oct 12 '18 at 14:54

You can Quantile to determine the bins:

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"]

enter image description here

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}

| improve this answer | |
  • $\begingroup$ ... the bin heigths are almost equal. $\endgroup$ – kglr Oct 12 '18 at 15:18
  • $\begingroup$ Seems to leave off the largest data point: Histogram[data1, Append[#, Max@binspec + 1] & /@ binspec, "Count"] Need to nudge the last quantile, I guess. $\endgroup$ – Michael E2 Oct 12 '18 at 15:24
  • $\begingroup$ 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[]. $\endgroup$ – Michael E2 Oct 12 '18 at 15:27
  • $\begingroup$ @MichaelE2, just saw both your comments. Thank you. $\endgroup$ – kglr Oct 12 '18 at 15:43
  • 1
    $\begingroup$ @JohnTaylor, you can use MovingAverage[binspecs[[1]], 2]. $\endgroup$ – kglr Oct 13 '18 at 15:57

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}}]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.