TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
conditional_probability.cpp File Reference

Conditional Probability More...

#include <cassert>
#include <cmath>
#include <iostream>
#include <stdexcept>
#include <string>
Include dependency graph for conditional_probability.cpp:

Go to the source code of this file.

Namespaces

namespace  probability
 for std::string
namespace  conditional_probability
 Functions for the Conditional Probability algorithm implementation.

Functions

double probability::conditional_probability::conditional_compute (double p_intersect, double p_given)
 Computes the conditional probability P(X|Y).
static void test ()
 Self-test implementations.
int main ()
 Main function.

Detailed Description

Conditional Probability

In probability theory, conditional probability is a measure of the probability of an event occurring, given that another event (by assumption, presumption, assertion, or evidence) is already known to have occurred.

This particular method relies on event A occurring with some sort of relationship with another event B. In this situation, the event A can be analyzed by a conditional probability with respect to B.

If the event of interest is A and the event B is known or assumed to have occurred, "the conditional probability of A given B", or "the probability of A under the condition B" is given by the formula:

\[P(A \mid B) = \frac{P(A \cap B)}{P(B)}\]

where: \(P(B) > 0\).

Author
SomrajBanik

Definition in file conditional_probability.cpp.

Function Documentation

◆ conditional_compute()

double probability::conditional_probability::conditional_compute ( double p_intersect,
double p_given )

Computes the conditional probability P(X|Y).

Parameters
p_intersectprobability that both X and Y occur, in [0, P(Y)]
p_givenprobability of the event that is given (Y), in (0, 1]
Returns
P(X|Y)
Exceptions
std::invalid_argumentif p_given is not in (0, 1], or if p_intersect is not in [0, p_given]

Definition at line 51 of file conditional_probability.cpp.

51 {
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}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 159 of file conditional_probability.cpp.

159 {
160 test(); // run self test implementations
161 return 0;
162}
void test()

◆ test()

void test ( )
static

Self-test implementations.

Returns
void

tolerance for double comparison

Definition at line 70 of file conditional_probability.cpp.

70 {
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}
double conditional_compute(double p_intersect, double p_given)
Computes the conditional probability P(X|Y).