Пример #1
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Beta_function">Beta function</seealso>
        public void Run()
        {
            // 1. Compute the Beta function at z = 1.0, w = 3.0
            Console.WriteLine(@"1. Compute the Beta function at z = 1.0, w = 3.0");
            Console.WriteLine(SpecialFunctions.Beta(1.0, 3.0));
            Console.WriteLine();

            // 2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0
            Console.WriteLine(@"2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0");
            Console.WriteLine(SpecialFunctions.BetaLn(1.0, 3.0));
            Console.WriteLine();

            // 3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7
            Console.WriteLine(@"3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7");
            Console.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 0.7));
            Console.WriteLine();

            // 4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0
            Console.WriteLine(@"4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0");
            Console.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 1.0));
            Console.WriteLine();

            // 5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7
            Console.WriteLine(@"5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7");
            Console.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 0.7));
            Console.WriteLine();

            // 6. Compute the Beta regularized  function at z = 1.0, w = 3.0, x = 1.0
            Console.WriteLine(@"6. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 1.0");
            Console.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 1.0));
            Console.WriteLine();
        }
Пример #2
0
        /// <summary>
        /// 贝叶斯估计
        /// </summary>
        /// <param name="arrays">输入为多组数据 每组数据的个数不同</param>
        /// <param name="isBalanced">数据是否平衡</param>
        /// <returns></returns>
        public static double[] _BayesEstimation(ArrayList arrays, bool isBalanced)
        {
            int arraysLength = arrays.Count; // 数据组数 a

            int[] arrayLengths = new int[arraysLength];
            for (int i = 0; i < arraysLength; i++)
            {
                var array = arrays[i];
                arrayLengths[i] = (array as double[]).Length;
            }
            int n = arrayLengths.Sum();

            double[] rets = _OneWayANOVA(arrays, isBalanced);                    // 先进行单因素方差分析
            double   SSe = rets[0], SSa = rets[1], MSe = rets[2], MSa = rets[3]; // 获取 SSe, SSa, MSe, MSa
            double   sigma_e_2, sigma_a_2, Ve, Va, r;

            Ve = arraysLength * (n - 1);
            Va = arraysLength - 1;
            r  = SSe / (SSe + SSa);

            sigma_e_2 = SSe / (Ve - 2) * SpecialFunctions.BetaRegularized(0.5 * Va, 0.5 * Ve - 1, r) / SpecialFunctions.BetaRegularized(0.5 * Va, 0.5 * Ve, r); // 参考 https://numerics.mathdotnet.com/Functions.html#Regularized-Beta
            sigma_a_2 = 1.0 / n * (
                SSa / (Va - 2) * SpecialFunctions.BetaRegularized(0.5 * Va - 1, 0.5 * Ve, r) / SpecialFunctions.BetaRegularized(0.5 * Va, 0.5 * Ve, r)
                - SSe / (Ve - 2) * SpecialFunctions.BetaRegularized(0.5 * Va, 0.5 * Ve - 1, r) / SpecialFunctions.BetaRegularized(0.5 * Va, 0.5 * Ve, r)
                );
            return(new double[] { sigma_e_2, sigma_a_2 });
        }
Пример #3
0
        /// <summary>
        /// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
        /// at the given probability. This is also known as the quantile or percent point function.
        /// </summary>
        /// <param name="p">The location at which to compute the inverse cumulative density.</param>
        /// <param name="location">The location (μ) of the distribution.</param>
        /// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
        /// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param>
        /// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
        /// <seealso cref="InverseCumulativeDistribution"/>
        /// <remarks>WARNING: currently not an explicit implementation, hence slow and unreliable.</remarks>
        public static double InvCDF(double location, double scale, double freedom, double p)
        {
            if (scale <= 0.0 || freedom <= 0.0)
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            // TODO JVG we can probably do a better job for Cauchy special case
            if (double.IsPositiveInfinity(freedom))
            {
                return(Normal.InvCDF(location, scale, p));
            }

            if (p == 0.5d)
            {
                return(location);
            }

            // TODO PERF: We must implement this explicitly instead of solving for CDF^-1
            return(Brent.FindRoot(x =>
            {
                var k = (x - location) / scale;
                var h = freedom / (freedom + (k * k));
                var ib = 0.5 * SpecialFunctions.BetaRegularized(freedom / 2.0, 0.5, h);
                return x <= location ? ib - p : 1.0 - ib - p;
            }, -800, 800, accuracy: 1e-12));
        }
