[PYTHON] Find the ratio of the area of Lake Biwa by the Monte Carlo method

Discraimer This Post contains content that is as good as or better than the technical content that Shiga citizens love Shiga Prefecture. If you want to read pure technical articles, please contact others. Nothing technically amazing has happened.

Shiga citizens

Hello. I am a citizen of Shiga prefecture. What do you think of when you hear the number 1/6?

Actually, this is the ratio of the area of Lake Biwa to the total area of Shiga prefecture. This number is a value that everyone in the prefecture who is in the fifth grade of elementary school or above should know, and a good proportion of the citizens of the prefecture know it.

This is because in Shiga Prefecture, a business called ** Floating School ** is being developed, and fifth graders in the prefecture can stay overnight on a learning boat (which is positioned as a prefectural school) floating on Lake Biwa. We will carry out training activities including. This ship is called ** Uminoko **, and in 2018, the ship that has been popular for over 30 years has retired and is now a new ship that has been renewed.

It's a pretty good experience, so it's one of the good things about Shiga citizens.

Please have a look once. : https://uminoko.jp

By the way, it is interesting that many elementary school students who board this sea urchin say that they can still sing the song "Ship of Hope" that they remember before the training. Of course, I am one of them.

On this ship, elementary school students in the prefecture will observe plankton, experience rope work, and carry out activities with a cutter (small ship). For that reason, I do pre- and post-learning to death, but one of the values that often appears is this 1/6.

Recently, KOKUYO has released a ** Rokubunnoichi field note ** because of its 1/6 name recognition. It uses paper produced from Yoshi on the coast of Lake Biwa, and it is quite good, so please purchase it.

https://www.kokuyo-shop.jp/sp/ProductDetail.aspx?sku=4560107742501

Now, let's move on to the main subject.

As I explained so far, it is 1/6, but unfortunately the people of Kyoto and Osaka all say this. ** "Is Shiga or about half Lake Biwa?" **

It is natural to think so. They probably don't see the rich and vast forests that surround Lake Biwa.

However, we cannot accept it as a matter of course.

That's why Osaka and Kyoto people can do it. Rather, we propose an area ratio experiment that can be done anywhere in the world.

Monte Carlo method

If you are reading this article, you know. It is also a very popular technique for finding pi. When calculating the circumference ratio, ** the ratio of the area of the circle to be contained in the square **.

Yes, the area ratio in a certain area can be stochastically calculated up to a certain accuracy. Therefore, the area of Lake Biwa in Shiga Prefecture is calculated by the Monte Carlo method.

Get the map

First, prepare a map that will be the basis for determining the area of Lake Biwa and the area of Shiga Prefecture. This is fine for the time being.

https://www.freemap.jp/itemFreeDlPage.php?b=shiga&s=shiga 1.png

Map processing

Next, color the blank map. This is a process that allows the software to identify the ** non-Shiga prefecture area ** ** Shiga prefecture land area ** ** Shiga prefecture Lake Biwa area ** on the map. Please fill it appropriately. This time, I used Seashore to ** rgb (255,38,0) in the land area of Shiga prefecture ** and rgb (0,0,255) `in the area of Lake Biwa in Shiga prefecture **. I colored it.

map.png

Puyo Puyo Mingu

Basically, it is the same as finding the pi, setting a random point in the area (square or Shiga prefecture) and determining whether it is in the specified area (circle or Lake Biwa). Repeat. I don't think I care much because what I'm really doing with the yen is the same, but what I have to worry about a little is that the ** denominator is not the epoch number. ** ** Divide the number of ** entering Lake Biwa ** by the number of ** entering Shiga prefecture **. Colored to identify this. As you all know, the color data of a certain coordinate in the image can be obtained by accessing the [y, x] address of the array that comes out by ʻimread` using OpenCV. I don't think there is anything else to worry about. For the time being, the number of trials is set to 50,000.


import cv2
import numpy as np

print("Biwako Area inspector")
print("©︎ Yusei ito 2020")
print("=========================")


print("Importing image..")
img = cv2.imread('./map.png') #Processed map image file
height, width, channels = img.shape[:3]
print("Image Imported.")
print("\tWidth:"+str(width))
print("\tHeight:"+str(height))

count_max=50000
count_shiga=0
count_biwako=0

for i in range(0,count_max):
    px=img[np.random.randint(height),np.random.randint(width)]#Be attention. Array is formed as [y,x] and return is formed [blue,green,red]
    if (px[0]==0 and px[1]==38 and px[2]==255):
        count_shiga+=1
    if (px[0]==255 and px[1]==0 and px[2]==0):
        count_shiga+=1
        count_biwako+=1
    if (count_max%10==0):
        devisionBy=1 if count_shiga==0 else count_shiga
        print(str(i)+"\t"+str(count_biwako/devisionBy))


print("All Process succeed.\r\n\r\n")    
print("Attempt:"+str(count_max))
print("Shiga:"+str(count_shiga))
print("Biwako:"+str(count_biwako))

print("Overall Score:"+str(count_biwako/count_shiga))


Since the execution result is redundant, paste it only at the very end. Since it is Rokubunnoichi, the expected value is 1/6 = 0.1666666 ....


Attempt:50000
Shiga:21143
Biwako:3493
Overall Score:0.16520834318687036

It is good. It's pretty good.

That's why Shiga Prefecture is a good place. Please come once.

A complete set of things about this post is on GitHub. Matching please see.

https://github.com/YuseiIto/Biwako_Rate

Recommended Posts

Find the ratio of the area of Lake Biwa by the Monte Carlo method
Speed comparison of each language by Monte Carlo method
Increase the speed of the Monte Carlo method of Cython cut-out implementation.
Sprinkle rice grains to find the circumference (Monte Carlo method)
Estimating π by Monte Carlo method
Find the area of the union of overlapping rectangles
I couldn't install pypy3.6-7.3.1 on macOS + pyenv, but I could install pypy3.6-7.3.0. I felt the wind of pypy by the Monte Carlo method.
How to find the area of the Voronoi diagram
Dominion compression play analyzed by Monte Carlo method
#Monte Carlo method to find pi using Python
The copy method of pandas.DataFrame is deep copy by default
Understand the metropolitan hasting method (one of the methods in Markov chain Monte Carlo method) with implementation
Reuse the behavior of the @property method by using a descriptor [16/100]
Find the diameter of the graph by breadth-first search (Python memory)
Introduction to Monte Carlo Method
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
Python: Find the required plate thickness for the limit buckling pressure of the exposed tube by the Brent method
cv2.Canny (): Makes the adjustment of edge detection by the Canny method nice
Feature extraction by TF method using the result of morphological analysis
Find out the mystery change of Pokédex description by Levenshtein distance
Find the definition of the value of errno
Simulate Monte Carlo method in Python
Graph the ratio of topcoder, Codeforces and TOEIC by rating (Pandas + seaborn)
I tried to find the optimal path of the dreamland by (quantum) annealing
Find the minimum value of a function by particle swarm optimization (PSO)