Poisson likelihood

Least-squares fitting minimizes a sum of squared residuals. For detector counts, a Poisson likelihood provides a more appropriate objective, particularly when many pixels contain only a few counts.

This example minimizes the Poisson deviance

$$ D = 2 \sum_i \left[s_i-n_i+n_i\ln\left(\frac{n_i}{s_i}\right)\right], $$

where $n_i$ is the observed count and $s_i$ is the expected count for pixel $i$. For $n_i=0$, the logarithmic term is defined as zero. The deviance differs from the Cash statistic only by a data-dependent constant, so both have the same minimum.

Unlike a residual vector, a scalar objective directly defines the quantity to be minimized. This is more general than the default reduction of a residual vector to a sum of squares: it can express criteria such as likelihoods and deviances. A scalar objective must be used with a scalar minimizer; the example selects the derivative-free Nelder-Mead method. The synthetic observations are generated locally as Poisson-distributed integer counts instead of using the Gaussian-noise helper shared by the least-squares examples.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = ["bornagain>=25,<26", "lmfit"]
# ///
"""
Fit GISAS count data with a scalar Poisson likelihood.

Scalar objectives support criteria beyond sums of squared residuals.
"""
import bornagain as ba
ba.require_versions("bornagain>=25,<26")
from bornagain import deg, nm
import numpy as np
import lmfit


def get_sample(P):
    """
    Spheres on a hexagonal lattice, parameterized for fitting.
    """
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-6, 2e-8)

    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-4, 2e-8)

    particle = ba.Particle(particle_mat, ba.Sphere(P["radius"]))

    lattice = ba.HexagonalLattice2D(P["length"], 0)
    struct = ba.Crystal2D(particle, lattice)
    struct.setDecayFunction(ba.Profile2DCauchy(100*nm, 100*nm, 0))

    particle_layer = ba.Layer(ba.Vacuum())
    particle_layer.deposit2D(struct)

    sample = ba.Sample()
    sample.addLayer(particle_layer)
    sample.addLayer(ba.Layer(substrate_mat))
    return sample


def get_simulation(P):
    """
    GISAS simulation for the parameterized hexagonal lattice.
    """
    n_pix = 100

    beam = ba.Beam(1e8, 0.1*nm, 0.2*deg)
    detector = ba.SphericalDetector(n_pix, -1*deg, 1*deg, n_pix, 0, 2*deg)

    simulation = ba.ScatteringSimulation(beam, get_sample(P), detector)
    return simulation


def fake_data():
    """
    Noisy synthetic data for a known hexagonal lattice.
    """
    P = {"radius": 5*nm, "length": 14*nm}
    return get_simulation(P).simulate().noisy(0.1, 0.1)


if __name__ == '__main__':
    true_values = {"radius": 5*nm, "length": 14*nm}
    count_scale = 0.001

    sim_result = get_simulation(true_values).simulate()
    mean_counts = count_scale * sim_result.intensities().ravel()
    observed_counts = np.random.default_rng(42).poisson(mean_counts)

    def scalar_objective(P):
        """
        Returns Poisson deviance, not a sum of squared residuals.
        """
        sim_result = get_simulation(P.valuesdict()).simulate()
        expected_counts = count_scale * sim_result.intensities().ravel()
        expected_counts = np.maximum(expected_counts,
                                     np.finfo(float).tiny)
        terms = expected_counts - observed_counts
        positive = observed_counts > 0
        terms[positive] += observed_counts[positive] * np.log(
            observed_counts[positive] / expected_counts[positive])
        return 2*np.sum(terms)

    P = lmfit.Parameters()
    P.add('radius', value=4.5*nm, min=4*nm, max=6*nm)
    P.add('length', value=13.5*nm, min=13*nm, max=15*nm)

    result = lmfit.minimize(scalar_objective, P, method="nelder")
    result.params.pretty_print()
    print(f"Poisson deviance: {result.residual[0]:.6g}")
    finalP = result.params.valuesdict()
    ba.showSample3D(get_sample(finalP), sample_size=300*nm, seed=0)
auto/Examples/fit/scatter2d/poisson_likelihood.py