In C++, binomial random variables represent the results of a series of separate experiments, where each experiment can result in either success or failure. These experiments adhere to the binomial distribution, with "n" denoting the total number of experiments and "p" representing the probability of success in each individual experiment.
Properties of Binomial Random Variables:
The following five properties of binomial random variables:
- Fixed Number of Trials (n) The outcomes of a fixed number ('n') of independent trials are represented by binomial random variables. This fixed number remains constant throughout the process or experiment.
- Two Possible Results In a binomial experiment, there are two possible outcomes for each trial: success or failure. Only one of these possible outcomes may happen at a time since they are mutually exclusive.
- Constant Probability of Success (p) The probability of success, or "p", remains the same at every trial. It indicates that each trial has the same chance of success.
- Independent Trials Every trial's result is distinct from the others. Trial results are independent of one another. This feature makes computations easier to understand and allows the use of probability rules.
- Discrete Values Non-negative integers are the discrete values that binomial random variables can take. The values, which range from 0 to 'n', indicate the number of successes out of 'n' trials.
Binomial Random Variable Formula:
The formula provided calculates the probability of a binomial random variable.
P(X=k) = n!/k!*(n-k)!*p^k*(1-p)^(n-k)
- 'n' is the number of trials.
- 'p' is the probability of success in each trial.
- 'k' is the number of successes in n trials.
For a binomial random variable X, the mean and variance can be calculated using the formulas below, with 'n' representing the total number of trials and 'p' indicating the probability of success.
Expected Value (Mean):
E(X) = n×p
Variance:
Var(X)=n×p×(1−p)
Example:
Let's consider an example to demonstrate the Binomial Random Variable concept in C++.
#include <iostream>
#include <cmath>
using namespace std;
int nCr(int n, int r)
{
if (r > n / 2)
r = n - r;
int result = 1;
for (int i = 1; i <= r; i++)
{
result *= (n - r + i);
result /= i;
}
return result;
}
float BinomialProbability(int n, int k, float p)
{
return nCr(n, k) * pow(p, k) *
pow(1 - p, n - k);
}
int main()
{
int n, k, m;
float p;
cout << "Please enter the number of coin tosses (n): ";
cin >> n;
cout << "Enter the number of heads (k): ";
cin >> k;
cout << "Enter the probability of each head (p): ";
cin >> p;
if (n < 0 || m < 0 || p < 0 || p > 1)
{
cout << "Invalid input. Please enter non-negative values for n and k, and probability s between 0 and 1." << endl;
return 1;
}
float prob_ability = BinomialProbability(n, k, p);
cout << "The probability of " << k;
cout << " heads when a coin is tossed " << n;
cout << " times where the probability of each head is " << p << endl;
cout << " is = " << prob_ability << endl;
return 0;
}
Output:
Please enter the number of coin tosses (n): 10
Enter the number of heads (k): 5
Enter the probability of each head (p): 0.5
The probability of 5 heads when a coin is tossed 10 times where the probability of each head is 0.5
is = 0.246094
Explanation:
This C++ code calculates the likelihood of obtaining a specific number of heads (k) when flipping a biased coin n times, where each head's probability is denoted as "p".
The formula nCr = n! / (r! * (n - r)!) is employed by the nCr function to compute the combination "n choose r". This method optimizes calculations by interchanging 'r' with 'n - r' when needed to streamline computation.
The BinomialProbability function utilizes the binomial probability equation and the combination function nCr to calculate the likelihood of obtaining precisely 'k' occurrences of heads out of 'n' trials, where each trial has a probability 'p' of resulting in heads.
The 'n', 'k', and 'p' values are specified by the user within the main function. The function validates the provided inputs to confirm that 'p' falls within the range of 0 to 1, triggering an error message display if this condition is not met.
The function calculates the likelihood of obtaining 'm' heads in 'n' coin tosses using a biased coin, and if the provided inputs are valid, it will show the result. Finally, it returns 0 to indicate a successful completion.