Пример #4
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="a">The α shape parameter of the Beta distribution. Range: α ≥ 0.</param>
        /// <param name="b">The β shape parameter of the Beta distribution. Range: β ≥ 0.</param>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        /// <seealso cref="CumulativeDistribution"/>
        public static double CDF(double a, double b, double x)
        {
            if (a < 0.0 || b < 0.0)
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            if (x < 0.0)
            {
                return(0.0);
            }

            if (x >= 1.0)
            {
                return(1.0);
            }

            if (double.IsPositiveInfinity(a) && double.IsPositiveInfinity(b))
            {
                return(x < 0.5 ? 0.0 : 1.0);
            }

            if (double.IsPositiveInfinity(a))
            {
                return(x < 1.0 ? 0.0 : 1.0);
            }

            if (double.IsPositiveInfinity(b))
            {
                return(x >= 0.0 ? 1.0 : 0.0);
            }

            if (a == 0.0 && b == 0.0)
            {
                if (x >= 0.0 && x < 1.0)
                {
                    return(0.5);
                }

                return(1.0);
            }

            if (a == 0.0)
            {
                return(1.0);
            }

            if (b == 0.0)
            {
                return(x >= 1.0 ? 1.0 : 0.0);
            }

            if (a == 1.0 && b == 1.0)
            {
                return(x);
            }

            return(SpecialFunctions.BetaRegularized(a, b, x));
        }
Пример #5
0
        public static double InvCDF(double a, double b, double p)
        {
            //if (a < 0.0 || b < 0.0 || p < 0.0 || p > 1.0) {
            //    throw new ArgumentException(Resources.InvalidDistributionParameters);
            //}

            return(Brent.FindRoot(x => SpecialFunctions.BetaRegularized(a, b, x) - p, 0.0, 1.0, accuracy: 1e-12));
        }
