From 99d3dbd37edd75ebbc5fa89457b4f706f047a9d0 Mon Sep 17 00:00:00 2001 From: Sumeet Padavala Date: Mon, 23 Jul 2018 23:00:44 +0530 Subject: [PATCH 1/2] Add Python code to Monte Carlo Integration --- .../code/python/monte_carlo.py | 35 +++++++++++++++++++ .../monte_carlo_integration.md | 4 +++ 2 files changed, 39 insertions(+) create mode 100644 contents/monte_carlo_integration/code/python/monte_carlo.py diff --git a/contents/monte_carlo_integration/code/python/monte_carlo.py b/contents/monte_carlo_integration/code/python/monte_carlo.py new file mode 100644 index 000000000..b8d2920b9 --- /dev/null +++ b/contents/monte_carlo_integration/code/python/monte_carlo.py @@ -0,0 +1,35 @@ +# submitted by hybrideagle +from random import random +from math import pi + + +def inCircle(xPos, yPos): + radius = 1 + # Compute euclidian distance from origin + return (xPos * xPos + yPos * yPos) < (radius * radius) + + +def monteCarlo(n): + """ + Computes PI using the monte carlo method using `n` points + """ + piCount = 0 + + for i in range(n): + pointX = random() + pointY = random() + if inCircle(pointX, pointY): + piCount += 1 + + # This is using a quarter of the unit sphere in a 1x1 box. + # The formula is pi = (boxLength^2 / radius^2) * (piCount / n), but we + # are only using the upper quadrant and the unit circle, so we can use + # 4*piCount/n instead + # piEstimate = 4*piCount/n + piEstimate = 4 * piCount / n + print('Pi is {0:} ({1:.4f}% error)'.format( + piEstimate, (pi - piEstimate) / pi * 100)) + + +if __name__ == "__main__": + monteCarlo(100000) diff --git a/contents/monte_carlo_integration/monte_carlo_integration.md b/contents/monte_carlo_integration/monte_carlo_integration.md index 84a158e92..7c5c5edaa 100644 --- a/contents/monte_carlo_integration/monte_carlo_integration.md +++ b/contents/monte_carlo_integration/monte_carlo_integration.md @@ -59,6 +59,8 @@ each point is tested to see whether it's in the circle or not: [import:13-15, lang:"java"](code/java/MonteCarlo.java) {% sample lang="swift" %} [import:15-17 lang:"swift"](code/swift/monte_carlo.swift) +{% sample lang="python" %} +[import:6-10 lang:"python"](code/python/monte_carlo.python) {% endmethod %} If it's in the circle, we increase an internal count by one, and in the end, @@ -112,6 +114,8 @@ Feel free to submit your version via pull request, and thanks for reading! [import, lang:"java"](code/java/MonteCarlo.java) {% sample lang="swift" %} [import, lang:"swift"](code/swift/monte_carlo.swift) +{% sample lang="python" %} +[import, lang:"python"](code/python/monte_carlo.python) {% endmethod %} From f94e22021dce5bda08b74dbc42d19da0b530e068 Mon Sep 17 00:00:00 2001 From: Sumeet Padavala Date: Tue, 24 Jul 2018 09:04:06 +0530 Subject: [PATCH 2/2] Change all variables to snake case --- .../code/python/monte_carlo.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/contents/monte_carlo_integration/code/python/monte_carlo.py b/contents/monte_carlo_integration/code/python/monte_carlo.py index b8d2920b9..1ec73c33d 100644 --- a/contents/monte_carlo_integration/code/python/monte_carlo.py +++ b/contents/monte_carlo_integration/code/python/monte_carlo.py @@ -3,33 +3,34 @@ from math import pi -def inCircle(xPos, yPos): +def in_circle(x_pos, y_pos): radius = 1 # Compute euclidian distance from origin - return (xPos * xPos + yPos * yPos) < (radius * radius) + return (x_pos * x_pos + y_pos * y_pos) < (radius * radius) -def monteCarlo(n): +def monte_carlo(n): """ Computes PI using the monte carlo method using `n` points """ - piCount = 0 + pi_count = 0 for i in range(n): - pointX = random() - pointY = random() - if inCircle(pointX, pointY): - piCount += 1 + x = random() + y = random() + if in_circle(x, y): + pi_count += 1 # This is using a quarter of the unit sphere in a 1x1 box. # The formula is pi = (boxLength^2 / radius^2) * (piCount / n), but we # are only using the upper quadrant and the unit circle, so we can use # 4*piCount/n instead # piEstimate = 4*piCount/n - piEstimate = 4 * piCount / n + pi_estimate = 4 * pi_count / n print('Pi is {0:} ({1:.4f}% error)'.format( - piEstimate, (pi - piEstimate) / pi * 100)) + pi_estimate, (pi - pi_estimate) / pi * 100)) +# If this file was run directly if __name__ == "__main__": - monteCarlo(100000) + monte_carlo(100000)