Python implements Monte Carlo method (code example)

不言
Release: 2019-01-14 11:29:02
forward
6906 people have browsed it

The content of this article is about implementing the Monte Carlo method (code example) in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The Monte Carlo method is a statistical simulation method proposed by von Neumann and Ulam. Under a large number of random numbers, according to the probability estimation results, the more random data, the more The results are more precise. Below we will use python to implement the Monte Carlo method.

1. First, we do a simple approximate calculation of pi. In this process, we need to use random numbers, so we need to import the numpy library first usingimport numpy as np.

2. Code implementation:

import numpy as np total = 8000000 count = 0 for i in range(total): x = np.random.rand() y = np.random.rand() dis = (x**2+y**2)**0.5 if dis 

3. In the above program, we use 8000000 random numbers for delivery, so that the results obtained will be more accurate, and it will take a certain amount of time to run the program. The final result is as follows

Python implements Monte Carlo method (code example)

4. Let’s perform a simple application. The picture below is a picture I drew randomly in the drawing tool. , we can use the Monte Carlo method to estimate the area of the black part in the picture.

Python implements Monte Carlo method (code example)

5. The above graph is irregular. We only need to know that when a large number of random numbers are thrown, the random numbers appear in the black part. probability, and then multiply the total area to estimate the area of the black part. We know that the black rgb code is (0,0,0), so we need to calculate the probability of random numbers when the rgb code is (0,0,0).

6. Code implementation:

from PIL import Image import numpy as np im = Image.open("C:/Users/21974/Desktop/handwrite2.PNG") total = 9000000 count = 0 defin = 0 width = im.size[0] height = im.size[1] for i in range(total): #用蒙特卡罗方法获得估计值 x = np.random.randint(0, width-1) y = np.random.randint(0, height-1) k = im.getpixel((x, y)) if k[0]+k[1]+k[2] == 0: count += 1 print(int(width*height*count/total)) for i in range(width): #用遍历获得准确值 for j in range(height): k = im.getpixel((i, j)) if k[0] + k[1] + k[2] == 0: defin += 1 print(defin)
Copy after login

The above code can be divided into two parts. The first for is followed by the estimated value of the area obtained by the Monte Carlo method, and the second for is followed by It is the exact value of the area obtained by traversing all pixels, and then compares the two outputs.

Python implements Monte Carlo method (code example)

We used 9,000,000 random numbers in the above program. It can be seen that the two output results are not very different.

The above is the detailed content of Python implements Monte Carlo method (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!