Question

I have this random function to calculate pi Monte Carlo style:

Enter image description here

max=10000000;
format long;

in = 0;
tic
for k=1:max
    x = rand();
    y = rand();
    if sqrt(x^2 + y^2) < 1
        in = in + 1;
    end
end

toc
calc_pi = 4*in/max 
epsilon = abs(pi - calc_pi)

calcPi(100000000);

If I could iterate this 10e100 times, could this algorithm compete with the world record? If so, how can I find the number of iteration that will give the Nth digit?

Was it helpful?

Solution

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)')

Monte Carlo result

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top