mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 02:25:57 +08:00
feat: added addition_rule.cpp for probability (#732)
* added addition_rule.cpp for probability * fixed cpplint errors * Disable actions-gh-pages Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
28
probability/addition_rule.cpp
Normal file
28
probability/addition_rule.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
|
||||
// calculates the probability of the events A or B for independent events
|
||||
|
||||
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
|
||||
|
||||
double addition_rule_dependent(double A, double B, double B_given_A) {
|
||||
return (A + B) - (A * B_given_A);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user