Solved – Monte carlo integration in spherical coordinates

integralmonte carlosampling

I was playing around with writing a code for Montecarlo integration of a function defined in spherical coordinates. As a first simple rapid test I decided to write a test code to obtain the solid angle under an angle $\theta_m$. For two random number $u$ and $v$ in $[0,1)$. I generate a homogeneous random sampling of the spherical angle using

$$\phi=2\pi u$$

$$\theta = \arccos(1-2v)$$

For $N$ generated points, I have $M$ points for which $\theta < \theta_m$. My first idea was that since I have an homogeneous sampling I should have obtained the correct solid angle $\Omega=2 \pi (1-\cos (\theta_m))$ simply as $4\pi\times M/N$.
Actually it looks like I the correct result comes out only if I use:

$$\Omega=\sum_{i=1}^M \frac{4\pi}{N} 2 \cos(\theta_i)$$

I can not see the reason why this should be correct.
The probability distribution function in $\theta$ is $PDF=1/2 \sin(\theta)$ so I would rather expect I should normalize each point of the sum by this function but this doesn't work. What am I doing wrong and how could I justify the cosine? Many thanks!

Best Answer

Your calculations are correct and you should get the right Monte Carlo estimate from your formula $4\pi M/N$ with the mapping $(u,v)\to(\phi,\theta)$ you presented. So I would guess it's an error in your code and not in your reasoning. You shouldn't need the cosine weighting factor. Perhaps if you show your code we can spot the error.

Also, on a related note, there's a really nice paper by James Arvo called Stratified Sampling of 2-Manifolds which explains how to construct an area-preserving mapping from the unit-square to a 2-manifold in general, but also specifically for a (hemi-)sphere.

Related Question