Monte Carlo Method: Essence and Examples of Application

Lecture



Monte Carlo methods (MCM) — a group of numerical methods for studying random processes. The essence of the method is as follows: the process is described by a mathematical model using a random-number generator, the model is computed repeatedly, and the probabilistic characteristics of the process under consideration are calculated from the data obtained. For example, to find out by the Monte Carlo method what the average distance will be between two random points in a circle, one needs to take the coordinates of a large number of random pairs of points within the boundaries of a given circle, compute the distance for each pair, and then calculate the arithmetic mean of them.

The methods are used to solve problems in various fields of physics, chemistry, mathematics, economics, optimization, control theory and others.

The name of the method comes from the Monte Carlo district, known for its casinos.

The Monte Carlo method has many different applications. It is used in the following areas: in industry, for modeling the variability of production processes; in physics, chemistry and biology, for modeling various phenomena; in games, for modeling artificial intelligence, for example in the Chinese game of Go; in finance, for valuing derivative financial instruments and options. In essence, the Monte Carlo method is used everywhere.

The modern version of the method was formed within the framework of the Manhattan Project, where it was used to model the distances that neutrons could travel through various materials. The idea of modeling based on generating a set of random values had already existed for some time, but received particular development during the creation of the atomic bomb, subsequently spreading to many other fields of knowledge.

A major advantage of the Monte Carlo method is that it allows an element of randomness and the complexity of the real world to be taken into account in the model. Moreover, the method is robust with respect to changes in various parameters, such as the distribution of the random variable. It is based on the law of large numbers.

One of the typical examples of using the Monte Carlo method is problems in which it is necessary to find the mathematical expectation of some random variable. To do this, one needs to generate a set of random values of this variable and find the mean. The random variable is usually characterized by a certain probability distribution.

The essence of the Monte Carlo method

The essence of the method is as follows: for the target random variable a set of random values is generated, and then the required values are calculated on its basis.

Monte Carlo Method: Essence and Examples of Application

Monte Carlo Method: Essence and Examples of Application

The essence of the Monte Carlo method is as follows: it is required to find the value A of some quantity under study. To do this, a random variable X is chosen whose mathematical expectation equals A: M(X) = A. In practice, one proceeds as follows: N trials are performed, yielding N possible values of X, their arithmetic mean is computed, and it is taken as an estimate (approximate value) A ’ of the sought number A. As a rule, a program is written to carry out a single random trial. The computational error is, as a rule, proportional to D/ sqrt (N), where D – is some constant. This means that N must be large, so the method relies heavily on computing power. It is clear that achieving high precision this way is impossible. This is one of the drawbacks of the method. In many problems it is possible to significantly increase precision by choosing a calculation approach corresponding to a significantly smaller D.

Unlike analytical methods, which seek a solution in the form of a series in eigenfunctions, Monte Carlo methods seek solutions in the form of statistical sums. To apply them, a description of the probabilistic process suffices, and its formulation as an integral equation is not required; the error estimate is extremely simple, and their accuracy depends only weakly on the dimensionality of the space. I confirmed this myself by running experiments to solve two simple problems. The results of the experiments demonstrated their accuracy, which is why the Monte Carlo method is used to solve many complex problems that are very difficult or impossible to solve by other methods

History

Buffon's algorithm for determining the number Pi

Random variables have been used to solve various applied problems for quite a long time. An example is the method for determining the number Pi proposed by Buffon back in 1777. The essence of the method was to drop a needle of length Monte Carlo Method: Essence and Examples of Application onto a plane ruled with parallel lines spaced Monte Carlo Method: Essence and Examples of Application apart from one another (see Fig. 1).

Monte Carlo Method: Essence and Examples of Application
Figure 1. Buffon's method

The probability (as is clear from the further context, this is not actually a probability but the mathematical expectation of the number of intersections per trial; it becomes a probability only under the condition Monte Carlo Method: Essence and Examples of Application) that the segment will cross a line is related to the number Pi:

Monte Carlo Method: Essence and Examples of Application

where

  • Monte Carlo Method: Essence and Examples of Application — the distance from the end of the needle to the nearest line;
  • Monte Carlo Method: Essence and Examples of Application — the angle of the needle relative to the lines.

This integral is easy to evaluate: Monte Carlo Method: Essence and Examples of Application (provided that Monte Carlo Method: Essence and Examples of Application), so by counting the proportion of segments that cross the lines, one can approximately determine this number. As the number of trials increases, the accuracy of the result obtained will increase.

