Exemplo n.º 1
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Cauchy_distribution">Cauchy distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Cauchy distribution class with parameters Location = 1 and Scale = 2.
            var cauchy = new Cauchy(1, 2);
            Console.WriteLine(@"1. Initialize the new instance of the Cauchy distribution class with parameters Location = {0} and Scale = {1}", cauchy.Location, cauchy.Scale);
            Console.WriteLine();

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

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

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

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

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

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

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

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

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

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

            Console.WriteLine();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x).
        /// </summary>
        /// <param name="x">The location at which to compute the cumulative distribution function.</param>
        /// <param name="alpha">The stability (α) of the distribution. Range: 2 ≥ α > 0.</param>
        /// <param name="beta">The skewness (β) of the distribution. Range: 1 ≥ β ≥ -1.</param>
        /// <param name="scale">The scale (c) of the distribution. Range: c > 0.</param>
        /// <param name="location">The location (μ) of the distribution.</param>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        /// <seealso cref="CumulativeDistribution"/>
        public static double CDF(double alpha, double beta, double scale, double location, double x)
        {
            if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0)
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            if (alpha == 2d)
            {
                return(Normal.CDF(location, Constants.Sqrt2 * scale, x));
            }
            if (alpha == 1d && beta == 0d)
            {
                return(Cauchy.CDF(location, scale, x));
            }

            if (alpha == 0.5d && beta == 1d)
            {
                return(SpecialFunctions.Erfc(Math.Sqrt(scale / (2 * (x - location)))));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
        /// </summary>
        /// <param name="alpha">The stability (α) of the distribution. Range: 2 ≥ α > 0.</param>
        /// <param name="beta">The skewness (β) of the distribution. Range: 1 ≥ β ≥ -1.</param>
        /// <param name="scale">The scale (c) of the distribution. Range: c > 0.</param>
        /// <param name="location">The location (μ) of the distribution.</param>
        /// <param name="x">The location at which to compute the density.</param>
        /// <returns>the log density at <paramref name="x"/>.</returns>
        /// <seealso cref="DensityLn"/>
        public static double PDFLn(double alpha, double beta, double scale, double location, double x)
        {
            if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0)
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            if (alpha == 2d)
            {
                return(Normal.PDFLn(location, Constants.Sqrt2 * scale, x));
            }
            if (alpha == 1d && beta == 0d)
            {
                return(Cauchy.PDFLn(location, scale, x));
            }

            if (alpha == 0.5d && beta == 1d && x >= location)
            {
                return((Math.Log(scale / Constants.Pi2)) / 2 - scale / (2 * (x - location)) - 1.5 * Math.Log(x - location));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 4
0
 public void CanCreateCauchy()
 {
     var n = new Cauchy();
     Assert.AreEqual(0.0, n.Location);
     Assert.AreEqual(1.0, n.Scale);
 }
Exemplo n.º 5
0
 public void ValidateInverseCumulativeDistribution(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     for (var i = 0; i < 11; i++)
     {
         var x = i - 5.0;
         double expected = (Math.Atan((x - location)/scale))/Math.PI + 0.5;
         Assert.AreEqual(x, n.InverseCumulativeDistribution(expected), 1e-12);
         Assert.AreEqual(x, Cauchy.InvCDF(location, scale, expected), 1e-12);
     }
 }
Exemplo n.º 6
0
 public void CanSampleSequence()
 {
     var n = new Cauchy();
     var ied = n.Samples();
     GC.KeepAlive(ied.Take(5).ToArray());
 }
Exemplo n.º 7
0
 public void CanSample()
 {
     var n = new Cauchy();
     n.Sample();
 }
Exemplo n.º 8
0
 public void ValidateDensityLn(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     for (var i = 0; i < 11; i++)
     {
         var x = i - 5.0;
         double expected = -Math.Log((Constants.Pi * scale) * (1.0 + (((x - location) / scale) * ((x - location) / scale))));
         Assert.AreEqual(expected, n.DensityLn(x));
         Assert.AreEqual(expected, Cauchy.PDFLn(location, scale, x));
     }
 }
Exemplo n.º 9
0
 public void ValidateMaximum(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     Assert.AreEqual(Double.PositiveInfinity, n.Maximum);
 }
Exemplo n.º 10
0
 public void SetBadScaleFail()
 {
     var n = new Cauchy();
     Assert.That(() => n.Scale = -1.0, Throws.ArgumentException);
 }
Exemplo n.º 11
0
 public void ValidateSkewnessThrowsNotSupportedException()
 {
     var n = new Cauchy(-0.0, 2.0);
     Assert.Throws<NotSupportedException>(() => { var s = n.Skewness; });
 }
Exemplo n.º 12
0
 public void ValidateEntropy(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     Assert.AreEqual(Math.Log(4.0 * Constants.Pi * scale), n.Entropy);
 }
Exemplo n.º 13
0
 public void SetBadScaleFail()
 {
     var n = new Cauchy();
     Assert.Throws<ArgumentOutOfRangeException>(() => n.Scale = -1.0);
 }
Exemplo n.º 14
0
 public void SetBadLocationFail()
 {
     var n = new Cauchy();
     Assert.Throws<ArgumentOutOfRangeException>(() => n.Location = Double.NaN);
 }
Exemplo n.º 15
0
 public void CanCreateCauchy(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     Assert.AreEqual(location, n.Location);
     Assert.AreEqual(scale, n.Scale);
 }
Exemplo n.º 16
0
 public void ValidateMedian(double location, double scale)
 {
     var n = new Cauchy(location, scale);
     Assert.AreEqual(location, n.Median);
 }
Exemplo n.º 17
0
 public void ValidateToString()
 {
     var n = new Cauchy(1d, 2d);
     Assert.AreEqual("Cauchy(x0 = 1, γ = 2)", n.ToString());
 }
Exemplo n.º 18
0
 public void SetBadLocationFail()
 {
     var n = new Cauchy();
     Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException);
 }