From a15efa71d864e27817c6d8cc0698bda35aa25c8a Mon Sep 17 00:00:00 2001 From: Tyson Cung Date: Sat, 11 Apr 2026 23:58:53 +0000 Subject: [PATCH] fix(statistics): add probability() function (#105) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement probability(favorable_outcomes, possible_outcomes) in statistics.py. Returns favorable_outcomes / possible_outcomes as a float — e.g. probability(1, 6) ≈ 0.1667 (rolling a specific value on a dice). Guards: - Raises ValueError when possible_outcomes == 0 - Raises ValueError when either argument is negative --- simple_equ/economics/statistics.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/simple_equ/economics/statistics.py b/simple_equ/economics/statistics.py index f9be6c2..eb6f38b 100644 --- a/simple_equ/economics/statistics.py +++ b/simple_equ/economics/statistics.py @@ -97,6 +97,26 @@ def linear_regression(x: list[float] | list[int], y: list[float] | list[int]) -> return slope, intercept +def probability(favorable_outcomes: int | float, possible_outcomes: int | float) -> float: + """[Summary]: Return the probability of an event as a float. + + [Description]: Computes the classical probability by dividing the number of + favorable outcomes by the total number of possible outcomes. Raises a + ValueError when possible_outcomes is zero or when either argument is + negative. + + [Usage]: Typical usage example: + + result = probability(1, 6) # probability of rolling a 4 on a dice + print(result) # 0.16666... + """ + if possible_outcomes == 0: + raise ValueError("possible_outcomes must be greater than zero") + if favorable_outcomes < 0 or possible_outcomes < 0: + raise ValueError("outcomes must be non-negative") + return favorable_outcomes / possible_outcomes + + def dot(x, w): """[Summary]: Return the dot product of two equally sized iterables.