binomial dist Algorithm

The binomial distribution algorithm is a probability distribution model that describes the number of successful outcomes in a fixed number of independent Bernoulli trials with the same probability of success. It is a fundamental concept in probability theory and statistics, often used to model the number of successes or positive outcomes in a specific number of trials, such as flipping a coin or conducting a survey. The binomial distribution can be used to calculate the probability of observing a particular number of successes in a fixed number of trials, given a known probability of success for each individual trial. The algorithm for calculating binomial distribution involves two main components: the binomial coefficient, which represents the number of ways to choose a given number of successes from the total number of trials, and the probability of success raised to the power of the number of successes and the probability of failure raised to the power of the number of failures. The binomial distribution is typically represented by the formula P(X = k) = C(n, k) * p^k * (1-p)^(n-k), where P(X = k) is the probability of observing k successes in n trials, C(n, k) represents the binomial coefficient, p is the probability of success, and n and k are the total number of trials and successes, respectively. This formula can be used to calculate the probability of various outcomes for a wide range of scenarios, making the binomial distribution algorithm a versatile and essential tool in probability and statistics.
#include <iostream>
#include <cmath>

// the binomial distribution models the number of
// successes in a sequence of n independent events

// n : number of trials
// p : probability of success
// x : desired successes

// finds the expected value of a binomial distribution

double binomial_expected(double n, double p) {
    return n * p;
}

// finds the variance of the binomial distribution

double binomial_variance(double n, double p) {
    return n * p * (1 - p);
}

// finds the standard deviation of the binomial distribution

double binomial_standard_deviation(double n, double p) {
    return sqrt(binomial_variance(n, p));
}

// Computes n choose r
// n being the trials and r being the desired successes

double nCr(double n, double r) {
    double numerator = n;
    double denominator = r;

    for (int i = n - 1 ; i >= ((n - r) + 1); i--) {
        numerator *= i;
    }

    for (int i = 1; i < r ; i++) {
        denominator *= i;
    }

    return numerator / denominator;
}

// calculates the probability of exactly x successes

double binomial_x_successes(double n, double p, double x) {
    return nCr(n, x) * pow(p, x) * pow(1-p, n-x);
}

// calculates the probability of a result within a range (inclusive, inclusive)

double binomial_range_successes(
    double n, double p, double lower_bound, double upper_bound) {
    double probability = 0;
    for (int i = lower_bound; i <= upper_bound; i++) {
        probability += nCr(n, i) * pow(p, i) * pow(1 - p, n - i);
    }
    return probability;
}

int main() {
    std::cout << "expected value : "
        <<binomial_expected(100, 0.5) << std::endl;

    std::cout << "variance : "
        << binomial_variance(100, 0.5) << std::endl;

    std::cout << "standard deviation : "
        << binomial_standard_deviation(100, 0.5) << std::endl;

    std::cout << "exactly 30 successes : "
        << binomial_x_successes(100, 0.5, 30) << std::endl;

    std::cout << "45 or more successes : "
        << binomial_range_successes(100, 0.5, 45, 100) << std::endl;

    return 0;
}

LANGUAGE:

DARK MODE: