/// <summary>
        ///   Creates a OxyPlot's graph for the Hazard Function.
        /// </summary>
        ///
        public PlotModel CreateHF()
        {
            try
            {
                double[] y;
                try { y = supportPoints.Apply(instance.HazardFunction); }
                catch
                {
                    try
                    {
                        var general = GeneralContinuousDistribution
                                      .FromDensityFunction(instance.Support, instance.ProbabilityFunction);
                        y = supportPoints.Apply(general.HazardFunction);
                    }
                    catch
                    {
                        var general = GeneralContinuousDistribution
                                      .FromDistributionFunction(instance.Support, instance.DistributionFunction);
                        y = supportPoints.Apply(general.HazardFunction);
                    }
                }

                return(createBaseModel(range, "HF", supportPoints, y, instance is UnivariateDiscreteDistribution));
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        ///   Creates a OxyPlot's graph for the Quantile Function.
        /// </summary>
        ///
        public PlotModel CreateICDF()
        {
            try
            {
                double[] y;
                try { y = probabilities.Apply(instance.InverseDistributionFunction); }
                catch
                {
                    try
                    {
                        var general = GeneralContinuousDistribution
                                      .FromDensityFunction(instance.Support, instance.ProbabilityFunction);
                        y = probabilities.Apply(general.InverseDistributionFunction);
                    }
                    catch
                    {
                        var general = GeneralContinuousDistribution
                                      .FromDistributionFunction(instance.Support, instance.DistributionFunction);
                        y = probabilities.Apply(general.InverseDistributionFunction);
                    }
                }

                return(createBaseModel(unit, "QDF", probabilities, y, false));
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public void ConstructorTest7()
        {
            var original = new LaplaceDistribution(location: 4, scale: 2);

            var laplace = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testLaplace(laplace);
        }
Exemplo n.º 4
0
        public void ConstructorTest15()
        {
            var original = new NakagamiDistribution(shape: 2.4, spread: 4.2);

            var nakagami = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testNakagami(nakagami);
        }
Exemplo n.º 5
0
        public void ConstructorTest2()
        {
            var original = new NormalDistribution(mean: 4, stdDev: 4.2);

            var normal = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testNormal(normal);
        }
Exemplo n.º 6
0
        public void ConstructorTest12()
        {
            var original = new GompertzDistribution(eta: 4.2, b: 1.1);

            var gompertz = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testGompertz(gompertz);
        }
Exemplo n.º 7
0
        public void ConstructorTest11()
        {
            var original = new ChiSquareDistribution(degreesOfFreedom: 7);

            var chisq = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testChiSquare(chisq);
        }
Exemplo n.º 8
0
        public void ConstructorTest8()
        {
            var original = new LognormalDistribution(location: 0.42, shape: 1.1);

            var log = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testLognormal(log);
        }
Exemplo n.º 9
0
        public void ConstructorTest16()
        {
            var original = new VonMisesDistribution(mean: 0.42, concentration: 1.2);

            var vonMises = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testVonMises(vonMises, 100);
        }
Exemplo n.º 10
0
        public void ConstructorTest3()
        {
            var original = new InverseGaussianDistribution(mean: 0.42, shape: 1.2);

            var invGaussian = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            testInvGaussian(invGaussian);
        }
        /// <summary>
        ///   Creates a OxyPlot's graph for the Complementary Cumulative Distribution Function.
        /// </summary>
        ///
        public PlotModel CreateCCDF()
        {
            double[] y;
            try { y = supportPoints.Apply(instance.ComplementaryDistributionFunction); }
            catch
            {
                var general = GeneralContinuousDistribution
                              .FromDensityFunction(instance.Support, instance.ProbabilityFunction);
                y = supportPoints.Apply(general.ComplementaryDistributionFunction);
            }

            return(createBaseModel(range, "CCDF", supportPoints, y, instance is UnivariateDiscreteDistribution));
        }
Exemplo n.º 12
0
        public void MedianTest2()
        {
            NormalDistribution original = new NormalDistribution(0.4, 2.2);

            var target = GeneralContinuousDistribution.FromDistributionFunction(
                original.Support, original.DistributionFunction);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
            Assert.AreEqual(target.Median, original.Median, 1e-10);

            target = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5), 1e-10);
            Assert.AreEqual(target.Median, original.Median, 1e-10);
        }
Exemplo n.º 13
0
        public void MedianTest()
        {
            var laplace = new LaplaceDistribution(location: 2, scale: 0.42);

            var target = GeneralContinuousDistribution.FromDensityFunction(
                laplace.Support, laplace.ProbabilityDensityFunction);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
            Assert.AreEqual(laplace.Median, target.Median, 1e-10);

            target = GeneralContinuousDistribution.FromDistributionFunction(
                laplace.Support, laplace.DistributionFunction);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5), 1e-10);
            Assert.AreEqual(laplace.Median, target.Median, 1e-10);
        }
Exemplo n.º 14
0
        public void ConstructorTest3()
        {
            var original = new InverseGaussianDistribution(mean: 0.42, shape: 1.2);

            var invGaussian = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = invGaussian.DistributionFunction(i);

                double diff = Math.Abs(expected - actual);
                Assert.AreEqual(expected, actual, 0.1);
            }

            testInvGaussian(invGaussian);
        }
Exemplo n.º 15
0
        public void ConstructorTest2()
        {
            var original = new NormalDistribution(mean: 4, stdDev: 4.2);

            var normal = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = normal.DistributionFunction(i);

                double diff = Math.Abs(expected - actual);
                Assert.AreEqual(expected, actual, 1e-6);
            }

            testNormal(normal, 1);
        }
