From dfb83520791e895d57f8768251f640cc4d216dbd Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Fri, 31 Jan 2020 21:03:04 -0500 Subject: [PATCH 1/6] Added montecarlo.py This algorithm uses a Monte Carlo simulation to estimate the value of pi. --- montecarlo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 montecarlo.py diff --git a/montecarlo.py b/montecarlo.py new file mode 100644 index 000000000000..d2934245b9c0 --- /dev/null +++ b/montecarlo.py @@ -0,0 +1,44 @@ +""" +@author: MatteoRaso +""" + +def piEstimator(iterations: int): + """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. + 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 + """ + import numpy as np + circle_dots = 0 + + #A local function to see if a dot lands in the circle. + def circle(x: float, y: float): + distance_from_centre = np.sqrt((x ** 2) + (y ** 2)) + #Our circle has a radius of 1, so a distance greater than 1 would land outside the circle. + if distance_from_centre > 1: + return False + else: + return True + + for i in range(0, iterations): + #Random float from -1.0 to 1.0. + x = np.random.uniform(-1.0, 1.0) + y = np.random.uniform(-1.0, 1.0) + if circle(x,y) == True: + circle_dots += 1 + + #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 = proportion * 4 + print("The estimated value of pi is ", pi) + print("The numpy value of pi is ", np.pi) + print("The total error is ", abs(np.pi - pi)) + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From b1b5755d12fc37a329f080286f0467bf7f45267d Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Fri, 31 Jan 2020 21:04:31 -0500 Subject: [PATCH 2/6] Rename montecarlo.py to maths/montecarlo.py --- montecarlo.py => maths/montecarlo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename montecarlo.py => maths/montecarlo.py (98%) diff --git a/montecarlo.py b/maths/montecarlo.py similarity index 98% rename from montecarlo.py rename to maths/montecarlo.py index d2934245b9c0..0a3f5bd4a638 100644 --- a/montecarlo.py +++ b/maths/montecarlo.py @@ -41,4 +41,4 @@ def circle(x: float, y: float): if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From b5d58763f6ac80f8df9510ed03301e4a3de38a26 Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Sun, 2 Feb 2020 02:44:03 -0500 Subject: [PATCH 3/6] Add files via upload --- montecarlo.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 montecarlo.py diff --git a/montecarlo.py b/montecarlo.py new file mode 100644 index 000000000000..13cb40db3d59 --- /dev/null +++ b/montecarlo.py @@ -0,0 +1,43 @@ +""" +@author: MatteoRaso +""" + + +def piEstimator(iterations: int): + """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. + 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 + """ + from numpy import pi, sqrt + from random import uniform + + circle_dots = 0 + + # A local function to see if a dot lands in the circle. + def circle(x: float, y: float): + 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 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) + print("The numpy value of pi is ", pi) + print("The total error is ", abs(pi - pi_estimate)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From a230e3344570d4ce25b2b350794423ab2b5892be Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Sun, 2 Feb 2020 02:45:07 -0500 Subject: [PATCH 4/6] Delete montecarlo.py --- maths/montecarlo.py | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 maths/montecarlo.py diff --git a/maths/montecarlo.py b/maths/montecarlo.py deleted file mode 100644 index 0a3f5bd4a638..000000000000 --- a/maths/montecarlo.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -@author: MatteoRaso -""" - -def piEstimator(iterations: int): - """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. - 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 - """ - import numpy as np - circle_dots = 0 - - #A local function to see if a dot lands in the circle. - def circle(x: float, y: float): - distance_from_centre = np.sqrt((x ** 2) + (y ** 2)) - #Our circle has a radius of 1, so a distance greater than 1 would land outside the circle. - if distance_from_centre > 1: - return False - else: - return True - - for i in range(0, iterations): - #Random float from -1.0 to 1.0. - x = np.random.uniform(-1.0, 1.0) - y = np.random.uniform(-1.0, 1.0) - if circle(x,y) == True: - circle_dots += 1 - - #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 = proportion * 4 - print("The estimated value of pi is ", pi) - print("The numpy value of pi is ", np.pi) - print("The total error is ", abs(np.pi - pi)) - -if __name__ == "__main__": - import doctest - doctest.testmod() From 1743993590b8b869b1bb791fbccb32f0bb70f07e Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Sun, 2 Feb 2020 02:45:41 -0500 Subject: [PATCH 5/6] Rename montecarlo.py to maths/montecarlo.py --- montecarlo.py => maths/montecarlo.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename montecarlo.py => maths/montecarlo.py (100%) diff --git a/montecarlo.py b/maths/montecarlo.py similarity index 100% rename from montecarlo.py rename to maths/montecarlo.py From 26bdb01ff5e203689ecf3fa7831779bcb69d5cb9 Mon Sep 17 00:00:00 2001 From: MatteoRaso <33975162+MatteoRaso@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:56:53 -0500 Subject: [PATCH 6/6] Update montecarlo.py --- maths/montecarlo.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/montecarlo.py b/maths/montecarlo.py index 13cb40db3d59..903012429c06 100644 --- a/maths/montecarlo.py +++ b/maths/montecarlo.py @@ -1,9 +1,10 @@ """ @author: MatteoRaso """ +from numpy import pi, sqrt +from random import uniform - -def piEstimator(iterations: int): +def pi_estimator(iterations: int): """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. @@ -13,8 +14,7 @@ def piEstimator(iterations: int): 5. Multiply this value by 4 to get your estimate of pi. 6. Print the estimated and numpy value of pi """ - from numpy import pi, sqrt - from random import uniform + circle_dots = 0