New Version 5.1!

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

Download now!

QuickStart Samples

Discrete Distributions QuickStart Sample (C#)

Illustrates how to use the classes that represent discrete probability distributions in the Extreme.Statistics.Distributions namespace in C#.

Visual Basic code F# code IronPython 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
	/// discrete probabililty distributions.
	/// </summary>
	class DiscreteDistributions
  /// <summary>
		/// The main entry point for the application.
		/// </summary>
  static void Main(string[] args)
   // This QuickStart Sample demonstrates the capabilities of
   // the classes that implement discrete probability distributions.
   // These classes inherit from the DiscreteDistribution class.
   // For an illustration of classes that implement discrete probability
   // distributions, see the ContinuousDistributions QuickStart Sample.
   // We illustrate the properties and methods of discrete distribution
   // using a binomial distribution. The same properties and methods
   // apply to all other discrete distributions.

   // Constructing distributions

   // Many discrete probability distributions are related to Bernoulli trials,
   // events with a certain probability, p, of success. The number of trials
   // is often one of the distribution's parameters.

   // The binomial distribution has two constructors. Here, we create a
   // binomial distribution for 6 trials with a probability of success of 0.6:
   BinomialDistribution binomial = new BinomialDistribution(6, 0.6);

   // The distribution's parameters are available through the
   // NumberOfTrials and ProbabilityOfSuccess properties:
   Console.WriteLine("# of trials:          {0:F5}", binomial.NumberOfTrials);
   Console.WriteLine("Prob. of success:     {0:F5}", binomial.ProbabilityOfSuccess);

   // Basic statistics

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

   // The Variance and StandardDeviation are also available:
   Console.WriteLine("Variance:             {0:F5}", binomial.Variance);
   Console.WriteLine("Standard deviation:   {0:F5}", binomial.StandardDeviation);

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

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

   // Distribution functions

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

   // The probability density function (PDF) is available as the 
   // Probability method:
   Console.WriteLine("PDF(4) =            {0:F5}", binomial.Probability(4));
   // The Probability method has an overload that returns the probability
   // that a variate lies between two values:
   Console.WriteLine("Probability(3, 5) = {0:F5}", binomial.Probability(3, 5));

   // Random variates

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

   // The GetExpectedHistogram method returns a Histogram that contains the
   // expected number of samples in each bin:
   Histogram h = binomial.GetExpectedHistogram(100);
   Console.WriteLine("Expected distribution of 100 samples:");
   foreach(HistogramBin bin in h.Bins)
    Console.WriteLine("{0} success(es) -> {1}", bin.LowerBound, bin.Value);

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