/// <summary> /// Returns the probability of an event occuring in k out of n trials, given /// that the probability of the event occuring in any one trial is p. /// </summary> /// <param name="p">Probability that the event occurs in any one trial.</param> /// <param name="n">Number of trials.</param> /// <param name="k">Number of times the event occurs in n trials.</param> /// <returns></returns> public static double BinomialProbabilityDensityFunction(double p, int n, int k) { //# of combinations can exceed max double for n > 1020 if (n > 1020) { double logB = MMath.LogCombin(n, k) + k * Math.Log(p) + (n - k) * Math.Log(1 - p); return(Math.Exp(logB)); } return(MMath.Combinations(n, k) * Math.Pow(p, k) * Math.Pow(1 - p, n - k)); }
/// <summary> /// For n independent draws from a standard uniform distribution (range = 0-1), the probability that the minimum is less than or equal to x. /// </summary> public static double IndependentStandardUniformMinimumCumulativeDistributionFunction(double x, int nDistributions) { double p = 0; int sign = 1; for (int i = 1; i < nDistributions; i++) { p += MMath.Combinations(nDistributions, i) * Math.Pow(x, i) * sign; sign *= -1; } return p; }
/// <summary> /// The Irwin-Hall distribution results from the sum on n independent standard uniform variables /// </summary> /// <param name="x">The value at which to evaluate the distribution.</param> /// <param name="n">The number of standard uniform variables.</param> public static double IrwinHallProbabilityDensityFunction(double x, int n) { if (x < 0 || x > n) { return(0.0); } double d = 0; for (int i = 0; i <= n; i++) { d += Math.Pow(-1, i) * MMath.Combinations(n, i) * Math.Pow(x - i, n - 1) * Math.Sign(x - i); } d *= 0.5; d /= MMath.Factorial(n - 1); return(d); }