示例#1
0
        private void fitMLE(double sum1, double sum2, double n)
        {
            double[] gradient = new double[2];

            var bfgs = new BoundedBroydenFletcherGoldfarbShanno(numberOfVariables: 2);

            bfgs.LowerBounds[0] = 1e-100;
            bfgs.LowerBounds[1] = 1e-100;
            bfgs.Solution[0]    = this.alpha;
            bfgs.Solution[1]    = this.beta;

            bfgs.Function = (double[] parameters) =>
                            BetaDistribution.LogLikelihood(sum1, sum2, n, parameters[0], parameters[1]);

            bfgs.Gradient = (double[] parameters) =>
                            BetaDistribution.Gradient(sum1, sum2, n, parameters[0], parameters[1], gradient);

            if (!bfgs.Minimize())
            {
                throw new ConvergenceException();
            }

            this.alpha = bfgs.Solution[0];
            this.beta  = bfgs.Solution[1];
        }
示例#2
0
        /// <summary>
        ///   Estimates a new Beta distribution from a set of weighted observations.
        /// </summary>
        ///
        public static BetaDistribution Estimate(double[] samples, double[] weights)
        {
            var beta = new BetaDistribution(1, 1);

            beta.Fit(samples, weights, null);
            return(beta);
        }
示例#3
0
        /// <summary>
        ///   Estimates a new Beta distribution from a set of observations.
        /// </summary>
        ///
        public static BetaDistribution Estimate(double[] samples, BetaOptions options)
        {
            var beta = new BetaDistribution(1, 1);

            beta.Fit(samples, (double[])null, options);
            return(beta);
        }
        public void BetaDistributionConstructorTest()
        {
            double expected, actual;

            {
                BetaDistribution target = new BetaDistribution(1.73, 4.2);
                actual = target.ProbabilityDensityFunction(-1);
                expected = 0;
                Assert.AreEqual(expected, actual, 1e-7);
                Assert.IsFalse(Double.IsNaN(actual));

                actual = target.ProbabilityDensityFunction(0);
                expected = 0;
                Assert.AreEqual(expected, actual, 1e-7);
                Assert.IsFalse(Double.IsNaN(actual));

                actual = target.ProbabilityDensityFunction(1);
                expected = 0;
                Assert.AreEqual(expected, actual, 1e-7);
                Assert.IsFalse(Double.IsNaN(actual));

                actual = target.ProbabilityDensityFunction(0.2);
                expected = 2.27095841;
                Assert.AreEqual(expected, actual, 1e-7);
                Assert.IsFalse(Double.IsNaN(actual));

                actual = target.ProbabilityDensityFunction(0.4);
                expected = 1.50022749;
                Assert.AreEqual(expected, actual, 1e-7);
                Assert.IsFalse(Double.IsNaN(actual));
            }
        }
        /// <summary>
        ///   Generates a random vector of observations from the
        ///   Beta distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="alpha">The shape parameter α (alpha).</param>
        /// <param name="beta">The shape parameter β (beta).</param>
        /// <param name="min">The minimum possible value a.</param>
        /// <param name="max">The maximum possible value b.</param>
        /// <param name="samples">The number of samples to generate.</param>
        ///
        /// <returns>An array of double values sampled from the specified Beta distribution.</returns>
        ///
        public static double[] Random(double alpha, double beta, double min, double max, int samples)
        {
            double[] r = BetaDistribution.Random(alpha, beta, samples);

            if (min != 0 || max != 1)
            {
                for (int i = 0; i < r.Length; i++)
                {
                    r[i] = r[i] * (max - min) + min;
                }
            }

            return(r);
        }
