22Suppose I am given the following information:
upper limit of distribution
lower limit of distribution
number of bins in the distribution
a number to place in one of the available bins
Eventually, the goal will be to find the distribution of many of these numbers being placed. First, how to place a number in the proper bin?
Naively, one could cycle through all bins, testing to see if the number belongs in that bin.
binsize=1000
Tmax = 1
Tmin = 0
stepsize = (Tmax-Tmin)/binsize
do indx = 1,binsize
step = Tmin + indx*stepsize
if ((T < (step+stepsize)).AND.(T > step)) then
bins = bins+1
endif
enddo
However, if bins size is large (which results in a smoother distribution), then the number of elements must be large (1E6 or 1E7). Thus, the total number of operations goes like (1E6)*1000
A better method is to be direct:
Given bin limits 0,1 and bin size 10, the step size is (1-0)/10
The bins are
0 --> 0.1
0.1 --> 0.2
0.2 --> 0.3
0.3 --> 0.4
0.4 --> 0.5
0.5 --> 0.6
0.6 --> 0.7
0.7 --> 0.8
0.8 --> 0.9
0.9 --> 1.0
"By observation," 0.75 belongs in the seventh bin since
0.7 < 0.75 < 0.8
factor the step size from the limits of the inequality,
0.1*7 < 0.75 < 0.1*8
replacing the 0.1 with the step size,
step*7 < 0.75 < step*(7+1)
multiply everything by inverse step size
7 < (0.75/step) < 7+1
Thus
floor(0.75/step) = 7
But this only is valid when the limits are 0,1. Happily, all distributions can be shifted and scaled back to 0,1 limits. For example, Suppose my limits are 2,4 and 5 bins. Given 3.4524,
step = (4-2)/5 = 0.4
2.0 --> 2.4
2.4 --> 2.8
2.8 --> 3.2
3.2 --> 3.6
3.6 --> 4.0
3.2 < 3.4524 < 3.6
shift down by the minimum
1.2 < 1.4524 < 1.6
then rescale by the difference 4-2
1.2/2 < 1.4524/2 < 1.6/2
0.6 < 0.7262 < 0.8
now divide by step size
0.6/0.4 < 0.7262/0.4 < 0.8/0.4
1.5 < 1.8155 < 2
so shift then scale is wrong
floor((3.4524 - min)/(max-min)) = 1, not the desired 4