3
$\begingroup$

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 an homogeneous random sampling of the spherical angle using $\phi=2\pi u$ and $\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 \frac{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=\frac{1}{2} \sin(\theta)$ so I would rather expect I should normalize each point of the sum by this function but this doesn't works. What am I doing wrong and how could I justify the cosinus? Many thanks!

  • 0
    I'msorry it was$a$missprint, I had to write $\theta=arccos(1-2v)$. I have corrected the question now.2011-05-23

1 Answers 1

2

You can considerably simplify this. The angle $\phi$ doesn't occur anywhere, so you're not actually using $u$ or $\phi$, so we can ignore them. The angle $\theta$ only occurs in the form $\cos\theta$, so it makes sense to switch to $z=\cos\theta$ instead.

Restated in this way, your question is whether generating (presumably uniformly distributed) random numbers $v$ in $[0,1)$ and calculating $z=1-2v$ will lead to $z>z_m$ in a fraction $M/N=2\pi(1-z_m)/(4\pi)=(1-z_m)/2$ of cases. This linear relationship is clearly correct, so your formula is OK and it seems there must be something wrong in your program instead if this is not working.

More generally speaking, when you're using both math and code, it's always important to keep an open mind about in which of the two domains the error might lie. Your question seems to indicate that you may have prematurely decided that the code was OK and the error must be in the math.

  • 0
    Thank you for your answer, it pushed me even more to look into the code. Actually a monday morning mind opened by a cup of coffee helped my to find the bug in the code I couldn't see before!2011-05-23