示例#6
0
        /// <summary>
        ///   Generates a random vector of observations from the
        ///   Beta distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="alpha">The shape parameter α (alpha).</param>
        /// <param name="beta">The shape parameter β (beta).</param>
        /// <param name="min">The minimum possible value a.</param>
        /// <param name="max">The maximum possible value b.</param>
        /// <param name="samples">The number of samples to generate.</param>
        /// <param name="result">The location where to store the samples.</param>
        /// <param name="source">The random number generator to use as a source of randomness.
        ///   Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param>
        ///
        /// <returns>An array of double values sampled from the specified Beta distribution.</returns>
        ///
        public static double[] Random(double alpha, double beta, double min, double max, int samples, double[] result, Random source)
        {
            BetaDistribution.Random(alpha, beta, samples, result, source);

            if (min != 0 || max != 1)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = result[i] * (max - min) + min;
                }
            }

            return(result);
        }
        public void BetaFitTest1()
        {
            double[] x = { 0.1, 0.5, 0.3, 0.8, 0.6, 0.7, 0.9, 0.9, 0.9, 0.9 };

            BetaDistribution target = new BetaDistribution(0, 1);

            target.Fit(x);

            Assert.AreEqual(1.1810718232044195, target.Alpha);
            Assert.AreEqual(0.60843093922651903, target.Beta);
        }
        public void GradientTest()
        {
            for (double a = 0.1; a < 3; a += 0.1)
            {
                for (double b = 0.1; b < 3; b += 0.1)
                {
                    var target = new BetaDistribution(a, b);

                    Assert.AreEqual(a, target.Alpha);
                    Assert.AreEqual(b, target.Beta);

                    FiniteDifferences fd = new FiniteDifferences(2);
                    fd.Function = (double[] parameters) => BetaDistribution.LogLikelihood(samples, parameters[0], parameters[1]);

                    double[] expected = fd.Compute(a, b);
                    double[] actual = BetaDistribution.Gradient(samples, a, b);

                    Assert.IsTrue(expected[0].IsRelativelyEqual(actual[0], 0.05));
                    Assert.IsTrue(expected[1].IsRelativelyEqual(actual[1], 0.05));
                }
            }
        }
        public void LogLikelihoodTest()
        {
            var target = new BetaDistribution(3.0, 2.0);

            double sum = 0;
            for (int i = 0; i < samples.Length; i++)
                sum -= target.LogProbabilityDensityFunction(samples[i]);

            double expected = sum;
            double actual = BetaDistribution.LogLikelihood(samples, target.Alpha, target.Beta);

            Assert.AreEqual(expected, actual, 1e-10);
        }
        public void BetaMLEFit_IntWeights()
        {
            double[] x = { 1.0, 0.1, 0.5, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.9, 0.9 };
            int[] w = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2 };

            BetaDistribution target = new BetaDistribution(0, 1);


            var options = new BetaOptions()
            {
                Method = BetaEstimationMethod.MaximumLikelihood
            };

            target.Fit(x, w, options);

            Assert.AreEqual(1.1810718232044195, target.Alpha);
            Assert.AreEqual(0.60843093922651903, target.Beta, 1e-10);
        }
        public void BetaMLEFit_RealWeights()
        {
            double[] x = { 1.0, 0.1, 0.5, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.9, 0.9 };
            int[] w = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2 };

            BetaDistribution target = new BetaDistribution(0, 1);


            var options = new BetaOptions()
            {
                Method = BetaEstimationMethod.MaximumLikelihood
            };

            target.Fit(x, w.ToDouble(), options);

            Assert.AreEqual(1.1401591160220996, target.Alpha);
            Assert.AreEqual(0.58735469613259694, target.Beta);
        }
        public void BetaMLEFitTest1()
        {
            double[] x = samples;

            {
                BetaDistribution target = new BetaDistribution(0, 1);
                var options = new BetaOptions() { Method = BetaEstimationMethod.Moments };
                target.Fit(x, options);

                Assert.AreEqual(1, target.Alpha, 0.04);
                Assert.AreEqual(3, target.Beta, 0.5);
            }

            {
                BetaDistribution target = new BetaDistribution(0, 1);
                var options = new BetaOptions() { Method = BetaEstimationMethod.MaximumLikelihood };
                target.Fit(x, options);

                Assert.AreEqual(1, target.Alpha, 0.04);
                Assert.AreEqual(3, target.Beta, 0.55);
            }
        }
        public void BetaFit_IntWeights()
        {
            double[] x = { 1.0, 0.1, 0.5, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.9, 0.9 };
            int[] w = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2 };

            BetaDistribution target = new BetaDistribution(0, 1);

            target.Fit(x, w);

            Assert.AreEqual(1.1810718232044195, target.Alpha);
            Assert.AreEqual(0.60843093922651903, target.Beta, 1e-8);
        }
        public void MedianTest()
        {
            double alpha = 0.42;
            double beta = 1.57;

            BetaDistribution target = new BetaDistribution(alpha, beta);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
        public void BetaMeanTest3()
        {
            int trials = 100;
            int successes = 78;

            BetaDistribution betaDistribution = new BetaDistribution(successes, trials);

            double mean = betaDistribution.Mean; // 0.77450980392156865
            double median = betaDistribution.Median; // 0.77630912598534851
            double p025 = betaDistribution.InverseDistributionFunction(p: 0.025); // 0.68899653915764347
            double p975 = betaDistribution.InverseDistributionFunction(p: 0.975); // 0.84983461640764513

            double orig025 = betaDistribution.DistributionFunction(p025);
            double orig975 = betaDistribution.DistributionFunction(p975);

            Assert.AreEqual(0.025, orig025, 1e-8);
            Assert.AreEqual(0.975, orig975, 1e-8);
            Assert.AreEqual(0.77450980392156865, mean, 1e-9);
            Assert.AreEqual(0.7763091275412235, median, 1e-9);
            Assert.AreEqual(0.68899667463246894, p025, 1e-6);
            Assert.AreEqual(0.84983461640764513, p975, 1e-6);
        }
        public void BetaMeanTest2()
        {
            int trials = 161750;
            int successes = 10007;

            BetaDistribution betaDistribution = new BetaDistribution(successes, trials);

            double mean = betaDistribution.Mean; // 0.06187249616697166
            double median = betaDistribution.Median; // 0.06187069085946604
            double p025 = betaDistribution.InverseDistributionFunction(p: 0.025); // 0.06070354581334864
            double p975 = betaDistribution.InverseDistributionFunction(p: 0.975); // 0.0630517079399996

            string str = betaDistribution.ToString();

            Assert.AreEqual(trials, betaDistribution.Trials);
            Assert.AreEqual(successes, betaDistribution.Successes);


            Assert.AreEqual(0.06187249616697166, mean);
            Assert.AreEqual(0.06187069085946604, median, 1e-6);
            Assert.AreEqual(0.06070354581334864, p025, 1e-6);
            Assert.AreEqual(0.0630517079399996, p975, 1e-6);
            Assert.AreEqual("B(x; α = 10008, β = 151744)", str);
        }
示例#17
0
        public void BetaMeanTest()
        {
            double alpha = 0.42;
            double beta = 1.57;

            BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);

            double mean = betaDistribution.Mean; // 0.21105527638190955
            double median = betaDistribution.Median; // 0.11577706212908731
            double var = betaDistribution.Variance; // 0.055689279830523512
            double chf = betaDistribution.CumulativeHazardFunction(x: 0.27); // 1.1828193992944409
            double cdf = betaDistribution.DistributionFunction(x: 0.27); // 0.69358638272337991
            double pdf = betaDistribution.ProbabilityDensityFunction(x: 0.27); // 0.94644031936694828
            double lpdf = betaDistribution.LogProbabilityDensityFunction(x: 0.27); // -0.055047364344046057
            double hf = betaDistribution.HazardFunction(x: 0.27); // 3.0887671630877072
            double ccdf = betaDistribution.ComplementaryDistributionFunction(x: 0.27); // 0.30641361727662009
            double icdf = betaDistribution.InverseDistributionFunction(p: cdf); // 0.26999999068687469

            string str = betaDistribution.ToString(System.Globalization.CultureInfo.InvariantCulture); // "B(x; α = 0.42, β = 1.57)

            Assert.AreEqual(0.21105527638190955, mean);
            Assert.AreEqual(0.11577706212908731, median);
            Assert.AreEqual(0.055689279830523512, var);
            Assert.AreEqual(1.1828193992944409, chf);
            Assert.AreEqual(0.69358638272337991, cdf);
            Assert.AreEqual(0.94644031936694828, pdf);
            Assert.AreEqual(-0.055047364344046057, lpdf);
            Assert.AreEqual(3.0887671630877072, hf);
            Assert.AreEqual(0.30641361727662009, ccdf);
            Assert.AreEqual(0.27, icdf, 1e-10);
            Assert.AreEqual("B(x; α = 0.42, β = 1.57)", str);
        }