Пример #6
0
        public static double CDF(double d1, double d2, double x)
        {
            //if (d1 <= 0.0 || d2 <= 0.0) {
            //    throw new ArgumentException(Resources.InvalidDistributionParameters);
            //}

            return(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d1 * x + d2)));
        }
 public void BetaRegularized(
     [Values(0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 0.100000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 2.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000, 5.500000)] double a,
     [Values(0.100000, 0.100000, 0.100000, 1.500000, 1.500000, 1.500000, 2.500000, 2.500000, 2.500000, 5.500000, 5.500000, 5.500000, 0.100000, 0.100000, 0.100000, 1.500000, 1.500000, 1.500000, 2.500000, 2.500000, 2.500000, 5.500000, 5.500000, 5.500000, 0.100000, 0.100000, 0.100000, 1.500000, 1.500000, 1.500000, 2.500000, 2.500000, 2.500000, 5.500000, 5.500000, 5.500000, 0.100000, 0.100000, 0.100000, 1.500000, 1.500000, 1.500000, 2.500000, 2.500000, 2.500000, 5.500000, 5.500000, 5.500000)] double b,
     [Values(0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000, 0.100000, 0.500000, 0.800000)] double x,
     [Values(0.40638509393627598963947434031370208398700911383034, 0.5, 0.56029080977665439586092261707431380702999082404228, 0.83793618164513694339361766577607270440777026543289, 0.96453423693668031005965611526662377555163072304903, 0.99288897919542998751072045939791723468184549496473, 0.88585310309894667823419197676046986281131524104959, 0.98784074184406228317625751436240720987391458581291, 0.99906884630106408932550166100889714490764416359312, 0.94519184147610137383076366494492317664691771837548, 0.99917702583101287494949180374093750006040749104055, 0.99999619645266501604882303584892931538704382134298, 0.0023637194748231330327322712604688049136736986021871, 0.03546576306331968994034388473337622444836927695097, 0.10619861080705813882414161079467267590245761692272, 0.052044019330913933551809009997594089261007490064035, 0.5, 0.85762151006735301922188173009147886735366387396138, 0.097880642941379793645839194076629344959184516635409, 0.71220659078919378102517835116335248271261286098728, 0.96627128455142020796603976406510565066301101575357, 0.2492200779249636429835386068413815093625886315581, 0.95270739368361339952038048248181862978690743677285, 0.99963193911681378117173263163355516583213310848701, 0.00015291978644767572330394885397661411972523657814463, 0.012159258155937716823742485637592790126085414187091, 0.063159516487818477363383984899205310365144861115492, 0.006207395720448073457778825918558833562830463492662, 0.28779340921080621897482164883664751728738713901272, 0.74897173558328583047772369611785208404431673216919, 0.015374720442541245985473611768528587536206924004576, 0.5, 0.92281137475779334211841505067903343661808086762039, 0.065915491253258347344782598110283193803051483319306, 0.87186678766868243532031253918149387446781682306342, 0.99857234512565487924356478796968439529913692090276, 0.000000076971146008440526509370378069895435149621047562911, 0.00082297416898712505050819625906249993959250895944939, 0.01964656911825443767408345737200950450690987324177, 0.0000085380512231662773545327104780279605728164994539023, 0.047292606316386600479619517518181370213092563227146, 0.46453697358156781101222907948783656601193056166297, 0.000036476564661926426853537763285519105619982887967225, 0.12813321233131756467968746081850612553218317693658, 0.73579303531824700577792236664278017906658620718916, 0.00051241472879389331855418481833750204851457778933154, 0.5, 0.98491460241721309518021565227501009891015897915977)] double f)
 {
     AssertHelpers.AlmostEqual(f, SpecialFunctions.BetaRegularized(a, b, x), 12);
 }
        public void ValidateCumulativeDistribution(double d1, double d2, double x)
        {
            var    n        = new FisherSnedecor(d1, d2);
            double expected = SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + (x * d1)));

            Assert.That(n.CumulativeDistribution(x), Is.EqualTo(expected));
            Assert.That(FisherSnedecor.CDF(d1, d2, x), Is.EqualTo(expected));
        }
        public void ValidateInverseCumulativeDistribution(double d1, double d2, double x)
        {
            var    n = new FisherSnedecor(d1, d2);
            double p = SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + (x * d1)));

            Assert.That(n.InverseCumulativeDistribution(p), Is.EqualTo(x).Within(1e-8));
            Assert.That(FisherSnedecor.InvCDF(d1, d2, p), Is.EqualTo(x).Within(1e-8));
        }
Пример #10
0
        public void ValidateCumulativeDistribution(
            [Values(0.1, 1.0, 10.0, 0.1, 1.0, 10.0, 0.1, 1.0, 10.0, 0.1, 1.0, 10.0)] double d1,
            [Values(0.1, 0.1, 0.1, 1.0, 1.0, 1.0, 0.1, 0.1, 0.1, 1.0, 1.0, 1.0)] double d2,
            [Values(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0)] double x)
        {
            var n = new FisherSnedecor(d1, d2);

            Assert.AreEqual(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + (x * d1))), n.CumulativeDistribution(x));
        }
Пример #11
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="r">The number of successes (r) required to stop the experiment. Range: r ≥ 0.</param>
        /// <param name="p">The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1.</param>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        /// <seealso cref="CumulativeDistribution"/>
        public static double CDF(double r, double p, double x)
        {
            if (!(r >= 0.0 && p >= 0.0 && p <= 1.0))
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }

            return(1 - SpecialFunctions.BetaRegularized(x + 1, r, 1 - p));
        }
Пример #12
0
        public static double InvCDF(double d1, double d2, double p)
        {
            //if (d1 <= 0.0 || d2 <= 0.0) {
            //    throw new ArgumentException(Resources.InvalidDistributionParameters);
            //}

            return(Brent.FindRoot(
                       x => SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d1 * x + d2)) - p,
                       0, 1000, accuracy: 1e-12));
        }
