Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions maths/binomial_distribution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""For more information about the Binomial Distribution -
https://en.wikipedia.org/wiki/Binomial_distribution"""

from math import factorial

from math import comb

Check failure on line 4 in maths/binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/binomial_distribution.py:4:22: W291 Trailing whitespace help: Remove trailing whitespace

Check failure on line 4 in maths/binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

maths/binomial_distribution.py:4:1: I001 Import block is un-sorted or un-formatted help: Organize imports

Check failure on line 4 in maths/binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/binomial_distribution.py:4:22: W291 Trailing whitespace help: Remove trailing whitespace

Check failure on line 4 in maths/binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

maths/binomial_distribution.py:4:1: I001 Import block is un-sorted or un-formatted help: Organize imports

def binomial_distribution(successes: int, trials: int, prob: float) -> float:
"""
Expand All @@ -26,9 +25,7 @@
if not 0 < prob < 1:
raise ValueError("prob has to be in range of 1 - 0")
probability = (prob**successes) * ((1 - prob) ** (trials - successes))
# Calculate the binomial coefficient: n! / k!(n-k)!
coefficient = float(factorial(trials))
coefficient /= factorial(successes) * factorial(trials - successes)
coefficient = comb(trials, successes)
return probability * coefficient


Expand Down
8 changes: 8 additions & 0 deletions maths/test_binomial_distribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import pytest
from maths.binomial_distribution import binomial_distribution

Check failure on line 3 in maths/test_binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

maths/test_binomial_distribution.py:2:1: I001 Import block is un-sorted or un-formatted help: Organize imports

Check failure on line 3 in maths/test_binomial_distribution.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

maths/test_binomial_distribution.py:2:1: I001 Import block is un-sorted or un-formatted help: Organize imports

def test_large_number_of_trials() -> None:
assert binomial_distribution(85, 171, 0.5) == pytest.approx(
0.060749764626165485
)
Loading