Home  >  Article  >  Backend Development  >  Python implements Monte Carlo method (code example)

Python implements Monte Carlo method (code example)

不言
不言forward
2019-01-14 11:29:026947browse

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 using import 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 <p>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</p><p><span class="img-wrap"><img src="https://img.php.cn//upload/image/549/675/172/1547436392667253.png" title="1547436392667253.png" alt="Python implements Monte Carlo method (code example)"></span></p><p>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. </p><p   style="max-width:90%"><span class="img-wrap"><img src="https://img.php.cn//upload/image/583/174/740/1547436406520588.png" title="1547436406520588.png" alt="Python implements Monte Carlo method (code example)"></span></p><p>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). </p><p>6. Code implementation: </p><pre class="brush:php;toolbar:false">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)

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete