Skip to content

Commit c987949

Browse files
authored
Update probability_of_n_heads_in_m_tossing.py
1 parent 205cb87 commit c987949

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

maths/probability_of_n_heads_in_m_tossing.py

+41-40
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
1+
12
import numpy as np
23

34

45
def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int:
56
"""
6-
The probability of getting exactly n heads in m tossing.
7+
The probability of getting exactly n heads in m tossing.
78
8-
Algorithm Explanation:
9-
If you toss 0 time -> 0 head
10-
Distribution [1] -> Meaning: 1 in the 0-index
9+
Algorithm Explanation:
10+
If you toss 0 time -> 0 head
11+
Distribution [1] -> Meaning: 1 in the 0-index
1112
12-
If you toss 1 time -> 0 or 1 head
13-
Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes
13+
If you toss 1 time -> 0 or 1 head
14+
Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes
1415
15-
If you toss 2 times -> 0 to 2 heads
16-
Distribution [0.25 0.5 0.25] ->
17-
Meaning: probability of n heads from the distribution
18-
{HH, HT, TH, TT}
16+
If you toss 2 times -> 0 to 2 heads
17+
Distribution [0.25 0.5 0.25] ->
18+
Meaning: probability of n heads from the distribution
19+
{HH, HT, TH, TT}
1920
20-
If you toss 3 times -> 0 to 3 heads
21-
Distribution [0.125 0.375 0.375 0.125] ->
22-
Meaning: probability of n heads from the distribution
23-
{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}
21+
If you toss 3 times -> 0 to 3 heads
22+
Distribution [0.125 0.375 0.375 0.125] ->
23+
Meaning: probability of n heads from the distribution
24+
{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT}
2425
25-
Therefore,
26-
Probability_distribution(N+1) =
27-
[Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2
26+
Therefore,
27+
Probability_distribution(N+1) =
28+
[Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2
2829
29-
I used that method in my paper
30-
Titled: Uncertainty-aware Decisions in Cloud Computing:
31-
Foundations and Future Directions
32-
Journal: ACM Computing Surveys (CSUR)
30+
I used that method in my paper
31+
Titled: Uncertainty-aware Decisions in Cloud Computing:
32+
Foundations and Future Directions
33+
Journal: ACM Computing Surveys (CSUR)
3334
34-
>>> import numpy as np
35-
>>> Probability_of_n_heads_in_m_tossing(.2,.5)
36-
Traceback (most recent call last):
37-
...
38-
ValueError: The function only accepts integer values
39-
>>> Probability_of_n_heads_in_m_tossing(-1,5)
40-
Traceback (most recent call last):
41-
...
42-
ValueError: The function is not defined for negative values
43-
>>> Probability_of_n_heads_in_m_tossing(3,2)
44-
Traceback (most recent call last):
45-
...
46-
ValueError: Head count should be smaller than toss count
35+
>>> import numpy as np
36+
>>> probability_of_n_heads_in_m_tossing(.2,.5)
37+
Traceback (most recent call last):
38+
...
39+
ValueError: The function only accepts integer values
40+
>>> probability_of_n_heads_in_m_tossing(-1,5)
41+
Traceback (most recent call last):
42+
...
43+
ValueError: The function is not defined for negative values
44+
>>> probability_of_n_heads_in_m_tossing(3,2)
45+
Traceback (most recent call last):
46+
...
47+
ValueError: Head count should be smaller than toss count
4748
48-
>>> Probability_of_n_heads_in_m_tossing(1,1)
49-
0.5
50-
>>> Probability_of_n_heads_in_m_tossing(0,2)
51-
0.25
52-
>>> Probability_of_n_heads_in_m_tossing(2,3)
53-
0.375
49+
>>> probability_of_n_heads_in_m_tossing(1,1)
50+
0.5
51+
>>> probability_of_n_heads_in_m_tossing(0,2)
52+
0.25
53+
>>> probability_of_n_heads_in_m_tossing(2,3)
54+
0.375
5455
"""
5556
if head_count != int(head_count) or toss_count != int(toss_count):
5657
raise ValueError("The function only accepts integer values")

0 commit comments

Comments
 (0)