An Invitation to the Monte Carlo Method: Simple Integration and Simulation
Total Page:16
File Type:pdf, Size:1020Kb
An Invitation to the Monte Carlo Method: Simple Integration and Simulation addprocs(4) import PyCall @everywhere using PyCall @pyimport IPython.display as IPyD 1.0 - Introduction IPyD.Image(url="https://upload.wikimedia.org/wikipedia/commons/8/8a/STAN_UL AM_HOLDING_THE_FERMIAC.jpg", width=400) 1.0.1 - From Solitaire to Neutron Screening: The History of the Monte Carlo Method In a nutshell, a Monte Carlo method is some computational algorithm that utilizes repeated random sampling (either via a true random number generator or with respect to some probability distribution). By accepting or rejecting these random points according to some conditions placed on the system, one can estimate some observable or quantity by performing statistical analysis in the large number limit. The first true Monte Carlo simulation was done by famed physicist and Nobel laureate Enrico Fermi in the 1930s, with the use of a brass and glass analog computer now known as the Monte Carlo trolley or FERMIAC (pictured above with mathematician Stanislaw Ulam, whom we'll get to in a moment.) Before the birth of the early electronic supercomputer ENIAC (the FERMIAC's namesake), Fermi and his students used the FERMIAC to study the properties of neutrons as they traveled through a material. The placement of the drums was determined by the material in question and the energy of the neutrons ("slow" or "fast"), the latter chosen from a pseudorandom number. The neutron then traveled to a new location (also determined by a set of random numbers), and the trolley rolled along and drew the path of the neutron on top of a scale model of the nuclear device. If the neutron entered a new material, the drums were recalibrated. Below, we see the FERMIAC being used for some calculation: IPyD.Image(url="https://upload.wikimedia.org/wikipedia/commons/8/8d/FERMIAC .jpg", width=450) The first full-blown Monte Carlo simulation was born out of the post-war period of nuclear physics research at Los Alamos National Laboratory. Although Enrico Fermi was the first to utilize such statistical sampling for the study of neutron physics, it was Polish-American mathematician Stanislaw Ulam who took the critical step of generalizing the method to large- scale computation. Ulam's insight can be traced back to the mid-1940s, not in an attempt to solve some difficult physical problem or create some new thermonuclear weapon, but instead to his love of solitaire. He wondered if there was any way to calculate the probability of winning a game of Canfield solitaire, a notoriously difficult variant of the traditional card game known for its low probability of winning. Despite the simple nature of the game, the combinatorics behind a winning game is highly non-trivial. Instead of attempting to find an analytical solution to this problem, Ulam imagined playing a hundred games and building an approximate probability distribution by emprically observing how many games were won and how many were lost. Such a simulation could be done on a computer, stochastically sampling the probability density by playing a large number of games via psuedorandom number generation. Subsequently, we build the probability distribution piece by piece. The importance of such a method in the physical sciences was noticed by Ulam's colleague at Los Alamos, the polymath and father of game theory John von Neumann. Together with von Neumann, Ulam was able to apply his method of empirically building probability distributions to the problem of radiation shielding. With the average distance a single neutron could travel before striking a nucleus found experimentally, Ulam's empirical construction of some general probability distribution could be used to find the estimated penetration depth of an ensemble of neutrons in a given material. In essence, von Neumann treated the history of a single neutron as a game of solitaire, sampling the probability distribution via pseudorandom number generation to determine if a given neutron will scatter or collide, or if fission will be induced. A "geneology" of each neutron can then be generated. With the creation of the ENIAC (Electronic Numerical Integrator and Computer) at the University of Pennsylvania in early 1946, computer simulations of such a system finally became a reality, and soon Ulam and von Neumann's algorithm was applied to a variety of difficult statistical problems, ranging from the hydrogen bomb to issues in physical chemistry. Ulam and von Neumann chose the moniker "Monte Carlo" upon suggestion by fellow Los Alamos physicist Nicholas Metropolis, in reference to Ulam's uncle who was known to frequent the popular Monacan casino. We'll learn more about Metropolis and his contributions to the now-famous Monte Carlo method later in these notes. The implementation of the method differs greatly depending on what system (physical, mathematical, economic, etc.) we are talking about. Our goal is to dive into quantum Monte Carlo for many-body systems, but to do that we have to build up a dictionary of basic functions and algorithms to efficiently and simply stochastically sample the given probability distribution, as well as to visualize the results. For that, we begin with a simple Monte Carlo integration example, and then procede to importance sampling and from there dip our toes into some simple physical systems of historical interest before we tackle the big guns. 1.0.2 - The Theory behind Monte Carlo Integration The reason for taking Monte Carlo integration as our first example is three-fold. First of all, it's a nice, simple introduction that people can understand without any knowledge of physics. Second, we can generalize the functions used in this code for future, more complex Monte Carlo programs. Third, Monte Carlo integration is, in fact, a general extension of regular discrete integration. To see this in it's entirety, we start with the definition of some probability distribution function . The probability of finding a random variable in this distribution between points and is then given by In the case of a uniformly random distribution in the interval , it is important to note that If we want the expected value of some function over the probability distribution, we just have to integrate over this probability distribution: Therefore, assuming a uniformly random distribution as above, we can estimate the integral of from a to b as follows: Because we are considering a uniformly random distribution, it is very easy to find via stochastically sampling across the given data range, selecting only the data that is "within" the function . This gives us a strategy for Monte Carlo integration for integration: calculate the expected value of some function via stochastic sampling, divide by the total number of points in the interval you're sampling, and then multiply by the total length/area/volume of the interval. Note that this is very similar to Simpson's rule or the trapazoidal rule, except here we sum over random points of the function rather than systematically finding some polynomial to tabulate the integral. 1.1 - Our First Monte Carlo Simulation: Numerically finding 1.1.1 - Buffon's Needle and Lazzarini's approximation of Pi IPyD.Image(url="https://upload.wikimedia.org/wikipedia/commons/5/5e/Buffon_ 1707-1788.jpg",width=350) The calculation of via stochastic sampling is nothing new to Monte Carlo. One of the first estimations of via random simulation is an application of Buffon's needle problem, a mathematical puzzle first proposed by Georges-Louis Leclerc, Comte de Buffon (shown above) in the 18th century. To understand the problem, imagine that we drop a needle on a wood panel floor, with each panel of the same width. What is the probability that a needle will fall on the border between a two panels? If we take a needle of length of length and a floor with boards of width , we can quantify this probability by defining the angle between the needle and the plane perpendicular to the panel border. If the end of the needle that is farthest away from the border is within a horizontal distance of , the needle will cross the border. We can thus integrate from to and divide by the total corresponding width in this region to obtain the solution to Buffon's problem: The application of Buffon's probability to the experimental determination of was first noticed by Mario Lazzarini at the dawn of the 20th century. By simply rearranging the above formula, we find a relationship for that depends upon a Monte Carlo-style stochastic sampling: Due to the simple nature of this system, it would be a reasonable starting point to begin with Buffon's needle problem and see if we can obtain a value for . The algorithm is simple: 1) Drop N needles of length onto a "board" with panels and n borders a distance apart. 2) Count the number of needles that land on a border. 3) Calculate the probability , and subsequently find the value of via the equation The easiest way to do this is by setting the width to in some arbitrary units. We can then initialize the number of needles on the border to be zero (e.g., on_border=0), and loop through Monte Carlo steps. During each step, we'll throw a needle of length onto the board, determine if its on the border, and, if it did indeed fall on the border between two panels, add it to on_border. If we take the panels to be arranged in the y-direction, then we can simplify our work by only taking simulating the initial -point of the head and the relative angle between the needle head and the horizonal axis. The code is given below: # MC_Buffon # author(s): Joshuah Heath # # function to calculate the