Calculate Pi using Monte Carlo Simulations in Python (Vectorized)

Category: Python - Miscellaneous

I saw something like this in C++ as an introduction to Monte Carlo, so I decided to make something similar in Python. My original code used for loops, but I vectorized it with no small amount of effort, and it now runs orders of magnitude faster. For example, I can calculate pi to .002% error with 100,000,000 randomized coordinates in approximately 15 seconds. Careful to start small, because memory fills up quickly, and the computer will run slow if you overstep your RAM. I'm able to go up to a bit more than 150 million without compromising speed and functionality in other tasks.

For those who are curious, vectorization is a technique whereby numpy (or similar) arrays replace things like loops. They're a bit tricky to write (at least for me), but they work beautifully.

It might be useful for visualization to plot the occurrence of data points, and observe the randomness Date: 27 March, 2012


Calculate - Carlo - Monte - Pi - Python

Homepage: http://code.activestate.com/recipes/577578-calculate-pi-using-monte-carlo-simulations-in-pyth/?in=lang-python

Developer: Zach Pace

License: Freeware

Operating System: Windows

Add a Comment

all are required fields

     
What do you think of this resource?

Select Your Rate:

Votes:0

 

Related Scripts Download

Need to calculate your page load speed with PHP.

developer Developer: Scott Connell
license License: Freeware
operating systems Operating System: Unix


A free library of PHP tools for calculating holidays.

developer Developer: Dan Kaplan
license License: Freeware
operating systems Operating System: All


Delphi code for a small program that is able to calculate sines of various angles.

developer Developer: planetsourcecode.com
license License: Freeware
operating systems Operating System: All


The MaxFx Tax Calculator is a sales tax calculator written in Delphi.

developer Developer: planetsourcecode.com
license License: Freeware
operating systems Operating System: All


This script contains a function to calculate the percent of requests that were "refused" by Apache server due to the clients ability to read the file from their cache instead.

developer Developer: code.activestate.com
license License: Freeware
operating systems Operating System: All


Both of those combinations/permutations can be with or without repetition.

developer Developer: mathworks.com
license License: Freeware
operating systems Operating System: All


Some of the indicators calculated are GRS80 free air anomalies, EIGEN-CG03C (an remaining topography reduction), normal gravity GRS67 and GRS80.

developer Developer: mathworks.com
license License: Freeware
operating systems Operating System: All


keep (key, value)s in a map.

developer Developer: Hui Zhang
license License: Freeware
operating systems Operating System: Windows


A module that allows you to not to repeat yourself (DRY) while writing typical PyGTK constructions (an object containing PyGTK widgets with its methods servicing widget events) by calling connect() automatically.

developer Developer: Pavel Krc
license License: Freeware
operating systems Operating System: Windows