Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Py monte carlo #184

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified contents/bitlogic/res/nand.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/bitlogic/res/nor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/bitlogic/res/not.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/bitlogic/res/or.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/bitlogic/res/xor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/FT_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/butterfly_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/fft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/radix-2screen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/radix-2screen_positive.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/radix-8screen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/cooley_tukey/res/sinusoid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/euclidean_algorithm/res/modulus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/euclidean_algorithm/res/subtraction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/forward_euler_method/res/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/forward_euler_method/res/instability.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/git_and_version_control/res/clone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/git_and_version_control/res/fork.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/huffman_encoding/res/huffman_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions contents/monte_carlo_integration/code/python/monte_carlo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import math
import random


def in_circle(x, y, radius = 1):
"""Return True if the point is in the circle and False otherwise."""
return (x*x + y*y) < radius*radius

def monte_carlo(n_samples, radius = 1):
"""Return the estimate of pi using the monte carlo algorithm."""
in_circle_count = 0
for i in range(n_samples):

# Sample x, y from the uniform distribution
x = random.uniform(0, radius)
y = random.uniform(0, radius)

# Count the number of points inside the circle
if(in_circle(x, y, radius)):
in_circle_count += 1

# Since we've generated points in upper left quadrant ([0,radius], [0, radius])
# We need to multiply the number of points by 4
pi_estimate = 4 * in_circle_count / (n_samples)

return pi_estimate

if __name__ == '__main__':

pi_estimate = monte_carlo(100000)
percent_error = 100*abs(math.pi - pi_estimate)/math.pi

print("The estimate of pi is: {:.3f}".format(pi_estimate))
print("The percent error is: {:.3f}".format(percent_error))

4 changes: 4 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="py" %}
[import:5-7 lang:"python"](code/python/monte_carlo.py)
{% endmethod %}

If it's in the circle, we increase an internal count by one, and in the end,
Expand Down Expand Up @@ -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="py" %}
[import, lang:"python"](code/python/monte_carlo.py)
{% endmethod %}


Expand Down
Binary file modified contents/monte_carlo_integration/res/13311.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/monte_carlo_integration/res/195583.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/monte_carlo_integration/res/31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/monte_carlo_integration/res/monte_carlo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/monte_carlo_integration/res/square_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/notation/res/cplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified contents/taylor_series_expansion/res/function_sum.png
Binary file modified contents/tree_traversal/code/scratch/scratch_tree.png
Binary file modified contents/tree_traversal/res/3-tree.png
Binary file modified contents/tree_traversal/res/BFS_simple.png
Binary file modified contents/tree_traversal/res/DFS_in.png
Binary file modified contents/tree_traversal/res/DFS_post.png
Binary file modified contents/tree_traversal/res/DFS_pre.png
Binary file modified contents/tree_traversal/res/binary_tree.png
Binary file modified contents/verlet_integration/code/labview/verlet_labview.png
Binary file modified contents/verlet_integration/code/scratch/verlet_scratch.png