Ruan Yifeng: Introduction to Monte Carlo Method

Ruan Yifeng: Introduction to Monte Carlo Method

1. Overview

The Monte Carlo method is a calculation method. The principle is to understand a system through a large number of random samples and then obtain the value to be calculated.

It is very powerful and flexible, yet fairly simple to understand and implement. For many problems, it is often the simplest computational approach, and sometimes the only feasible approach.

It was born from the "Manhattan Project" in the United States in the 1940s. Its name comes from the gambling city of Monte Carlo, symbolizing probability.

2. Calculation of π

The first example is how to calculate pi using the Monte Carlo method.

There is a tangent circle inside the square, and the ratio of their areas is π/4.

Now, randomly generate 10,000 points (i.e., 10,000 coordinate pairs (x, y)) inside this square, and calculate their distance from the center point to determine whether they fall inside the circle.

If the points are evenly distributed, then the points inside the circle should account for π/4 of all the points, so multiplying this ratio by 4 gives the value of π. Using an R script to randomly simulate 30,000 points, the estimated value of π differs from the true value by 0.07%.

3. Calculation of integrals

By generalizing the above method, we can calculate the value of any integral.

For example, to calculate the integral of the function y = x2 in the interval [0, 1], we need to find the area of ​​the red part in the figure below.

The value of this function at point (1,1) is 1, so the entire red area is inside a square with an area of ​​1. A large number of random points are generated inside the square, and we can calculate how many points fall into the red area (judgment condition y < x2). This proportion is the required integral value.

Using Matlab to simulate 1 million random points, the result is 0.3328.

4. Traffic jam

Monte Carlo methods can be used not only for calculations, but also for simulating random motion within a system. The following example simulates a traffic jam on a single lane.

According to the Nagel-Schreckenberg model, the motion of the vehicle satisfies the following rules.

  • The current velocity is v.
  • If there is no car in front, its speed will increase to v + 1 in the next second until it reaches the prescribed maximum speed limit.
  • If there is a car in front of it at a distance d, and d < v, then its speed will drop to d – 1 in the next second.
  • In addition, the driver will randomly decelerate with probability p, reducing the speed to v – 1 in the next second.

On a straight line, 100 points are randomly generated, representing 100 cars on the road, and the probability p is 0.3.

In the above picture, the horizontal axis represents distance (from left to right) and the vertical axis represents time (from top to bottom), so each row represents the road conditions in the next second.

As you can see, the model randomly generates traffic jams (the black clusters in the graph). This proves that even if there is no reason, a single lane will cause traffic jams.

5. Product thickness

A product is composed of eight stacked parts. That is, the sum of the thicknesses of these 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 there is a certain probability that the thickness of each part will exceed the error. What is the probability that the thickness of the product will exceed 27mm?

Take 100,000 random samples, each with 8 values, corresponding to the thickness of 8 parts. Calculation shows that the qualified rate of the product is 99.9979%, that is, there is a probability of 21 in a million that the thickness will exceed 27mm.

6. Securities Market

The stock market is sometimes active and sometimes quiet. Here is your prediction for the market.

  • If trading is slow, you will sell 50,000 shares at an average price of $11.
  • If trading is active, you will sell 100,000 shares at an average price of $8.
  • If trading is mild, you would sell 75,000 shares at an average price of $10.

It is known that your cost per share is between 5.5 yuan and 7.5 yuan, and the average is 6.5 yuan. What will be your net profit for the next transaction?

Take 1,000 random samples, each with two values: one is the cost of the security (uniformly distributed between 5.5 yuan and 7.5 yuan), and the other is the current market state (cold, active, mild, each with a one-third chance).

The simulation results show that the average net profit is $92,427.

<<:  Share: HTML5 game development experience and development tools

>>:  Share: HTML5 game development experience and development tools

Recommend

Check out the three common explosive points of “screen-sweeping” marketing!

The author reviewed and sorted out nine marketing...

Chen Xudong: Lenovo has revived Moto's revolutionary and innovative spirit

After being first released in the US market in Ju...

VMplay Ai Qiwei: The era of free app downloads is coming

In this era of app proliferation, have you ever t...

Sister Abao "Ordinary People's Workplace Counterattack Training Camp"

Course Catalog ├──Chapter 1 Opening Ceremony | └─...

Ejection seat, the skyguard of the skyguard

Some people say that the last line of defense to ...

official! Tik Tok advertising promotion quotation list~

Tik Tok has seized the domestic video market and ...

What is a nuclear submarine? How is it different from a normal submarine?

Produced by: Science Popularization China Author:...

Winter Olympics mascot: I was almost able to recognize the prototype!

The mascot of this year's Winter Olympics, &q...

25 "Empty-handed" APP promotion methods, you deserve it

Due to various reasons, our company is not like m...