大量试验的蒙特卡罗模拟 [英] Monte Carlo simulations by large no of trials

查看:55
本文介绍了大量试验的蒙特卡罗模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的程序.

import math
import random

def inside_unit_circle(point):
    """
    Compute distance of point from origin
    """
    distance = math.sqrt(point[0] ** 2 + point[1] ** 2)
    return distance < 1


def estimate_mystery(num_trials):
    """
    Main function
    """
    num_inside = 0

    for dumm_idx in range(num_trials):
        new_point = [2 * random.random() - 1, 2 * random.random() - 1]
        if inside_unit_circle(new_point):
            num_inside += 1

    return float(num_inside) / num_trials

print estimate_mystery(10000)

该程序使用 random.random() 生成一组随机点,这些点均匀分布在角位于

This program uses random.random() to generates a random set of points that are uniformly distributed over the square with corners at

(1, 1) (−1, 1)
(1,−1) (−1,−1)

这里,均匀分布意味着正方形中的每个点都有相等的生成机会.然后该方法测试这些点是否位于单位圆内.

Here, being uniformly distribution means that each point in the square has an equal chance of being generated. The method then tests whether these points lie inside a unit circle.

随着试验次数的增加,estimate_mystery 返回的值趋向于一个特定的值,该值具有一个简单的表达式,涉及一个众所周知的常量.在下面输入此值作为数学表达式.(不要输入浮点数.)

As one increases the number of trials, the value returned by estimate_mystery tends towards a specific value that has a simple expression involving a well-known constant. Enter this value as a math expression below. (Do not enter a floating point number.)

推荐答案

因此,您需要以越来越多的试验次数运行estimate_mystery.当您这样做时,很明显该值会增加到以下简单表达式:

So you need to run estimate_mystery with increasingly higher numbers of trials. As you do so, it will become clear that the value increases to the following simple expression:

(\sum_{k=1}^{\infty} \frac{e^{i\pi(k+1)}}{2k-1})

然而,应该注意的是,这不是唯一的正确答案.以下内容也是有效的,其中 \zeta 是黎曼 zeta 函数:

It should be noted, however, that this is not the only correct answer. The following would have been valid too, where \zeta is the Riemann zeta function:

然而,这不包括众所周知的常量e.

However, this does not include the well-known constant e.

我不知道为什么这令人困惑.很明显 sum 表达式是正确的,而且写得很清楚:图像下方的代码是非常标准的 LaTeX 数学表达式格式.但是为了说明它的正确性,这里有一个图显示了当将总和取为 n 时的收敛性,并且将估计之谜运行到 n 时:

I'm not sure why this is confusing. It's quite clear that the sum expression is correct, and it's written quite clearly: the code below the image is very standard LaTeX formatting for mathematical expressions. But to illustrate its correctness, here's a plot showing the convergence when taking the sum to n, and running estimate_mystery up to n as well:

嗯……也许这不是你的问题?它还应该收敛到以下内容,其中 \gamma 是在复平面上围绕 z=0 的单位圆:

Hrmm... maybe this wasn't what your question wanted? It should also converge to the following, where \gamma is a unit circle around z=0 on the complex plane:

(-i\oint_\gamma z^{-3}e^{\frac{z}{2}}dz)

这篇关于大量试验的蒙特卡罗模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