From fa61d71fd970c6f3de81c99674bd838eca5b07ec Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 14:32:12 +0300 Subject: [PATCH 01/10] Add files via upload --- maths/binomial_distribution.py | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 maths/binomial_distribution.py diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py new file mode 100644 index 000000000000..199b5b691b2b --- /dev/null +++ b/maths/binomial_distribution.py @@ -0,0 +1,66 @@ +"""For more information about the Binomial Distribution - + https://en.wikipedia.org/wiki/Binomial_distribution""" + + +def binomial_distribution(k, n, p) -> float: + """ + + Returns probability of k successes out of n tries, + with p probability for one success + + use: binomial_distribution(k, n, p): + k - successes + n - independent Bernoulli trials + p - probability for one succes + + The function uses the factorial function + in order to calculate the binomial coefficient + + >>> binomial_distribution(3, 5, 0.7) + 0.30870000000000003 + + >>> binomial_distribution (2, 4, 0.5) + 0.375 + + >>> binomial_distribution (2, 4, -0.5) + Traceback (most recent call last): + ... + raise ValueError("p - Probability has to be in range of 1 - 0") + ValueError: p - Probability has to be in range of 1 - 0 + """ + if k > n: + raise ValueError("""k must be lower or equal to n""") + if n < 0 or k < 0 or type(k) != int or type(n) != int: + raise ValueError("the function is defined for non-negative integers k and n") + if p > 1 or p < 0: + raise ValueError("p - Probability has to be in range of 1 - 0") + probability = (p**k)*(1-p)**(n-k) + # Calculate the binomial coefficient: + # Calculate n! / k!(n-k)! + coefficient = factorial(n)/(factorial(k)*factorial(n-k)) + + return probability * coefficient + + +# Implementation of the factorial function, +# used to calculate the binomial coefficient: +def factorial(n) -> int: + """ + Factorial - The product of all positive integers less than or equal to n + """ + + if type(n) != int: + raise ValueError("factorial(n) accepts only integer values") + if n < 0: + raise ValueError("factorial(n) works only for non-negative numbers") + if n == 0: + return 1 + result = 1 + for i in range(1, n+1): + result *= i + return result + +if __name__ == "__main__": + print ("Probability of 2 successes out of 4 trails") + print ("with probability of 0.75 is : ") + print (str(binomial_distribution(2, 4, 0.75))) From 5bd59c2fd646657825712811895790d19f328266 Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 16:30:21 +0300 Subject: [PATCH 02/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 36 +++++++++++++--------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 199b5b691b2b..3d407591ba0e 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -2,17 +2,12 @@ https://en.wikipedia.org/wiki/Binomial_distribution""" -def binomial_distribution(k, n, p) -> float: +def binomial_distribution(successes: int, trials: int, prob: float) -> float: """ Returns probability of k successes out of n tries, with p probability for one success - use: binomial_distribution(k, n, p): - k - successes - n - independent Bernoulli trials - p - probability for one succes - The function uses the factorial function in order to calculate the binomial coefficient @@ -22,22 +17,20 @@ def binomial_distribution(k, n, p) -> float: >>> binomial_distribution (2, 4, 0.5) 0.375 - >>> binomial_distribution (2, 4, -0.5) - Traceback (most recent call last): - ... - raise ValueError("p - Probability has to be in range of 1 - 0") - ValueError: p - Probability has to be in range of 1 - 0 """ - if k > n: - raise ValueError("""k must be lower or equal to n""") - if n < 0 or k < 0 or type(k) != int or type(n) != int: - raise ValueError("the function is defined for non-negative integers k and n") - if p > 1 or p < 0: - raise ValueError("p - Probability has to be in range of 1 - 0") - probability = (p**k)*(1-p)**(n-k) + if successes > trials: + raise ValueError("""successes must be lower or equal to trials""") + if trials < 0 or successes < 0: + raise ValueError("the function is defined for non-negative integers") + if type(successes) != int or type(trials) != int: + raise ValueError("the function is defined for non-negative integers") + if prob > 1 or prob < 0: + raise ValueError("prob has to be in range of 1 - 0") + probability = (prob**successes)*(1-prob)**(trials-successes) # Calculate the binomial coefficient: # Calculate n! / k!(n-k)! - coefficient = factorial(n)/(factorial(k)*factorial(n-k)) + coefficient = factorial(trials) + coefficient /= (factorial(successes)*factorial(trials-successes)) return probability * coefficient @@ -61,6 +54,5 @@ def factorial(n) -> int: return result if __name__ == "__main__": - print ("Probability of 2 successes out of 4 trails") - print ("with probability of 0.75 is : ") - print (str(binomial_distribution(2, 4, 0.75))) + from doctest import testmod + testmod() From 0ac09bc01a14ca2957fd5511fdabbca4c33befe6 Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 16:48:37 +0300 Subject: [PATCH 03/10] Update maths/binomial_distribution.py Co-authored-by: Christian Clauss --- maths/binomial_distribution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 3d407591ba0e..673565991f14 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -22,7 +22,7 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: raise ValueError("""successes must be lower or equal to trials""") if trials < 0 or successes < 0: raise ValueError("the function is defined for non-negative integers") - if type(successes) != int or type(trials) != int: + if not isinstance(successes, int) or not isinstance(trials, int): raise ValueError("the function is defined for non-negative integers") if prob > 1 or prob < 0: raise ValueError("prob has to be in range of 1 - 0") From 3e864039b1443b184f0e8aafbfc99cf0caccc0a1 Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 18:13:59 +0300 Subject: [PATCH 04/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 673565991f14..3f864eb7fe1d 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -22,15 +22,15 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: raise ValueError("""successes must be lower or equal to trials""") if trials < 0 or successes < 0: raise ValueError("the function is defined for non-negative integers") - if not isinstance(successes, int) or not isinstance(trials, int): + if type(successes) != int or type(trials) != int: raise ValueError("the function is defined for non-negative integers") if prob > 1 or prob < 0: raise ValueError("prob has to be in range of 1 - 0") - probability = (prob**successes)*(1-prob)**(trials-successes) + probability = (prob ** successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: # Calculate n! / k!(n-k)! coefficient = factorial(trials) - coefficient /= (factorial(successes)*factorial(trials-successes)) + coefficient /= (factorial(successes) * factorial(trials - successes)) return probability * coefficient @@ -49,10 +49,11 @@ def factorial(n) -> int: if n == 0: return 1 result = 1 - for i in range(1, n+1): + for i in range(1, n + 1): result *= i return result + if __name__ == "__main__": from doctest import testmod testmod() From 491171f8d212dfd71a610ace2c440da089c98f31 Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 18:32:03 +0300 Subject: [PATCH 05/10] Update maths/binomial_distribution.py Co-authored-by: Christian Clauss --- maths/binomial_distribution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 3f864eb7fe1d..b9c781e7f87e 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -22,7 +22,7 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: raise ValueError("""successes must be lower or equal to trials""") if trials < 0 or successes < 0: raise ValueError("the function is defined for non-negative integers") - if type(successes) != int or type(trials) != int: + if not isinstance(successes, int) or not isinstance(trials, int): raise ValueError("the function is defined for non-negative integers") if prob > 1 or prob < 0: raise ValueError("prob has to be in range of 1 - 0") From 5b6709393572376878877af80122ec92864b829e Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 18:35:27 +0300 Subject: [PATCH 06/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index b9c781e7f87e..470f3fb0583b 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -42,7 +42,7 @@ def factorial(n) -> int: Factorial - The product of all positive integers less than or equal to n """ - if type(n) != int: + if not isinstance(n, int): raise ValueError("factorial(n) accepts only integer values") if n < 0: raise ValueError("factorial(n) works only for non-negative numbers") From ff64627d87e287e5f189f7883826c6ff58a84d3f Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 20:46:25 +0300 Subject: [PATCH 07/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 470f3fb0583b..4e198759f2c1 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -1,3 +1,4 @@ +from math import factorial """For more information about the Binomial Distribution - https://en.wikipedia.org/wiki/Binomial_distribution""" @@ -35,23 +36,6 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: return probability * coefficient -# Implementation of the factorial function, -# used to calculate the binomial coefficient: -def factorial(n) -> int: - """ - Factorial - The product of all positive integers less than or equal to n - """ - - if not isinstance(n, int): - raise ValueError("factorial(n) accepts only integer values") - if n < 0: - raise ValueError("factorial(n) works only for non-negative numbers") - if n == 0: - return 1 - result = 1 - for i in range(1, n + 1): - result *= i - return result if __name__ == "__main__": From 82b182394436f5e3953b297d7ace19f3d8125318 Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 20:51:44 +0300 Subject: [PATCH 08/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 4e198759f2c1..664bafcb28ef 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -36,8 +36,6 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: return probability * coefficient - - if __name__ == "__main__": from doctest import testmod testmod() From c4b3196faa294f98a7115bff932679cd42becb4a Mon Sep 17 00:00:00 2001 From: XxSamixX123 <60960667+XxSamixX123@users.noreply.github.com> Date: Mon, 13 Jul 2020 21:08:56 +0300 Subject: [PATCH 09/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 664bafcb28ef..130006b492f4 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -30,7 +30,7 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: probability = (prob ** successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: # Calculate n! / k!(n-k)! - coefficient = factorial(trials) + coefficient = float(factorial(trials)) coefficient /= (factorial(successes) * factorial(trials - successes)) return probability * coefficient From 26b67ace37f4f48ccd7e2c1aaa42e431b1d56298 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 13 Jul 2020 20:42:59 +0200 Subject: [PATCH 10/10] Update binomial_distribution.py --- maths/binomial_distribution.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index 130006b492f4..a74a5a7ed994 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -1,23 +1,20 @@ -from math import factorial """For more information about the Binomial Distribution - https://en.wikipedia.org/wiki/Binomial_distribution""" +from math import factorial def binomial_distribution(successes: int, trials: int, prob: float) -> float: """ + Return probability of k successes out of n tries, with p probability for one + success - Returns probability of k successes out of n tries, - with p probability for one success - - The function uses the factorial function - in order to calculate the binomial coefficient + The function uses the factorial function in order to calculate the binomial + coefficient >>> binomial_distribution(3, 5, 0.7) 0.30870000000000003 - >>> binomial_distribution (2, 4, 0.5) 0.375 - """ if successes > trials: raise ValueError("""successes must be lower or equal to trials""") @@ -25,17 +22,19 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: raise ValueError("the function is defined for non-negative integers") if not isinstance(successes, int) or not isinstance(trials, int): raise ValueError("the function is defined for non-negative integers") - if prob > 1 or prob < 0: + 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: - # Calculate n! / k!(n-k)! + # Calculate the binomial coefficient: n! / k!(n-k)! coefficient = float(factorial(trials)) - coefficient /= (factorial(successes) * factorial(trials - successes)) - + coefficient /= factorial(successes) * factorial(trials - successes) return probability * coefficient if __name__ == "__main__": from doctest import testmod + testmod() + print("Probability of 2 successes out of 4 trails") + print("with probability of 0.75 is:", end=" ") + print(binomial_distribution(2, 4, 0.75))