TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
conditional_probability.cpp
Go to the documentation of this file.
1
24
25#include <cassert>
26#include <cmath>
27#include <iostream>
28#include <stdexcept>
29#include <string>
30
35namespace probability {
51double conditional_compute(double p_intersect, double p_given) {
52 if (p_given <= 0.0 || p_given > 1.0) {
53 throw std::invalid_argument("The given probability must be in (0, 1].");
54 }
55 if (p_intersect < 0.0 || p_intersect > p_given) {
56 throw std::invalid_argument(
57 "P(intersection) must be between 0 and the given probability.");
58 }
59
60 return p_intersect / p_given;
61}
62
63} // namespace conditional_probability
64} // namespace probability
65
70static void test() {
72
73 const double epsilon = 1e-9;
74
75 std::cout << "\n\n";
76 std::cout << "Testing conditional probability...\n\n";
77
78 // Test case 1
79 std::cout << "Test 1: P(A and B) = 0.2, P(B) = 0.5\n";
80 std::cout << "Expected: P(A|B) = 0.4\n";
81 std::cout << "Result: P(A|B) = " << conditional_compute(0.2, 0.5) << "\n";
82 assert(std::fabs(conditional_compute(0.2, 0.5) - 0.4) < epsilon);
83 std::cout << "--PASSED--\n\n";
84
85 // Test case 2
86 std::cout << "Test 2: A always happens when B does (B is a subset of A)\n";
87 std::cout << "Expected: P(A|B) = 1.0\n";
88 std::cout << "Result: P(A|B) = " << conditional_compute(0.5, 0.5) << "\n";
89 assert(std::fabs(conditional_compute(0.5, 0.5) - 1.0) < epsilon);
90 std::cout << "--PASSED--\n\n";
91
92 // Test case 3
93 std::cout << "Test 3: A and B never happen together (mutually exclusive "
94 "events)\n";
95 std::cout << "Expected: P(A|B) = 0.0\n";
96 std::cout << "Result: P(A|B) = " << conditional_compute(0.0, 0.3) << "\n";
97 assert(std::fabs(conditional_compute(0.0, 0.3) - 0.0) < epsilon);
98 std::cout << "--PASSED--\n\n";
99
100 // Test case 4
101 std::cout << "Test 4: One die, A = 'roll is 2', B = 'roll is even'\n";
102 std::cout << "Expected: P(A|B) = 1/3 or 0.333333(approx)\n";
103 std::cout << "Result: P(A|B) = "
104 << conditional_compute(1.0 / 6.0, 1.0 / 2.0) << " (approx) "
105 << "\n";
106 assert(std::fabs(conditional_compute(1.0 / 6.0, 1.0 / 2.0) - 1.0 / 3.0) <
107 epsilon);
108 std::cout << "--PASSED--\n\n";
109
110 // Error handling test 1 (Test case 5)
111 std::cout << "Test 5: Error handling - P(B) = 0 (invalid)\n";
112 bool thrown = false;
113 try {
114 conditional_compute(0.2, 0.0);
115 } catch (const std::invalid_argument &e) {
116 std::cout << "Error caught: " << e.what() << "\n";
117 assert(std::string(e.what()) ==
118 "The given probability must be in (0, 1].");
119 thrown = true;
120 }
121 assert(thrown);
122 std::cout << "--PASSED--\n\n";
123
124 // Error handling test 2 (Test case 6)
125 std::cout << "Test 6: Error handling - P(A and B) < 0 (invalid)\n";
126 thrown = false;
127 try {
128 conditional_compute(-0.1, 0.5);
129 } catch (const std::invalid_argument &e) {
130 std::cout << "Error caught: " << e.what() << "\n";
131 assert(std::string(e.what()) ==
132 "P(intersection) must be between 0 and the given probability.");
133 thrown = true;
134 }
135 assert(thrown);
136 std::cout << "--PASSED--\n\n";
137
138 // Error handling test 3 (Test case 7)
139 std::cout << "Test 7: Error handling - P(B) > 1 (invalid)\n";
140 thrown = false;
141 try {
142 conditional_compute(0.2, 1.5);
143 } catch (const std::invalid_argument &e) {
144 std::cout << "Error caught: " << e.what() << "\n";
145 assert(std::string(e.what()) ==
146 "The given probability must be in (0, 1].");
147 thrown = true;
148 }
149 assert(thrown);
150 std::cout << "--PASSED--\n\n";
151
152 std::cout << "All tests have successfully passed!\n";
153}
154
159int main() {
160 test(); // run self test implementations
161 return 0;
162}
void test()
double conditional_compute(double p_intersect, double p_given)
Computes the conditional probability P(X|Y).
int main()
Main function.
Functions for the Conditional Probability algorithm implementation.
for std::string