Can a Monte Carlo pi calculation be used for a world record?

ぐ巨炮叔叔 提交于 2019-12-01 00:38:04

This is a nice exercise for calculating pi, but it is probably a very inefficient one. Some remarks:

  • My statistics are rusty before I had my coffee, but I guess the error scales with 1 / sqrt(n_guess). To get N digits, you need an error of 10^(-N), so you would need roughly (10^N)^2 random guesses. If you would do 1e100 guesses, as you proposed, you would only get on the order of 50 digits of pi! The number of iteration is thus some exponentional function of the number of required digits, which is horribly slow. A good algorithm is maybe linear in the number of digits you want.

  • With the large number of guesses required, you have to start questioning the quality of your random number generator.

  • Your algorithm will be limited by floating-point errors to 1e-16 or so. Calculating digits of pi requires some sort of arbitrary precision number format.

  • To speed up your algorithm, you can leave out the sqrt().

  • Don't use a variable called max, this overwrites an existing function. Use n_guess or so.

Quick and dirty test to prove my theory (after coffee):

pie = @(n) 4 * nnz(rand(n,1).^2 + rand(n, 1).^2 < 1) / n;
ntrial = round(logspace(1, 8, 100));
pies = arrayfun(pie, ntrial);

loglog(ntrial, abs(pies - pi), '.k', ntrial, ntrial.^-.5, '--r')
xlabel('ntrials')
ylabel('epsilon')
legend('Monte Carlo', '1 / sqrt(ntrial)')

Short answer: no.

The world record is 1e13 digits, according to your link. If you could run the Monte Carlo algorithm to obtain 10e100 samples, you would obtain an estimate of pi with a relative RMS error of 1/sqrt(10e100) = .3e-50 (see below). This a precision of the order of the 50th digit only. Besides, it is only a "probabilistic" precision: you wouldn't be sure that the first 50 digits are correct; you could only tell that there would be a high probability of them being correct.

The general rule to find how many Monte Carlo samples you need for an N-digit precision is this: M Monte Carlo samples will give you a relative RMS precision of 1/sqrt(M). This means that the estimate deviates from the true value by a 1/sqrt(M) fraction of the true value, approximately. To be reasonably confident that the N-th digit is correct you need a relative RMS precision a little better than 10^-N, which according to the stated rule requires M = 10^(2N) samples.

Therefore, if you wanted a (probabilistic) precision of 1e13 digits you would need 10^2e13 Monte Carlo samples, which is unmanageable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!