Created
May 27, 2015 20:01
-
-
Save ioab/ab2898f05d5f5d7d51da to your computer and use it in GitHub Desktop.
Bionomial Distribution Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace Bionomial_Distribution | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| BionomialDistribution b = new BionomialDistribution(); | |
| double result = b.ProbabilityOfSuccess(0.1, 35, 10); | |
| Console.WriteLine(result); | |
| Console.WriteLine("After Rounding = {0}", Math.Round(result, 4)); | |
| } | |
| } | |
| /** | |
| * @summary: A class provides an implementation for the Binomial Distribution. | |
| */ | |
| class BionomialDistribution | |
| { | |
| /** | |
| * @summary: A method that applies the binomial Distribution. | |
| * | |
| * @param name="probabiltiyOfSuccess", Probability of success on a single trial. | |
| * @param name="totalNumberOfUsers", Number of trials. | |
| * @param name="numberOfActiveUsers", Number of successes (x). | |
| * | |
| */ | |
| public double ProbabilityOfSuccess(double probabiltiyOfSuccess, double numberOfTrials, double numberOfSuccesses) | |
| { | |
| double tempResult = 0.0; | |
| for (long i = 0; i <= numberOfSuccesses; i++) | |
| { | |
| tempResult += GetCombinations(numberOfTrials, i) * Math.Pow(probabiltiyOfSuccess, i) * Math.Pow(1 - probabiltiyOfSuccess, (numberOfTrials - i)); | |
| } | |
| return 1 - tempResult; | |
| } | |
| /** | |
| * @summary: A straightforward mathematical combinations implementation. | |
| * @param name="n" represents the set of numbers we choosing from. | |
| * @param name="r" represents the size of a one combination. | |
| */ | |
| public double GetCombinations(double n, double r) | |
| { | |
| return (GetFactorial(n) / (GetFactorial(r) * GetFactorial(n - r))); | |
| } | |
| /** | |
| * @summary: A recursive factorial method. | |
| */ | |
| public double GetFactorial(double n) | |
| { | |
| if (n <= 0) | |
| return 1; | |
| else | |
| return n * GetFactorial(n - 1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment