Exemple #1
1
        /// <summary>
        /// Run example
        /// </summary>
        public void Run()
        {
            // 1. Get 10 random samples of f(x) = (x * x) / 2 using continuous uniform distribution on [-10, 10]
            var uniform = new ContinuousUniform(-10, 10);
            var result = Generate.RandomMap(10, uniform, Function);
            Console.WriteLine(@" 1. Get 10 random samples of f(x) = (x * x) / 2 using continuous uniform distribution on [-10, 10]");
            for (var i = 0; i < result.Length; i++)
            {
                Console.Write(result[i].ToString("N") + @" ");
            }

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

            // 2. Get 10 random samples of f(x) = (x * x) / 2 using Exponential(1) distribution and retrieve sample points
            var exponential = new Exponential(1);
            double[] samplePoints = Generate.Random(10, exponential);
            result = Generate.Map(samplePoints, Function);
            Console.WriteLine(@"2. Get 10 random samples of f(x) = (x * x) / 2 using Exponential(1) distribution and retrieve sample points");
            Console.Write(@"Points: ");
            for (var i = 0; i < samplePoints.Length; i++)
            {
                Console.Write(samplePoints[i].ToString("N") + @" ");
            }

            Console.WriteLine();
            Console.Write(@"Values: ");
            for (var i = 0; i < result.Length; i++)
            {
                Console.Write(result[i].ToString("N") + @" ");
            }

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

            // 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution
            var chiSquare = new ChiSquared(10);
            result = Generate.RandomMap2(10, chiSquare, TwoDomainFunction);
            Console.WriteLine(@" 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution");
            for (var i = 0; i < result.Length; i++)
            {
                Console.Write(result[i].ToString("N") + @" ");
            }

            Console.WriteLine();
        }
 public void CanSample()
 {
     var n = new Exponential(1.0);
     n.Sample();
 }
 public void ValidateDensityLn(double lambda, double x)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(Math.Log(lambda) - (lambda*x), n.DensityLn(x));
     Assert.AreEqual(Math.Log(lambda) - (lambda*x), Exponential.PDFLn(lambda, x));
 }
 public void ValidateDensity(double lambda, double x)
 {
     var n = new Exponential(lambda);
     if (x >= 0)
     {
         Assert.AreEqual(lambda*Math.Exp(-lambda*x), n.Density(x));
         Assert.AreEqual(lambda*Math.Exp(-lambda*x), Exponential.PDF(lambda, x));
     }
     else
     {
         Assert.AreEqual(0.0, n.Density(lambda));
         Assert.AreEqual(0.0, Exponential.PDF(lambda, lambda));
     }
 }
 public void ValidateMaximum()
 {
     var n = new Exponential(1.0);
     Assert.AreEqual(Double.PositiveInfinity, n.Maximum);
 }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Exponential_distribution">Exponential distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Exponential distribution class with parameter Lambda = 1.
            var exponential = new Exponential(1);
            Console.WriteLine(@"1. Initialize the new instance of the Exponential distribution class with parameter Lambda = {0}", exponential.Lambda);
            Console.WriteLine();

            // 2. Distributuion properties:
            Console.WriteLine(@"2. {0} distributuion properties:", exponential);

            // Cumulative distribution function
            Console.WriteLine(@"{0} - Сumulative distribution at location '0.3'", exponential.CumulativeDistribution(0.3).ToString(" #0.00000;-#0.00000"));

            // Probability density
            Console.WriteLine(@"{0} - Probability density at location '0.3'", exponential.Density(0.3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability density at location '0.3'", exponential.DensityLn(0.3).ToString(" #0.00000;-#0.00000"));

            // Entropy
            Console.WriteLine(@"{0} - Entropy", exponential.Entropy.ToString(" #0.00000;-#0.00000"));

            // Largest element in the domain
            Console.WriteLine(@"{0} - Largest element in the domain", exponential.Maximum.ToString(" #0.00000;-#0.00000"));

            // Smallest element in the domain
            Console.WriteLine(@"{0} - Smallest element in the domain", exponential.Minimum.ToString(" #0.00000;-#0.00000"));

            // Mean
            Console.WriteLine(@"{0} - Mean", exponential.Mean.ToString(" #0.00000;-#0.00000"));

            // Median
            Console.WriteLine(@"{0} - Median", exponential.Median.ToString(" #0.00000;-#0.00000"));

            // Mode
            Console.WriteLine(@"{0} - Mode", exponential.Mode.ToString(" #0.00000;-#0.00000"));

            // Variance
            Console.WriteLine(@"{0} - Variance", exponential.Variance.ToString(" #0.00000;-#0.00000"));

            // Standard deviation
            Console.WriteLine(@"{0} - Standard deviation", exponential.StdDev.ToString(" #0.00000;-#0.00000"));

            // Skewness
            Console.WriteLine(@"{0} - Skewness", exponential.Skewness.ToString(" #0.00000;-#0.00000"));
            Console.WriteLine();

            // 3. Generate 10 samples of the Exponential distribution
            Console.WriteLine(@"3. Generate 10 samples of the Exponential distribution");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(exponential.Sample().ToString("N05") + @" ");
            }

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

            // 4. Generate 100000 samples of the Exponential(1) distribution and display histogram
            Console.WriteLine(@"4. Generate 100000 samples of the Exponential(1) distribution and display histogram");
            var data = new double[100000];
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = exponential.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 5. Generate 100000 samples of the Exponential(9) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the Exponential(9) distribution and display histogram");
            exponential.Lambda = 9;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = exponential.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 6. Generate 100000 samples of the Exponential(0.01) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the Exponential(0.01) distribution and display histogram");
            exponential.Lambda = 0.01;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = exponential.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
 public void ValidateToString()
 {
     var n = new Exponential(2d);
     Assert.AreEqual("Exponential(λ = 2)", n.ToString());
 }
 public void ValidateInverseCumulativeDistribution(double lambda, double x)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(x, n.InverseCumulativeDistribution(1.0 - Math.Exp(-lambda*x)));
     Assert.AreEqual(x, Exponential.InvCDF(lambda, 1.0 - Math.Exp(-lambda*x)));
 }
 public void ValidateSkewness(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(2.0, n.Skewness);
 }
 public void ValidateEntropy(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(1.0 - Math.Log(lambda), n.Entropy);
 }
 public void ValidateStdDev(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(1.0 / lambda, n.StdDev);
 }
 public void ValidateVariance(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(1.0 / (lambda * lambda), n.Variance);
 }
 public void SetLambdaFailsWithNegativeLambda()
 {
     var n = new Exponential(1.0);
     Assert.Throws<ArgumentOutOfRangeException>(() => n.Rate = -1.0);
 }
 public void CanSampleSequence()
 {
     var n = new Exponential(1.0);
     var ied = n.Samples();
     GC.KeepAlive(ied.Take(5).ToArray());
 }
 public void ValidateMode(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(0.0, n.Mode);
 }
 public void ValidateCumulativeDistribution(double lambda, double x)
 {
     var n = new Exponential(lambda);
     if (x >= 0.0)
     {
         Assert.AreEqual(1.0 - Math.Exp(-lambda*x), n.CumulativeDistribution(x));
         Assert.AreEqual(1.0 - Math.Exp(-lambda*x), Exponential.CDF(lambda, x));
     }
     else
     {
         Assert.AreEqual(0.0, n.CumulativeDistribution(x));
         Assert.AreEqual(0.0, Exponential.CDF(lambda, x));
     }
 }
 public void ValidateMedian(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(Math.Log(2.0) / lambda, n.Median);
 }
 public void CanCreateExponential(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(lambda, n.Rate);
 }
 public void ValidateMinimum()
 {
     var n = new Exponential(1.0);
     Assert.AreEqual(0.0, n.Minimum);
 }
 public void ValidateMean(double lambda)
 {
     var n = new Exponential(lambda);
     Assert.AreEqual(1.0 / lambda, n.Mean);
 }
 public void SetLambdaFailsWithNegativeLambda()
 {
     var n = new Exponential(1.0);
     Assert.That(() => n.Rate = -1.0, Throws.ArgumentException);
 }