New Version 6.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Histograms QuickStart Sample (C#)

Illustrates how to create histograms using the Histogram class in the Extreme.DataAnalysis namespace in C#.

Visual Basic code F# code IronPython code Back to QuickStart Samples

using System;

using Extreme.DataAnalysis;
using Extreme.Mathematics;
using Extreme.Statistics;

namespace Extreme.Numerics.QuickStart.CSharp
    /// <summary>
    /// Illustrates the use of the Histogram class. 
    /// </summary>
    class Histograms
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
            // Histograms are used to summarize the distribution of data.
            // This QuickStart sample creates a histogram from data 
            // in a variety of ways.

            // We use the test scores of students on a hypothetical national test.
            // First we create a NumericalVariable that holds the test scores.
            var group1Results = Vector.Create(
                62, 77, 61, 94, 75, 82, 86, 83, 64, 84,
                68, 82, 72, 71, 85, 66, 61, 79, 81, 73);

            // We can create a histogram with evenly spaced bins 
            // by specifying the lower bound, the upper bound, 
            // and the number of bins:
            var histogram1 = Histogram.CreateEmpty(50, 100, 5);
            // We can also provide the bounds explicitly:
            int[] bounds = new int[] {50, 62, 74, 88, 100};
            var histogram2 = Histogram.CreateEmpty(bounds);

            // Or we can first create an Index object
            var index = Index.CreateBins(bounds);
            var histogram3 = Histogram.CreateEmpty(index);

            // To tally the results, we simply call the Tabulate method.
            // The data can be supplied as a vector:
            // or simply as any enumerable, including an array:

            // You can add multiple data sets to the same histogram:
            histogram2.Tabulate(new int[] {74, 68, 89});
            // Or you can add individual data points using the Increment method.
            // This will increment the count of the bin that contains 
            // the specified value:

            // Histograms are just vectors, so the SetToZero method 
            // clears all the data:

            // The Bins property returns an index of bins:
            var bins = histogram1.Bins;
            // The Length property returns the total number of bins:
            Console.WriteLine("# bins: {0}", bins.Length);
            // For binned histograms, the bins are of type Interval<T>:
            Interval<int> bin = bins[2];
            // Interval structures have a lower bound, an upper bound:
            Console.WriteLine("Bin 2 has lower bound {0}.", bin.LowerBound);
            Console.WriteLine("Bin 2 has upper bound {0}.", bin.UpperBound);
            // You can get the value at a specific bin using the Get method:
            Console.WriteLine("Bin 2 has value {0}.", histogram1.Get(bin));

            // The histogram's FindBin method returns the Histogram bin
            // that contains a specified value:
            bin = histogram1.FindBin(83);
            Console.WriteLine("83 is in bin {0}", bin);

            // You can use the BinsAndValues property to iterate through all the bins
            // in a for-each loop:
            foreach(var pair in histogram1.BinsAndValues)
                Console.WriteLine("Bin {0}: {1}", pair.Key, pair.Value);

            // You can also create histograms for categorical data:
            var success = Vector.CreateCategorical(
                new bool[] { true, false, true, true, false });
            var histogram4 = success.CreateHistogram();
            // Bins for categorical histograms are just the categories:
            var successes = histogram4.Get(true);

            Console.Write("Press any key to exit.");