New Version 8.1!

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

Get from Nuget

QuickStart Samples

Discrete Distributions QuickStart Sample (Visual Basic)

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

C# code F# code IronPython code Back to QuickStart Samples

Option Infer On

Imports Extreme.DataAnalysis
Imports Extreme.Mathematics.Random
Imports Extreme.Statistics.Distributions

Namespace Extreme.Numerics.QuickStart.VB
    ' Demonstrates how to use classes that implement
    ' discrete probabililty distributions.
    Module DiscreteDistributions

        Sub Main()
            ' 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:
            Dim binomial As 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:
            Dim rng As New MersenneTwister
            Dim x As Integer = binomial.Sample(rng)
            ' The Sample method fills an array or vector with
            ' random variates. It has several overloads:
            Dim xArray As Integer() = New Integer(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:
            Dim h = binomial.GetExpectedHistogram(100)
            Console.WriteLine("Expected distribution of 100 samples:")
            For i = 0 To h.Length - 1
                Console.WriteLine("{0} success(es) -> {1}", i, h(i))

            Console.WriteLine("Press Enter key to continue.")
        End Sub

    End Module

End Namespace