/// <summary>
        /// Computes the cumulative distribution function of the 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)
        {
            int alpha = Minimum;
            int beta  = Maximum;

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

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

            var sum = 0.0;
            var k   = (int)Math.Ceiling(x - alpha) - 1;

            for (var i = alpha; i <= alpha + k; i++)
            {
                sum += SpecialFunctions.Binomial(_m, i) * SpecialFunctions.Binomial(_populationSize - _m, _n - i);
            }

            return(sum / SpecialFunctions.Binomial(_populationSize, _n));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 분포의 확률
        /// </summary>
        /// <param name="k">시도 횟수</param>
        /// <returns></returns>
        public double Probability(int k)
        {
            if (k < 0)
            {
                return(0.0);
            }

            if (k > _n)
            {
                return(0.0);
            }

            if (_p.ApproximateEqual(0.0) && k == 0)
            {
                return(1.0);
            }

            if (_p.ApproximateEqual(0.0))
            {
                return(0.0);
            }

            if (_p.ApproximateEqual(1.0) && k == _n)
            {
                return(1.0);
            }


            if (_p.ApproximateEqual(1.0))
            {
                return(0.0);
            }

            return(SpecialFunctions.Binomial(_n, k) * Math.Pow(_p, k) * Math.Pow(1.0 - _p, _n - k));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Computes the probability mass (PMF) at k, i.e. P(X = k).
        /// </summary>
        /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
        /// <returns>the probability mass at location <paramref name="k"/>.</returns>
        public double Probability(int k)
        {
            if (k < 0)
            {
                return(0.0);
            }
            if (k > _trials)
            {
                return(0.0);
            }
            if (_p == 0.0 && k == 0)
            {
                return(1.0);
            }
            if (_p == 0.0)
            {
                return(0.0);
            }
            if (_p == 1.0 && k == _trials)
            {
                return(1.0);
            }
            if (_p == 1.0)
            {
                return(0.0);
            }

            return(SpecialFunctions.Binomial(_trials, k) * Math.Pow(_p, k) * Math.Pow(1.0 - _p, _trials - k));
        }
Ejemplo n.º 4
0
 static void Main(string[] args)
 {
     // 1. Calcula o coeficiente binonmial
     Console.WriteLine("1. Calcular o coeficiente binomial ");
     while (true)
     {
         Console.WriteLine("Informe um número de 1 a 100 ( 999 -> Sai )");
         string input = Console.ReadLine();
         int    n;
         if (Int32.TryParse(input, out n))
         {
             if (n == 999)
             {
                 break;
             }
             Console.WriteLine(SpecialFunctions.Binomial(10, 8).ToString("N"));
             Console.WriteLine();
         }
         else
         {
             Console.WriteLine("Valor inválido...");
         }
     }
     Console.WriteLine("Pressione algo para encerrar...");
     Console.ReadLine();
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Factorial">Factorial</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Binomial_coefficient">Binomial coefficient</seealso>
        /// <seealso cref="http://en.wikipedia.org/wiki/Multinomial_theorem#Multinomial_coefficients">Multinomial coefficients</seealso>
        public void Run()
        {
            // 1. Compute the factorial of 5
            Console.WriteLine(@"1. Compute the factorial of 5");
            Console.WriteLine(SpecialFunctions.Factorial(5).ToString("N"));
            Console.WriteLine();

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

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

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

            // 5. Compute the multinomial coefficient: 10 choose 2, 3, 5
            Console.WriteLine(@"5. Compute the multinomial coefficient: 10 choose 2, 3, 5");
            Console.WriteLine(SpecialFunctions.Multinomial(10, new[] { 2, 3, 5 }).ToString("N"));
            Console.WriteLine();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Computes the probability mass (PMF) at k, i.e. P(X = k).
        /// </summary>
        /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
        /// <param name="population">The size of the population (N).</param>
        /// <param name="success">The number successes within the population (K, M).</param>
        /// <param name="draws">The number of draws without replacement (n).</param>
        /// <returns>the probability mass at location <paramref name="k"/>.</returns>
        public static double PMF(int population, int success, int draws, int k)
        {
            //if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) {
            //    throw new ArgumentException("InvalidDistributionParameters");
            //}

            return(SpecialFunctions.Binomial(success, k) * SpecialFunctions.Binomial(population - success, draws - k) / SpecialFunctions.Binomial(population, draws));
        }
Ejemplo n.º 7
0
        public void ValidateProbability(
            [Values(0, 1, 2, 2, 2, 10, 10, 10, 10)] int size,
            [Values(0, 1, 1, 1, 2, 1, 1, 5, 5)] int m,
            [Values(0, 1, 1, 1, 2, 1, 1, 3, 3)] int n,
            [Values(0, 1, 0, 1, 2, 0, 1, 1, 3)] int x)
        {
            var d = new Hypergeometric(size, m, n);

            Assert.AreEqual(SpecialFunctions.Binomial(m, x) * SpecialFunctions.Binomial(size - m, n - x) / SpecialFunctions.Binomial(size, n), d.Probability(x));
        }
Ejemplo n.º 8
0
 public void CanComputeBinomial()
 {
     AssertHelpers.AlmostEqualRelative(1, SpecialFunctions.Binomial(1, 1), 14);
     AssertHelpers.AlmostEqualRelative(10, SpecialFunctions.Binomial(5, 2), 14);
     AssertHelpers.AlmostEqualRelative(35, SpecialFunctions.Binomial(7, 3), 14);
     AssertHelpers.AlmostEqualRelative(1, SpecialFunctions.Binomial(1, 0), 14);
     AssertHelpers.AlmostEqualRelative(0, SpecialFunctions.Binomial(0, 1), 14);
     AssertHelpers.AlmostEqualRelative(0, SpecialFunctions.Binomial(5, 7), 14);
     AssertHelpers.AlmostEqualRelative(0, SpecialFunctions.Binomial(5, -7), 14);
 }
Ejemplo n.º 9
0
        public static Prediction GetSymmetricPrediction(double spotPrice, double volatility, double interestRate, double daysInFuture, int?numberOfPointsArg = null)
        {
            int numberOfPoints = numberOfPointsArg ?? DefaultNumberOfPredictionResults;

            volatility = 0.2;
            double t             = daysInFuture / (365 * numberOfPoints);
            double tmp           = interestRate / 100 - volatility * volatility / 2;
            double x             = Math.Sqrt(tmp * tmp * t * t + volatility * volatility * t);
            double probabilityUp = x.AlmostEqual(0)
                                                                                ? 0.5 + tmp / 2
                                                                                : 0.5 + (tmp * t) / (2 * x);
            double probabilityDown = 1 - probabilityUp;

            double[] stockPrice     = new double[numberOfPoints];
            double[] cumulativeProb = new double[numberOfPoints];

            double prevStockPrice = stockPrice[0] = Math.Log(spotPrice) - (numberOfPoints) * x;

            for (int i = 0; i < numberOfPoints; i++)
            {
                prevStockPrice = prevStockPrice + 2 * x;
                stockPrice[i]  = Math.Exp(prevStockPrice);
            }
            stockPrice[0] = Math.Exp(stockPrice[0]);

            double prevCumulativeProb = 0.0;

            for (int i = 0; i < numberOfPoints; i++)
            {
                double probability = SpecialFunctions.Binomial(numberOfPoints, i) *
                                     Math.Pow(probabilityDown, numberOfPoints - i) *
                                     Math.Pow(probabilityUp, i);

                probability      += prevCumulativeProb;
                cumulativeProb[i] = probability.CoerceZero(SignificantDoubleValue);

                prevCumulativeProb = probability;
            }

            for (int i = numberOfPoints / 2; i < numberOfPoints; i++)
            {
                cumulativeProb[i] = 1.0 - cumulativeProb[i];
                if (cumulativeProb[i] < 0)
                {
                    cumulativeProb[i] = 0;
                }
            }

            Prediction prediction = new Prediction(cumulativeProb, stockPrice, daysInFuture);

            return(prediction);
        }
Ejemplo n.º 10
0
        /// <summary> Gets possible lots. </summary>
        /// <remarks> superreeen, 09.11.2013. </remarks>
        /// <returns> The possible lots. </returns>
        //private static List<PossibleLot> GetPossibleLots()
        //{
        //    List<PossibleLot> findings = new List<PossibleLot>();

        //    decimal gain = 1;
        //    decimal raiser = 1;
        //    decimal totalGain = 0;
        //    decimal gainStepAllocate = 0;
        //    decimal gainAllocate = 0;

        //    for (int n = 5; n >= 0; n--)
        //    {
        //        gainAllocate = (gain * (n)) / (n + raiser);
        //        gain -= gainAllocate;

        //        for (int s = 2; s >= 0; s--)
        //        {
        //            gainStepAllocate = (gainAllocate * (s + 2)) / (s + raiser + 2);
        //            gainAllocate -= gainStepAllocate;
        //            totalGain += gainStepAllocate;

        //            raiser += 2;

        //            findings.Add(new PossibleLot()
        //            {
        //                Numbers = n,
        //                Stars = s,
        //                Probability = (decimal)(SpecialFunctions.Binomial(Numbers, n)
        //                            * SpecialFunctions.Binomial(MaxNumbers - Numbers, Numbers - n)
        //                            / SpecialFunctions.Binomial(MaxNumbers, Numbers)
        //                            * SpecialFunctions.Binomial(Stars, s)
        //                            * SpecialFunctions.Binomial(MaxStars - Stars, Stars - s)
        //                            / SpecialFunctions.Binomial(MaxStars, Stars)),
        //                Gain = gainStepAllocate
        //            });
        //        }
        //    }
        //    return findings;
        //}
        private static List <PossibleLot> GetPossibleLots()
        {
            // key numbers & stars
            Dictionary <int, decimal> distribution = new Dictionary <int, decimal>();

            distribution[52] = 0.300m;
            distribution[51] = 0.080m;
            distribution[50] = 0.020m;
            distribution[42] = 0.020m;
            distribution[41] = 0.010m;
            distribution[40] = 0.006m;
            distribution[32] = 0.006m;
            distribution[31] = 0.004m;
            distribution[30] = 0.002m;
            distribution[22] = 0.002m;
            distribution[21] = 0.040m;
            distribution[20] = 0.070m;
            distribution[12] = 0.090m;
            distribution[11] = 0.090m;
            distribution[10] = 0.110m;
            distribution[02] = 0.000m;
            distribution[01] = 0.000m;
            distribution[00] = 0.000m;

            List <PossibleLot> findings = new List <PossibleLot>();

            for (int n = 5; n >= 0; n--)
            {
                for (int s = 2; s >= 0; s--)
                {
                    findings.Add(new PossibleLot()
                    {
                        Numbers     = n,
                        Stars       = s,
                        Probability = (decimal)(SpecialFunctions.Binomial(Numbers, n)
                                                * SpecialFunctions.Binomial(MaxNumbers - Numbers, Numbers - n)
                                                / SpecialFunctions.Binomial(MaxNumbers, Numbers)
                                                * SpecialFunctions.Binomial(Stars, s)
                                                * SpecialFunctions.Binomial(MaxStars - Stars, Stars - s)
                                                / SpecialFunctions.Binomial(MaxStars, Stars)),
                        Gain = distribution[(n * 10) + s]
                    });
                }
            }
            return(findings);
        }
Ejemplo n.º 11
0
        // These are the two-sided coefficients, so not much use.
        private DenseVector TwoSidedCoefficients(int N)
        {
            if (N % 2 == 0)
            {
                throw new Exception("Error, FilterLength must be odd!");
            }

            int m = (int)(0.5 * (N - 3));
            int M = (int)(0.5 * (N - 1));

            double denominator = 1 / (Math.Pow(2, 2 * m + 1));

            double[] coeff = new double[M];
            for (int k = 0; k < M; ++k)
            {
                coeff[k] = (SpecialFunctions.Binomial(2 * m, m - k + 1) - SpecialFunctions.Binomial(2 * m, m - k - 1)) * denominator;
            }

            return(new DenseVector(coeff));
        }
Ejemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        public List <Finding> getFindings()
        {
            List <Finding> findings = new List <Finding>();

            double gain             = 1;
            double raiser           = 1;
            double totalGain        = 0;
            double gainStepAllocate = 0;
            double gainAllocate     = 0;

            for (int n = 5; n >= 0; n--)
            {
                gainAllocate = (gain * (n)) / (n + raiser);
                gain        -= gainAllocate;

                for (int s = 2; s >= 0; s--)
                {
                    gainStepAllocate = (gainAllocate * (s + 2)) / (s + raiser + 2);
                    gainAllocate    -= gainStepAllocate;
                    totalGain       += gainStepAllocate;

                    raiser += 2;

                    findings.Add(new Finding()
                    {
                        Numbers     = n,
                        Stars       = s,
                        Probability = SpecialFunctions.Binomial(NUMBERS, n)
                                      * SpecialFunctions.Binomial(MAXNUMBERS - NUMBERS, NUMBERS - n)
                                      / SpecialFunctions.Binomial(MAXNUMBERS, NUMBERS)
                                      * SpecialFunctions.Binomial(STARS, s)
                                      * SpecialFunctions.Binomial(MAXSTARS - STARS, STARS - s)
                                      / SpecialFunctions.Binomial(MAXSTARS, STARS),
                        Gain = gainStepAllocate
                    });
                }
            }
            return(findings);
        }
Ejemplo n.º 13
0
 private static void CalcularCoeficienteBinomial()
 {
     Console.WriteLine("1. Calcular o Coeficiente Binomial entre os números n e p");
     while (true)
     {
         Console.WriteLine("Informe o número n entre 1 a 200 ( 999 -> Sai )");
         string numero1 = Console.ReadLine();
         Console.WriteLine("Informe o número p entre 1 a 200 ( 999 -> Sai )");
         string numero2 = Console.ReadLine();
         int    n;
         int    p;
         if (Int32.TryParse(numero1, out n))
         {
             if (n == 999)
             {
                 break;
             }
             if (Int32.TryParse(numero2, out p))
             {
                 if (p == 999)
                 {
                     break;
                 }
                 Console.WriteLine($" C({n},{p}) = {SpecialFunctions.Binomial(n, p).ToString("N")}");
                 Console.WriteLine();
             }
             else
             {
                 Console.WriteLine("Valor de p inválido...");
             }
         }
         else
         {
             Console.WriteLine("Valor de n inválido...");
         }
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Returns the Matrix representing the Casteljau Algorithm for solving Bernstein functions
        /// </summary>
        /// <param name="numPoints">Number of points in this bezier curve</param>
        /// <returns>Returns a sparse matrix of the order numPoints X numPoints</returns>
        private Matrix <double> GetCasteljauMatrix(int numPoints)
        {
            if (_matrixCache.ContainsKey(numPoints))
            {
                return(_matrixCache[numPoints]);
            }
            Matrix <double> m = new MathNet.Numerics.LinearAlgebra.Double.SparseMatrix(numPoints, numPoints);

            int n = numPoints - 1;

            for (int i = 0; i <= n; i++)
            {
                for (int k = 0; k <= n - i; k++)
                {
                    // m_ij = -1^(i+k+n)*(n-1 choose k)*(n choose i)
                    m[i, k] = Math.Pow(-1, i + k + n) *
                              SpecialFunctions.Binomial(n - i, k) *
                              SpecialFunctions.Binomial(n, i);
                }
            }

            _matrixCache[numPoints] = m;
            return(m);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Computes the probability mass (PMF) at k, i.e. P(X = k).
 /// </summary>
 /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
 /// <returns>the probability mass at location <paramref name="k"/>.</returns>
 public double Probability(int k)
 {
     return(SpecialFunctions.Binomial(_success, k) * SpecialFunctions.Binomial(_population - _success, _draws - k) / SpecialFunctions.Binomial(_population, _draws));
 }
        public void ValidateProbability(int N, int m, int n, int x)
        {
            var d = new Hypergeometric(N, m, n);

            Assert.AreEqual <double>(SpecialFunctions.Binomial(m, x) * SpecialFunctions.Binomial(N - m, n - x) / SpecialFunctions.Binomial(N, n), d.Probability(x));
        }
Ejemplo n.º 17
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();
        }
Ejemplo n.º 18
0
        public void ValidateProbability(int size, int m, int n, int x)
        {
            var d = new Hypergeometric(size, m, n);

            Assert.AreEqual(SpecialFunctions.Binomial(m, x) * SpecialFunctions.Binomial(size - m, n - x) / SpecialFunctions.Binomial(size, n), d.Probability(x));
        }
 /// <summary>
 /// Computes values of the probability mass function.
 /// </summary>
 /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
 /// <returns>
 /// the probability mass at location <paramref name="k"/>.
 /// </returns>
 public double Probability(int k)
 {
     return(SpecialFunctions.Binomial(_m, k) * SpecialFunctions.Binomial(_populationSize - _m, _n - k) / SpecialFunctions.Binomial(_populationSize, _n));
 }
Ejemplo n.º 20
0
        public void ValidateProbability(int population, int success, int draws, int x)
        {
            var d = new Hypergeometric(population, success, draws);

            Assert.AreEqual(SpecialFunctions.Binomial(success, x) * SpecialFunctions.Binomial(population - success, draws - x) / SpecialFunctions.Binomial(population, draws), d.Probability(x));
        }