C# Probability Distribution Example

← All NMath Code Examples

 

using System;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use the probability distribution classes.
  /// </summary>
  /// <remarks>
  /// NMath Stats provides classes for computing the probability density
  /// function (PDF), the cumulative distribution function (CDF), the inverse
  /// cumulative distribution function, and random variable moments for a
  /// variety of probability distributions, including normal (Gaussian), Poisson,
  /// chi-square, gamma, beta, Students t, F, binomial, and negative binomial.
  /// </remarks>
  class ProbabilityDistributionExample
  {

    static void Main( string[] args )
    {
      // The distribution classes share a common interface, so once you
      // learn how to use one distribution class, it’s easy to use any of
      // the others. This code constructs an F distribution object with
      // degrees of freedom 9,24.
      int df1 = 9;
      int df2 = 24;
      var dist = new FDistribution( df1, df2 );

      Console.WriteLine();
      Console.WriteLine( dist );
      Console.WriteLine();

      // Pick a sample value for the F statistic.
      double fstat = 3.94;
      Console.WriteLine( "Sample F statistic = {0}", fstat );
      Console.WriteLine();

      // The PDF() method computes the probability density function
      // evaluated at a given value. The probability of
      // observing an F-statistic of 3.94 is given by:
      Console.WriteLine( "PDF = {0}", dist.PDF( fstat ) );

      // The CDF() method computes the cumulative density function
      // evaluated at a given value. Find the probability of
      // observing an F-statistic of 3.94 or less, and the probability
      // of observing an F-statistic greater than 3.94:
      Console.WriteLine( "CDF = {0}", dist.CDF( fstat ) );
      Console.WriteLine( "Upper tail probability = {0}", 1 - dist.CDF( fstat ) );
      Console.WriteLine();

      // The InverseCDF() computes the inverse cumulative density
      // function evaluated at a given value. Calculate left and right
      // critical values for 0.01 alpha level:
      double alpha = 0.01;
      Console.WriteLine( "Alpha = {0}", alpha );
      Console.WriteLine( "Left critical value = {0}", dist.InverseCDF( alpha ) );
      Console.WriteLine( "Right critical value = {0}", dist.InverseCDF( 1 - alpha ) );
      Console.WriteLine();

      // Properties are provided for getting the first four moments of a
      // distribution.
      Console.WriteLine( "Mean of distribution = {0}", dist.Mean );
      Console.WriteLine( "Variance of distribution = {0}", dist.Variance );
      Console.WriteLine( "Skewness of distribution = {0}", dist.Skewness );
      Console.WriteLine( "Kurtosis of distribution = {0}", dist.Kurtosis );
      Console.WriteLine();

      Console.WriteLine();
      Console.WriteLine( "Press Enter Key" );
      Console.Read();

    }  // Main

  }  // class

}  // namespace

← All NMath Code Examples
Top