histogram

Undocumented in source. Be warned that the author may not have intended to support it.
  1. Histogram!T histogram(T histMin, T histMax, T binSize, R values)
  2. Histogram!T histogram(R values, T histMin, T histMax, T binSize)
    histogram
    (
    R
    T = ElementType!R
    )
    if (
    isInputRange!R
    )
  3. Histogram!T histogram(T histMin, T histMax, T binSize)

Examples

// Generate a histogram of standard-normal-distributed numbers
// with 7+2 bins from -2.0 to 2.0. The additional two bins are
// the overflow bins.
auto h = histogram(-2.0, 2.0, 0.5, [
    0.697108, 0.019264, -1.838430, 1.831528, -0.804880, -1.558828,
    -0.131643, -0.306090, -0.397831, 0.037725, 0.328819, -0.640064,
    0.664097, 1.156503, -0.837012, -0.969499, -1.410276, 0.501637,
    1.521720, 1.392988, -0.619393, -0.039576, 1.937708, -1.325983,
    -0.677214, 1.390584, 1.798133, -1.094093, 2.263360, -0.462949,
    1.993554, 2.243889, 1.606391, 0.153866, 1.945514, 1.007849,
    -0.663765, -0.304843, 0.617464, 0.674804, 0.038555, 1.696985,
    1.473917, -0.244211, -1.410381, 0.201184, -0.923119, -0.220677,
    0.045521, -1.966340,
]);

assert(h.totalCount == 50);
assert(isClose(h.minValue, -1.96634, 0.01, 1e-5));
assert(isClose(h.maxValue, 2.26336, 0.01, 1e-5));
assert(isClose(h.sum, 10.3936, 0.01, 1e-5));
assert(isClose(h.mean, 0.2079, 0.01, 1e-5));
assert(isClose(h.percentile(0.5), 0.1429, 0.01, 1e-5));

enum inf = double.infinity;
auto expectedHist = [
    [-inf, 0.00],
    [-2.0, 0.12],
    [-1.5, 0.16],
    [-1.0, 0.32],
    [-0.5, 0.32],
    [ 0.0, 0.28],
    [ 0.5, 0.20],
    [ 1.0, 0.20],
    [ 1.5, 0.32],
    [ 2.0, 0.00],
];

foreach (idx, coord, double density; h)
{
    assert(isClose(expectedHist[idx][0], coord, 0.01, 1e-5));
    assert(isClose(expectedHist[idx][1], density, 0.01, 1e-5));
}
// Generate a histogram of geometric-distributed numbers.
auto h = logHistogram(0U, 50U, 10U, [
    3U, 4U, 23U, 2U, 0U, 2U, 9U, 0U, 17U, 2U, 0U, 5U, 5U, 35U, 0U, 16U,
    17U, 3U, 7U, 14U, 3U, 9U, 1U, 17U, 13U, 10U, 38U, 2U, 1U, 29U, 1U,
    5U, 49U, 40U, 2U, 1U, 13U, 5U, 1U, 1U, 2U, 4U, 1U, 0U, 0U, 7U, 7U,
    34U, 3U, 2U,
]);

assert(h.totalCount == 50);
assert(h.minValue == 0);
assert(h.maxValue == 49);
assert(h.sum == 465);
assert(isClose(h.mean, 9.3, 0.01, 1e-5));
assert(isClose(h.percentile(0.5), 4.5, 0.01, 1e-5));

auto expectedHist = [
    [ 0, 0.120],
    [ 1, 0.140],
    [ 2, 0.140],
    [ 3, 0.080],
    [ 4, 0.040],
    [ 5, 0.080],
    [ 6, 0.000],
    [ 7, 0.060],
    [ 8, 0.000],
    [ 9, 0.040],
    [10, 0.016],
    [20, 0.004],
    [30, 0.006],
    [40, 0.004],
    [50, 0.000],
];

foreach (idx, coord, double density; h)
{
    assert(isClose(expectedHist[idx][0], coord, 0.01, 1e-5));
    assert(isClose(expectedHist[idx][1], density, 0.01, 1e-5));
}

Meta