From eca966e46ce37aa5fb71fd1c76860aa6e61a4c40 Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sat, 22 Feb 2020 21:05:23 +0530 Subject: [PATCH 1/9] add example to estimate area under line using montecarlo --- maths/montecarlo.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index ce8f69f64a15..e02130918186 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -37,6 +37,29 @@ def circle(x: float, y: float): print("The total error is ", abs(pi - pi_estimate)) +def area_under_line_estimator(iterations: int): + """An implementation of the Monte Carlo method to find area under + y = x where x lies between 0 to 1 + 1. Let x be a uniformly distributed random variable between 0 and 1 + 2. expected value of x = integration of x from 0 to 1 + 3. Finding expected value of x: + a. Repeatedly draw x from uniform distribution + b. expected value = average of those values + 4. Actual value = 1/2 + 5. Print the estimated, actual and error value + """ + + estimate = sum(uniform(0,1) for i in range(iterations)) / iterations + + print("******************") + print("Estimating area under y=x where x varies from 0 to 1") + print("Expected value is ", 0.5) + print("Estimated value is ", estimate) + print("Total error is ", abs(estimate - 0.5)) + print("******************") + + return + if __name__ == "__main__": import doctest From 445cc49f34e50106e196f4319fc5aaec00fa05ac Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sat, 22 Feb 2020 21:48:03 +0530 Subject: [PATCH 2/9] separate estimate func and print statements --- maths/montecarlo.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index e02130918186..b7a511b46bd6 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -37,7 +37,7 @@ def circle(x: float, y: float): print("The total error is ", abs(pi - pi_estimate)) -def area_under_line_estimator(iterations: int): +def area_under_line_estimator(iterations: int) -> float: """An implementation of the Monte Carlo method to find area under y = x where x lies between 0 to 1 1. Let x be a uniformly distributed random variable between 0 and 1 @@ -46,11 +46,23 @@ def area_under_line_estimator(iterations: int): a. Repeatedly draw x from uniform distribution b. expected value = average of those values 4. Actual value = 1/2 - 5. Print the estimated, actual and error value + 5. Returns estimated value """ estimate = sum(uniform(0,1) for i in range(iterations)) / iterations + return estimate + + +def area_under_line_estimator_check(iterations: int): + """ Checks estimation error for area_under_line_estimator func + 1. Calls "area_under_line_estimator" function + 2. Compares with the expected value + 3. Prints estimated, expected and error value + """ + + estimate = area_under_line_estimator(iterations) + print("******************") print("Estimating area under y=x where x varies from 0 to 1") print("Expected value is ", 0.5) @@ -60,6 +72,7 @@ def area_under_line_estimator(iterations: int): return + if __name__ == "__main__": import doctest From cdf79d13c446d3675533a36921e5a3a9cb4fbfee Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sat, 22 Feb 2020 21:58:44 +0530 Subject: [PATCH 3/9] use mean from stats package --- maths/montecarlo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index b7a511b46bd6..6ce68005b152 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -3,6 +3,7 @@ """ from numpy import pi, sqrt from random import uniform +from statistics import mean def pi_estimator(iterations: int): @@ -49,7 +50,7 @@ def area_under_line_estimator(iterations: int) -> float: 5. Returns estimated value """ - estimate = sum(uniform(0,1) for i in range(iterations)) / iterations + estimate = mean(uniform(0,1) for i in range(iterations)) return estimate From 8e82dbc4843082e193649b4ebe2dde0caec75919 Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sat, 22 Feb 2020 22:11:18 +0530 Subject: [PATCH 4/9] avoid creating extra variable --- maths/montecarlo.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index 6ce68005b152..9cda4e3b2230 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -50,9 +50,7 @@ def area_under_line_estimator(iterations: int) -> float: 5. Returns estimated value """ - estimate = mean(uniform(0,1) for i in range(iterations)) - - return estimate + return mean(uniform(0,1) for i in range(iterations)) def area_under_line_estimator_check(iterations: int): From 44bc2fd4816ca62baa942c6af1a0d78235a51067 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Feb 2020 18:35:19 +0100 Subject: [PATCH 5/9] min_value: float=0.0, max_value: float=1.0 --- maths/montecarlo.py | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index 9cda4e3b2230..4cc9077ddf75 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -7,30 +7,26 @@ def pi_estimator(iterations: int): - """An implementation of the Monte Carlo method used to find pi. + """ + An implementation of the Monte Carlo method used to find pi. 1. Draw a 2x2 square centred at (0,0). 2. Inscribe a circle within the square. 3. For each iteration, place a dot anywhere in the square. - 3.1 Record the number of dots within the circle. + a. Record the number of dots within the circle. 4. After all the dots are placed, divide the dots in the circle by the total. 5. Multiply this value by 4 to get your estimate of pi. 6. Print the estimated and numpy value of pi """ - - circle_dots = 0 - # A local function to see if a dot lands in the circle. - def circle(x: float, y: float): + def in_circle(x: float, y: float) -> bool: distance_from_centre = sqrt((x ** 2) + (y ** 2)) # Our circle has a radius of 1, so a distance greater than 1 would land outside the circle. return distance_from_centre <= 1 - circle_dots = sum( - int(circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0))) for i in range(iterations) + # The proportion of guesses that landed in the circle + proportion = mean( + int(in_circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0))) for _ in range(iterations) ) - - # The proportion of guesses that landed within the circle - proportion = circle_dots / iterations # The ratio of the area for circle to square is pi/4. pi_estimate = proportion * 4 print("The estimated value of pi is ", pi_estimate) @@ -38,38 +34,35 @@ def circle(x: float, y: float): print("The total error is ", abs(pi - pi_estimate)) -def area_under_line_estimator(iterations: int) -> float: - """An implementation of the Monte Carlo method to find area under - y = x where x lies between 0 to 1 - 1. Let x be a uniformly distributed random variable between 0 and 1 - 2. expected value of x = integration of x from 0 to 1 +def area_under_line_estimator(iterations: int, min_value: float=0.0, max_value: float=1.0) -> float: + """ + An implementation of the Monte Carlo method to find area under + y = x where x lies between min_value to max_value + 1. Let x be a uniformly distributed random variable between min_value to max_value + 2. Expected value of x = integration of x from min_value to max_value 3. Finding expected value of x: a. Repeatedly draw x from uniform distribution - b. expected value = average of those values + b. Expected value = average of those values 4. Actual value = 1/2 5. Returns estimated value """ - - return mean(uniform(0,1) for i in range(iterations)) + return mean(uniform(min_value, max_value) for _ in range(iterations)) -def area_under_line_estimator_check(iterations: int): - """ Checks estimation error for area_under_line_estimator func +def area_under_line_estimator_check(iterations: int) -> None: + """ + Checks estimation error for area_under_line_estimator func 1. Calls "area_under_line_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value """ - estimate = area_under_line_estimator(iterations) - print("******************") print("Estimating area under y=x where x varies from 0 to 1") print("Expected value is ", 0.5) print("Estimated value is ", estimate) print("Total error is ", abs(estimate - 0.5)) print("******************") - - return if __name__ == "__main__": From e342d6d57e5aea9a8b3bfb25d93ca19903028765 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Feb 2020 18:45:27 +0100 Subject: [PATCH 6/9] Update montecarlo.py --- maths/montecarlo.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index 4cc9077ddf75..4980c5c55c8c 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -20,7 +20,8 @@ def pi_estimator(iterations: int): # A local function to see if a dot lands in the circle. def in_circle(x: float, y: float) -> bool: distance_from_centre = sqrt((x ** 2) + (y ** 2)) - # Our circle has a radius of 1, so a distance greater than 1 would land outside the circle. + # Our circle has a radius of 1, so a distance + # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle @@ -34,7 +35,9 @@ def in_circle(x: float, y: float) -> bool: print("The total error is ", abs(pi - pi_estimate)) -def area_under_line_estimator(iterations: int, min_value: float=0.0, max_value: float=1.0) -> float: +def area_under_line_estimator(iterations: int, + min_value: float=0.0, + max_value: float=1.0) -> float: """ An implementation of the Monte Carlo method to find area under y = x where x lies between min_value to max_value From def63145ccfe2ddafa6148d0e1c98d1bceaf8ad4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Feb 2020 18:58:27 +0100 Subject: [PATCH 7/9] Update montecarlo.py --- maths/montecarlo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index 4980c5c55c8c..8faa041ce06f 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -58,6 +58,7 @@ def area_under_line_estimator_check(iterations: int) -> None: 1. Calls "area_under_line_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value + >>> area_under_line_estimator_check() """ estimate = area_under_line_estimator(iterations) print("******************") From 01694677d3f23b6f33c1da2dc7fc198a75817c18 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Feb 2020 18:58:43 +0100 Subject: [PATCH 8/9] Rename montecarlo.py to monte_carlo.py --- maths/{montecarlo.py => monte_carlo.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{montecarlo.py => monte_carlo.py} (100%) diff --git a/maths/montecarlo.py b/maths/monte_carlo.py similarity index 100% rename from maths/montecarlo.py rename to maths/monte_carlo.py From dfd5901bbd749cda820600d54076909c74bb5c8f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 22 Feb 2020 19:06:28 +0100 Subject: [PATCH 9/9] Update monte_carlo.py --- maths/monte_carlo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 8faa041ce06f..4980c5c55c8c 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -58,7 +58,6 @@ def area_under_line_estimator_check(iterations: int) -> None: 1. Calls "area_under_line_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value - >>> area_under_line_estimator_check() """ estimate = area_under_line_estimator(iterations) print("******************")