Algorithm Search⌘K

Algorithmadvanced

Kahn's method for Compton scattering

Also called Klein-Nishina sampling.

Samples the energy and angle of a Compton scattered photon from the Klein-Nishina law by mixing two simple densities and rejecting, with no square roots or tables.

Time
O(N) average
Space
O(1) extra
Where
N = the number of samples
Properties
randomizedexactiterativerandomized
Origin
Herman Kahn, 1954

Kahn's method samples the scattering of a photon by a free electron at rest (Compton scattering) from the Klein-Nishina cross section. The photon energy before and after, in units of the electron rest energy mc2=510.999mc^2 = 510.999 keV, are α\alpha and α′\alpha', and the method samples the ratio q=α/α′q = \alpha/\alpha', which lies in [1,1+2α][1, 1 + 2\alpha]; the scattering angle then follows from the Compton formula. It writes the target density as a mixture of two densities that are easy to sample by inverse transform sampling and uses rejection sampling in each branch, with acceptance factors that are at most 1. The accepted samples have exactly the Klein-Nishina law. Each try needs three uniform numbers and a few arithmetic operations, with no square roots, logarithms or tables, and about 60 percent of the tries are accepted at the energies of nuclear medicine and reactor shielding. Other algorithms sample the same law (Koblinger's direct method, table methods with the alias method); this page is about Kahn's scheme.

In plain words

When an X-ray or gamma ray hits an electron, it bounces off in a new direction and loses some energy. A small angle means a small loss, and a back bounce means the largest loss. A physics formula from 1929 gives how likely each angle is.

A simulation must pick a random angle that follows this formula. There is no simple trick that turns one random number into such an angle. Kahn's method makes a guess from one of two simple shapes, chosen at random, and then keeps or throws away the guess with a test. Guesses that are kept follow the formula exactly. More than half of the guesses are kept at the usual energies, and every step uses only additions, multiplications and divisions.

The problem

A photon of energy EE scatters off a free electron at rest. Write α=E/(mc2)\alpha = E/(mc^2) and μ=cos⁡θ\mu = \cos\theta for the cosine of the scattering angle. The Compton formula gives the energy after the scattering, α′=α/(1+α(1−μ))\alpha' = \alpha/(1 + \alpha(1 - \mu)), so q=α/α′=1+α(1−μ)q = \alpha/\alpha' = 1 + \alpha(1 - \mu) runs from 11 (forward, μ=1\mu = 1) to 1+2α1 + 2\alpha (backward, μ=−1\mu = -1). The Klein-Nishina cross section per unit solid angle is

