-
Notifications
You must be signed in to change notification settings - Fork 185
Probability Addition Law: Compute P(A ∪ B) #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jeet009
wants to merge
11
commits into
Open-Deep-ML:main
Choose a base branch
from
Jeet009:additionLaw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5e2a9a9
Added Two New Questions(Probability)
Jeet009 558c2fc
Added Two New Questions(Probability)-184,185
Jeet009 b1d2a46
Updated the questions with fixes
Jeet009 84b8324
Updated the questions with fixes
Jeet009 b903254
Updated the questions with fixes(added new supported notations)
Jeet009 57e1293
Added Q: Linear Regression using OLS
Jeet009 f62a64c
Added new prob
Jeet009 4af6935
Added Question 186
Jeet009 1decb2f
Added Question 186
Jeet009 cd33bcf
Added new question
Jeet009 f92b0dc
Final Commit
Jeet009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added new question
- Loading branch information
commit cd33bcf4a4e337b03dd11f66778c312379bd059b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "id": "184", | ||
| "title": "Empirical Probability Mass Function (PMF)", | ||
| "difficulty": "easy", | ||
| "category": "Probability & Statistics", | ||
| "video": "", | ||
| "likes": "0", | ||
| "dislikes": "0", | ||
| "contributor": [ | ||
| { | ||
| "profile_link": "https://github.com/Jeet009", | ||
| "name": "Jeet Mukherjee" | ||
| } | ||
| ], | ||
| "description": "## Problem\n\nGiven a list of integer samples drawn from a discrete distribution, implement a function to compute the empirical Probability Mass Function (PMF). The function should return a list of `(value, probability)` pairs sorted by the value in ascending order. If the input is empty, return an empty list.", | ||
| "learn_section": "\n# Learn Section\n\n# Probability Mass Function (PMF) — Simple Explanation\n\nA **probability mass function (PMF)** describes how probabilities are assigned to the possible outcomes of a **discrete random variable**.\n\n- It tells you the chance of each specific outcome. \n- Each probability is non-negative. \n- The total of all probabilities adds up to 1.\n\n## Estimating from data\nIf the true probabilities are unknown, you can estimate them with an **empirical PMF**:\n- Count how often each outcome appears. \n- Divide by the total number of observations. \n\n## Example\nObserved sequence: `1, 2, 2, 3, 3, 3` (6 outcomes total)\n- \"1\" appears once → estimated probability = 1/6 \n- \"2\" appears twice → estimated probability = 2/6 = 1/3 \n- \"3\" appears three times → estimated probability = 3/6 = 1/2 \n\n\n ", | ||
| "starter_code": "def empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n # TODO: Implement the function\n pass", | ||
| "solution": "from collections import Counter\n\ndef empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n samples = list(samples)\n if not samples:\n return []\n total = len(samples)\n cnt = Counter(samples)\n result = [(k, cnt[k] / total) for k in sorted(cnt.keys())]\n return result", | ||
| "example": { | ||
| "input": "samples = [1, 2, 2, 3, 3, 3]", | ||
| "output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]", | ||
| "reasoning": "Counts are {1:1, 2:2, 3:3} over 6 samples, so probabilities are 1/6, 2/6, and 3/6 respectively, returned sorted by value." | ||
| }, | ||
| "test_cases": [ | ||
| { | ||
| "test": "print(empirical_pmf([1, 2, 2, 3, 3, 3]))", | ||
| "expected_output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]" | ||
| }, | ||
| { | ||
| "test": "print(empirical_pmf([5, 5, 5, 5]))", | ||
| "expected_output": "[(5, 1.0)]" | ||
| }, | ||
| { | ||
| "test": "print(empirical_pmf([]))", | ||
| "expected_output": "[]" | ||
| }, | ||
| { | ||
| "test": "print(empirical_pmf([0, 0, 1, 1, 1, 2]))", | ||
| "expected_output": "[(0, 0.3333333333333333), (1, 0.5), (2, 0.16666666666666666)]" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| { | ||
| "id": "187", | ||
| "title": "Probability Addition Law: Compute P(A ∪ B)", | ||
| "difficulty": "easy", | ||
| "category": "Probability & Statistics", | ||
| "video": "", | ||
| "likes": "0", | ||
| "dislikes": "0", | ||
| "contributor": [ | ||
| { | ||
| "profile_link": "https://github.com/Jeet009", | ||
| "name": "Jeet Mukherjee" | ||
| } | ||
| ], | ||
| "tinygrad_difficulty": "easy", | ||
| "pytorch_difficulty": "easy", | ||
| "description": "## Problem\n\nTwo events `A` and `B` in a probability space have the following probabilities:\n\n- P(A) = 0.6\n- P(B) = 0.5\n- P(A ∩ B) = 0.3\n\nUsing the probability addition law, compute `P(A ∪ B)`.\n\nImplement a function `prob_union(p_a, p_b, p_intersection)` that returns `P(A ∪ B)` as a float.\n\nRecall: P(A ∪ B) = P(A) + P(B) − P(A ∩ B).\n\nNote: If `A` and `B` are mutually exclusive (disjoint), then `P(A ∩ B) = 0` and the rule simplifies to `P(A ∪ B) = P(A) + P(B)`.", | ||
| "learn_section": "## Solution Explanation\n\nThe probability addition law for any two events A and B states:\n\n$$\nP(A \\cup B) = P(A) + P(B) - P(A \\cap B)\n$$\n\n- The union counts outcomes in A or B (or both).\n- We subtract the intersection once to correct double-counting.\n\n### Mutually exclusive (disjoint) events\nIf A and B cannot occur together, then \\(P(A \\cap B) = 0\\) and the addition rule simplifies to:\n\\[\nP(A \\cup B) = P(A) + P(B)\n\\]\n\n### Plug in the given values\n\nGiven: \\(P(A)=0.6\\), \\(P(B)=0.5\\), \\(P(A \\cap B)=0.3\\)\n\n\\[\nP(A \\cup B) = 0.6 + 0.5 - 0.3 = 0.8\n\\]\n\n### Validity checks\n- Probabilities must lie in [0, 1]. The result 0.8 is valid.\n- Given inputs must satisfy: \\(0 \\le P(A \\cap B) \\le \\min\\{P(A), P(B)\\}\\) and \\(P(A \\cap B) \\ge P(A) + P(B) - 1\\). Here, 0.3 is within [0.1, 0.5], so inputs are consistent.\n\n### Implementation outline\n- Accept three floats: `p_a`, `p_b`, `p_intersection`.\n- Optionally assert basic bounds to help users catch mistakes.\n- Return `p_a + p_b - p_intersection`.", | ||
| "starter_code": "# Implement your function below.\n\ndef prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B) using the addition law.\n\n\tAuto-detects mutually exclusive events by treating very small P(A ∩ B) as 0.\n\n\tArguments:\n\t- p_a: P(A)\n\t- p_b: P(B)\n\t- p_intersection: P(A ∩ B)\n\n\tReturns:\n\t- float: P(A ∪ B)\n\t\"\"\"\n\t# TODO: if p_intersection is ~0, return p_a + p_b; else return p_a + p_b - p_intersection\n\traise NotImplementedError", | ||
| "solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection of mutual exclusivity.\n\n\tIf p_intersection is numerically very small (≤ 1e-12), treat as 0 and\n\tuse the simplified rule P(A ∪ B) = P(A) + P(B).\n\t\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection", | ||
| "example": { | ||
| "input": "prob_union(0.6, 0.5, 0.3)", | ||
| "output": "0.8", | ||
| "reasoning": "By addition law: 0.6 + 0.5 − 0.3 = 0.8." | ||
| }, | ||
| "test_cases": [ | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))", | ||
| "expected_output": "0.8" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))", | ||
| "expected_output": "0.5" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))", | ||
| "expected_output": "0.5" | ||
| } | ||
| ], | ||
| "tinygrad_starter_code": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B). Treat very small P(A ∩ B) as 0 (mutually exclusive).\"\"\"\n\traise NotImplementedError", | ||
| "tinygrad_solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection (Tinygrad track).\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection", | ||
| "tinygrad_test_cases": [ | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))", | ||
| "expected_output": "0.8" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))", | ||
| "expected_output": "0.5" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))", | ||
| "expected_output": "0.5" | ||
| } | ||
| ], | ||
| "pytorch_starter_code": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B). Treat very small P(A ∩ B) as 0 (mutually exclusive).\"\"\"\n\traise NotImplementedError", | ||
| "pytorch_solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection (PyTorch track).\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection", | ||
| "pytorch_test_cases": [ | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))", | ||
| "expected_output": "0.8" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))", | ||
| "expected_output": "0.5" | ||
| }, | ||
| { | ||
| "test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))", | ||
| "expected_output": "0.5" | ||
| } | ||
| ] | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ## Problem | ||
|
|
||
| Two events `A` and `B` in a probability space have the following probabilities: | ||
|
|
||
| - P(A) = 0.6 | ||
| - P(B) = 0.5 | ||
| - P(A ∩ B) = 0.3 | ||
|
|
||
| Using the probability addition law, compute `P(A ∪ B)`. | ||
|
|
||
| Implement a function `prob_union(p_a, p_b, p_intersection)` that returns `P(A ∪ B)` as a float. | ||
|
|
||
| Recall: P(A ∪ B) = P(A) + P(B) − P(A ∩ B). | ||
|
|
||
| Note: If `A` and `B` are mutually exclusive (disjoint), then `P(A ∩ B) = 0` and the rule simplifies to `P(A ∪ B) = P(A) + P(B)`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "input": "prob_union(0.6, 0.5, 0.3)", | ||
| "output": "0.8", | ||
| "reasoning": "By addition law: 0.6 + 0.5 − 0.3 = 0.8." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| ## Solution Explanation | ||
|
|
||
| The probability addition law for any two events A and B states: | ||
|
|
||
| $$ | ||
| P(A \cup B) = P(A) + P(B) - P(A \cap B) | ||
| $$ | ||
|
|
||
| - The union counts outcomes in A or B (or both). | ||
| - We subtract the intersection once to correct double-counting. | ||
|
|
||
| ### Mutually exclusive (disjoint) events | ||
| If A and B cannot occur together, then \(P(A \cap B) = 0\) and the addition rule simplifies to: | ||
| \[ | ||
| P(A \cup B) = P(A) + P(B) | ||
| \] | ||
|
|
||
| ### Plug in the given values | ||
|
|
||
| Given: \(P(A)=0.6\), \(P(B)=0.5\), \(P(A \cap B)=0.3\) | ||
|
|
||
| \[ | ||
| P(A \cup B) = 0.6 + 0.5 - 0.3 = 0.8 | ||
| \] | ||
|
|
||
| ### Validity checks | ||
| - Probabilities must lie in [0, 1]. The result 0.8 is valid. | ||
| - Given inputs must satisfy: \(0 \le P(A \cap B) \le \min\{P(A), P(B)\}\) and \(P(A \cap B) \ge P(A) + P(B) - 1\). Here, 0.3 is within [0.1, 0.5], so inputs are consistent. | ||
|
|
||
| ### Implementation outline | ||
| - Accept three floats: `p_a`, `p_b`, `p_intersection`. | ||
| - Optionally assert basic bounds to help users catch mistakes. | ||
| - Return `p_a + p_b - p_intersection`. | ||
8 changes: 4 additions & 4 deletions
8
...86_pmf_normalization_constant 2/meta.json → ...ns/187_probability-addition-law/meta.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| def prob_union(p_a: float, p_b: float, p_intersection: float) -> float: | ||
| """Reference implementation for P(A ∪ B) with auto-detection of mutual exclusivity. | ||
|
|
||
| If p_intersection is numerically very small (≤ 1e-12), treat as 0 and | ||
| use the simplified rule P(A ∪ B) = P(A) + P(B). | ||
| """ | ||
| epsilon = 1e-12 | ||
| if p_intersection <= epsilon: | ||
| return p_a + p_b | ||
| return p_a + p_b - p_intersection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Implement your function below. | ||
|
|
||
| def prob_union(p_a: float, p_b: float, p_intersection: float) -> float: | ||
| """Return P(A ∪ B) using the addition law. | ||
|
|
||
| Auto-detects mutually exclusive events by treating very small P(A ∩ B) as 0. | ||
|
|
||
| Arguments: | ||
| - p_a: P(A) | ||
| - p_b: P(B) | ||
| - p_intersection: P(A ∩ B) | ||
|
|
||
| Returns: | ||
| - float: P(A ∪ B) | ||
| """ | ||
| # TODO: if p_intersection is ~0, return p_a + p_b; else return p_a + p_b - p_intersection | ||
| raise NotImplementedError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [ | ||
| { "test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))", "expected_output": "0.8" }, | ||
| { "test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))", "expected_output": "0.5" }, | ||
| { "test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))", "expected_output": "0.5" } | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for single line formulas use $ and for multiple lines use $$
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay Sure. Will keep this in mind.