Continuous Distributions in IronPython QuickStart Sample

Illustrates how to use the classes that represent continuous probability distributions in the Extreme.Statistics.Distributions namespace in IronPython.

View this sample in: C# Visual Basic F#

```Python
import numerics

from System import Array

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

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

# 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 distribution
# 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.
weibull = WeibullDistribution(3, 2, 3)

#
# Basic statistics
#

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

# The Variance and StandardDeviation are also available:
print "Variance:             {0:.5f}".format(weibull.Variance)
print "Standard deviation:   {0:.5f}".format(weibull.StandardDeviation)
# The inter-quartile range is another measure of scale:
print "Inter-quartile range: {0:.5f}".format(weibull.InterQuartileRange)

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

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

#
# Distribution functions
#

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

# Its complement is the survivor function:
print "SDF(4.5) =            {0:.5f}".format(weibull.SurvivorDistributionFunction(4.5))

# While its inverse is given by the InverseDistributionFunction method:
print "Inverse CDF(0.4) =    {0:.5f}".format(weibull.InverseDistributionFunction(0.4))

# The probability density function (PDF) is also available:
print "PDF(4.5) =            {0:.5f}".format(weibull.ProbabilityDensityFunction(4.5))
			
# The Probability method returns the probability that a variate lies between two values:
print "Probability(4.5, 5.5) = {0:.5f}".format(weibull.Probability(4.5, 5.5))
print 

#
# Random variates
#

# The Sample method returns a single random variate 
# using the specified random number generator:
rng = Random.MersenneTwister()
x = weibull.Sample(rng)
# The Sample method fills an array or vector with
# random variates. It has several overloads:
xVector = Vector.Create(100)
# 1. Fill all values:
weibull.Sample(rng, xVector)
# 2. Fill only a range (start index and length are supplied)
weibull.Sample(rng, xVector, 20, 50)
# The same two options are available with a DenseVector
# 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:
h = weibull.GetExpectedHistogram(3.0, 10.0, 5, 100)
print "Expected distribution of 100 samples:"
for bin in h.Bins:
	print "Between {0} and {1} -> {2}".format(bin.LowerBound, bin.UpperBound, bin.Value)
print 

# or by supplying an array of boundaries:
h = weibull.GetExpectedHistogram(Array[float]([ 3.0, 5.2, 7.4, 9.6, 11.8 ]), 100)
print "Expected distribution of 100 samples:"
for bin in h.Bins:
	print "Between {0} and {1} -> {2}".format(bin.LowerBound, bin.UpperBound, bin.Value)
```