Part III: Scientific Applications | Chapter 12

ML in Cosmology & Astrophysics

Galaxy classification, photometric redshifts, gravitational lensing, simulation-based inference, and neural emulators for cosmological simulations

The Data Deluge in Cosmology

Modern cosmological surveys produce datasets of unprecedented scale: the Vera C. Rubin Observatory (LSST) will catalogue ~20 billion galaxies; Euclid will map the 3D distribution of galaxies across 10 billion light-years; and the SKA will produce exabytes of radio data. Classical analysis pipelines cannot keep pace. Machine learning has become essential for extracting cosmological information from these massive, complex datasets.

This chapter covers the major ML applications in cosmology: galaxy morphology classification, photometric redshift estimation, gravitational lens detection, simulation-based inference for parameter estimation, and neural emulators that replace expensive N-body simulations.

1. Galaxy Morphology Classification

Galaxy morphology (spiral, elliptical, irregular, merging) correlates with physical properties such as star formation rate, stellar mass, and environment. The Hubble classification has been extended by citizen-science projects (Galaxy Zoo) and now by deep learning.

CNN Classification Pipeline

A convolutional neural network takes a multi-band galaxy image$I \in \mathbb{R}^{H \times W \times C}$ (where $C$ = number of photometric bands) and outputs class probabilities:

$$p(y = k | I) = \frac{e^{z_k}}{\sum_{j=1}^{K} e^{z_j}}, \quad z = f_\theta(I) \in \mathbb{R}^K$$

The cross-entropy loss for $N$ galaxies with soft labels (vote fractions from Galaxy Zoo) is:

$$\mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N}\sum_{k=1}^{K} p_k^{(i)} \log \hat{p}_k^{(i)}$$

Dieleman et al. (2015) achieved near-human accuracy using rotationally augmented CNNs. Modern approaches use equivariant CNNs that are exactly invariant under rotations, avoiding the need for augmentation.

Python
script.py73 lines

Click Run to execute the Python code

Code will be executed with Python 3 on the server

2. Photometric Redshift Estimation

Spectroscopic redshifts are accurate but expensive. Photometric redshifts (photo-z) estimate$z$ from broadband photometry (typically 5-10 filters), enabling redshift estimation for billions of galaxies.

Photo-z as a Regression Problem

Given magnitudes $\mathbf{m} = (m_u, m_g, m_r, m_i, m_z) \in \mathbb{R}^5$ (or equivalently, colours $c_{ij} = m_i - m_j$), we want to predict the redshift$z_{\text{spec}}$. The standard metrics are:

$$\text{bias} = \langle \Delta z \rangle, \quad \Delta z = \frac{z_{\text{phot}} - z_{\text{spec}}}{1 + z_{\text{spec}}}$$

$$\sigma_{\text{NMAD}} = 1.4826 \cdot \text{median}(|\Delta z - \text{median}(\Delta z)|)$$

The outlier fraction $\eta$ is the percentage of galaxies with$|\Delta z| > 0.15$. State-of-the-art photo-z methods achieve$\sigma_{\text{NMAD}} \sim 0.01\text{-}0.03$ with $\eta < 5\%$.

Probabilistic Photo-z: Mixture Density Networks

Rather than predicting a single redshift, a mixture density network (MDN) predicts the full posterior $p(z | \mathbf{m})$ as a Gaussian mixture:

$$p(z | \mathbf{m}) = \sum_{k=1}^{K} \pi_k(\mathbf{m}) \cdot \mathcal{N}(z \,|\, \mu_k(\mathbf{m}), \sigma_k^2(\mathbf{m}))$$

The network outputs $3K$ parameters: mixing coefficients$\pi_k$ (softmax), means $\mu_k$, and variances$\sigma_k^2$ (softplus). The loss is the negative log-likelihood:$\mathcal{L} = -\sum_i \log p(z_i^{\text{spec}} | \mathbf{m}_i)$. This captures multi-modal posteriors caused by colour-redshift degeneracies.

Python
script.py81 lines

Click Run to execute the Python code

Code will be executed with Python 3 on the server

3. Gravitational Lens Finding

Strong gravitational lensing produces spectacular arcs and multiple images of background galaxies. These rare events ($\sim 1$ per $10^4$ galaxies) contain information about dark matter substructure and the Hubble constant.

The Lens Equation

For a point mass $M$, the lens equation relates the source position$\beta$ to the image position $\theta$:

$$\beta = \theta - \frac{\theta_E^2}{\theta}, \quad \theta_E = \sqrt{\frac{4GM}{c^2}\frac{D_{ls}}{D_l D_s}}$$

where $\theta_E$ is the Einstein radius. For extended mass distributions, the convergence $\kappa(\boldsymbol{\theta})$ and shear$\gamma(\boldsymbol{\theta})$ characterise the local lensing effect:

$$\kappa(\boldsymbol{\theta}) = \frac{\Sigma(\boldsymbol{\theta})}{\Sigma_{\text{cr}}}, \quad \Sigma_{\text{cr}} = \frac{c^2}{4\pi G}\frac{D_s}{D_l D_{ls}}$$

CNNs trained on simulated lens images can find lenses orders of magnitude faster than human inspection, critical for upcoming surveys that will image billions of galaxies.

Python
script.py105 lines

Click Run to execute the Python code

