示例#1
0
        /// Returns the regularized beta function I(x, a, b).
        ///
        /// The implementation of this method is based on:
        /// <ul>
        /// <li>
        /// <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
        /// Regularized Beta Function</a>.</li>
        /// <li>
        /// <a href="http://functions.wolfram.com/06.21.10.0001.01">
        /// Regularized Beta Function</a>.</li>
        /// </ul>
        ///
        /// @param x the value.
        /// @param a Parameter {@code a}.
        /// @param b Parameter {@code b}.
        /// @param epsilon When the absolute value of the nth item in the
        /// series is less than epsilon the approximation ceases to calculate
        /// further elements in the series.
        /// @param maxIterations Maximum number of "iterations" to complete.
        /// @return the regularized beta function I(x, a, b)
        /// @throws NReco.Math3.Exception.MaxCountExceededException
        /// if the algorithm fails to converge.
        public static double regularizedBeta(double x,
                                             double a, double b,
                                             double epsilon, int maxIterations)
        {
            double ret;

            if (Double.IsNaN(x) ||
                Double.IsNaN(a) ||
                Double.IsNaN(b) ||
                x < 0 ||
                x > 1 ||
                a <= 0 ||
                b <= 0)
            {
                ret = Double.NaN;
            }
            else if (x > (a + 1) / (2 + b + a) &&
                     1 - x <= (b + 1) / (2 + b + a))
            {
                ret = 1 - regularizedBeta(1 - x, b, a, epsilon, maxIterations);
            }
            else
            {
                ContinuedFraction fraction = new BetaContinuedFraction(a, b);
                ret = Math.Exp((a * MathUtil.Log(x)) + (b * MathUtil.Log1p(-x)) - //(b * Math.log1p(-x))
                               MathUtil.Log(a) - logBeta(a, b)) *
                      1.0 / fraction.evaluate(x, epsilon, maxIterations);
            }

            return(ret);
        }
示例#2
0
文件: Beta.cs 项目: yrinleung/ntaste
        public static double regularizedBeta(double x, double a, double b, double epsilon, int maxIterations)
        {
            if ((((double.IsNaN(x) || double.IsNaN(a)) || (double.IsNaN(b) || (x < 0.0))) || ((x > 1.0) || (a <= 0.0))) || (b <= 0.0))
            {
                return(double.NaN);
            }
            if ((x > ((a + 1.0) / ((2.0 + b) + a))) && ((1.0 - x) <= ((b + 1.0) / ((2.0 + b) + a))))
            {
                return(1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations));
            }
            ContinuedFraction fraction = new BetaContinuedFraction(a, b);

            return((Math.Exp((((a * MathUtil.Log(x)) + (b * MathUtil.Log1p(-x))) - MathUtil.Log(a)) - logBeta(a, b)) * 1.0) / fraction.evaluate(x, epsilon, maxIterations));
        }
示例#3
0
     /// Returns the regularized beta function I(x, a, b).
     ///
     /// The implementation of this method is based on:
     /// <ul>
     /// <li>
     /// <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
     /// Regularized Beta Function</a>.</li>
     /// <li>
     /// <a href="http://functions.wolfram.com/06.21.10.0001.01">
     /// Regularized Beta Function</a>.</li>
     /// </ul>
     ///
     /// @param x the value.
     /// @param a Parameter {@code a}.
     /// @param b Parameter {@code b}.
     /// @param epsilon When the absolute value of the nth item in the
     /// series is less than epsilon the approximation ceases to calculate
     /// further elements in the series.
     /// @param maxIterations Maximum number of "iterations" to complete.
     /// @return the regularized beta function I(x, a, b)
     /// @throws NReco.Math3.Exception.MaxCountExceededException
     /// if the algorithm fails to converge.
    public static double regularizedBeta(double x,
                                         double a, double b,
                                         double epsilon, int maxIterations) {
        double ret;

        if (Double.IsNaN(x) ||
            Double.IsNaN(a) ||
            Double.IsNaN(b) ||
            x < 0 ||
            x > 1 ||
            a <= 0 ||
            b <= 0) {
            ret = Double.NaN;
        } else if (x > (a + 1) / (2 + b + a) &&
                   1 - x <= (b + 1) / (2 + b + a)) {
            ret = 1 - regularizedBeta(1 - x, b, a, epsilon, maxIterations);
        } else {
			ContinuedFraction fraction = new BetaContinuedFraction(a,b);
            ret = Math.Exp((a * MathUtil.Log(x)) + (b * MathUtil.Log1p(-x)) -  //(b * Math.log1p(-x))
                MathUtil.Log(a) - logBeta(a, b)) *
                1.0 / fraction.evaluate(x, epsilon, maxIterations);
        }

        return ret;
    }