A Given B vs B Given A Practice Problem
This data science coding problem helps you practice Conditional Probability & Bayes, a given b vs b given a, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Conditional Probability & Bayes.
- Problem ID: 232
- Problem key: 232-a-given-b-vs-b-given-a
- URL: https://datacrack.app/solve/232-a-given-b-vs-b-given-a
- Difficulty: easy
- Topic: Conditional Probability & Bayes
- Module: Probability
Problem Statement
# 🧩 A Given B vs B Given A
---
### 🎯 Goal
Calculate both $P(A \mid B)$ and $P(B \mid A)$, and see why they are usually different.
---
### 📖 Introduction
Conditional probability has direction.
These two expressions do not ask the same question:
$$
P(A \mid B)
$$
and:
$$
P(B \mid A)
$$
$P(A \mid B)$ means:
```text
Among the outcomes in B, how many are also in A?
```
$P(B \mid A)$ means:
```text
Among the outcomes in A, how many are also in B?
```
The overlap is the same in both calculations, but the denominator changes.
In general:
$$
P(A\mid B) \neq P(B\mid A)
$$
---
### 📥 Input
- `count_a`: number of outcomes in event $A$.
- `count_b`: number of outcomes in event $B$.
- `count_intersection`: number of outcomes in $A \cap B$.
Assume `count_a` and `count_b` are greater than 0.
### 📤 Output
Return a dictionary:
```python
{
"p_a_given_b": ...,
"p_b_given_a": ...
}
```
Round both values to 6 decimals.
---
### 💻 Task
Implement `compare_conditional_directions(count_a, count_b, count_intersection)`.
Your function should calculate:
$$
P(A\mid B)=\frac{|A\cap B|}{|B|}
$$
and:
$$
P(B\mid A)=\frac{|A\cap B|}{|A|}
$$
---
### 🧩 Starter Code
```python
def compare_conditional_directions(count_a, count_b, count_intersection):
pass
```
---
### 💡 Example
```python
compare_conditional_directions(
count_a=4,
count_b=3,
count_intersection=2
)
```
Expected output:
```python
{
"p_a_given_b": 0.666667,
"p_b_given_a": 0.5
}
```
---
### ⚠️ Common Mistakes
- Assuming $P(A\mid B)$ and $P(B\mid A)$ must be equal.
- Using the same denominator for both calculations.
- Forgetting that the event after the vertical bar is the condition.
Starter Code
def compare_conditional_directions(count_a, count_b, count_intersection):
pass