Использование метода Монте-Карло в питоне - PullRequest
0 голосов
/ 24 сентября 2018

Я работаю над первой версией вопроса, написанного на картинке ниже.Я сгенерировал одну случайную точку с помощью команды rand и проверил, была ли эта точка внутри круга.Принимает ли мой код количество функций Монте-Карло в качестве входных значений?Я полагаю, что выбрал N, количество точек должно быть достаточно маленьким, поэтому у меня не хватает памяти.Кроме того, когда я запускаю этот код, он запускается без ошибок, но график не отображается.Нужна помощь в тех местах, где я мог ошибиться.

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from random import random

xinside = []
yinside = []
xoutside = []
youtside = []

insidecircle = 0
totalpoints = 10**3

for _ in range(totalpoints):
    x = random()
    y = random()
    if x**2+y**2 <= 1:
        insidecircle += 1
        xinside.append(x)
        yinside.append(y)
    else:
        xoutside.append(x)
        youtside.append(y)

fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.scatter(xinside, yinside, color='g', marker='s')
ax.scatter(xoutside, youtside, color='r', marker='s')
fig.show()

1 Ответ

0 голосов
/ 24 сентября 2018

График не показывает, является загадочным, возможно, попробуйте plt.show().Кроме того, вы можете сохранить график, используя savefig.Вот рабочая функция (просто изменяющая ваш опубликованный код в вопросе) для первой части вашего кода вместе с желаемым графиком вывода.

import numpy as np
import matplotlib.pyplot as plt
from random import random

def monte_carlo(n_points):
    xin, yin, xout, yout = [[] for _ in range(4)] # Defining all 4 lists together
    insidecircle = 0

    for _ in range(n_points):
        x = random()
        y = random()
        if x**2+y**2 <= 1:
            insidecircle += 1
            xin.append(x)
            yin.append(y)
        else:
            xout.append(x)
            yout.append(y)

    print ("The estimated value of Pi for N = %d is %.4f" %(n_points, 4*insidecircle/n_points))

    fig, ax = plt.subplots()
    ax.set_aspect('equal')
    ax.scatter(xin, yin, color='g', marker='o', s=4)
    ax.scatter(xout, yout, color='r', marker='o', s=4)
    plt.savefig('monte_carlo.png')

n_points = 10**4
monte_carlo(n_points)

> The estimated value of Pi for N = 10000 is 3.1380

enter image description here

Векторизованный подход Вы можете назвать его однострочным, если исключите оператор print в функции.Я оставляю анализ времени как домашнее задание

import numpy as np
import matplotlib.pyplot as plt

def monte_carlo(n_points, x, y):
    pi_vec = 4*(x**2 + y**2 <= 1).sum()/n_points
    print ("The estimated value of Pi for N = %d is %.4f" %(n_points, pi_vec))

# Generate points
n_points = 10**4
x = np.random.random(n_points)
y = np.random.random(n_points)
# x = [random() for _ in range(n_points)] # alternative way to define x
# y = [random() for _ in range(n_points)] # alternative way to define y

# Call function
monte_carlo(n_points, x, y)

> The estimated value of Pi for N = 10000 is 3.1284

Кроме того, вы можете избавиться от двух переменных x и y, просто используя один массив точек x и y следующим образом:

pts = np.random.uniform(0, 1, 2*n_points).reshape((n_points, 2))
monte_carlo(n_points, pts) # Function will be def monte_carlo(n_points, pts):

А в функции используйте

pi_vec = 4*(pts[:,0]**2 + pts[:,1]**2 <= 1).sum()/n_points
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...