dσdΩ=re22(α′α)2(α′α+αα′−1+μ2),\frac{d\sigma}{d\Omega} = \frac{r_e^2}{2} \Big(\frac{\alpha'}{\alpha}\Big)^2 \Big(\frac{\alpha'}{\alpha} + \frac{\alpha}{\alpha'} - 1 + \mu^2\Big),

with rer_e the classical electron radius. With dΩ=2π dμd\Omega = 2\pi\, d\mu and dμ=−dq/αd\mu = -dq/\alpha, the density of qq on [1,1+2α][1, 1 + 2\alpha] is proportional to

f(q)=1q2(q+1q−1+μ2),μ=1−q−1α.f(q) = \frac{1}{q^2}\Big(q + \frac{1}{q} - 1 + \mu^2\Big), \qquad \mu = 1 - \frac{q - 1}{\alpha}.

The task is to draw qq (and so μ\mu and α′\alpha') with this density, for any α>0\alpha > 0. The azimuth of the new direction is uniform on [0,2π)[0, 2\pi) and is sampled separately. In the limit α→0\alpha \to 0 the law becomes the Thomson law with density proportional to 1+μ21 + \mu^2 in μ\mu.

The algorithm

  1. KAHN(α) ▷ α = E / (m c²) > 0
  2. 1loop
  3. 2draw ξ_1, ξ_2, ξ_3 uniform in [0, 1)
  4. 3if ξ_1 ≤ (1 + 2α) / (9 + 2α) then ▷ branch 1
  5. 4q ← 1 + 2α ξ_2 ▷ uniform on [1, 1 + 2α]
  6. 5if ξ_3 ≤ 4 (1/q - 1/q²) then accept
  7. 6else ▷ branch 2
  8. 7q ← (1 + 2α) / (1 + 2α ξ_2) ▷ density proportional to 1/q² on [1, 1 + 2α]
  9. 8μ ← 1 - (q - 1)/α; if ξ_3 ≤ (μ² + 1/q) / 2 then accept
  10. 9return α' = α / q and μ = 1 - (q - 1)/α

Branch 1 proposes qq uniformly; the factor 4(1/q−1/q2)=4(q−1)/q24(1/q - 1/q^2) = 4(q - 1)/q^2 has its maximum 1 at q=2q = 2, so it is a valid acceptance probability. Branch 2 proposes qq with density proportional to 1/q21/q^2, by inverse transform sampling: the distribution function of that density on [1,1+2α][1, 1 + 2\alpha] inverts to line L7. Its factor (μ2+1/q)/2(\mu^2 + 1/q)/2 is at most 1. The branch weights (1+2α)/(9+2α)(1 + 2\alpha)/(9 + 2\alpha) and 8/(9+2α)8/(9 + 2\alpha) make the sum of the two accepted densities proportional to ff, as the proof shows.

Example

Hand trace with α=1\alpha = 1 (a 511 keV photon). The weight of branch 1 is 3/11=0.2727273/11 = 0.272727.

try ξ1\xi_1 branch ξ2\xi_2 qq μ\mu test result
1 0.5 2 0.25 3/1.5=23/1.5 = 2 0 ξ3=0.6≤(0+0.5)/2=0.25\xi_3 = 0.6 \le (0 + 0.5)/2 = 0.25? reject
2 0.2 1 0.5 1+1=21 + 1 = 2 0 ξ3=0.9≤4(0.5−0.25)=1\xi_3 = 0.9 \le 4(0.5 - 0.25) = 1? accept

The photon scatters at 90 degrees with α′=1/2\alpha' = 1/2: it leaves with 255.5255.5 keV, and the electron takes the other half. A back scattering of the same photon (μ=−1\mu = -1) gives q=3q = 3 and 170.3170.3 keV.

The acceptance rate. The probability that one try is accepted is 4(1+2α)2α(9+2α)∫11+2αf(q) dq\frac{4(1 + 2\alpha)}{2\alpha(9 + 2\alpha)} \int_1^{1 + 2\alpha} f(q)\, dq. Numerical integration gives:

α\alpha EE accepted fraction
0.01 5.1 keV 0.591
0.1 51 keV 0.585
1 511 keV 0.627
2 1.02 MeV 0.644
5 2.55 MeV 0.589
10 5.11 MeV 0.474
100 51.1 MeV 0.110

As α→0\alpha \to 0 the fraction tends to 32/54=0.59332/54 = 0.593. At high energy the law is strongly forward and the two proposal shapes fit it badly, so the method gets slow; codes switch to another sampler there.

Moments. Numerical integration of ff gives the exact mean of μ\mu: 0.06860.0686 for α=0.1\alpha = 0.1, 0.29140.2914 for α=1\alpha = 1 and 0.44770.4477 for α=5\alpha = 5, so the scattering gets more forward as the energy grows. With 10510^5 samples of Kahn's method at α=1\alpha = 1 (random.Random(1)) the sample mean is 0.29120.2912, within the statistical error of about 0.00190.0019. At α=10−4\alpha = 10^{-4} the exact mean of μ2\mu^2 is the Thomson value 2/52/5.

Correctness

The claim is a theorem: the accepted qq has the density f(q)/∫ff(q)/\int f on [1,1+2α][1, 1 + 2\alpha] for every α>0\alpha > 0, so the method is exact. The reason is a short computation. Branch 1 has the proposal density 1/(2α)1/(2\alpha) and the acceptance 4(q−1)/q24(q - 1)/q^2. Branch 2 has the proposal density (1+2α)/(2αq2)(1 + 2\alpha)/(2\alpha q^2) and the acceptance (μ2+1/q)/2(\mu^2 + 1/q)/2. With the branch weights, the density of an accepted qq in one try is

4(1+2α)2α(9+2α)⋅1q2(q−1+μ2+1q),\frac{4(1 + 2\alpha)}{2\alpha(9 + 2\alpha)} \cdot \frac{1}{q^2}\Big(q - 1 + \mu^2 + \frac{1}{q}\Big),

which is a constant times f(q)f(q). A rejected try starts afresh, so the accepted value has the normalised density. The full proof is below.

Proof: Kahn's method samples the Klein-Nishina law exactlyfull

We assume exact arithmetic and exact independent uniform numbers ξ1,ξ2,ξ3\xi_1, \xi_2, \xi_3 on [0,1)[0, 1) in each try, independent between tries. Fix α>0\alpha > 0, write I=[1,1+2α]I = [1, 1 + 2\alpha], μ(q)=1−(q−1)/α\mu(q) = 1 - (q - 1)/\alpha, and

f(q)=1q2(q+1q−1+μ(q)2),q∈I,f(q) = \frac{1}{q^2}\Big(q + \frac{1}{q} - 1 + \mu(q)^2\Big), \qquad q \in I,

the Klein-Nishina density in qq up to a constant. As qq runs over II, μ\mu runs over [−1,1][-1, 1].

Lemma 1 (the target). The Klein-Nishina law for μ\mu is the law of μ(q)\mu(q) when qq has density proportional to ff on II.

Proof. The Klein-Nishina density in μ\mu is proportional to P2(P+1/P−1+μ2)P^2(P + 1/P - 1 + \mu^2) with P=α′/α=1/qP = \alpha'/\alpha = 1/q and q=1+α(1−μ)q = 1 + \alpha(1 - \mu). The map μ↦q\mu \mapsto q is affine with ∣dq/dμ∣=α|dq/d\mu| = \alpha, a constant, so the density in qq is the same expression divided by α\alpha, which is f(q)/αf(q)/\alpha. □\square

Lemma 2 (the acceptance factors are probabilities). For q∈Iq \in I, 0≤4(1/q−1/q2)≤10 \le 4(1/q - 1/q^2) \le 1 and 0≤(μ2+1/q)/2≤10 \le (\mu^2 + 1/q)/2 \le 1.

Proof. 4(1/q−1/q2)=1−(1−2/q)2∈[0,1]4(1/q - 1/q^2) = 1 - (1 - 2/q)^2 \in [0, 1] for q≥1q \ge 1. And μ2≤1\mu^2 \le 1, 0<1/q≤10 < 1/q \le 1. □\square

Lemma 3 (the proposals). In branch 1, line L4 gives qq with density 1/(2α)1/(2\alpha) on II. In branch 2, line L7 gives qq with density (1+2α)/(2αq2)(1 + 2\alpha)/(2\alpha q^2) on II.

Proof. The first is clear. For the second, q=(1+2α)/(1+2αξ2)q = (1 + 2\alpha)/(1 + 2\alpha \xi_2) is a decreasing function of ξ2\xi_2 from 1+2α1 + 2\alpha (at ξ2=0\xi_2 = 0) to 11 (at ξ2=1\xi_2 = 1). Its inverse is ξ2=((1+2α)/q−1)/(2α)\xi_2 = ((1 + 2\alpha)/q - 1)/(2\alpha) with ∣dξ2/dq∣=(1+2α)/(2αq2)|d\xi_2/dq| = (1 + 2\alpha)/(2\alpha q^2), which is the density of qq because ξ2\xi_2 is uniform. As a check, ∫I(1+2α)/(2αq2) dq=1+2α2α(1−11+2α)=1\int_I (1 + 2\alpha)/(2\alpha q^2)\, dq = \frac{1 + 2\alpha}{2\alpha}(1 - \frac{1}{1 + 2\alpha}) = 1. □\square

Lemma 4 (one try). For every measurable A⊆IA \subseteq I, the probability that one try accepts a value q∈Aq \in A is C∫Af(q) dqC \int_A f(q)\, dq with

C=4(1+2α)2α(9+2α).C = \frac{4(1 + 2\alpha)}{2\alpha(9 + 2\alpha)}.

Proof. Branch 1 is taken with probability w1=(1+2α)/(9+2α)w_1 = (1 + 2\alpha)/(9 + 2\alpha) and branch 2 with w2=8/(9+2α)w_2 = 8/(9 + 2\alpha), by ξ1\xi_1. Given the branch and qq, the try accepts with the factor of Lemma 2, because ξ3\xi_3 is uniform and independent. So the density of an accepted qq is, by Lemma 3,

w112α⋅4(1q−1q2)+w21+2α2αq2⋅12(μ2+1q).w_1 \frac{1}{2\alpha} \cdot 4\Big(\frac{1}{q} - \frac{1}{q^2}\Big) + w_2 \frac{1 + 2\alpha}{2\alpha q^2} \cdot \frac{1}{2}\Big(\mu^2 + \frac{1}{q}\Big).

The first term is 4(1+2α)2α(9+2α)⋅q−1q2\frac{4(1 + 2\alpha)}{2\alpha(9 + 2\alpha)} \cdot \frac{q - 1}{q^2} and the second is 4(1+2α)2α(9+2α)⋅μ2+1/qq2\frac{4(1 + 2\alpha)}{2\alpha(9 + 2\alpha)} \cdot \frac{\mu^2 + 1/q}{q^2}. Their sum is C(q−1+μ2+1/q)/q2=Cf(q)C (q - 1 + \mu^2 + 1/q)/q^2 = C f(q). □\square

Theorem (exactness). KAHN(α\alpha) ends with probability 1, the number of tries has the geometric law with success probability p=C∫Ifp = C \int_I f, and the returned qq has the density f/∫Iff/\int_I f on II. So the returned μ\mu has the Klein-Nishina law and α′=α/q\alpha' = \alpha/q is the Compton energy.

Proof. By Lemma 4 with A=IA = I, each try accepts with probability p>0p > 0 (f>0f > 0 on II), independently of the other tries. So the number of tries is geometric and finite with probability 1. The returned value is the value of the first accepted try. Because tries are independent and identical, its law is the law of qq in one try given acceptance: P(q∈A)=C∫Af/(C∫If)P(q \in A) = C \int_A f / (C \int_I f). Lemma 1 turns this into the Klein-Nishina law of μ\mu. ■\blacksquare

The efficiency. p(α)=C∫11+2αf(q) dqp(\alpha) = C \int_1^{1 + 2\alpha} f(q)\, dq. As α→0\alpha \to 0, put q=1+α(1−μ)q = 1 + \alpha(1 - \mu); then f→1+μ2f \to 1 + \mu^2, dq=α dμdq = \alpha\, d\mu, and ∫If≈α∫−11(1+μ2) dμ=8α/3\int_I f \approx \alpha \int_{-1}^1 (1 + \mu^2)\, d\mu = 8\alpha/3, while C≈4/(18α)C \approx 4/(18\alpha). So p→32/54=16/27≈0.593p \to 32/54 = 16/27 \approx 0.593. The same limit shows that the accepted law tends to the Thomson law 38(1+μ2)\frac{3}{8}(1 + \mu^2), which has E[μ2]=2/5E[\mu^2] = 2/5.

Complexity

Each try costs three uniform numbers and O(1)O(1) arithmetic, with no transcendental functions. The number of tries is geometric with success probability p(α)p(\alpha) from the table, so the expected cost of one sample is 1/p(α)1/p(\alpha) tries: about 1.6 tries for α≤5\alpha \le 5 and about 9 at α=100\alpha = 100. A run of NN samples costs O(N)O(N) in expectation for a fixed α\alpha, with O(1)O(1) space.

Properties

  • Exact. No table, fit or truncation; the law is exact up to rounding.
  • Cheap per try. Only additions, multiplications and divisions. This mattered in 1954 and still matters on hardware where logarithms are slow.
  • Valid for all energies. The acceptance factors are at most 1 for every α>0\alpha > 0, but the efficiency falls at high energy.
  • Free electrons only. It samples the Klein-Nishina law for an electron at rest. Binding effects need a correction.

Variants and improvements

  • Koblinger's method. Koblinger wrote ff as a mixture of four terms that can each be sampled exactly by inversion, with no rejection. Codes use it at higher energies, where Kahn's method is slow.
  • Bound electrons. Multiply the Klein-Nishina law by an incoherent scattering function S(x,Z)S(x, Z) of the momentum transfer, and reject with S/Smax⁡S/S_{\max} after Kahn's step. This lowers forward scattering at low energy.
  • Doppler broadening. For bound electrons with a momentum distribution, sample the electron momentum from the Compton profile and correct the scattered energy.
  • Tables. Tabulate the inverse distribution function in qq for a grid of α\alpha and interpolate, or use the alias method on a fine grid. This is fast but approximate.
  • Other rejection schemes. Butcher and Messel wrote another mixture with rejection for their shower calculations, and the EGS codes use a scheme of that type.

When to use it

Use Kahn's method to sample free-electron Compton scattering in photon transport at energies up to a few MeV, for example in shielding, medical physics and gamma-ray detector codes. At higher energies use Koblinger's direct method or a table. Where binding matters (low energies, high atomic number), add the incoherent scattering function correction.

Pitfalls

  • The wrong density. The density in μ\mu and the density in qq differ by the factor dμ/dq=−1/αd\mu/dq = -1/\alpha, which is constant, so they have the same shape; but a density in θ\theta needs the factor sin⁡θ\sin\theta. Mixing these up gives the wrong angles.
  • The direction update. The sampled μ\mu is the cosine relative to the old direction. The new direction must be built by rotating the old one by θ\theta and a uniform azimuth, not set to μ\mu along a fixed axis.
  • Units. α\alpha is the energy in units of mc2=0.510999mc^2 = 0.510999 MeV. Using MeV directly gives a wrong law.
  • Small α\alpha. Line L8 divides by α\alpha. For α\alpha near the smallest positive float the rounding of μ\mu is poor; use the Thomson limit there.
  • The weights of the branches. The weight of branch 1 is (1+2α)/(9+2α)(1 + 2\alpha)/(9 + 2\alpha), not 1/21/2. With wrong weights the method is still a valid sampler of some law, so tests on ranges do not catch the error; compare moments with the exact law.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

kahn_compton_sampling.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/kahn-compton-sampling/. The authors are listed in prov/7W42N2.jsonl.
"""Kahn's rejection method for the Klein-Nishina distribution.

A photon of energy E scatters off a free electron at rest. Write
alpha = E / (m c^2) and q = alpha / alpha' = E / E', so that the
Compton formula reads q = 1 + alpha (1 - mu), with mu the cosine of the
scattering angle, and q runs over [1, 1 + 2 alpha]. The Klein-Nishina
cross section, as a density in q, is proportional to

    (1 / q^2) (q + 1/q - 1 + mu^2),   mu = 1 - (q - 1) / alpha.

Kahn's method mixes two easy densities in q (uniform, and the density of
(1 + 2 alpha) / (1 + 2 alpha xi)) with weights (1 + 2 alpha) / (9 + 2 alpha)
and 8 / (9 + 2 alpha), and accepts with a factor at most 1 in each branch.
The accepted q has exactly the Klein-Nishina law. The method uses no
square roots, logarithms or tables.
"""

import math


def kahn_sample(alpha: float, rng):
    """Sample one Compton scattering. Returns (alpha_out, mu, tries)."""
    tries = 0
    p1 = (1.0 + 2.0 * alpha) / (9.0 + 2.0 * alpha)
    while True:  # @line L1
        tries += 1
        r1, r2, r3 = rng.random(), rng.random(), rng.random()  # @line L2
        if r1 <= p1:  # @line L3
            q = 1.0 + 2.0 * alpha * r2  # @line L4
            if r3 <= 4.0 * (1.0 / q - 1.0 / (q * q)):  # @line L5
                break
        else:  # @line L6
            q = (1.0 + 2.0 * alpha) / (1.0 + 2.0 * alpha * r2)  # @line L7
            mu = 1.0 - (q - 1.0) / alpha
            if r3 <= 0.5 * (mu * mu + 1.0 / q):  # @line L8
                break
    mu = 1.0 - (q - 1.0) / alpha
    return alpha / q, mu, tries  # @line L9


def klein_nishina_mu_density(alpha: float, mu: float) -> float:
    """Unnormalized Klein-Nishina density in mu: P^2 (P + 1/P - 1 + mu^2), P = E'/E."""
    p = 1.0 / (1.0 + alpha * (1.0 - mu))
    return p * p * (p + 1.0 / p - 1.0 + mu * mu)


def kahn_efficiency(alpha: float, steps: int = 2000) -> float:
    """Exact probability that one try of Kahn's method is accepted (Simpson's rule in q)."""
    k = 4.0 * (1.0 + 2.0 * alpha) / ((9.0 + 2.0 * alpha) * 2.0 * alpha)
    a, b = 1.0, 1.0 + 2.0 * alpha
    h = (b - a) / steps
    total = 0.0
    for i in range(steps + 1):
        q = a + i * h
        mu = 1.0 - (q - 1.0) / alpha
        f = (q + 1.0 / q - 1.0 + mu * mu) / (q * q)
        total += f * (1 if i in (0, steps) else (4 if i % 2 else 2))
    return k * total * h / 3.0


def scattered_energy(energy_kev: float, mu: float, mc2_kev: float = 510.99895) -> float:
    """Compton formula E' = E / (1 + (E / m c^2)(1 - mu))."""
    return energy_kev / (1.0 + energy_kev / mc2_kev * (1.0 - mu))
test_kahn_compton_sampling.py 85 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/kahn-compton-sampling/. The authors are listed in prov/7W42N2.jsonl.
import math
import random

import pytest

from kahn_compton_sampling import kahn_efficiency, kahn_sample, klein_nishina_mu_density, scattered_energy


def kn_moments(alpha, steps=4000):
    """Exact mean of mu and mu^2 under Klein-Nishina, by Simpson's rule in mu."""
    h = 2.0 / steps
    z = m1 = m2 = 0.0
    for i in range(steps + 1):
        mu = -1.0 + i * h
        f = klein_nishina_mu_density(alpha, mu) * (1 if i in (0, steps) else (4 if i % 2 else 2))
        z += f
        m1 += f * mu
        m2 += f * mu * mu
    return m1 / z, m2 / z


def reference_sample(alpha, rng):
    """Brute force: uniform mu, accepted with density / max (the max is at mu = 1, where it is 2)."""
    while True:
        mu = 2.0 * rng.random() - 1.0
        if 2.0 * rng.random() <= klein_nishina_mu_density(alpha, mu):
            return mu


@pytest.mark.parametrize("alpha", [0.1, 1.0, 5.0])
def test_moments_match_klein_nishina(alpha):
    rng = random.Random(int(alpha * 10) + 1)
    n = 20000
    mus = [kahn_sample(alpha, rng)[1] for _ in range(n)]
    e1, e2 = kn_moments(alpha)
    m1 = sum(mus) / n
    m2 = sum(m * m for m in mus) / n
    sd1 = math.sqrt(max(e2 - e1 * e1, 1e-12))
    assert abs(m1 - e1) < 4.0 * sd1 / math.sqrt(n)
    assert abs(m2 - e2) < 4.0 / math.sqrt(n)  # mu^2 lies in [0, 1], so its sd is below 1


def test_histogram_matches_brute_force():
    alpha, n, nb = 1.0, 20000, 8
    rng = random.Random(42)
    a = [kahn_sample(alpha, rng)[1] for _ in range(n)]
    b = [reference_sample(alpha, rng) for _ in range(n)]
    for j in range(nb):
        lo, hi = -1.0 + 2.0 * j / nb, -1.0 + 2.0 * (j + 1) / nb
        pa = sum(1 for m in a if lo <= m < hi) / n
        pb = sum(1 for m in b if lo <= m < hi) / n
        assert abs(pa - pb) < 4.0 * math.sqrt(2.0 * max(pa, 1e-3) / n)


@pytest.mark.parametrize("alpha", [0.1, 1.0, 5.0])
def test_acceptance_rate(alpha):
    rng = random.Random(7)
    n = 20000
    tries = sum(kahn_sample(alpha, rng)[2] for _ in range(n))
    eff = kahn_efficiency(alpha)
    assert 0.0 < eff <= 1.0
    assert n / tries == pytest.approx(eff, abs=4.0 * math.sqrt(eff * (1 - eff) / tries))


def test_thomson_limit_and_ranges():
    rng = random.Random(3)
    n = 20000
    out = [kahn_sample(1e-4, rng) for _ in range(n)]
    m2 = sum(mu * mu for _, mu, _ in out) / n
    assert abs(m2 - 0.4) < 4.0 * 0.3 / math.sqrt(n)  # (1 + mu^2) law: <mu^2> = 2/5
    rng = random.Random(4)
    for _ in range(2000):
        a_out, mu, _ = kahn_sample(2.0, rng)
        assert -1.0 - 1e-12 <= mu <= 1.0 + 1e-12
        assert 2.0 / 5.0 - 1e-12 <= a_out <= 2.0 + 1e-12
        assert a_out == pytest.approx(2.0 / (1.0 + 2.0 * (1.0 - mu)))


def test_hand_values():
    # A 511 keV photon has alpha = 1; at 90 degrees, q = 2 and E' is half of E.
    assert scattered_energy(510.99895, 0.0) == pytest.approx(255.499475)
    # Back scattering of a 1 MeV photon: E' = E / (1 + 2 alpha).
    assert scattered_energy(1000.0, -1.0) == pytest.approx(1000.0 / (1.0 + 2000.0 / 510.99895))

History

Oskar Klein and Yoshio Nishina derived the cross section in 1929 from Dirac's equation. Herman Kahn, then at RAND, collected the sampling methods for Monte Carlo transport in his 1954 report Applications of Monte Carlo, and Section 23 of the report gives the rejection scheme for the Klein-Nishina formula. The method went into the photon codes of the following decades. Koblinger gave a direct sampling method for photon energies above 1.4 MeV in 1975, and some codes use both, with Kahn's method at the lower energies.

References

  1. Herman Kahn, Applications of Monte Carlo, RAND Corporation report AECU-3259, 1954 (revised 1956 as RM-1237-AEC), Section 23, the Klein-Nishina scattering formula
  2. O. Klein and Y. Nishina, Über die Streuung von Strahlung durch freie Elektronen nach der neuen relativistischen Quantendynamik von Dirac, Zeitschrift für Physik 52, 1929, 853-868, the cross section for the scattering of a photon by a free electron
  3. Ivan Lux and Laszlo Koblinger, Monte Carlo Particle Transport Methods, Neutron and Photon Calculations, CRC Press, 1991, the chapter on photon transport, with the sampling of Compton scattering
  4. Malvin H. Kalos and Paula A. Whitlock, Monte Carlo Methods, second edition, Wiley-VCH, 2008, Chapter 3 (sampling random variables), composition and rejection

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example