Your task is to make a histogram given a sample of arbitrary size.
Input
A float array or any other reasonable form of input with an arbitrary number of elements.
Output
The histogram; more to follow.
How to make a histogram
We'll use the sample: [1.1, 3, 5, 13, 15.5, 21, 29.7, 63, 16, 5, 5.1, 5.01, 51, 61, 13]
To make a histogram, you first separate your observations into classes. The ideal number of classes is the lowest k
such that 2^k > n
, where k
is the number of classes, and n
is the number of observations. In our case, k = 4
. The number of bars is not final. This k
value's purpose is only to find the width of each class.
The width of each class is the range of your data set divided by the number of classes. The range of our data set is 63 - 1.1 = 61.9
, so the width of each class is 15.475
. This means that the first class is [1.1, 16.575)
, the second is [16.575, 32.05)
and so on. The last class is [63.0, 78.475)
. The second value of the last class (b
in [a,b)
) must be greater than the max value of the set.
Now we rearrange our data into the classes; for us:
[1.1, 16.575): [1.1, 3, 5, 5, 5.01, 5.1, 13, 13, 15.5, 16]
[16.575, 32.05): [21, 29.7]
[32.05, 47.525): []
[47.525, 63.0): [51, 61]
[63.0, 78.475): [63]
And finally, we graph!
For our purposes, a bar will be a ▉
stacked for the number of observations in the class (because I think that looks good). You can count the ▉
as one byte in your source code. There should be no space between bars (unless there is an empty bar), nor do you need to label axes or any of that fancy stuff. There can be trailing whitespace at the end of your output. In our case, that would be:
▉
▉
▉
▉
▉
▉
▉▉
▉▉ ▉
▉▉ ▉▉
This is code-golf, so shortest code in bytes wins!
b
must be greater than the max value ifb
is the (open) upper bound of the histogram \$\endgroup\$The width of each class is the range of your data set divided by the number of classes
so the only value that can be inside the last class is the max value. If I'm wrong, could you please give me an example? \$\endgroup\$