Пример #13
0
        /// <summary>
        /// Computes the cumulative distribution function of the Beta distribution.
        /// </summary>
        /// <param name="x">The location at which to compute the cumulative density.</param>
        /// <returns>the cumulative density at <paramref name="x"/>.</returns>
        public double CumulativeDistribution(double x)
        {
            if (x < 0.0)
            {
                return(0.0);
            }

            if (x >= 1.0)
            {
                return(1.0);
            }

            if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB))
            {
                return(x < 0.5 ? 0.0 : 1.0);
            }

            if (Double.IsPositiveInfinity(_shapeA))
            {
                return(x < 1.0 ? 0.0 : 1.0);
            }

            if (Double.IsPositiveInfinity(_shapeB))
            {
                return(x >= 0.0 ? 1.0 : 0.0);
            }

            if (_shapeA == 0.0 && _shapeB == 0.0)
            {
                if (x >= 0.0 && x < 1.0)
                {
                    return(0.5);
                }

                return(1.0);
            }

            if (_shapeA == 0.0)
            {
                return(1.0);
            }

            if (_shapeB == 0.0)
            {
                return(x >= 1.0 ? 1.0 : 0.0);
            }

            if (_shapeA == 1.0 && _shapeB == 1.0)
            {
                return(x);
            }

            return(SpecialFunctions.BetaRegularized(_shapeA, _shapeB, x));
        }
Пример #14
0
        /// <summary>
        /// 확률 분포 계산을 위한 누적분포함수
        /// </summary>
        /// <param name="x">The location at which to compute the cumulative distribution function.</param>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        public double CumulativeDistribution(double x)
        {
            if (x < 0.0)
            {
                return(0.0);
            }

            if (x >= 1.0)
            {
                return(1.0);
            }

            if (Double.IsPositiveInfinity(_a) && Double.IsPositiveInfinity(_b))
            {
                return(x < 0.5 ? 0.0 : 1.0);
            }

            if (Double.IsPositiveInfinity(_a))
            {
                return(x < 1.0 ? 0.0 : 1.0);
            }

            if (Double.IsPositiveInfinity(_b))
            {
                return(x >= 0.0 ? 1.0 : 0.0);
            }

            if (Math.Abs(_a - 0.0) < double.Epsilon && Math.Abs(_b - 0.0) < double.Epsilon)
            {
                if (x >= 0.0 && x < 1.0)
                {
                    return(0.5);
                }

                return(1.0);
            }

            if (Math.Abs(_a - 0.0) < double.Epsilon)
            {
                return(1.0);
            }

            if (Math.Abs(_b - 0.0) < double.Epsilon)
            {
                return(x >= 1.0 ? 1.0 : 0.0);
            }

            if (Math.Abs(_a - 1.0) < double.Epsilon && Math.Abs(_b - 1.0) < double.Epsilon)
            {
                return(x);
            }

            return(SpecialFunctions.BetaRegularized(_a, _b, x));
        }
Пример #15
0
        /// <summary>
        /// Computes the cumulative distribution function of the Student t-distribution.
        /// </summary>
        /// <param name="x">The location at which to compute the cumulative density.</param>
        /// <returns>the cumulative density at <paramref name="x"/>.</returns>
        public double CumulativeDistribution(double x)
        {
            // TODO JVG we can probably do a better job for Cauchy special case
            if (Double.IsPositiveInfinity(_dof))
            {
                return(Normal.CumulativeDistribution(_location, _scale, x));
            }

            var k  = (x - _location) / _scale;
            var h  = _dof / (_dof + (k * k));
            var ib = 0.5 * SpecialFunctions.BetaRegularized(_dof / 2.0, 0.5, h);

            return(x <= _location ? ib : 1.0 - ib);
        }
Пример #16
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>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        public double CumulativeDistribution(double x)
        {
            if (x < 0.0)
            {
                return(0.0);
            }
            if (x > _trials)
            {
                return(1.0);
            }

            double k = Math.Floor(x);

            return(SpecialFunctions.BetaRegularized(_trials - k, k + 1, 1 - _p));
        }
Пример #17
0
        public static double CDF(double location, double scale, double freedom, double x)
        {
            //if (scale <= 0.0 || freedom <= 0.0) {
            //    throw new ArgumentException(Resources.InvalidDistributionParameters);
            //}

            // TODO JVG we can probably do a better job for Cauchy special case
            if (double.IsPositiveInfinity(freedom))
            {
                return(Normal.CDF(location, scale, x));
            }

            var k  = (x - location) / scale;
            var h  = freedom / (freedom + (k * k));
            var ib = 0.5 * SpecialFunctions.BetaRegularized(freedom / 2.0, 0.5, h);

            return(x <= location ? ib : 1.0 - ib);
        }