In 1864, Captain Fox, recovering from a wound and looking for a way to occupy himself, carried out the needle-dropping experiment . The results are presented in the following table:

Number of throws Number of intersections Needle length Distance between lines Rotation Value of Pi Error
First attempt 500 236 3 4 none 3.1780 +3,6⋅10-2
Second attempt 530 253 3 4 present 3.1423 +7,0⋅10-4
Third attempt 590 939 5 2 present 3.1416 +4,7⋅10-5

Comments:

  • Lengths are given in inches.
  • Rotating the plane was used (and, as the results show, successfully) in order to reduce systematic error .
  • In the third attempt, the needle length was greater than the distance between the lines, which made it possible, without increasing the number of throws, to effectively increase the number of events and improve accuracy (several intersections could occur in a single throw).

Calculating the number Pi

The number Pi can be calculated using the Monte Carlo method.

By inscribing a circle in a square (the diameter of the circle equals the side of the square), the ratio of the area of the circle to the area of the square can be expressed as follows:

Monte Carlo Method: Essence and Examples of Application

If we can calculate this ratio, then we can obtain the value of the number Pi.

Let us fill the square with points having random coordinates. We calculate the ratio of the number of points that fall inside the circle to the total number of points. We multiply the result by 4 to obtain the value of the number Pi.

Monte Carlo Method: Essence and Examples of Application

The greater the number of points, the closer the obtained value is to the true value of the number Pi.

Monte Carlo Method: Essence and Examples of Application

This simple example demonstrates the Monte Carlo method in action.

example in the R language

# estimating pi using Monte Carlo methods. This code won't run perfectly for you, because I

# didn't save everything I did, like adding vectors to a dataframe named Pi.

# Just the important stuff is here.
center <- c(2.5, 2.5) # the center of the circle
radius <- 2.5
distanceFromCenter <- function(a) {
sqrt(sum((center - a) ^ 2))
}
# let's define a 5 by 5 square matrix
points <- c(0,0, 0,5, 5,5, 5,0)
square <- matrix(points, nrow = 4, ncol = 2, byrow = TRUE)
# now all I need to do is make matrix A a matrix of real simulated points.
n <- 100 # number of points
A <- matrix(runif(n*2, min=0, max=5), nrow = n, ncol = 2, byrow = T) # random sampling occurs here!!!
## An alternate way to generate randoms, with faster convergence
# library(randtoolbox)
# A <- matrix(5*halton(n*2), nrow = n, ncol = 2, byrow = T)
# here's how you'll test if it's in the circle.
b <- apply(A, 1, distanceFromCenter)
# d <- subset(b, b < radius) # if you know a better way to do this part, email me.
# num <- length(d) / length(b)
num <- mean(b < radius)
piVec <- c()
for (i in 1:2000) {
n <- i
A <- matrix(runif(n*2, min=0, max=5), nrow = n, ncol = 2, byrow = T)
b <- apply(A, 1, distanceFromCenter)
d <- subset(b, b < radius)
num <- length(d) / length(b)
piVec[i] = num*4
}
library(data.table)
Pi <- data.frame(piVec)
Pi <- data.table(Pi)
Pi[, ind := seq(0, 1999)]
Pi[, error := abs(pi - piVec)]
Pi <- data.frame(Pi)
names(Pi) <- c("guess", "ind", "error")
##### Graphing the error
# note - if you want this part to work for you, you'll have to create the appropriate data frame from the piVec vector.
library(ggplot2)
ggplot(Pi, aes(x = ind, y = error)) +
geom_line(colour = '#388E8E') +
ggtitle("Error") +
xlab("Sample Size") +
ylab("Error")

The relationship between stochastic processes and differential equations

The development of the mathematical apparatus of stochastic methods began at the end of the 19th century. In 1899, Lord Rayleigh showed that a one-dimensional random walk on an infinite lattice can give an approximate solution to one type of parabolic differential equation . Andrey Nikolaevich Kolmogorov, in 1931, gave a great impetus to the development of stochastic approaches to solving various mathematical problems, since he succeeded in proving that Markov chains are connected with certain integro-differential equations. In 1933, Ivan Georgievich Petrovsky showed that a random walk forming a Markov chain is asymptotically related to the solution of an elliptic partial differential equation. After these discoveries it became clear that stochastic processes can be described by differential equations and, accordingly, studied using the mathematical methods, well developed by that time, for solving such equations.