示例#18
0
        /// <summary>
        ///   Generates a random observation from a
        ///   Beta distribution with the given parameters.
        /// </summary>
        ///
        /// <param name="alpha">The shape parameter α (alpha).</param>
        /// <param name="beta">The shape parameter β (beta).</param>
        /// <param name="min">The minimum possible value a.</param>
        /// <param name="max">The maximum possible value b.</param>
        /// <param name="source">The random number generator to use as a source of randomness.
        ///   Default is to use <see cref="Accord.Math.Random.Generator.Random"/>.</param>
        ///
        /// <returns>A random double value sampled from the specified Beta distribution.</returns>
        ///
        public static double Random(double alpha, double beta, double min, double max, Random source)
        {
            double r = BetaDistribution.Random(alpha, beta, source);

            return(r * (max - min) + min);
        }
        public void BetaMeanTest()
        {
            double alpha = 0.42;
            double beta = 1.57;

            BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);

            double mean = betaDistribution.Mean; // 0.21105527638190955
            double median = betaDistribution.Median; // 0.11577706212908731
            double var = betaDistribution.Variance; // 0.055689279830523512
            double mode = betaDistribution.Mode;    // 57.999999999999957

            double chf = betaDistribution.CumulativeHazardFunction(x: 0.27); // 1.1828193992944409
            double cdf = betaDistribution.DistributionFunction(x: 0.27); // 0.69358638272337991
            double pdf = betaDistribution.ProbabilityDensityFunction(x: 0.27); // 0.94644031936694828
            double lpdf = betaDistribution.LogProbabilityDensityFunction(x: 0.27); // -0.055047364344046057
            double hf = betaDistribution.HazardFunction(x: 0.27); // 3.0887671630877072
            double ccdf = betaDistribution.ComplementaryDistributionFunction(x: 0.27); // 0.30641361727662009
            double icdf = betaDistribution.InverseDistributionFunction(p: cdf); // 0.26999999068687469

            string str = betaDistribution.ToString(System.Globalization.CultureInfo.InvariantCulture); // "B(x; α = 0.42, β = 1.57)

            Assert.AreEqual(0.21105527638190955, mean);
            Assert.AreEqual(0.11577706212908731, median);
            Assert.AreEqual(57.999999999999957, mode);
            Assert.AreEqual(0.055689279830523512, var);
            Assert.AreEqual(1.1828193992944409, chf);
            Assert.AreEqual(0.69358638272337991, cdf);
            Assert.AreEqual(0.94644031936694828, pdf);
            Assert.AreEqual(-0.055047364344046057, lpdf);
            Assert.AreEqual(3.0887671630877072, hf);
            Assert.AreEqual(0.30641361727662009, ccdf);
            Assert.AreEqual(0.27, icdf, 1e-10);
            Assert.AreEqual("B(x; α = 0.42, β = 1.57)", str);

            Assert.IsFalse(Double.IsNaN(median));

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

            Assert.AreEqual(0.00045925525776717733, range1.Min);
            Assert.AreEqual(0.72381020663218609, range1.Max);
            Assert.AreEqual(0.0000099485893745082635, range2.Min);
            Assert.AreEqual(0.89625688707910811, range2.Max);
            Assert.AreEqual(0.0000099485893745082432, range3.Min);
            Assert.AreEqual(0.89625688707910811, range3.Max);
        }
        public void BetaFit_RealWeights()
        {
            double[] x = { 1.0, 0.1, 0.5, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.9, 0.9 };
            int[] w = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2 };

            BetaDistribution target = new BetaDistribution(0, 1);

            target.Fit(x, w.ToDouble());

            Assert.AreEqual(1.1401591160220996, target.Alpha);
            Assert.AreEqual(0.58735469613259694, target.Beta);
        }