Estimate of $\pi$¶

Draw two independent random numbers $x,y \in [0,1]$ and compute $x^2+y^2$. Assign a mark

$$ m_i = \begin{cases} 1, & x^2+y^2 \leq 1,\\ 0, & x^2+y^2 > 1. \end{cases} $$

The probability of receiving a mark equals the area of the quarter circle:

$$ p_c = \frac{\pi}{4}. $$

After $N$ independent trials, the observed fraction of marks is

$$ \langle m \rangle = \frac{1}{N}\sum_{i=1}^{N}m_i = p. $$

As $N$ becomes large, $p \longrightarrow p_c$.

Since each mark is either 0 or 1, $m_i^2=m_i$. Therefore,

$$ \begin{aligned} \langle m^2 \rangle &= \frac{1}{N}\sum_{i=1}^{N}m_i^2 = p,\\ \langle m^2 \rangle_c &= \langle m^2 \rangle-\langle m \rangle^2 = p(1-p). \end{aligned} $$

The standard deviation of the individual marks is

$$ \sigma_m = \sqrt{\langle m^2\rangle_c} = \sqrt{p(1-p)}. $$ Repeating a measurement more times does not shrink the spread of its results; grouping trials and taking each group average as a new result produces a smaller spread.

Now run the code $K$ times, each with $N$ independent trials. Each run produces an average mark $\langle m\rangle_r$. The mean of these $K$ averages is

$$ s = \frac{1}{K}\sum_{r=1}^{K}\langle m\rangle_r. $$

Their variance is

$$ \mathrm{err}^2 = \frac{1}{K}\sum_{r=1}^{K} \left(\langle m\rangle_r-s\right)^2. $$

For fixed $N$, as $K$ becomes large,

$$ s \longrightarrow p_c, \qquad \mathrm{err}^2 \longrightarrow \frac{p_c(1-p_c)}{N}. $$

Here $\mathrm{err}$ measures the spread of the results from runs of $N$ trials. Increasing $K$ determines this spread more accurately; it does not reduce it. The variance is intensive in $K$.

In [1]:
let
    # estimate based on mean and std. deviation

	N_trials = 2500

    retv = zeros(N_trials)

	for i1 in 1:N_trials

        if sum( rand(2) .^ 2 ) < 1.0
            retv[i1] = 1
        end
			
	end

    av = sum(retv)/N_trials

    # std. dev. of marks
    # sig = sqrt(sum(@. retv^2)/N_trials - av^2)
    sig = sqrt(sum(@. (retv - av)^2)/N_trials)

    pc = pi/4
	println("mean:", [av, pc])
    # std. deviation will not go down
    println("std. deviation: ", [sig, sqrt(pc*(1-pc))])
	
end
mean:[0.7604, 0.7853981633974483]
std. deviation: [0.42683936088416186, 0.410545841934081]
In [2]:
let
    function run(N)
        # 1 run = average over N trials
        retv = zeros(N)

    	for i1 in 1:N
            if sum( rand(2) .^ 2 ) < 1.0
                retv[i1] = 1
            end	
    	end

        return sum(retv)/N
    end

    # 1 run = N trials, NK runs
    N, NK = 10, 2500
    full_set = [run(N) for i1 in 1:NK]
    av = sum(full_set)/NK

    ret = 0.0
    for entry in full_set
        ret += (entry-av)^2
    end
    ret = ret / NK
    err = sqrt(ret)

    pc = pi/4
    println([av, pc])
    println([err, sqrt(pc*(1-pc)/N)])
    
end
    
[0.7925199999999998, 0.7853981633974483]
[0.1282343542113409, 0.1298259944423163]

mean distance between two points in an N-dim cube¶

randomly pick 2 points in an N-dim cube, what is the average distance?

For 1D:

$\frac{\int dx_1 dx_2 \vert x_1-x_2 \vert }{\int dx_1 \int dx_2}$

The answer is (shockingly) 1/3.

See the world record here. "

In [3]:
let

	function dist_cal(ndim)

		ret = 0.0
		# not so accurate
		Nmax = 5E6
		d(x1, x2)= sqrt(sum(@. (x1-x2)^2))

		for i1 in 1:Nmax
			ret += d(rand(ndim), rand(ndim)) / Nmax
		end

		return ret
	end

	[dist_cal(ndim) for ndim in 1:10]

end
Out[3]:
10-element Vector{Float64}:
 0.33340246569676923
 0.5211691733226804
 0.6616855071596984
 0.7778235097651309
 0.8785743625766398
 0.9689304097758934
 1.0516023862587782
 1.1280099211438885
 1.1999644329694368
 1.267472184104953
In [4]:
let

	# 1D limit
	dx = 1E-4
    xarr = range(0.0, 1.0, step = dx)

    ret = 0.0
    for x1 in xarr, x2 in xarr
        ret += abs(x1-x2) * dx^2
    end

    println(ret)
	
end
0.333433340000315