In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to use Python to achieve a small experiment based on Monte Carlo algorithm, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Using Python to realize a small experiment based on Monte Carlo algorithm
Monte Carlo algorithm thought
The Monte Carlo method is a general term for a class of random algorithms, proposed by the famous mathematician von Neumann, who named it after Monte Carlo in Monaco, the world-famous gambling city in the mid-1940s.
Explain the idea of Monte Carlo algorithm in a popular way. If there are 1000 apples in the basket, let you take one with your eyes closed at a time and pick out the biggest one. So you take a random one with your eyes closed, and then randomly compare it with the first one, leave the big one, and then randomly take one. Compared with the previous one, you can leave the big one. Every time you take it, you leave at least the largest apple for now, and over and over again, the more times you take it, the more likely it is to pick out the largest apple, but unless you pick all 1000 apples, otherwise you can't be sure that the one you pick out in the end is the biggest one. In other words, the Monte Carlo algorithm is that the more samples, the more can find the best solution, but only try to find the best, there is no guarantee that it is the best.
In contrast to it is the idea of Las Vegas algorithms. If there is a lock, there are 1000 keys to choose, but only one is right. So every time you randomly take a key to try, if you can't open it, you can change it. The more times you try, the more likely you are to open the optimal solution, but until you open it, the wrong keys are useless. So the Las Vegas algorithm is to try to find the best solution, but there is no guarantee that it will be found. Assuming that no key can open the lock after 999 attempts, the real key is the 1000th, but the sample does not have the 1000th choice, then the Las Vegas algorithm cannot find the key to the lock.
Monte Carlo and Las Vegas are two famous casinos themselves, so they are named because there are many random algorithms in the process. They only summarize the characteristics of random algorithms, and the algorithms themselves may be complex or simple. The choice between these two types of random algorithms is often limited by the problem. If the problem requires that a solution must be given within finite sampling, but it does not require an optimal solution, then the Monte Carlo algorithm should be used. On the other hand, if the problem requires that the optimal solution must be given, but there is no restriction on sampling, then the Las Vegas algorithm should be used.
Monte Carlo algorithm experiment
So it seems that the theoretical support of Monte Carlo method is actually the law of large numbers in the theory of probability or statistics. The basic principle is simply described as follows: first, a large number of simulations, and then calculate the number of times an event occurs, and then divide the number of occurrences by the total number of simulations to get the desired results. Next we use three classic small experiments to learn the idea of Monte Carlo algorithm.
1. Calculate the pi (π) value of pi
Experimental principle: there is a tangent circle inside the square, and the ratio of circle area to square area is (PixRxR) / (2Rx2R) = Pi/4. N points are randomly generated in this square, assuming that the probability of the point falling in the circle is P, then P = circle area / square area, then P = Pi/4. How to calculate the probability P of a point falling in a circle? The distance between the point and the center point can be calculated to determine whether it falls inside the circle. If these points are uniformly distributed, M represents the number of points falling into the circle, and N represents the total number of points, then pi Pi=4P=4xM/N.
The steps of the experiment:
(1) set the center of the circle at the origin (0) and form a circle with R as the radius, then the area of the circle is PixRxR.
(2) if the circle is circumscribed by a square, and the coordinate is (- R ~ mai R) (R ~ m ~ R) (R ~ R) (- R ~ ~ R), then the area of the square is R
(3) immediately take the point (Xmai Y) so that-R
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.