Reprint: http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html
Monte Carlo method is a computational method. The principle is to understand a system by a large number of random samples, and then get the value to be computed.
It is very powerful and flexible, and quite simple to understand and easy to implement. For many problems, it is often the simplest method of calculation, and sometimes even the only feasible method.
I. Calculation of π
The first example is how to calculate pi π by Monte Carlo method.
There is a tangent circle inside the square, and the ratio of their area is Π/4.
Now, within this square, randomly generate 10,000 points (i.e., 10,000 coordinate pairs (x, y)) and calculate their distance from the center point to determine if they fall within the circle.
If these points are evenly distributed, then the points within the circle should occupy the π/4 of all points, so multiply this ratio by 4, which is the value of π. The R language script randomly simulates 30,000 points, and the estimated value of π differs from the real value by 0.07%.
Second, the calculation of the integral
The above method can be generalized to calculate the value of any one integral.
For example, calculating the function y = x2 the integral in the [0, 1] interval is to find the area of the red part.
This function has a value of 1 at the () point, so the entire red area is inside a square with an area of 1. Within the square, a large number of random points are generated, and you can calculate how many points fall in the red area (judging condition y < x2). This proportion is the required integral value.
Using MATLAB to simulate 1 million random points, the result is 0.3328.
Third, traffic jam
The Monte Carlo method can be used not only for calculation, but also for simulating stochastic motion inside the system. The following example simulates a single-lane traffic jam.
According to the Nagel-schreckenberg model, the vehicle's motion satisfies the following rules.
- The current speed is v.
- If there is no car in front, it will increase to V + 1 in the next second, until the specified maximum speed limit is reached.
- If there is a car in front, the distance is D, and D < V, then it slows down to d-1 in the next second.
- In addition, the driver will slow down randomly with probability p, reducing the speed of the next second to V-1.
In a straight line, randomly generate 100 points, representing 100 vehicles on the road, and the probability p is 0.3.
, the horizontal axis represents the distance (from left to right), and the vertical axes represent time (top to bottom), so each line represents the next second of the road condition.
As you can see, the model randomly generates traffic jams (the black-gathered part of the graph). This proves that a single lane can cause traffic jams even for no reason.
Iv. Product Thickness
A product is made up of eight parts stacked. In other words, the total thickness of the eight parts is equal to the thickness of the product.
It is known that the thickness of the product must be controlled within 27mm, but each part has a certain probability, the thickness will exceed the error. How much probability is there, the product thickness will exceed 27mm?
Take 100,000 random samples, each with 8 values, corresponding to the respective thickness of the 8 parts. The calculation found that the passing rate of the product is 99.9979%, that is, the probability of 21, the thickness will exceed 27mm.
V. Securities Market
Stock markets are sometimes brisk trading and sometimes trading is deserted. Here is your forecast for the market.
- If the deal is deserted, you will sell 50,000 shares at an average price of 11 yuan.
- If trading is active, you will sell 100,000 shares at an average price of 8 yuan.
- If the deal is moderate, you will sell 75,000 shares at an average price of 10 yuan.
Your cost is known to be between $5.5 and $7.5 per share, with an average of 6.5 yuan. What is your net profit for the next transaction?
Take 1000 random samples, each with two values: one is the cost of the security (5.5 yuan to 7.5 yuan evenly distributed), the other is the current market state (deserted, active, moderate, one-third possible).
The average net profit for the simulation was 92, $427.
Monte Carlo Method Learning (I.)