Discrete Distributions in IronPython QuickStart Sample

Illustrates how to use the classes that represent discrete probability distributions in the Numerics.NET.Statistics.Distributions namespace in IronPython.

View this sample in: C# Visual Basic F#

```Python
import numerics

from System import Array

from Extreme.Statistics import *
from Extreme.Statistics.Distributions import *

# Demonstrates how to use classes that implement
# discrete probabililty distributions.

# 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:
binomial = BinomialDistribution(6, 0.6)

# The distribution's parameters are available through the
# NumberOfTrials and ProbabilityOfSuccess properties:
print "# of trials:          {0}".format(binomial.NumberOfTrials)
print "Prob. of success:     {0:.5f}".format(binomial.ProbabilityOfSuccess)


#
# Basic statistics
#

# The Mean property returns the mean of the distribution:
print "Mean:                 {0:.5f}".format(binomial.Mean)

# The Variance and StandardDeviation are also available:
print "Variance:             {0:.5f}".format(binomial.Variance)
print "Standard deviation:   {0:.5f}".format(binomial.StandardDeviation)

# As are the skewness:
print "Skewness:             {0:.5f}".format(binomial.Skewness)

# The Kurtosis property returns the kurtosis supplement.
# The Kurtosis property for the normal distribution returns zero.
print "Kurtosis:             {0:.5f}".format(binomial.Kurtosis)
print 


#
# Distribution functions
#

# The (cumulative) distribution function (CDF) is implemented by the
# DistributionFunction method:
print "CDF(4) =            {0:.5f}".format(binomial.DistributionFunction(4))

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

#
# Random variates
#

# The Sample method returns a single random variate 
# using the specified random number generator:
rng = Random.MersenneTwister()
x = binomial.Sample(rng)
# The Sample method fills an array or vector with
# random variates. It has several overloads:
xArray = Array.CreateInstance(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:
h = binomial.GetExpectedHistogram(100)
print "Expected distribution of 100 samples:"
for bin in h.Bins:
	print "{0} success(es) -> {1}".format(bin.LowerBound, bin.Value)

```