Пример #18
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="p">The success probability (p) in each trial. Range: 0 ≤ p ≤ 1.</param>
        /// <param name="n">The number of trials (n). Range: n ≥ 0.</param>
        /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
        /// <seealso cref="CumulativeDistribution"/>
        public static double CDF(double p, int n, double x)
        {
            if (!(p >= 0.0 && p <= 1.0 && n >= 0))
            {
                throw new ArgumentException(Resources.InvalidDistributionParameters);
            }
            if (x < 0.0)
            {
                return(0.0);
            }
            if (x > n)
            {
                return(1.0);
            }
            double k = Math.Floor(x);

            return(SpecialFunctions.BetaRegularized(n - k, k + 1, 1 - p));
        }
Пример #19
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>
 /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
 /// <seealso cref="CDF"/>
 public double CumulativeDistribution(double x)
 {
     return(SpecialFunctions.BetaRegularized(_freedom1 / 2.0, _freedom2 / 2.0, _freedom1 * x / ((_freedom1 * x) + _freedom2)));
 }
Пример #20
0
        /// <summary>
        /// Executes the example.
        /// </summary>
        public override void ExecuteExample()
        {
            // <seealso cref="http://en.wikipedia.org/wiki/Beta_function">Beta function</seealso>
            MathDisplay.WriteLine("<b>Beta fuction</b>");

            // 1. Compute the Beta function at z = 1.0, w = 3.0
            MathDisplay.WriteLine(@"1. Compute the Beta function at z = 1.0, w = 3.0");
            MathDisplay.WriteLine(SpecialFunctions.Beta(1.0, 3.0).ToString());
            MathDisplay.WriteLine();

            // 2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0
            MathDisplay.WriteLine(@"2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0");
            MathDisplay.WriteLine(SpecialFunctions.BetaLn(1.0, 3.0).ToString());
            MathDisplay.WriteLine();

            // 3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7
            MathDisplay.WriteLine(@"3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7");
            MathDisplay.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 0.7).ToString());
            MathDisplay.WriteLine();

            // 4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0
            MathDisplay.WriteLine(@"4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0");
            MathDisplay.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 1.0).ToString());
            MathDisplay.WriteLine();

            // 5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7
            MathDisplay.WriteLine(@"5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7");
            MathDisplay.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 0.7).ToString());
            MathDisplay.WriteLine();

            // 6. Compute the Beta regularized  function at z = 1.0, w = 3.0, x = 1.0
            MathDisplay.WriteLine(@"6. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 1.0");
            MathDisplay.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 1.0).ToString());
            MathDisplay.WriteLine();



            MathDisplay.WriteLine("<b>Common functions</b>");

            // 1. Calculate the Digamma function at point 5.0
            // <seealso cref="http://en.wikipedia.org/wiki/Digamma_function">Digamma function</seealso>
            MathDisplay.WriteLine(@"1. Calculate the Digamma function at point 5.0");
            MathDisplay.WriteLine(SpecialFunctions.DiGamma(5.0).ToString());
            MathDisplay.WriteLine();

            // 2. Calculate the inverse Digamma function at point 1.5
            MathDisplay.WriteLine(@"2. Calculate the inverse Digamma function at point 1.5");
            MathDisplay.WriteLine(SpecialFunctions.DiGammaInv(1.5).ToString());
            MathDisplay.WriteLine();

            // 3. Calculate the 10'th Harmonic number
            // <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number">Harmonic number</seealso>
            MathDisplay.WriteLine(@"3. Calculate the 10'th Harmonic number");
            MathDisplay.WriteLine(SpecialFunctions.Harmonic(10).ToString());
            MathDisplay.WriteLine();

            // 4. Calculate the generalized harmonic number of order 10 of 3.0.
            // <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number#Generalized_harmonic_numbers">Generalized harmonic numbers</seealso>
            MathDisplay.WriteLine(@"4. Calculate the generalized harmonic number of order 10 of 3.0");
            MathDisplay.WriteLine(SpecialFunctions.GeneralHarmonic(10, 3.0).ToString());
            MathDisplay.WriteLine();

            // 5. Calculate the logistic function of 3.0
            // <seealso cref="http://en.wikipedia.org/wiki/Logistic_function">Logistic function</seealso>
            MathDisplay.WriteLine(@"5. Calculate the logistic function of 3.0");
            MathDisplay.WriteLine(SpecialFunctions.Logistic(3.0).ToString());
            MathDisplay.WriteLine();

            // 6. Calculate the logit function of 0.3
            // <seealso cref="http://en.wikipedia.org/wiki/Logit">Logit function</seealso>
            MathDisplay.WriteLine(@"6. Calculate the logit function of 0.3");
            MathDisplay.WriteLine(SpecialFunctions.Logit(0.3).ToString());
            MathDisplay.WriteLine();

            // <seealso cref="http://en.wikipedia.org/wiki/Error_function">Error function</seealso>
            MathDisplay.WriteLine("<b>Error function</b>");

            // 1. Calculate the error function at point 2
            MathDisplay.WriteLine(@"1. Calculate the error function at point 2");
            MathDisplay.WriteLine(SpecialFunctions.Erf(2).ToString());
            MathDisplay.WriteLine();

            // 2. Sample 10 values of the error function in [-1.0; 1.0]
            MathDisplay.WriteLine(@"2. Sample 10 values of the error function in [-1.0; 1.0]");
            var data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.Erf);

            for (var i = 0; i < data.Length; i++)
            {
                MathDisplay.Write(data[i].ToString("N") + @" ");
            }

            MathDisplay.WriteLine();
            MathDisplay.WriteLine();

            // 3. Calculate the complementary error function at point 2
            MathDisplay.WriteLine(@"3. Calculate the complementary error function at point 2");
            MathDisplay.WriteLine(SpecialFunctions.Erfc(2).ToString());
            MathDisplay.WriteLine();

            // 4. Sample 10 values of the complementary error function in [-1.0; 1.0]
            MathDisplay.WriteLine(@"4. Sample 10 values of the complementary error function in [-1.0; 1.0]");
            data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.Erfc);
            for (var i = 0; i < data.Length; i++)
            {
                MathDisplay.Write(data[i].ToString("N") + @" ");
            }

            MathDisplay.WriteLine();
            MathDisplay.WriteLine();

            // 5. Calculate the inverse error function at point z=0.5
            MathDisplay.WriteLine(@"5. Calculate the inverse error function at point z=0.5");
            MathDisplay.WriteLine(SpecialFunctions.ErfInv(0.5).ToString());
            MathDisplay.WriteLine();

            // 6. Sample 10 values of the inverse error function in [-1.0; 1.0]
            MathDisplay.WriteLine(@"6. Sample 10 values of the inverse error function in [-1.0; 1.0]");
            data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.ErfInv);
            for (var i = 0; i < data.Length; i++)
            {
                MathDisplay.Write(data[i].ToString("N") + @" ");
            }

            MathDisplay.WriteLine();
            MathDisplay.WriteLine();

            // 7. Calculate the complementary inverse error function at point z=0.5
            MathDisplay.WriteLine(@"7. Calculate the complementary inverse error function at point z=0.5");
            MathDisplay.WriteLine(SpecialFunctions.ErfcInv(0.5).ToString());
            MathDisplay.WriteLine();

            // 8. Sample 10 values of the complementary inverse error function in [-1.0; 1.0]
            MathDisplay.WriteLine(@"8. Sample 10 values of the complementary inverse error function in [-1.0; 1.0]");
            data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.ErfcInv);
            for (var i = 0; i < data.Length; i++)
            {
                MathDisplay.Write(data[i].ToString("N") + @" ");
            }

            MathDisplay.WriteLine();


            // <seealso cref="http://en.wikipedia.org/wiki/Factorial">Factorial</seealso>
            MathDisplay.WriteLine("<b>Factorial</b>");

            // 1. Compute the factorial of 5
            MathDisplay.WriteLine(@"1. Compute the factorial of 5");
            MathDisplay.WriteLine(SpecialFunctions.Factorial(5).ToString("N"));
            MathDisplay.WriteLine();

            // 2. Compute the logarithm of the factorial of 5
            MathDisplay.WriteLine(@"2. Compute the logarithm of the factorial of 5");
            MathDisplay.WriteLine(SpecialFunctions.FactorialLn(5).ToString("N"));
            MathDisplay.WriteLine();


            // <seealso cref="http://en.wikipedia.org/wiki/Binomial_coefficient">Binomial coefficient</seealso>
            MathDisplay.WriteLine("<b>Binomial coefficient</b>");

            // 3. Compute the binomial coefficient: 10 choose 8
            MathDisplay.WriteLine(@"3. Compute the binomial coefficient: 10 choose 8");
            MathDisplay.WriteLine(SpecialFunctions.Binomial(10, 8).ToString("N"));
            MathDisplay.WriteLine();

            // 4. Compute the logarithm of the binomial coefficient: 10 choose 8
            MathDisplay.WriteLine(@"4. Compute the logarithm of the binomial coefficient: 10 choose 8");
            MathDisplay.WriteLine(SpecialFunctions.BinomialLn(10, 8).ToString("N"));
            MathDisplay.WriteLine();

            // <seealso cref="http://en.wikipedia.org/wiki/Multinomial_theorem#Multinomial_coefficients">Multinomial coefficients</seealso>
            MathDisplay.WriteLine("<b>Multinomial coefficient</b>");

            // 5. Compute the multinomial coefficient: 10 choose 2, 3, 5
            MathDisplay.WriteLine(@"5. Compute the multinomial coefficient: 10 choose 2, 3, 5");
            MathDisplay.WriteLine(SpecialFunctions.Multinomial(10, new[] { 2, 3, 5 }).ToString("N"));
            MathDisplay.WriteLine();


            // <seealso cref="http://en.wikipedia.org/wiki/Gamma_function">Gamma function</seealso>
            MathDisplay.WriteLine("<b>Gamma function</b>");

            // 1. Compute the Gamma function of 10
            MathDisplay.WriteLine(@"1. Compute the Gamma function of 10");
            MathDisplay.WriteLine(SpecialFunctions.Gamma(10).ToString("N"));
            MathDisplay.WriteLine();

            // 2. Compute the logarithm of the Gamma function of 10
            MathDisplay.WriteLine(@"2. Compute the logarithm of the Gamma function of 10");
            MathDisplay.WriteLine(SpecialFunctions.GammaLn(10).ToString("N"));
            MathDisplay.WriteLine();

            // 3. Compute the lower incomplete gamma(a, x) function at a = 10, x = 14
            MathDisplay.WriteLine(@"3. Compute the lower incomplete gamma(a, x) function at a = 10, x = 14");
            MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 14).ToString("N"));
            MathDisplay.WriteLine();

            // 4. Compute the lower incomplete gamma(a, x) function at a = 10, x = 100
            MathDisplay.WriteLine(@"4. Compute the lower incomplete gamma(a, x) function at a = 10, x = 100");
            MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 100).ToString("N"));
            MathDisplay.WriteLine();

            // 5. Compute the upper incomplete gamma(a, x) function at a = 10, x = 0
            MathDisplay.WriteLine(@"5. Compute the upper incomplete gamma(a, x) function at a = 10, x = 0");
            MathDisplay.WriteLine(SpecialFunctions.GammaUpperIncomplete(10, 0).ToString("N"));
            MathDisplay.WriteLine();

            // 6. Compute the upper incomplete gamma(a, x) function at a = 10, x = 10
            MathDisplay.WriteLine(@"6. Compute the upper incomplete gamma(a, x) function at a = 10, x = 100");
            MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 10).ToString("N"));
            MathDisplay.WriteLine();

            // 7. Compute the lower regularized gamma(a, x) function at a = 10, x = 14
            MathDisplay.WriteLine(@"7. Compute the lower regularized gamma(a, x) function at a = 10, x = 14");
            MathDisplay.WriteLine(SpecialFunctions.GammaLowerRegularized(10, 14).ToString("N"));
            MathDisplay.WriteLine();

            // 8. Compute the lower regularized gamma(a, x) function at a = 10, x = 100
            MathDisplay.WriteLine(@"8. Compute the lower regularized gamma(a, x) function at a = 10, x = 100");
            MathDisplay.WriteLine(SpecialFunctions.GammaLowerRegularized(10, 100).ToString("N"));
            MathDisplay.WriteLine();

            // 9. Compute the upper regularized gamma(a, x) function at a = 10, x = 0
            MathDisplay.WriteLine(@"9. Compute the upper regularized gamma(a, x) function at a = 10, x = 0");
            MathDisplay.WriteLine(SpecialFunctions.GammaUpperRegularized(10, 0).ToString("N"));
            MathDisplay.WriteLine();

            // 10. Compute the upper regularized gamma(a, x) function at a = 10, x = 10
            MathDisplay.WriteLine(@"10. Compute the upper regularized gamma(a, x) function at a = 10, x = 100");
            MathDisplay.WriteLine(SpecialFunctions.GammaUpperRegularized(10, 10).ToString("N"));
            MathDisplay.WriteLine();

            MathDisplay.WriteLine("<b>Numerical stability</b>");

            // 1. Compute numerically stable exponential of 10 minus one
            MathDisplay.WriteLine(@"1. Compute numerically stable exponential of 4.2876 minus one");
            MathDisplay.WriteLine(SpecialFunctions.ExponentialMinusOne(4.2876).ToString());
            MathDisplay.WriteLine();

            // 2. Compute regular System.Math exponential of 15.28 minus one
            MathDisplay.WriteLine(@"2. Compute regular System.Math exponential of 4.2876 minus one ");
            MathDisplay.WriteLine((Math.Exp(4.2876) - 1).ToString());
            MathDisplay.WriteLine();

            // 3. Compute numerically stable hypotenuse of a right angle triangle with a = 5, b = 3
            MathDisplay.WriteLine(@"3. Compute numerically stable hypotenuse of a right angle triangle with a = 5, b = 3");
            MathDisplay.WriteLine(SpecialFunctions.Hypotenuse(5, 3).ToString());
            MathDisplay.WriteLine();
        }
