Essays/Histogram

From J Wiki
Jump to navigation Jump to search

The dyad I. and the monad #/.~ can be used to compute histograms efficiently:

histogram=: <: @ (#/.~) @ (i.@#@[ , I.)

The left argument is a list of interval start points. The right argument is an array of data. The result is a list of counts of the number of data points in each interval.

The following example illustrates the central limit theorem: the sum of independent identically distributed random variables converges in distribution to a normal random variable.

   d=: +/ 10 1e6 ?.@$ 21
   e=: 5 * i.40


   h=: e histogram d
   $ h
40
   h
0 0 0 0 1 6 38 151 400 1093 2417 5274 9732 17014 26482 39266 54622
   70277 85594 96077 101359 101033 94374 82241 67322 52201 36777
   24419 15174 8526 4666 2091 880 348 99 34 12 0 0 0

   load 'plot'
   plot e;h

Histogram.jpg



Contributed by Roger Hui.

See Also