使用Monte Carlo Simulation限制计算π [英] Calculating π using a Monte Carlo Simulation limitations

查看:85
本文介绍了使用Monte Carlo Simulation限制计算π的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个与此非常类似的问题,所以我将在最后提及以前的解决方案,我有一个网站,它使用客户端的CPU计算π并将其存储在服务器上,到目前为止,我已经:

I have asked a question very similar to this so I will mention the previous solutions at the end, I have a website that calculates π with the client's CPU while storing it on a server, so far I've got:

'701.766.448.388'指向圆内,总共为'893.547.800.000',这些数字是使用此代码计算的. (工作示例位于: https://jsfiddle.net/d47zwvh5/2/)

'701.766.448.388' points inside the circle, and '893.547.800.000' in total, these numbers are calculated using this code. (working example at: https://jsfiddle.net/d47zwvh5/2/)

let inside = 0;
let size = 500;

for (let i = 0; i < iterations; i++) {
  var Xpos = Math.random() * size;
  var Ypos = Math.random() * size;

  var dist = Math.hypot(Xpos - size / 2, Ypos - size / 2);

  if (dist < size / 2) {
    inside++;
  }
}

问题

(4 * 701.766.448.388)/893.547.800.000 = 3,141483638

(4 * 701.766.448.388) / 893.547.800.000 = 3,141483638

这是我们得到的结果,直到第四个数字4应该为5为止都是正确的.

This is the result we get, which is correct until the fourth digit, 4 should be 5.

以前的问题:

  1. 我搞砸了距离计算.
  2. 我将圆圈的位置从0 ... 499设置为0 ... 500
  3. 我没有使用float,这降低了分辨率"

免责声明

可能只是我已经达到极限,但是本次演示使用了100万点并获得了3.16.考虑到我有大约9000亿,我认为这可能更准确.

It might just be that I've reached a limit but this demonstration used 1 million points and got 3.16. considering I've got about 900 billion I think it could be more precisely.

我确实知道,如果我想计算π,这不是正确的方法,但是我只是想确保一切都正确,所以我希望有人能发现错误或我只需要更多的点".

I do understand that if I want to calculate π this isn't the right way to go about it, but I just want to make sure that everything is right so I was hoping anyone could spot something wrong or do I just need more 'dots'.

编辑:关于数字的不现实性,有很多提及,这些提及在正确的地方,我现在将其更新为正确的.

There are quite a few mentions about how unrealistic the numbers where, these mentions where correct and I have now updated them to be correct.

推荐答案

您可以轻松估算应该得到的错误类型(错误条),这就是蒙特卡洛的魅力所在.为此,您必须计算第二个动量并估算方差和标准偏差.好消息是,收集的价值将与您的均值相同,因为您只是在1比1后加1.

You could easily estimate what kind of error (error bars) you should get, that's the beauty of the Monte Carlo. For this, you have to compute second momentum and estimate variance and std.deviation. Good thing is that collected value would be the same as what you collect for mean, because you just added up 1 after 1 after 1.

然后,您可以获取模拟sigma的估计值以及所需值的误差线.抱歉,我对Java语言的了解不够,所以这里的代码在C#中:

Then you could get estimation of the simulation sigma, and error bars for desired value. Sorry, I don't know enough Javascript, so code here is in C#:

using System;

namespace Pi
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong N = 1_000_000_000UL; // number of samples
            var rng = new Random(312345); // RNG

            ulong v  = 0UL; // collecting mean values here
            ulong v2 = 0UL; // collecting squares, should be the same as mean
            for (ulong k = 0; k != N; ++k) {
                double x = rng.NextDouble();
                double y = rng.NextDouble();

                var r = (x * x + y * y < 1.0) ? 1UL : 0UL;

                v  += r;
                v2 += r * r;
            }

            var mean = (double)v / (double)N;
            var varc = ((double)v2 / (double)N - mean * mean ) * ((double)N/(N-1UL)); // variance
            var stdd = Math.Sqrt(varc); // std.dev, should be sqrt(Pi/4 (1-Pi/4))
            var errr = stdd / Math.Sqrt(N);

            Console.WriteLine($"Mean = {mean}, StdDev = {stdd}, Err = {errr}");

            mean *= 4.0;
            errr *= 4.0;

            Console.WriteLine($"PI (1 sigma) = {mean - 1.0 * errr}...{mean + 1.0 * errr}");
            Console.WriteLine($"PI (2 sigma) = {mean - 2.0 * errr}...{mean + 2.0 * errr}");
            Console.WriteLine($"PI (3 sigma) = {mean - 3.0 * errr}...{mean + 3.0 * errr}");
        }
    }
}

在获得10个 9 个样本后

Mean = 0.785405665, StdDev = 0.410540627166729, Err = 1.29824345388086E-05
PI (1 sigma) = 3.14157073026184...3.14167458973816
PI (2 sigma) = 3.14151880052369...3.14172651947631
PI (3 sigma) = 3.14146687078553...3.14177844921447

看起来正确.很容易看出,在理想情况下,方差等于(Pi/4)*(1-Pi/4).真的没有必要计算v2,只需在仿真后将其设置为v.

which looks about right. It is easy to see that in ideal case variance would be equal to (Pi/4)*(1-Pi/4). It is really not necessary to compute v2, just set it to v after simulation.

坦率地说,我不知道为什么你没有得到预期的结果.求和的精确度损失可能是答案,或者我怀疑,由于播种和重叠序列(因此实际N大大低于900万亿),您的模拟没有生成独立的样本.

I, frankly, don't know why you're getting not what's expected. Precision loss in summation might be the answer, or what I suspect, you simulation is not producing independent samples due to seeding and overlapping sequences (so actual N is a lot lower than 900 trillion).

但是使用这种方法,您可以控制错误并检查计算的进行方式.

But using this method you control error and check how computation is going.

更新

我已插入您的电话号码,以表明您明显低估了价值.代码

I've plugged in your numbers to show that you're clearly underestimating the value. Code

    N  = 893_547_800_000UL;
    v  = 701_766_448_388UL;
    v2 = v;

    var mean = (double)v / (double)N;
    var varc = ((double)v2 / (double)N - mean * mean ) * ((double)N/(N-1UL)); 
    var stdd = Math.Sqrt(varc); // should be sqrt(Pi/4 (1-Pi/4))
    var errr = stdd / Math.Sqrt(N);

    Console.WriteLine($"Mean = {mean}, StdDev = {stdd}, Err = {errr}");

    mean *= 4.0;
    errr *= 4.0;

    Console.WriteLine($"PI (1 sigma) = {mean - 1.0 * errr}...{mean + 1.0 * errr}");
    Console.WriteLine($"PI (2 sigma) = {mean - 2.0 * errr}...{mean + 2.0 * errr}");
    Console.WriteLine($"PI (3 sigma) = {mean - 3.0 * errr}...{mean + 3.0 * errr}");

并输出

Mean = 0.785370909522692, StdDev = 0.410564786603016, Err = 4.34332975349809E-07
PI (1 sigma) = 3.14148190075886...3.14148537542267
PI (2 sigma) = 3.14148016342696...3.14148711275457
PI (3 sigma) = 3.14147842609506...3.14148885008647

因此,很明显,您在某处存在问题(代码?表示中丢失的准确性?累加中的准确性?重复/非独立采样?)

So, clearly you have problem somewhere (code? accuracy lost in representation? accuracy lost in summation? repeated/non-independent sampling?)

这篇关于使用Monte Carlo Simulation限制计算π的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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