Skip to content

probability_of_n_heads_in_m_tossing #11440

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

Closed
wants to merge 8 commits into from
67 changes: 67 additions & 0 deletions maths/probability_of_n_heads_in_m_tossing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

"""

Check failure on line 2 in maths/probability_of_n_heads_in_m_tossing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E999)

maths/probability_of_n_heads_in_m_tossing.py:2:1: E999 SyntaxError: Unexpected indentation
Algorithm Explanation:
If you toss 0 time -> 0 head
Distribution [1] -> Meaning: 1 in the 0-index

If you toss 1 time -> 0 or 1 head
Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes

If you toss 2 times -> 0 to 2 heads
Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT}

Check failure on line 11 in maths/probability_of_n_heads_in_m_tossing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/probability_of_n_heads_in_m_tossing.py:11:89: E501 Line too long (103 > 88)

If you toss 3 times -> 0 to 3 heads
Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}

Check failure on line 14 in maths/probability_of_n_heads_in_m_tossing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/probability_of_n_heads_in_m_tossing.py:14:89: E501 Line too long (137 > 88)

Therefore,
Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2

Check failure on line 17 in maths/probability_of_n_heads_in_m_tossing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/probability_of_n_heads_in_m_tossing.py:17:89: E501 Line too long (101 > 88)

I used that method in my paper
Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions

Check failure on line 20 in maths/probability_of_n_heads_in_m_tossing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/probability_of_n_heads_in_m_tossing.py:20:89: E501 Line too long (89 > 88)
Journal: ACM Computing Surveys (CSUR)
"""


import numpy

def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int:
"""
Calculate the factorial of specified number (n!).

>>> import math
>>> all(factorial(i) == math.factorial(i) for i in range(20))
True
>>> Probability_of_n_heads_in_m_tossing(.2,.5)
Traceback (most recent call last):
...
ValueError: The function only accepts integer values
>>> Probability_of_n_heads_in_m_tossing(-1,5)
Traceback (most recent call last):
...
ValueError: The function is not defined for negative values
>>> Probability_of_n_heads_in_m_tossing(3,2)
Traceback (most recent call last):
...
ValueError: Head count should be smaller than toss count

>>> Probability_of_n_heads_in_m_tossing(1,1)
0.5
>>> Probability_of_n_heads_in_m_tossing(0,2)
0.25
>>> Probability_of_n_heads_in_m_tossing(2,3)
0.375
"""
if head_count != int(head_count) or toss_count != int(toss_count):
raise ValueError("The function only accepts integer values")
if head_count < 0 or toss_count < 0:
raise ValueError("The function only accepts positive values")
if head_count > toss_count:
raise ValueError("Head count should be smaller than toss count")

value = numpy.ones(1)

for iter1 in range(toss_count):
value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0)
value = value/2

return value[head_count]
Loading