Exemplo n.º 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];
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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>
        ///
        /// <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);
        }
Exemplo n.º 5
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>
        ///
        /// <returns>A random double value sampled from the specified Beta distribution.</returns>
        ///
        public static double Random(double alpha, double beta, double min, double max)
        {
            double r = BetaDistribution.Random(alpha, beta);

            return(r * (max - min) + min);
        }