Exemplo n.º 16
0
        public void ConstructorTest7()
        {
            var original = new LaplaceDistribution(location: 4, scale: 2);

            var laplace = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = laplace.DistributionFunction(i);

                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-5));
                Assert.IsFalse(Double.IsNaN(expected));
                Assert.IsFalse(Double.IsNaN(actual));
            }

            testLaplace(laplace);
        }
Exemplo n.º 17
0
        public void ConstructorTest11()
        {
            var original = new ChiSquareDistribution(degreesOfFreedom: 7);

            var chisq = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = chisq.DistributionFunction(i);

                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-5));
                Assert.IsFalse(Double.IsNaN(actual));
                Assert.IsFalse(Double.IsNaN(expected));
            }

            testChiSquare(chisq);
        }
Exemplo n.º 18
0
        public void ConstructorTest15()
        {
            var original = new NakagamiDistribution(shape: 2.4, spread: 4.2);

            var nakagami = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = nakagami.DistributionFunction(i);

                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-2));
                Assert.IsFalse(double.IsNaN(expected));
                Assert.IsFalse(double.IsNaN(actual));
            }

            testNakagami(nakagami);
        }
Exemplo n.º 19
0
        public void ConstructorTest12()
        {
            var original = new GompertzDistribution(eta: 4.2, b: 1.1);

            var gompertz = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +7; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = gompertz.DistributionFunction(i);

                Assert.IsTrue(expected.IsRelativelyEqual(actual, 1e-3));
                Assert.IsFalse(double.IsNaN(expected));
                Assert.IsFalse(double.IsNaN(actual));
            }

            testGompertz(gompertz);
        }
Exemplo n.º 20
0
        public void ConstructorTest8()
        {
            var original = new LognormalDistribution(location: 0.42, shape: 1.1);

            var log = GeneralContinuousDistribution.FromDensityFunction(
                original.Support, original.ProbabilityDensityFunction);

            for (double i = -10; i < +10; i += 0.1)
            {
                double expected = original.DistributionFunction(i);
                double actual   = log.DistributionFunction(i);

                double diff = Math.Abs(expected - actual);
                Assert.AreEqual(expected, actual, 1e-2);
                Assert.IsFalse(Double.IsNaN(expected));
                Assert.IsFalse(Double.IsNaN(actual));
            }

            testLognormal(log);
        }
Exemplo n.º 21
0
        public void UsageTest()
        {
            // Let's suppose we have a formula that defines a probability distribution
            // but we dont know much else about it. We don't know the form of its cumulative
            // distribution function, for example. We would then like to know more about
            // it, such as the underlying distribution's moments, characteristics, and
            // properties.

            // Let's suppose the formula we have is this one:
            double mu    = 5;
            double sigma = 4.2;

            Func <double, double> df = x => 1.0 / (sigma * Math.Sqrt(2 * Math.PI))
                                       * Math.Exp(-Math.Pow(x - mu, 2) / (2 * sigma * sigma));

            // And for the moment, let's also pretend we don't know it is actually the
            // p.d.f. of a Gaussian distribution with mean 5 and std. deviation of 4.2.

            // So, let's create a distribution based _solely_ on the formula we have:
            var distribution = GeneralContinuousDistribution.FromDensityFunction(df);

            // Now, we can check everything that we can know about it:

            double mean   = distribution.Mean;                                    // 5
            double median = distribution.Median;                                  // 5
            double var    = distribution.Variance;                                // 17.64
            double mode   = distribution.Mode;                                    // 5

            double cdf  = distribution.DistributionFunction(x: 1.4);              // 0.19568296915377595
            double pdf  = distribution.ProbabilityDensityFunction(x: 1.4);        // 0.065784567984404935
            double lpdf = distribution.LogProbabilityDensityFunction(x: 1.4);     // -2.7213699972695058

            double ccdf = distribution.ComplementaryDistributionFunction(x: 1.4); // 0.80431703084622408
            double icdf = distribution.InverseDistributionFunction(p: cdf);       // 1.3999999997024655

            double hf  = distribution.HazardFunction(x: 1.4);                     // 0.081789351041333558
            double chf = distribution.CumulativeHazardFunction(x: 1.4);           // 0.21776177055276186

            Assert.AreEqual(5.0000000000000071, mean, 1e-10);
            Assert.AreEqual(4.9999999999999991, median, 1e-5);
            Assert.AreEqual(4.9999999992474002, mode, 1e-7);
            Assert.AreEqual(17.639999999999958, var, 1e-10);
            Assert.AreEqual(0.21776177055276186, chf, 1e-10);
            Assert.AreEqual(0.19568296915377595, cdf, 1e-10);
            Assert.AreEqual(0.065784567984404935, pdf, 1e-10);
            Assert.AreEqual(-2.7213699972695058, lpdf, 1e-10);
            Assert.AreEqual(0.081789351041333558, hf, 1e-10);
            Assert.AreEqual(0.80431703084622408, ccdf, 1e-10);
            Assert.AreEqual(1.3999999997024655, icdf, 1e-7);

            var range1 = distribution.GetRange(0.95);
            var range2 = distribution.GetRange(0.99);
            var range3 = distribution.GetRange(0.01);

            Assert.AreEqual(-1.9083852331957445, range1.Min);
            Assert.AreEqual(11.908385199564195, range1.Max);
            Assert.AreEqual(-4.7706612258820975, range2.Min);
            Assert.AreEqual(14.770661223067844, range2.Max);
            Assert.AreEqual(-4.7706612258820993, range3.Min);
            Assert.AreEqual(14.770661223067844, range3.Max);
        }