The birth of the Monte Carlo method in Los Alamos

Monte Carlo Method: Essence and Examples of Application
Stanislaw Ulam holding the FERMIAC

First Enrico Fermi in the 1930s in Italy, and then John von Neumann and Stanislaw Ulam in the 1940s in Los Alamos, suggested that the connection between stochastic processes and differential equations could be used «in reverse». They proposed using a stochastic approach to approximate multidimensional integrals in the transport equations that arise in connection with the problem of neutron motion in an isotropic medium.

The idea was developed by Ulam who, while playing solitaire during his recovery from an illness, wondered what the probability was that the solitaire game would come out. Instead of using the combinatorial reasoning usual for such problems, Ulam suggested that one could simply run the experiment a large number of times and, by counting the number of successful outcomes, estimate the probability. However, because of the need to carry out a large number of repetitive experimental actions, the method did not become widespread.

With the advent of the first electronic computer, ENIAC, which could generate pseudorandom numbers at high speed and use them in mathematical models, interest in stochastic methods was revived. Stanislaw Ulam discussed his ideas with John von Neumann, who ultimately used ENIAC for the statistical-sampling method proposed by Ulam in solving various neutron-transport problems . Due to the need to shut down ENIAC for a significant time at the end of 1946, in order to continue research into neutron transport, Enrico Fermi even designed a specialized analog computer named FERMIAC (by analogy with ENIAC, but indicating Fermi's authorship), which also implemented the Monte Carlo method at the mechanical level.

After computers came into use, a major breakthrough occurred, and the Monte Carlo method was applied to many problems for which the stochastic approach proved more effective than other mathematical methods. Nevertheless, the use of this technique also had its limitations, owing to the need for a very large number of computations to obtain results with high accuracy.

The year of birth of the term «Monte Carlo method» is considered to be 1949, when the article by Metropolis and Ulam «The Monte Carlo Method» was published. The name of the method comes from the name of a commune in the Principality of Monaco, widely known for its numerous casinos, since roulette is one of the most widely known generators of random numbers. Stanislaw Ulam writes in his autobiography «Adventures of a Mathematician» that the name was suggested by Nicholas Metropolis in honor of his uncle, who was a gambler.

Further development and the present day

In the 1950s the method was used for calculations in the development of the hydrogen bomb. The main credit for developing the method during this period belongs to staff at US Air Force laboratories and the RAND Corporation. Among the first to apply the Monte Carlo method to the calculation of particle showers were the Soviet physicists A. A. Varfolomeev and I. A. Svetlolobov .

In the 1970s, in the new field of mathematics — computational complexity theory, it was shown that there exists a class of problems whose complexity (the number of computations required to obtain an exact answer) grows exponentially with the dimensionality of the problem. Sometimes, by sacrificing accuracy, one can find an algorithm whose complexity grows more slowly, but there is a large number of problems for which this cannot be done (for example, the problem of determining the volume of a convex body in n-dimensional Euclidean space), and the Monte Carlo method is the only way to obtain a sufficiently accurate answer in an acceptable amount of time.

At present, the main efforts of researchers are directed toward creating efficient Monte Carlo algorithms for various physical, chemical, and social processes for parallel computing systems.

Integration by the Monte Carlo method

Monte Carlo Method: Essence and Examples of Application
Figure 2. Numerical integration of a function by the deterministic method

Suppose it is necessary to take the integral of some function. Let us use an informal geometric description of the integral and understand it as the area under the graph of this function.

To determine this area, one can use one of the usual numerical integration methods: divide the segment into subsegments, calculate the area under the function's graph on each of them, and sum them. Suppose that for the function shown in Figure 2, it is sufficient to divide it into 25 segments and, consequently, to compute 25 function values. Now suppose we are dealing with an Monte Carlo Method: Essence and Examples of Application-dimensional function. Then we need Monte Carlo Method: Essence and Examples of Application segments and just as many function-value computations. When the dimensionality of the function exceeds 10, the task becomes enormous. Since high-dimensional spaces occur, in particular, in problems of string theory, as well as in many other physical problems involving systems with many degrees of freedom, a solution method is needed whose computational complexity does not depend so strongly on dimensionality. It is precisely this property that the Monte Carlo method possesses.

The standard Monte Carlo integration algorithm

Suppose it is required to compute a definite integral

Monte Carlo Method: Essence and Examples of Application

Consider a random variable Monte Carlo Method: Essence and Examples of Application, uniformly distributed on the interval of integration Monte Carlo Method: Essence and Examples of Application. Then Monte Carlo Method: Essence and Examples of Application will also be a random variable, whose expected value is expressed as

Monte Carlo Method: Essence and Examples of Application

where Monte Carlo Method: Essence and Examples of Application — is the probability density of the random variable Monte Carlo Method: Essence and Examples of Application, equal to Monte Carlo Method: Essence and Examples of Application on the interval Monte Carlo Method: Essence and Examples of Application. Thus, the desired integral is expressed as

Monte Carlo Method: Essence and Examples of Application

but the expected value of the random variable Monte Carlo Method: Essence and Examples of Application can easily be estimated by simulating this random variable and computing the sample mean.

So, we throw Monte Carlo Method: Essence and Examples of Application points, uniformly distributed over Monte Carlo Method: Essence and Examples of Application, and for each point Monte Carlo Method: Essence and Examples of Application we compute Monte Carlo Method: Essence and Examples of Application. Then we compute the sample mean: Monte Carlo Method: Essence and Examples of Application. As a result, we obtain an estimate of the integral:

Monte Carlo Method: Essence and Examples of Application

The accuracy of the estimate depends only on the number of points Monte Carlo Method: Essence and Examples of Application.

This method also has a geometric interpretation. It is very similar to the deterministic method described above, with the difference that instead of uniformly dividing the region of integration into small intervals and summing the areas of the resulting «columns», we scatter random points over the region of integration, and on each of them we build the same kind of «column», defining its width as Monte Carlo Method: Essence and Examples of Application, and sum their areas.

The geometric Monte Carlo integration algorithm

Monte Carlo Method: Essence and Examples of Application
Figure 3. Numerical integration of a function by the Monte Carlo method

To determine the area under the graph of a function, the following stochastic algorithm can be used:

  • bound the function with a rectangle (an n-dimensional parallelepiped in the case of multiple dimensions), whose area Monte Carlo Method: Essence and Examples of Application can easily be computed; any side of the rectangle touches at least one point of the function's graph, but does not cross it;
  • «scatter» a certain number of points into this rectangle (parallelepiped) (Monte Carlo Method: Essence and Examples of Application points), whose coordinates will be chosen at random;
  • determine the number of points (Monte Carlo Method: Essence and Examples of Application points) that fall under the graph of the function;
  • the area of the region bounded by the function and the coordinate axes, Monte Carlo Method: Essence and Examples of Application is given by the expression Monte Carlo Method: Essence and Examples of Application

For a small number of dimensions of the function being integrated, the performance of Monte Carlo integration is much lower than the performance of deterministic methods. Nevertheless, in some cases, when the function is given implicitly and it is necessary to determine a region defined by complex inequalities, the stochastic method may prove preferable.

Using importance sampling

With the same number of random points, the accuracy of the computation can be increased by bringing the region bounding the desired function closer to the function itself. To do this, one must use random variables with a distribution whose shape is as close as possible to the shape of the function being integrated. This is the basis of one of the methods for improving convergence in Monte Carlo computations: importance sampling.

Optimization

Various variations of the Monte Carlo method can be used to solve optimization problems. For example, the simulated annealing algorithm.

Application in physics

Computer simulation plays an important role in modern physics, and the Monte Carlo method is one of the most widespread across many fields, from quantum physics to solid-state physics, plasma physics, and astrophysics.

The Metropolis algorithm

Traditionally, the Monte Carlo method was used to determine various physical parameters of systems in a state of thermodynamic equilibrium. Suppose there is a set of Monte Carlo Method: Essence and Examples of Application possible states of a physical system Monte Carlo Method: Essence and Examples of Application. To determine the average value Monte Carlo Method: Essence and Examples of Application of some quantity Monte Carlo Method: Essence and Examples of Application, it is necessary to compute Monte Carlo Method: Essence and Examples of Application, where the summation is carried out over all states Monte Carlo Method: Essence and Examples of Application from Monte Carlo Method: Essence and Examples of Application, and Monte Carlo Method: Essence and Examples of Application — is the probability of state Monte Carlo Method: Essence and Examples of Application.

Dynamic (kinetic) formulation

Direct simulation by the Monte Carlo method

Direct simulation by the Monte Carlo method of some physical process involves simulating the behavior of the individual elementary parts of the physical system. In essence, this direct simulation is close to solving the problem from first principles, though for the sake of speeding up computations, the application of certain physical approximations is usually permitted. Examples include calculations of various processes by the method of molecular dynamics: on one hand, the system is described through the behavior of its elementary constituent parts, while on the other hand, the interaction potential used is often empirical.

Examples of direct simulation by the Monte Carlo method:

  • Simulation of the irradiation of solids by ions in the binary collision approximation.
  • Direct Monte Carlo simulation of rarefied gases.
  • Most kinetic Monte Carlo models belong to the direct type (in particular, the study of molecular beam epitaxy).

Quantum Monte Carlo method

The quantum Monte Carlo method is widely used to study complex molecules and solids. This name unites several different methods. The first of these is the variational Monte Carlo method, which is essentially the numerical integration of multidimensional integrals arising in the solution of the Schrodinger equation. To solve a problem involving 1000 electrons, one must take 3000-dimensional integrals, and in solving such problems the Monte Carlo method has an enormous performance advantage compared to other numerical integration methods. Another variety of the Monte Carlo method — is the diffusion Monte Carlo method.

Examples of problems solved by the Monte Carlo method

  • calculation of a queuing system;
  • calculation of the quality and reliability of products;
  • message transmission theory;
  • computation of a definite integral;
  • problems of computational mathematics ;
  • problems of neutron physics and others

Other examples of applications of the Monte Carlo method.

A mathematical description of the characteristics of light propagation can be performed analytically using the Maxwell theory equation or through transport theory. The applicability of Maxwell's equations is limited due to the difficulties involved in deriving exact analytical solutions. On the other hand, the heuristic character of transport theory allows numerical methods to be used to solve the transport of photons through absorbing and scattering media. The Monte Carlo method is widely used to solve problems of radiative transfer because of its flexibility and simplicity in simulating energy transport in arbitrary geometries with complex boundary conditions. Nevertheless, most simulations do not take into account the polarization of light.

statistical methods for simulating electron transport in matter.

Mathematical modeling of the processes of interaction between ionizing radiation and objects of complex geometry and internal structure is of great importance in many applications. In particular, within the framework of problems of X-ray diagnostics of materials and structures, it is necessary to determine and study X-ray images of objects , and when studying the electromagnetic effect of penetrating radiation, it is necessary to analyze the distribution of fluxes of relativistic electrons arising from the interaction of ionizing radiation with the materials of objects. Computational algorithms based on statistical modeling by the Monte Carlo method of the processes of transport and interaction of radiation with matter . The advantage of the Monte Carlo method over alternative methods based on the numerical solution of the kinetic equation is determined by the convenience and suitability of this method for solving complex boundary-value problems in multicomponent media. The effectiveness of applying the Monte Carlo method is currently determined, first, by the development of ways to reduce the statistical error of computation results and, second, by progress in creating high-speed multiprocessor computing systems. Statistical modeling of the transport of electrons and other charged particles presents considerably greater difficulty than modeling the transport of photons.

Monte Carlo N-Particle Transport ( MCNP ) - is a general-purpose continuous-energy, generalized-geometry, time-dependent Monte Carlo radiation transport code, designed for tracking many types of particles over a wide range of energies and developed by Los Alamos National Laboratory . Specific areas of application include, among others, radiation protection and dosimetry, radiation shielding , radiography , medical physics, nuclear criticality safety , detector design and analysis, nuclear well logging for oil wells , accelerator target design , fission, and the design, decontamination, and decommissioning of a thermonuclear reactor . The code handles an arbitrary three-dimensional configuration of materials in geometric cells bounded by first- and second-degree surfaces and fourth-degree elliptical tori.

Challenges within the Monte Carlo method

The main challenge is the generation of independent random variables. This is not as simple a task as it might seem at first glance. In the code examples, we simply called the built-in functions of R or Python to generate random numbers, but this process can be much more complex. If necessary, you can refer to the scientific literature on this topic.

Another problem consists in how to ensure convergence of the error. Note that in the example of computing the number Pi, the error stopped decreasing. In most applications of the Monte Carlo method, very large samples are used to address this issue.

See also

  • Feynman-Kac formula
  • Pseudorandom number generators
  • Las Vegas (algorithm)
  • Sampling
  • AQUA@home
  • QMC@Home
  • QuantumFire
  • Buffon's needle problem
  • Entropic simulation method
  • Wang-Landau algorithm
  • Using the Monte Carlo method for tree search in the game of Go.
created: 2014-09-29
updated: 2026-03-09
704



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "probabilistic processes"

Terms: probabilistic processes