Introductory notes of Monte Carlo method

Introduction

Principle: According to the sampling method under the problem scenario, the system is simulated through a large number of random samples to obtain the required calculation parameters.

It can be considered that Monte Carlo methods are roughly divided into two categories. The first type is that the problem to be solved is inherently random, and this random process can be directly simulated with the help of the computing power of the computer. In this category, in nuclear physics research, the analysis of the transmission process of neutrons in the reactor is a more typical example. The second category is that the problem to be solved can be transformed into a certain random distribution of characteristic numbers, such as approximately estimating the circumference of the circle through the method of casting points, approximately calculating the definite integral, the probability of random events, or the expected value of random variables.

This article focuses on explaining the second type of situation.

Monte Carlo approximation to find the pi (pointing method)

import random

def monte_carlo():
    n = 1000000
    r = 1.0
    a, b = (0.0, 0.0)
    x_neg, x_pos = a - r, a + r
    y_neg, y_pos = b - r, b + r

    count = 0
    for i in range(0, n):
        x = random.uniform(x_neg, x_pos)
        y = random.uniform(y_neg, y_pos)
        if x*x + y*y <= 1.0:
            count += 1

    print (count / float(n)) * 4

Monte Carlo Find Definite Integral

The earliest Monte Carlo methods are all for solving some summation or integration problems that are not easy to solve.

Let's start with a simple example where the probability distribution is uniform :

Assuming that the independent variable x is uniformly distributed between [a, b], for
Insert picture description here
Insert picture description here
a value of sampling (the sampling process, which will be mentioned later) to represent all the values ​​of f(x) in the interval [a, b], the solution is too Rough. Then you can sample n values ​​in the interval [a,b]: x0,x1,...xn−1, and use their average to represent all the values ​​of f(x) in the interval [a,b], namely:
Insert picture description here

But in most cases, the probability distribution is not evenly distributed.

If you know the probability distribution function p(x) of x in the interval [a, b], the sum can be performed like this:
Insert picture description here
this form is the general form of the Monte Carlo method. (The form of continuous function, but the same holds when discrete).

Substituting the condition of uniform distribution, that is Insert picture description here, it is not difficult to find that uniform distribution can also be used as a special case of p(x).

Simple record about sampling

The probability distribution function p(x) of x is obtained, and then a sample set containing n x that also obeys this distribution can be sampled based on it. How to sample it?

For the common uniform distribution uniform(0,1) , sampling samples is easy. In fact, other common probability distributions , whether they are discrete distributions or continuous distributions, can be obtained by transforming their samples from uniform(0,1) samples .

For a distribution that is not a common probability distribution, a feasible method is to use acceptance-rejection sampling to obtain, or a sample close to the distribution. If p(x) is very complicated, you can set a common distribution q(x) that the program can sample, such as the Gaussian distribution, and then reject some samples according to a certain method.
Insert picture description here
In the example above, first set a Gaussian distribution q(x) and constant k so that p(x) is always below kq(x).

Next, the convenient sampling q(x) is sampled according to the method mentioned above. Then, sample from the uniform distribution (0, kq(z0)) to obtain a value u. If it does not fall in the actual distribution area, the sampling is rejected. Repeat the above process to get n accepted samples: z0, z1,...zn−1, finally
Insert picture description here
visible, set an easy-to-sample distribution, and then reject some samples according to a certain method, so as to achieve close to the actual probability distribution p(x ) The purpose of distribution.

However, acceptance-rejection sampling can only partially meet our needs. In many cases, it is still difficult to obtain a sample set of probability distributions, such as some slightly special two-dimensional distributions and high-dimensional unusual distributions.

Guess you like

Origin blog.csdn.net/u013598957/article/details/113073421