Code will be executed with Python 3 on the server

4. Simulation-Based Inference (SBI)

In cosmology, the likelihood $p(D|\theta)$ is often intractable — we cansimulate data from the model but cannot write down the likelihood in closed form. Simulation-based inference (also called likelihood-free inference) uses simulations to approximate the posterior $p(\theta|D)$ directly.

Neural Posterior Estimation (NPE)

NPE trains a conditional density estimator (e.g., normalising flow)$q_\phi(\theta | D)$ to approximate the posterior:

Step 1: Sample parameters from the prior:$\theta_i \sim p(\theta)$

Step 2: Run simulator:$D_i \sim p(D | \theta_i)$

Step 3: Train the network by minimising:

$$\mathcal{L}(\phi) = -\frac{1}{N}\sum_{i=1}^{N} \log q_\phi(\theta_i | D_i)$$

At inference time, given observed data $D_{\text{obs}}$, the trained network directly outputs the approximate posterior$q_\phi(\theta | D_{\text{obs}}) \approx p(\theta | D_{\text{obs}})$. This amortises inference: a single forward pass replaces expensive MCMC sampling.

Neural Likelihood Estimation (NLE)

Alternatively, one can learn the likelihood $q_\phi(D|\theta) \approx p(D|\theta)$and then use standard MCMC with the learned likelihood. The advantage is that classical statistical tests (e.g., likelihood ratio) remain available.

A third approach, Neural Ratio Estimation (NRE), directly learns the likelihood-to-evidence ratio$r(\theta, D) = p(D|\theta)/p(D)$ via binary classification between$(\theta, D)$ pairs drawn jointly vs. marginally.

Python
script.py100 lines

Click Run to execute the Python code

Code will be executed with Python 3 on the server

5. Neural Emulators for Cosmological Simulations

Full N-body cosmological simulations (e.g., Illustris, EAGLE) take millions of CPU-hours. Neural emulators learn to predict simulation outputs as a function of cosmological parameters, enabling rapid exploration of parameter space.

Power Spectrum Emulation

The matter power spectrum $P(k)$ encodes the clustering of matter as a function of wavenumber $k$. A neural emulator learns:

$$\hat{P}(k; \theta_{\text{cosmo}}) = \text{NN}_\phi(k, \Omega_m, \Omega_b, h, n_s, \sigma_8)$$

Training data consists of power spectra computed from N-body simulations at different cosmologies (e.g., a Latin hypercube sampling of the parameter space). The emulator interpolates between training cosmologies.

Examples include EuclidEmulator, CosmicEmu, and CAMELS emulators. Accuracies of$< 1\%$ are achieved for $P(k)$ over the relevant range of scales and cosmologies, with speed-ups of $10^5\text{-}10^6$ over full simulations.

Python
script.py96 lines

Click Run to execute the Python code

Code will be executed with Python 3 on the server

6. Further Applications

Weak Lensing Mass Mapping

CNNs and U-Nets reconstruct dark matter mass maps from weak lensing shear measurements, outperforming traditional Kaiser-Squires inversion by capturing non-Gaussian information in the convergence field.

Transient Classification

RNNs and transformers classify astronomical transients (supernovae, kilonovae, tidal disruption events) from their multi-band light curves in real time, essential for triggering follow-up observations.

21cm Cosmology

Neural networks extract cosmological information from noisy 21cm intensity maps, separating the faint cosmological signal from foreground contamination that is$10^4\text{-}10^5$ times brighter.

Gravitational Wave Detection

Deep learning detects and characterises gravitational wave signals in LIGO/Virgo data, performing matched filtering equivalent to searching $\sim 10^5$ templates in milliseconds rather than hours.

7. Challenges & Open Problems

Distribution Shift

ML models trained on simulations must generalise to real observations. Systematic differences between simulated and real data (e.g., imperfect PSF models, selection effects, foreground contamination) cause distribution shift. Domain adaptation and calibration techniques are essential.

Uncertainty Quantification

Cosmological parameter constraints require rigorous uncertainty estimates. Standard neural networks produce point predictions without well-calibrated uncertainties. Bayesian neural networks, ensemble methods, and conformal prediction are being explored to provide coverage guarantees for ML-derived constraints.

Interpretability

When an ML model discovers an anomaly or makes a surprising classification, physicists need to understand why. Gradient-based attribution (saliency maps), attention visualisation, and symbolic distillation help extract physical insight from black-box models.

Scalability

Future surveys will produce petabytes of data. ML pipelines must scale to billions of objects while maintaining latency requirements for real-time transient classification and alert brokering. Distributed training, model compression, and edge deployment are active research areas.

Chapter Summary

  • Galaxy classification uses CNNs to morphologically classify billions of galaxies, matching human accuracy.
  • Photometric redshifts are estimated from broadband photometry using neural networks; mixture density networks capture multi-modal posteriors.
  • Gravitational lens finding is automated by CNNs trained on simulated lensing images, critical for upcoming surveys.
  • Simulation-based inference (NPE, NLE, NRE) enables Bayesian parameter estimation when the likelihood is intractable, amortising inference via neural density estimators.
  • Neural emulators replace expensive N-body simulations, predicting power spectra and summary statistics as functions of cosmological parameters with $< 1\%$ error and $10^5\times$ speed-up.