While that is not as complicated as one might think, I wanted to start out with a very simple article first. In this article, we will write a simple Python script to estimate the value of $\pi$. In a follow-up post, we will introduce the concept of a random walk and use it to model a stock's price action. In the last article of this series, we will show how to predict the probability of reaching a given percentage of profit on an option trade.
So how can we use random numbers to estimate a constant like $\pi$? We start with a unit circle circumscribed by a square. Since the circle has a radius of one, the side of the square must have a length of two.
Figure 1: A unit circle centered at the origin circumscribed by a square. |
Figure 2: We will only consider the upper left quadrant. |
$$\frac{N_{inside}}{N_{total}} \approx \frac{A_{circle}}{A_{square}} = \frac{ \frac{1}{4} \pi R^2}{1}.$$
Fig. 3 shows an animation of this process. For the sake of clarity, we only include 25 points in this animation whereas the actual calculation we will use significantly more.
$$\sqrt{ x^2 + y^2} \leq 1^2$$.
The code below implements this calculation using the Python's Numpy library. It should print "pi = 3.140936" to the screen.
# Python 2.7 script to estimate the value of pi using Monte Carlo techniques
import numpy as np # Use a constant random seed for debugging purposes np.random.seed(0) # Number of random points to choose N = 1000000 # Create a 2-dimensional numpy array. Each row will correspond to one # point with the entry in the first column being the X value and the # second column providing the Y values. p = np.random.rand(N, 2) # Use the built-in norm function to calculate the distance from the origin n = np.linalg.norm(p, axis = 1, keepdims = True) # Sum up the number of entries in the circle. That is count the number of # points who have a norm less than or equal to one number_in_circle = np.sum(n <= 1) # Print out the results print 'pi = ', 4 * float(number_in_circle) / float(N)
While this example borders on the trivial, it serves as a good starting point for more sophisticated calculations. As noted in the introduction, I've used Monte Carlo methods in financial modeling (which we will discuss in later posts), as well as in complex physical systems such as modeling the loss of active material from cycling batteries.
Some comments on vectorization
The code above uses a "vectorized" implementation. If you are familiar with the concept, there is no need to read further. If not, I'd like to take a moment to explain the concept because we will use it heavily in future articles.
Numpy is a package for numerical calculations in Python. It provides a significant speed increase over writing code in straight Python because it is written in C rather than Python itself. It also provides the ability to "vectorize" calculations meaning operation can be performed on whole vectors or matrices without having to explicitly loop through the elements. This can further enhance speed considerably.
Figure 5: Compute the L2 norm of a vector $p$, and compare the result to that of calculating the distance between $p$ and the point (0, 0) using the Pythagorean theorem. |
The vectorization used here is straightforward and pretty intuitive, but it is important to have a handle on the concept. In subsequent articles where we discuss ransom walks in the context of modeling stock behavior, we will lean heavily on this as well as SciPy's ability to deal with sparse matrices rapidly. This is a bit more abstract, and thus it is a good idea to make sure one understands the simple case before jumping into the deep end.
Articles in this series
Part II: Monte Carlo Techniques: Modeling Stock Price ActionPart III: Monte Carlo Techniques: Calculating the Probability of Making 50% of Max Profit on Short Option Positions
Part IV: Monte Carlo Techniques: Estimating Correlations between Option Positions and and Their Underlying Stock
This comment has been removed by a blog administrator.
ReplyDelete