Extreme Optimization > QuickStart Samples > Continuous Distributions QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Continuous Distributions QuickStart Sample (C#)

Illustrates how to use the classes that implement continuous probability distributions (Extreme.Statistics.Distributions namespace) in C#.

VB.NET code F# code Back to QuickStart Samples

using System;

using Extreme.Statistics;
using Extreme.Statistics.Distributions;

namespace Extreme.Statistics.Quickstart.CSharp
{
    /// <summary>
    /// Demonstrates how to use classes that implement
    /// continuous probabililty distributions.
    /// </summary>
    class ContinuousDistributions
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // This QuickStart Sample demonstrates the capabilities of
            // the classes that implement continuous probability distributions.
            // These classes inherit from the ContinuousDistribution class.
            //
            // For an illustration of classes that implement discrete 
            // probability distributions, see the DiscreteDistributions 
            // QuickStart Sample.
            // 
            // We illustrate the properties and methods of continuous 
            // distributions using a Weibull distribution. The same properties 
            // and methods apply to all other continuous distributions.

            // 
            // Constructing distributions
            //

            // Most distributions have one or more parameters with
            // different definitions.
            //
            // The location parameter is always related to the mean of
            // the distribution. When omitted, its default value is zero.
            //
            // The scale parameter is always directly related to the standard
            // deviation. A larger scale parameter means that the distribution
            // is wider. When omitted, its default value is one.

            // The Weibull distribution has three constructors. 
            // The most complete constructor takes a location, scale, 
            // and shape parameter.
            WeibullDistribution weibull = new WeibullDistribution(3, 2, 3);

            //
            // Basic statistics
            //

            // The Mean property returns the mean of the distribution:
            Console.WriteLine("Mean:                 {0:F5}",
                weibull.Mean);

            // The Variance and StandardDeviation are also available:
            Console.WriteLine("Variance:             {0:F5}",
                weibull.Variance);
            Console.WriteLine("Standard deviation:   {0:F5}",
                weibull.StandardDeviation);
            // The inter-quartile range is another measure of scale:
            Console.WriteLine("Inter-quartile range: {0:F5}",
                weibull.InterQuartileRange);

            // As are the skewness:
            Console.WriteLine("Skewness:             {0:F5}",
                weibull.Skewness);

            // The Kurtosis property returns the kurtosis supplement.
            // The Kurtosis property for the normal distribution returns zero.
            Console.WriteLine("Kurtosis:             {0:F5}",
                weibull.Kurtosis);
            Console.WriteLine();

            //
            // Distribution functions
            //

            // The (cumulative) distribution function (CDF) is implemented
            // by the DistributionFunction method:
            Console.WriteLine("CDF(4.5) =            {0:F5}",
                weibull.DistributionFunction(4.5));

            // Its complement is the survivor function:
            Console.WriteLine("SDF(4.5) =            {0:F5}",
                weibull.SurvivorDistributionFunction(4.5));

            // While its inverse is given by the InverseDistributionFunction 
            // method:
            Console.WriteLine("Inverse CDF(0.4) =    {0:F5}",
                weibull.InverseDistributionFunction(0.4));

            // The probability density function (PDF) is also available:
            Console.WriteLine("PDF(4.5) =            {0:F5}",
                weibull.ProbabilityDensityFunction(4.5));
            
            // The Probability method returns the probability that 
            // a variate lies between two values:
            Console.WriteLine("Probability(4.5, 5.5) = {0:F5}",
                weibull.Probability(4.5, 5.5));
            Console.WriteLine();

            //
            // Random variates
            //

            // The GetRandomVariate method returns a single random variate 
            // using the specified random number generator:
            System.Random rng = new Random.MersenneTwister();
            double x = weibull.GetRandomVariate(rng);
            // The GetRandomVariates method fills an array or vector with
            // random variates. It has several overloads:
            double[] xArray = new double[100];
            // 1. Fill all values:
            weibull.GetRandomVariates(rng, xArray);
            // 2. Fill only a range (start index and length are supplied)
            weibull.GetRandomVariates(rng, xArray, 20, 50);
            // The same two options are available with a GeneralVector
            // instead of a double array.

            // The GetExpectedHistogram method returns a Histogram that
            // contains the expected number of samples in each bin, 
            // given the total number of samples. The bins are specified by 
            // lower and upper bounds and number of bins:
            Histogram h = weibull.GetExpectedHistogram(3.0, 10.0, 5, 100);
            Console.WriteLine("Expected distribution of 100 samples:");
            foreach(HistogramBin bin in h.Bins)
                Console.WriteLine("Between {0} and {1} -> {2}", 
                    bin.LowerBound, bin.UpperBound, bin.Value);
            Console.WriteLine();

            // or by supplying an array of boundaries:
            h = weibull.GetExpectedHistogram(
                new double[] {3.0, 5.2, 7.4, 9.6, 11.8}, 100);
            Console.WriteLine("Expected distribution of 100 samples:");
            foreach(HistogramBin bin in h.Bins)
                Console.WriteLine("Between {0} and {1} -> {2}", 
                    bin.LowerBound, bin.UpperBound, bin.Value);

            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