Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dstats.summary.histogram #19

Open
JackStouffer opened this issue Feb 12, 2016 · 1 comment
Open

dstats.summary.histogram #19

JackStouffer opened this issue Feb 12, 2016 · 1 comment

Comments

@JackStouffer
Copy link

Histogram function would be very helpful. Bonus points if it has a overload that allows for explicitly setting the bins with an array/range.

@JackStouffer
Copy link
Author

JackStouffer commented May 11, 2016

auto hist(Range)(Range data, uint bins) if (isForwardRange!Range)
{
    immutable minimum = data
        .save
        .reduce!min
        .floor
        .lround;
    immutable maximum = data
        .save
        .reduce!max
        .ceil
        .lround;
    immutable bin_width = (maximum - minimum) / bins;

    auto results = iota(0, bins + 1)
        .map!(a => tuple(double(0), minimum + (bin_width * a)))
        .array;

    foreach (item; data)
    {
        immutable bin = (lround(item) - minimum) / bin_width;
        results[bin][0]++;
    }

   return results;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant