2020-05-29 04:34:29 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Addition rule of probabilities
|
|
|
|
*/
|
2020-04-26 17:51:38 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-05-29 04:34:29 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
double addition_rule_independent(double A, double B)
|
|
|
|
{
|
2020-04-26 17:51:38 +08:00
|
|
|
return (A + B) - (A * B);
|
|
|
|
}
|
|
|
|
|
2020-05-29 04:34:29 +08:00
|
|
|
/** 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
|
|
|
|
*/
|
2020-05-30 07:26:30 +08:00
|
|
|
double addition_rule_dependent(double A, double B, double B_given_A)
|
|
|
|
{
|
2020-04-26 17:51:38 +08:00
|
|
|
return (A + B) - (A * B_given_A);
|
|
|
|
}
|
|
|
|
|
2020-05-29 04:34:29 +08:00
|
|
|
/** Main function */
|
2020-05-30 07:26:30 +08:00
|
|
|
int main()
|
|
|
|
{
|
2020-04-26 17:51:38 +08:00
|
|
|
double A = 0.5;
|
|
|
|
double B = 0.25;
|
|
|
|
double B_given_A = 0.05;
|
|
|
|
|
2020-05-29 04:34:29 +08:00
|
|
|
std::cout << "independent P(A or B) = " << addition_rule_independent(A, B)
|
|
|
|
<< std::endl;
|
2020-04-26 17:51:38 +08:00
|
|
|
|
|
|
|
std::cout << "dependent P(A or B) = "
|
2020-05-29 04:34:29 +08:00
|
|
|
<< addition_rule_dependent(A, B, B_given_A) << std::endl;
|
2020-04-26 17:51:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|