diff --git a/autolens/point/fit/abstract.py b/autolens/point/fit/abstract.py index a55d86117..aa1795095 100644 --- a/autolens/point/fit/abstract.py +++ b/autolens/point/fit/abstract.py @@ -115,9 +115,9 @@ def magnifications_at_positions(self) -> aa.ArrayIrregular: These magnifications are used for two purposes: - 1) For a source-plane chi-squared calculation, the residuals are multiplied by the magnification to account for - how the noise in the image-plane positions is magnified to the source-plane, thus defining a - better chi-squared. + 1) For the opt-in `weighting = "magnification"` source-plane chi-squared, the residuals are multiplied by the + magnification to approximate how the noise in the image-plane positions maps to the source-plane (the + default `"jacobian"` weighting uses the full lensing Jacobian instead). 2) For fitting the fluxes of point sources, the magnification is used to scale the flux of the point source in the source-plane to the image-plane, thus computing the model image-plane fluxes. diff --git a/autolens/point/fit/positions/source/separations.py b/autolens/point/fit/positions/source/separations.py index cdf0a620b..51fd2c2ec 100644 --- a/autolens/point/fit/positions/source/separations.py +++ b/autolens/point/fit/positions/source/separations.py @@ -30,11 +30,13 @@ class FitPositionsSource(AbstractFitPositions): #: How each back-traced position's residual from the source-plane centre is weighted: - #: `"magnification"` — the traditional scalar `µᵢ²/σᵢ²` weighting with the magnified-noise - #: normalization (the long-standing behaviour of this class, and the Lenstool convention); - #: `"jacobian"` — the per-image precision tensor `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹` with the observed-plane - #: normalization, matching `FitPositionsSourceSolved` but with the centre a free parameter. - weighting = "magnification" + #: `"jacobian"` (the default) — the per-image precision tensor `Bᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`, so each residual is + #: measured in the image-plane noise ellipse (the Gravity.jl source-plane likelihood, Lombardi 2024 + #: Eq. 46; Keeton 2001 Eq. 10; Lenstool's tensor mode, `forme 8`); + #: `"magnification"` (opt-in only) — the scalar isotropic approximation `Bᵢ ≈ (µᵢ²/σᵢ²) I₂` of + #: Jullo et al. 2007 Eq. 10, which is biased near critical curves (see `__init__`). + #: Both use the same model-independent normalization `Σᵢ log((2π)² σᵢ⁴)`. + weighting = "jacobian" def __init__( self, @@ -63,22 +65,28 @@ def __init__( 3) Compute the distance of each ray-traced position to the source-plane centre and compute the residuals, - 4) Compute the magnification of each image-plane position via the Hessian of the tracer's deflection angles. + 4) Compute the lensing Jacobian `Aᵢ` of each image-plane position via the Hessian of the tracer's + deflection angles. - 5) Compute the residuals of each position as the difference between the source-plane centre and each - ray-traced position. + 5) Compute the residual vector `δᵢ = β̂ᵢ − c` of each position, the difference between each ray-traced + position `β̂ᵢ` and the source-plane centre `c`. - 6) Compute the chi-squared of each position as the square of the residual multiplied by the magnification and - divided by the RMS noise-map value. + 6) Compute the chi-squared of each position as `δᵢᵀ Bᵢ δᵢ`, with the precision tensor `Bᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹` + (`Θᵢ = σᵢ⁻² I₂`, see `autolens.point.fit.solved.precision_tensor_components_from`), so each residual is + measured in the image-plane noise ellipse. - 7) Sum the chi-squared values to compute the overall log likelihood of the fit. + 7) Sum the chi-squared values and the model-independent noise normalization `Σᵢ log((2π)² σᵢ⁴)` to + compute the overall log likelihood of the fit. - Steps 4-6 describe the default `weighting = "magnification"` scalar convention. Setting the - `weighting` class attribute to `"jacobian"` instead weights each vector residual `β̂ᵢ − c` (with `c` - the profile's free `centre`) by the per-image precision tensor `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹` (see - `autolens.point.fit.solved.precision_tensor_components_from`), with the observed-plane noise - normalization matching `FitPositionsSourceSolved` — the same tensor likelihood as that class, but - with the centre sampled as a free parameter rather than solved and marginalized. + Steps 4-6 describe the default `weighting = "jacobian"`. This is the Gravity.jl source-plane likelihood + (Lombardi 2024, arXiv:2406.15280, Eq. 46; its marginalised-source form, Eq. 58, is + `FitPositionsSourceSolved`), Keeton 2001 Eq. 10, and Lenstool's tensor mode (`forme 8`). It stays + robust near critical curves and costs nothing extra over the scalar magnification. + + Setting the `weighting` class attribute to `"magnification"` instead uses the scalar isotropic + approximation `Bᵢ ≈ (µᵢ²/σᵢ²) I₂` of Jullo et al. 2007 Eq. 10, i.e. `χ²ᵢ = |δᵢ|² µᵢ²/σᵢ²`. This is exact + only along the tangential direction and for a radial eigenvalue of 1. Near critical curves it + over-weights the radial direction by about `µ²`, so it is biased, and it is provided as an opt-in only. Point source fitting uses name pairing, whereby the `name` of the `Point` object is paired to the name of the point source dataset to ensure that point source datasets are fitted to the correct point source. @@ -167,9 +175,9 @@ def chi_squared_map(self) -> float: """ Returns the chi-squared of the point-source source-plane fit. - For `weighting = "magnification"` this is the squared residuals multiplied by the magnifications - squared, divided by the noise-map values squared. For `weighting = "jacobian"` it is the per-image - quadratic form `(β̂ᵢ−c)ᵀ Wᵢ (β̂ᵢ−c)` with the precision tensor `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`. + For the default `weighting = "jacobian"` it is the per-image quadratic form `(β̂ᵢ−c)ᵀ Bᵢ (β̂ᵢ−c)` with the + precision tensor `Bᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`. For the opt-in `weighting = "magnification"` this is the squared + residuals multiplied by the magnifications squared, divided by the noise-map values squared. """ if self.weighting == "magnification": return self.residual_map**2.0 / ( @@ -190,26 +198,13 @@ def chi_squared_map(self) -> float: @property def noise_normalization(self) -> float: """ - Returns the noise normalization of the fit's Gaussian likelihood. + Returns the noise normalization of the fit's Gaussian likelihood, `Σᵢ log((2π)² σᵢ⁴)`. - For `weighting = "magnification"` this is the long-standing magnified-noise source-plane-data - convention `Σᵢ log(2π µᵢ⁻²σᵢ²)`. For `weighting = "jacobian"` it is the observed-plane - (model-independent) convention `Σᵢ log((2π)² σᵢ⁴)` matching `FitPositionsSourceSolved` (see that - class's docstring for why a model-dependent normalization would spuriously favour - high-magnification models). + This is the observed-plane (model-independent) normalization of the 2-D Gaussian over each image + position (Lombardi 2024 Eq. 46, `log|Θᵢ/2π|`), used for both weightings: for `"magnification"` it is + Eq. 46 with `Bᵢ ≈ µᵢ²Θᵢ`. A model-dependent normalization (e.g. `log det Bᵢ`, or the 1-D + `Σᵢ log(2π σᵢ²/µᵢ²)`) would spuriously favour high-magnification models. """ - if self.weighting == "magnification": - return self._xp.sum( - self._xp.log( - 2 - * np.pi - * ( - self.magnifications_at_positions.array**-2.0 - * self.noise_map.array**2.0 - ) - ) - ) - sigma_sq = self.noise_map.array**2.0 return self._xp.sum(self._xp.log((2.0 * np.pi) ** 2.0 * sigma_sq**2.0)) @@ -229,8 +224,10 @@ class FitPositionsSourceSolved(SolvedCentre, FitPositionsSource): The lens equation is Taylor-expanded around each observed image position `θ̂ᵢ`, giving a source-plane position that is linear in the per-image back-traced position `β̂ᵢ` (`SolvedCentre._beta_hat`, computed the same way as `FitPositionsSource.model_data`) and the per-image precision `Wᵢ` (`weighting = "jacobian"` by - default — the tensor weighting `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`; set `weighting = "magnification"` for the scalar - `Wᵢ = (µᵢ²/σᵢ²) I₂` weighting used by `FitPositionsSource`, for Lenstool-style comparisons). This makes the + default, as for `FitPositionsSource` — the tensor weighting `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`; set + `weighting = "magnification"` for the opt-in scalar `Wᵢ = (µᵢ²/σᵢ²) I₂` approximation of Jullo et al. 2007 + Eq. 10, which is biased near critical curves). This is the marginalised-source likelihood of Lombardi 2024 + Eq. 58. This makes the source-plane centre solvable in closed form (`SolvedCentre.source_plane_coordinate`, `β*`), and the likelihood analytically marginalizes over it (flat prior): @@ -243,9 +240,8 @@ class FitPositionsSourceSolved(SolvedCentre, FitPositionsSource): The normalization deliberately uses the observed-plane precision `Θᵢ` (model-independent), NOT `det Wᵢ`: this is a likelihood of the *observed image-plane positions* under the linearized lens equation (Lombardi 2024 Eq. 46), so the Gaussian normalization is over the data space. A `det Wᵢ` normalization would add - `-2Σᵢ log|µᵢ|`-like model-dependent terms that spuriously favour high-magnification models. (The - long-standing `FitPositionsSource` uses the magnified-noise source-plane-data convention instead — its - normalization is intentionally unchanged.) + `-2Σᵢ log|µᵢ|`-like model-dependent terms that spuriously favour high-magnification models. The same + normalization is used for both weightings, and is inherited from `FitPositionsSource`. Must be paired (by name) with a parameter-free profile such as `ag.ps.PointSolved`: a `centre`-bearing profile (`ag.ps.Point` / `ag.ps.PointFlux`) raises (see `SolvedCentre.source_plane_coordinate`), since its @@ -287,16 +283,6 @@ def chi_squared(self) -> float: """ return self._xp.sum(self.chi_squared_map.array) - @property - def noise_normalization(self) -> float: - """ - `noise_norm = Σᵢ log((2π)² / det Θᵢ) = Σᵢ log((2π)² σᵢ⁴)` — the observed-plane (model-independent) - Gaussian normalization of the linearized image-plane likelihood (see class docstring; NOT `det Wᵢ`, - which would spuriously favour high-magnification models). - """ - sigma_sq = self.noise_map.array**2.0 - return self._xp.sum(self._xp.log((2.0 * np.pi) ** 2.0 * sigma_sq**2.0)) - @property def marginalization_term(self) -> float: """ diff --git a/autolens/point/fit/solved.py b/autolens/point/fit/solved.py index e17f98cb9..49334d8ee 100644 --- a/autolens/point/fit/solved.py +++ b/autolens/point/fit/solved.py @@ -34,10 +34,15 @@ the tensor is exact in every regime: isotropically each stretch is `√µ` (giving `µᵢ/σᵢ²`), while near a critical curve the tangential stretch is `≈ µ` (giving `µᵢ²/σᵢ²` along the arc). -The `weighting = "magnification"` class-attribute option instead uses the scalar weighting -`Wᵢ = (µᵢ²/σᵢ²) I₂` already used by `FitPositionsSource.chi_squared_map` — the traditional -Lenstool-style approximation, which coincides with the tensor only in the near-critical -tangential limit (NOT isotropically) — retained for comparisons with that convention. +This tensor weighting (`weighting = "jacobian"`, the default of both `FitPositionsSource` and +`FitPositionsSourceSolved`) measures each source-plane residual in the image-plane noise ellipse. +It is the Gravity.jl source-plane likelihood (Lombardi 2024 Eq. 46; marginalised source Eq. 58), +Keeton 2001 Eq. 10, and Lenstool's tensor mode (`forme 8`). + +The opt-in `weighting = "magnification"` class-attribute option instead uses the scalar isotropic +weighting `Wᵢ = (µᵢ²/σᵢ²) I₂` of Jullo et al. 2007 Eq. 10. It is exact only tangentially and for a +radial eigenvalue of 1; near critical curves it over-weights the radial direction by about `µ²`, +so it is biased. The solved source-plane centre is then the precision-weighted mean of the per-image back-traced positions `β̂ᵢ`: @@ -97,10 +102,12 @@ def precision_tensor_components_from(fit, weighting: str) -> Tuple: The point-source fit object (any `AbstractFitPositions` subclass) providing `positions`, `tracer`, `name`, `noise_map` and `_xp`. weighting - `"jacobian"` — the tensor weighting `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`, with `Aᵢ` the lensing Jacobian at the observed - position (see module docstring). - `"magnification"` — the traditional scalar weighting `Wᵢ = (µᵢ²/σᵢ²) I₂`, matching - `FitPositionsSource.chi_squared_map` (the near-critical tangential limit of the tensor). + `"jacobian"` (the default) — the tensor weighting `Wᵢ = Aᵢ⁻ᵀΘᵢAᵢ⁻¹`, with `Aᵢ` the lensing Jacobian at + the observed position, so each residual is measured in the image-plane noise ellipse (Lombardi 2024 + Eq. 46; Keeton 2001 Eq. 10; Lenstool `forme 8`; see module docstring). + `"magnification"` (opt-in) — the scalar isotropic approximation `Wᵢ = (µᵢ²/σᵢ²) I₂` of Jullo et al. + 2007 Eq. 10, exact only tangentially and for a radial eigenvalue of 1; near critical curves it + over-weights the radial direction by about `µ²`, so it is biased. """ xp = fit._xp precision_scalar = _as_array(fit.noise_map) ** -2.0 # Θ = σ⁻² I @@ -114,8 +121,8 @@ def precision_tensor_components_from(fit, weighting: str) -> Tuple: if weighting != "jacobian": raise exc.PointProfileMismatchException( f"Unsupported weighting '{weighting}' for the source-plane position residuals. " - f"Valid options are 'jacobian' (tensor weighting, the default of the *Solved fit classes) or " - f"'magnification' (scalar isotropic weighting, the default of `FitPositionsSource`)." + f"Valid options are 'jacobian' (tensor weighting, the default) or " + f"'magnification' (opt-in scalar isotropic weighting)." ) lens_calc = _lens_calc_for(fit) diff --git a/autolens/point/plot/source_plane_panel.py b/autolens/point/plot/source_plane_panel.py index 55c8005d1..6ae04123c 100644 --- a/autolens/point/plot/source_plane_panel.py +++ b/autolens/point/plot/source_plane_panel.py @@ -130,7 +130,7 @@ def presentation_from(fit_positions) -> SourcePlanePresentation: Returns how the source-plane panel presents a positions fit, paired to its class. The class is looked up in :func:`presentation_table` along its method resolution order, so a user subclass of a - fit class (e.g. ``FitPositionsSource`` with ``weighting = "jacobian"``) is presented as its parent. The weighting + fit class (e.g. ``FitPositionsSource`` with ``weighting = "magnification"``) is presented as its parent. The weighting of a source-plane fit is read from the fit itself. A class with no entry is presented as context only, which never draws a chi-squared the fit may not compute. """ diff --git a/test_autolens/point/fit/positions/source/test_separations.py b/test_autolens/point/fit/positions/source/test_separations.py index fc1509269..29da9c90b 100644 --- a/test_autolens/point/fit/positions/source/test_separations.py +++ b/test_autolens/point/fit/positions/source/test_separations.py @@ -22,8 +22,12 @@ def test__fit_positions_source__all_residual_quantities_correct_without_mass_pro assert fit.normalized_residual_map.in_list == [1.0 / 0.5, 2.0 / 1.0] assert fit.chi_squared_map.in_list == [(1.0 / 0.5) ** 2.0, 2.0 ** 2.0] assert fit.chi_squared == pytest.approx(8.0, 1.0e-4) - assert fit.noise_normalization == pytest.approx(2.28945, 1.0e-4) - assert fit.log_likelihood == pytest.approx(-5.14472988, 1.0e-4) + # Σᵢ log((2π)² σᵢ⁴) = 2 log(4π²) + 4 log(0.5); with no mass A = I, so jacobian χ² = magnification χ². + assert fit.noise_normalization == pytest.approx( + 2.0 * np.log(4.0 * np.pi**2) + 4.0 * np.log(0.5), 1.0e-8 + ) + assert fit.noise_normalization == pytest.approx(4.57891954, 1.0e-6) + assert fit.log_likelihood == pytest.approx(-6.28945977, 1.0e-6) def test__fit_positions_source__with_isothermal_mass_profile__magnification_reduces_source_positions(): @@ -48,10 +52,27 @@ def test__fit_positions_source__with_isothermal_mass_profile__magnification_redu [1.1111049387688177, 1.0526308864400329], 1.0e-4 ) assert fit.model_data.in_list == [(0.0, 0.9), (0.0, 1.9)] - assert fit.chi_squared_map.in_list == pytest.approx( - [3.9999555592589244, 3.9999947369459807], 1.0e-4 + + # The residuals (0, r - 0.1) are radial, and the radial eigenvalue of an isothermal Jacobian is 1, so + # the default jacobian χ²ᵢ = (r - 0.1)² / σᵢ², i.e. (0.9 / 0.5)² and 1.9². + assert fit.chi_squared_map.in_list == pytest.approx([3.24, 3.61], 1.0e-6) + noise_normalization = 2.0 * np.log(4.0 * np.pi**2) + 4.0 * np.log(0.5) + assert fit.log_likelihood == pytest.approx( + -0.5 * (3.24 + 3.61 + noise_normalization), 1.0e-6 + ) + assert fit.log_likelihood == pytest.approx(-5.71445977, 1.0e-6) + + # The opt-in scalar weighting multiplies by µᵢ² = 1 / (1 - 0.1 / r)², over-weighting the radial + # residual: χ²ᵢ = (r - 0.1)² µᵢ² / σᵢ² = r² / σᵢ² = 4 for both, with the same normalization. + fit = FitPositionsSourceMagnification( + name="point_0", data=positions, noise_map=noise_map, tracer=tracer, solver=None + ) + + assert fit.chi_squared_map.in_list == pytest.approx([4.0, 4.0], 1.0e-4) + assert fit.noise_normalization == pytest.approx(noise_normalization, 1.0e-8) + assert fit.log_likelihood == pytest.approx( + -0.5 * (8.0 + noise_normalization), 1.0e-5 ) - assert fit.log_likelihood == pytest.approx(-4.98805743691215, 1.0e-4) def test__fit_positions_source__multi_plane_tracer__model_data_traces_to_correct_source_plane(): @@ -84,8 +105,57 @@ class FitPositionsSourceJacobian(al.FitPositionsSource): weighting = "jacobian" -def test__fit_positions_source__default_weighting_is_magnification(): - assert al.FitPositionsSource.weighting == "magnification" +class FitPositionsSourceMagnification(al.FitPositionsSource): + weighting = "magnification" + + +def test__fit_positions_source__default_weighting_is_jacobian(): + assert al.FitPositionsSource.weighting == "jacobian" + + +def test__fit_positions_source__jacobian__true_model_outranks_perturbed_models_near_critical(): + """ + An elliptical power law with slope 2.3 (so the radial eigenvalue of the Jacobian is not 1) and a quad of + images traced exactly from the source centre, with magnifications up to ~17. Under the default jacobian + weighting the true model must have a higher log likelihood than small perturbations of it. + """ + source_centre = (0.05, 0.0) + + positions = al.Grid2DIrregular( + [ + (0.947716092108, -0.370287233244), + (-0.50956321869, 0.809453183983), + (-0.69058165779, -0.617211899037), + (0.043787760967, 0.960520345829), + ] + ) + noise_map = al.ArrayIrregular([0.005, 0.005, 0.005, 0.005]) + + def fit_from(einstein_radius=1.0, centre=(0.0, 0.0)): + mass = al.mp.PowerLaw( + centre=centre, ell_comps=(0.1, 0.05), einstein_radius=einstein_radius, slope=2.3 + ) + tracer = al.Tracer( + galaxies=[ + al.Galaxy(redshift=0.5, mass=mass), + al.Galaxy(redshift=1.0, point_0=al.ps.Point(centre=source_centre)), + ] + ) + return al.FitPositionsSource( + name="point_0", data=positions, noise_map=noise_map, tracer=tracer, solver=None + ) + + fit_true = fit_from() + + assert np.max(fit_true.magnifications_at_positions.array) > 10.0 + assert fit_true.chi_squared == pytest.approx(0.0, abs=1.0e-4) + + for fit_perturbed in [ + fit_from(einstein_radius=1.01), + fit_from(centre=(0.005, 0.0)), + fit_from(centre=(0.0, 0.005)), + ]: + assert fit_true.log_likelihood > fit_perturbed.log_likelihood def test__fit_positions_source__jacobian_weighting__matches_solved_at_solved_centre(): diff --git a/test_autolens/point/fit/test_fit_dataset.py b/test_autolens/point/fit/test_fit_dataset.py index 7eb7cc532..a4676f912 100644 --- a/test_autolens/point/fit/test_fit_dataset.py +++ b/test_autolens/point/fit/test_fit_dataset.py @@ -65,7 +65,8 @@ def test__fit_dataset__nonmatching_point_name__positions_and_flux_are_none( (al.FitPositionsImagePair, -22.14472), (al.FitPositionsImagePairRepeat, -22.14472), (al.FitPositionsImagePairAll, -24.6435280294), - (al.FitPositionsSource, -12.9947298), + # No mass (A = I): χ² = 0.02 / 0.5² + (2.9² + 3.9²) = 23.70, norm = 2 log(4π²) + 4 log(0.5). + (al.FitPositionsSource, -14.13945977), ], ids=[ "FitPositionsImagePair", diff --git a/test_autolens/point/plot/test_source_plane_panel.py b/test_autolens/point/plot/test_source_plane_panel.py index 88b359a93..5482f4fec 100644 --- a/test_autolens/point/plot/test_source_plane_panel.py +++ b/test_autolens/point/plot/test_source_plane_panel.py @@ -16,8 +16,8 @@ SOURCE_CENTRE = (0.07, 0.07) -class FitPositionsSourceJacobian(al.FitPositionsSource): - weighting = "jacobian" +class FitPositionsSourceMagnification(al.FitPositionsSource): + weighting = "magnification" # Every positions fit class and the presentation its source-plane panel must use. @@ -27,8 +27,8 @@ class FitPositionsSourceJacobian(al.FitPositionsSource): (al.FitPositionsImagePairRepeatSolved, "image", True, None), (al.FitPositionsImagePairAll, "image", False, None), (al.FitPositionsImagePairAllSolved, "image", True, None), - (al.FitPositionsSource, "source", False, "magnification"), - (FitPositionsSourceJacobian, "source", False, "jacobian"), + (al.FitPositionsSource, "source", False, "jacobian"), + (FitPositionsSourceMagnification, "source", False, "magnification"), (al.FitPositionsSourceSolved, "source", True, "jacobian"), ] @@ -212,7 +212,7 @@ def test__plot_source_plane_panel__no_inset_without_caustics(dataset, solver): def test__source_plane_data__matches_the_fits_own_chi_squared(dataset, solver): - magnification = _fit(al.FitPositionsSource, dataset, solver).positions + magnification = _fit(FitPositionsSourceMagnification, dataset, solver).positions data = spp.source_plane_data_from(magnification) # Traced exactly as the fit traces them. @@ -225,7 +225,7 @@ def test__source_plane_data__matches_the_fits_own_chi_squared(dataset, solver): ) # The A·σ ellipse is the covariance of the jacobian chi-squared. - jacobian = _fit(FitPositionsSourceJacobian, dataset, solver).positions + jacobian = _fit(al.FitPositionsSource, dataset, solver).positions jacobian_data = spp.source_plane_data_from(jacobian) assert spp.chi_squared_from( jacobian_data.traced - jacobian_data.centre, jacobian_data.covariances