Achieve little experiment based on Monte Carlo algorithms in Python

Achieve little experiment based on Monte Carlo algorithms in Python

Monte Carlo algorithm ideas

Monte Carlo (Monte Carlo) method is a type collectively randomized algorithms, is the author of the famous mathematician von Neumann, he used the world famous Las Vegas in the mid-1940s - Monaco Monte Carlo named this method.

Explain the idea of ​​popular Monte Carlo algorithm. If 1000 was a basket with apples, so every time you get an eyes closed, pick out the biggest. So you took a random eyes closed, and then take a random than the first one, leaving big, and then take a random, compared with the previous leave, they can leave you each get a large ...... once, leaving at least Apple is currently the largest, so the cycle, the greater the more the number of take, pick out the biggest apple possibility, but unless you have to pick apples 1000 again, otherwise you can not be sure the final singled out it is the biggest one. That is, the Monte Carlo algorithm is the more samples, the more you can find the best solution, but just try to find the best, not necessarily the best guarantee.

In contrast with its Las Vegas idea of ​​the algorithm. If there is a lock with the key 1000 is selected, but only one is right. So every time you take a random key to try, it can not open and back to one. The more times you try to open the greater the chance of an optimal solution, but before opening, those wrong keys are of no use. So Las Vegas algorithm is to try to find the best solution, but not guaranteed to be found. Assume tried 999 times without any play a key to unlock the real key is the first 1000, but did not sample the 1000th selected, Las Vegas algorithm can not find the key to open the lock.

Monte Carlo and Las Vegas itself is two famous casino, gambling reflected in a number of randomized algorithms, so take this name. They simply summarizes the characteristics of the random algorithm, the algorithm itself may be complex or simple, the choice between these two types of stochastic algorithms, often subject to the limitations of the problem. If the problem is required in a limited sampling, it must be given a solution, but not required to be the optimal solution, then use Monte Carlo algorithm. Conversely, if the issue requires it must be given the optimal solution, but there is no limit on sampling, then use the Las Vegas algorithm.

Monte Carlo algorithm experiment

So it seems to support the theory of Monte Carlo method is actually a law of large numbers in probability theory or statistics. The basic principle is simple description is to a large number of simulation, and then calculate the number of times an event occurs, and then divided by the total number of occurrences by the number of simulations to obtain the desired results. Here we have the classic three small experiments to study at the Monte Carlo algorithm thought.

1. calculate pi pi (π) value

Principle: In the interior square has a tangential circle, circle ratio area / area of ​​a square (PixRxR) / (2Rx2R) = Pi / 4. Randomly generated within the square n points, assuming that the point falls within the circle probability is P, then P = circular area / area of ​​the square, the P = Pi / 4. How to calculate the probability P points fall within the circle? To compute a distance to the center point, falls inside the circle is determined, if these points are evenly distributed within the circle represents administered points fall with M, N represents the total number of points of administration, the pi Pi = 4P = 4xM / N.

Experimental Procedure:

(1) is provided in the center of the origin (0,0), R is the radius to form a circle, the circle area PixRxR

(2) the external square circle, coordinates (-R, -R) (R, -R) (R, R) (- R, R), the area of ​​the square is R * R

(3) then takes the point (X, Y), such that -R <= X <= R and -R <= Y <= R, i.e. the point in the square

(4) by the equation XxX + YxY <= RxR point is determined in the circumferential (side length of a right triangle formula).

The number (5) is provided for all points (i.e. the number of experiments) is N, the number of points fall within the circle (step satisfies points. 4) is M, P = M / N, then Pi = 4xM / N .

(6) operating results for the 3.143052

def cal_pai_mc(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
 m = 0
 for i in range(0, n+1):
 x = random.uniform(x_neg, x_pos)
 y = random.uniform(y_neg, y_pos)
 if x**2 + y**2 <= 1.0:
 m += 1
 return (m / float(n)) * 4
复制代码

2. The definite integral value calculation function

Principle: when the required function f (x) from a to b of the definite integral, we can use a relatively easily calculated rectangular area surrounded by the integration interval function (assuming an area of ​​Area), definite integral value is actually seek area under the curve. To this point randomly administered rectangular frame, statistics fall (x) below function f points at an amount ratio of the number of all points is P, then it can be estimated accordingly function f (x) the definite integral from a to b of Area × P. Here we will set to a and b are 0 and 1, the function f (x) = x2.

Operating results for the 0.333749

def cal_integral_mc(n = 1000000):
 x_min, x_max = 0.0, 1.0
 y_min, y_max = 0.0, 1.0
 m = 0
 for i in range(0, n+1):
 x = random.uniform(x_min, x_max)
 y = random.uniform(y_min, y_max)
 # x*x > y 表示该点位于曲线的下面。
 if x*x > y:
 m += 1
 #所求的积分值即为曲线下方的面积与正方形面积的比
 return m / float(n)
复制代码

3. The calculation function extremum avoid local extremum

Principle: extreme value is "maximum" and "minimum" and collectively. If a function in a neighborhood of a point everywhere determined value of the function at the point value is greater than or equal to any other function value points in the vicinity of the point, the point is said function value is a function of the "pole great value. " If the function is less than or equal to the value of the function at any point, the other point of the neighboring point, called the function "minimum" value at that point function. Here randomly generated in the interval [2,2] on a number, determined its corresponding y, find out which is the maximum value that the maximum value of the function [2,2] on.

It was found that the maximum value 185.1204262706596 run, maxima is 1.5144491499169481

def cal_extremum_mc(n = 1000000):
 y_max = 0.0
 x_min, x_max = -2.0, 2.0
 y = lambda x:200*np.sin(x)*np.exp(-0.05*x)#匿名函数
 for i in range(0, n+1):
 x0 = random.uniform(x_min, x_max)
 if y(x0) > y_max:
 y_max = y(x0)
 x_max = x0
 return y_max, x_max
复制代码

These three examples also called Monte Carlo-based investment point method, whereby the value obtained is not a precise value but an approximate value. When the number of investment point of growing, this approximation is also closer to the true value.

More Python tutorials related knowledge will gradually update all experience into this summary, we have any knowledge of Python to promising that want to learn, you can also leave a message Oh!


Reproduced in: https: //juejin.im/post/5d01df30518825259c03370d

Guess you like

Origin blog.csdn.net/weixin_34187822/article/details/93183677