Extreme Optimization > QuickStart Samples > Histograms QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Histograms QuickStart Sample (C#)

Illustrates how to use the Histogram class (Extreme.Statistics namespace) in C#.

VB.NET code Back to QuickStart Samples

using System;

using Extreme.Statistics;

namespace Extreme.Statistics.QuickStart.CSharp
{
    /// <summary>
    /// Illustrates the use of the Histogram class. 
    /// </summary>
    class Histograms
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        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.
            double[] group1Data = new double[]
                {62, 77, 61, 94, 75, 82, 86, 83, 64, 84, 
                 68, 82, 72, 71, 85, 66, 61, 79, 81, 73};    
            NumericalVariable group1Results =
                new NumericalVariable("Class 1", group1Data);

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

            // Or we can first create a NumericalScale object
            NumericalScale scale = new NumericalScale(50, 100, 5);
            Histogram histogram3 = new Histogram(scale);

            // To tally the results, we simply call the Tabulate method.
            // The data can be supplied as a NumericalVariable:
            histogram1.Tabulate(group1Results);
            // or simply as a Double array:
            histogram2.Tabulate(group1Data);

            // You can add multiple data sets to the same histogram:
            histogram2.Tabulate(new double[] {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:
            histogram2.Increment(83);
            histogram2.Increment(78);

            
            // The Clear method clears all the data:
            histogram2.Clear();


            // The Bins property returns a collection of HistogramBin objects:
            HistogramBinCollection bins = histogram1.Bins;
            // It has a Count property that returns the total number of bins:
            Console.WriteLine("# bins: {0}", bins.Count);
            // and an indexer property that returns a HistogramBin object:
            HistogramBin bin = bins[2];
            // HistogramBin objects have a lower bound, an upper bound,
            // and a value:
            Console.WriteLine("Bin 2 has lower bound {0}.", bin.LowerBound);
            Console.WriteLine("Bin 2 has upper bound {0}.", bin.UpperBound);
            Console.WriteLine("Bin 2 has value bound {0}.", bin.Value);

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

            // You can use the Bins property to iterate through all the bins
            // in a foreach loop:
            foreach(HistogramBin bin2 in histogram1.Bins)
                Console.WriteLine("Bin {0}: {1}", bin2.Index, bin2.Value);

            // The histogram's GetTotals method returns a double array 
            // that contains the total for each bin in the histogram:
            double[] totals = histogram1.GetTotals();

            Console.Write("Press any key to exit.");
            Console.ReadLine();
        }
    }
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army