views:

334

answers:

1

I need to generate bins for the purposes of calculating a histogram. Language is C#. Basically I need to take in an array of decimal numbers and generate a histogram plot out of those.

Haven't been able to find a decent library to do this outright so now I'm just looking for either a library or an algorithm to help me do the binning of the data.

So...

  • Are there any C# libraries out there that will take in an array of decimal data and output a binned histogram?
  • Is there generic algorithm for building the bins to be used in generated a histogram?
+2  A: 

Here is a simple bucket function I use. Sadly, .NET generics doesn't support a numerical type contraint so you will have to implement a different version of the following function for decimal, int, double, etc.

public static List<int> Bucketize(this IEnumerable<decimal> source, int totalBuckets)
{
    var min = decimal.MaxValue;
    var max = decimal.MinValue;
    var buckets = new List<int>();

    foreach (var value in source)
    {
        min = Math.Min(min, value);
        max = Math.Max(max, value);
    }
    var bucketSize = (max - min) / totalBuckets;
    foreach (var value in source)
    {
        int bucketIndex = 0;
        if (bucketSize > 0.0)
        {
            bucketIndex = (int)((value - min) / bucketSize);
            if (bucketIndex == totalBuckets)
            {
                bucketIndex--;
            }
        }
        buckets[bucketIndex]++;
    }
    return buckets;
}
Jake Pearson
I had written an almost identical algorithm using the SAS language and was going to have to have my dev translate it into C#. Thanks for this.
Jay Stevens