Пример #21
0
        public void ValidateCumulativeDistribution(double d1, double d2, double x)
        {
            var n = new FisherSnedecor(d1, d2);

            Assert.AreEqual <double>(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + x * d1)), n.CumulativeDistribution(x));
        }
Пример #22
0
 /// <summary>
 /// 확률 분포 계산을 위한 누적분포함수
 /// </summary>
 /// <param name="x">The location at which to compute the cumulative distribution function.</param>
 /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
 public double CumulativeDistribution(double x)
 {
     return(SpecialFunctions.BetaRegularized(_d1 / 2.0, _d2 / 2.0, _d1 * x / ((_d1 * x) + _d2)));
 }
Пример #23
0
 public void BetaRegularizedLargeArguments()
 {
     Assert.That(
         SpecialFunctions.BetaRegularized(3846.2382301675848, 738420369.64263761, 0.0000052331266889206809),
         Is.EqualTo(0.61624368331298802128).Within(1e-5));
 }
Пример #24
0
 public void BetaRegularized(double a, double b, double x, double f)
 {
     AssertHelpers.AlmostEqualRelative(f, SpecialFunctions.BetaRegularized(a, b, x), 11);
 }
Пример #25
0
        public void ValidateCumulativeDistribution([Values(0.0, 0.1, 1.0)] double r, [Values(0.0, 0.3, 1.0)] double p, [Values(0, 1, 2, 3, 5)] int x)
        {
            var d = new NegativeBinomial(r, p);

            Assert.AreEqual(SpecialFunctions.BetaRegularized(r, x + 1.0, p), d.CumulativeDistribution(x), 1e-12);
        }
Пример #26
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>
 /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
 public double CumulativeDistribution(double x)
 {
     return(1 - SpecialFunctions.BetaRegularized(x + 1, _trials, 1 - _p));
 }
Пример #27
0
        public void ValidateCumulativeDistribution(double r, double p, int x)
        {
            var d = new NegativeBinomial(r, p);

            Assert.AreEqual(SpecialFunctions.BetaRegularized(r, x + 1.0, p), d.CumulativeDistribution(x), 1e-12);
        }