Files
thealgorithms--c-plus-plus/probability/addition_rule.cpp
T
wehub-resource-sync 29cfe479ab
Awesome CI Workflow / Code Formatter (push) Waiting to run
Awesome CI Workflow / Compile checks (macOS-latest) (push) Blocked by required conditions
Awesome CI Workflow / Compile checks (ubuntu-latest) (push) Blocked by required conditions
Awesome CI Workflow / Compile checks (windows-latest) (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 12:37:28 +08:00

43 lines
1.1 KiB
C++

/**
* @file
* @brief Addition rule of probabilities
*/
#include <iostream>
/**
* calculates the probability of the independent events A or B for independent
* events
* \parama [in] A probability of event A
* \parama [in] B probability of event B
* \returns probability of A and B
*/
double addition_rule_independent(double A, double B) {
return (A + B) - (A * B);
}
/** Calculates the probability of the events A or B for dependent events
* note that if value of B_given_A is unknown, use chainrule to find it
* \parama [in] A probability of event A
* \parama [in] B probability of event B
* \parama [in] B_given_A probability of event B condition A
* \returns probability of A and B
*/
double addition_rule_dependent(double A, double B, double B_given_A) {
return (A + B) - (A * B_given_A);
}
/** Main function */
int main() {
double A = 0.5;
double B = 0.25;
double B_given_A = 0.05;
std::cout << "independent P(A or B) = " << addition_rule_independent(A, B)
<< std::endl;
std::cout << "dependent P(A or B) = "
<< addition_rule_dependent(A, B, B_given_A) << std::endl;
return 0;
}