Example #1
0
        /// <summary>
        /// Returns the CDF of the F distribution.
        /// </summary>
        /// <param name="x">Value at which the distribution is evaluated.</param>
        /// <param name="k1">Degrees of freedom for numerator chi-sqared distribution.</param>
        /// <param name="k2">Degrees of freedom for denominator chi-sqared distribution.</param>
        public static double FCumulativeDensityFunction(double x, int k1, int k2)
        {
            //Tested against Excel for
            //k1 & k2 = 1, 2, 3, ... , 9, 10, 20, ... , 100
            //x = 0.0, 0.1, 0.2, ... , 6.0
            //maximum discrepancy was less than 0.16% for (x, k1, k2) = (6, 1, 9)

            if (k1 <= 0 || k2 <= 0)
            {
                throw new ArgumentException("k1 and k2 must be greater than 0.");
            }
            if (x < 0)
            {
                throw new ArgumentException("x must be greater than 0.");
            }

            if (x == 0)
            {
                return(0.0);
            }
            double x2 = (k1 * x) / (k1 * x + k2);
            double p  = MMath.RegularizedIncompleteBetaFunction(x2, 0.5 * k1, 0.5 * k2);

            return(Math.Min(1, p));
        }
Example #2
0
        /// <summary>
        /// Returns the CDF of a beta distribution with shape parameters a and b.
        /// PDF is: f(x) = x^(a-1) * (1-x)^(b-1) / B(a,b)
        /// </summary>
        /// <param name="x">Value of the random variable for which the CDF is beign evaluated. x is between 0 and 1.</param>
        /// <param name="a">First shape parameter.</param>
        /// <param name="b">Second shape parameter.</param>
        /// <returns></returns>
        public static double BetaCumulativeDistributionFunction(double x, double a, double b)
        {
            if (x < 0 || x > 1)
            {
                throw new ArgumentException("x must be between 0 and 1");
            }

            return(MMath.RegularizedIncompleteBetaFunction(x, a, b));
        }
Example #3
0
 /// <summary>
 /// Returns the CDF of Student's t distribution.
 /// </summary>
 /// <param name="x">Value at which the distribution is evaluated.</param>
 /// <param name="k">Degrees of freedom.</param>
 public static double StudentsTCumulativeDensityFunction(double x, double k)
 {
     //Tested against Excel for
     //df = 1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 200, 30, 400, 500 and
     //x = -7.0, -6.9, -6.8, ..., 7.0
     //maximum discrepancy was less than 0.04%
     if (double.IsNaN(x))
     {
         return(double.NaN);
     }
     if (x == 0)
     {
         return(0.5);
     }
     if (x > 0)
     {
         double x2 = k / (x * x + k);
         return(1.0 - 0.5 * MMath.RegularizedIncompleteBetaFunction(x2, 0.5 * k, 0.5));
     }
     return(1.0 - StudentsTCumulativeDensityFunction(-x, k));
 }