---
title: 'HAMLET: HMC for Cosmological Field Reconstruction'
url: https://www.emergentmind.com/topics/hamlet
type: topic
---

# HAMLET: HMC for Cosmological Field Reconstruction

HAMLET, the **HAmiltonian Monte carlo reconstruction of the Local EnvironmenT**, is a Bayesian forward-modeling algorithm for reconstructing the **linear large scale density field** $\delta(\mathbf r)$ and its associated **3D velocity field** $\mathbf v(\mathbf r)$ from peculiar-velocity surveys such as Cosmicflows. Its defining feature is a joint posterior over Fourier density modes, true distances, and optional nonlinear-velocity dispersion, so that the **lognormal distance–velocity transformation** is treated inside the inference procedure rather than by an external pre-correction step. In the formulation introduced by Valade et al., HAMLET was tested on Cosmicflows mock catalogues with up to **30 000 data points** and was reported to outperform earlier Gibbs-sampling reconstructions by **two to four orders of magnitude in CPU time**, with the gain attributed to both Hamiltonian Monte Carlo and GPU execution [2203.02369].

## 1. Reconstruction target and physical setting

HAMLET addresses the inference problem defined by peculiar-velocity cosmography: given measured **distance moduli** $\mu_i$ and **redshifts** $z_i$, infer the underlying large-scale density and velocity fields. In linear theory, the connection between density and velocity is expressed in Fourier space as

$$
\tilde{\mathbf v}(\mathbf k)= -i\,H_0\,f(\Omega_0)\,\frac{\mathbf k}{k^2}\,\tilde\delta(\mathbf k),
$$

so reconstruction of the Fourier modes $\tilde\delta(\mathbf k)$ determines $\mathbf v(\mathbf r)$.

The observational difficulty is not merely sparsity and noise. Distance moduli are modeled with Gaussian errors $\sigma_{\mu,i}$, but converting $\mu \rightarrow d$ and then to line-of-sight peculiar velocity introduces a **lognormal bias**, often conflated with Malmquist bias. HAMLET is designed to “undo” that effect by treating the **true distances** as latent variables in the posterior, instead of applying a separate correction to derived peculiar velocities. This places the distance transformation, the velocity prediction, and the cosmological prior inside a single inferential object [2203.02369].

A plausible implication is that HAMLET is best understood not as a post-processing method for catalogued peculiar velocities, but as a forward model from raw survey observables to latent cosmic fields. That distinction matters because the dominant non-Gaussianity enters through the distance transformation rather than through the prior on the linear density field.

## 2. Bayesian posterior, likelihood, and priors

The unknowns are the set of true distances $D=\{d_i\}$, the Fourier modes $\Delta=\{\tilde\delta_k\}$, and optionally a nonlinear-velocity dispersion $\sigma_{\mathrm{nl}}$. With data $\{\mu_i,z_i\}$, the posterior is

$$
P(\Delta,D,\sigma_{\mathrm{nl}} \mid \mu,z)\propto L(\mu,z \mid \Delta,D,\sigma_{\mathrm{nl}})\,P(\Delta,D,\sigma_{\mathrm{nl}}).
$$

The likelihood factorizes over objects and over distance and radial-velocity components,

$$
L(\mu,z \mid \cdot)=\prod_{i=1}^N
L_i^\mu(\mu_i|d_i)\cdot L_i^{v_r}(z_i|\tilde\delta,D,\sigma_{\mathrm{nl}}).
$$

For distances, HAMLET uses

$$
L_i^\mu(\mu_i|d_i)=
(2\pi\,\sigma_{\mu,i}^2)^{-1/2}
\exp\!\left[-\frac{\bigl(\mu_i-5\log_{10}(d_L(d_i)/10\,\mathrm{pc})\bigr)^2}{2\,\sigma_{\mu,i}^2}\right],
$$

with $d_L(d)=d(1+z(d))$.

For radial peculiar velocities, the observed quantity is defined as

$$
v_{r,i}^{\mathrm{obs}}=c\,\frac{z_i-z(d_i)}{1+z(d_i)},
$$

while the model prediction is

$$
v_{r,i}^{\mathrm{pred}}=\hat r_i\cdot \mathbf v(\mathbf r_i|\Delta)=
\hat r_i\cdot \mathrm{FT}^{-1}\!\left[-i\,H_0\,f(\Omega_0)\,\frac{\mathbf k}{k^2}\,\tilde\delta(\mathbf k)\right].
$$

The corresponding likelihood is

$$
L_i^{v_r}
=(2\pi\,\kappa_i^2)^{-1/2}
\exp\!\left[-\frac{\bigl(v_{r,i}^{\mathrm{obs}}-v_{r,i}^{\mathrm{pred}}\bigr)^2}{2\,\kappa_i^2}\right],
$$

with

$$
\kappa_i^2=\sigma_{\mathrm{nl}}^2+\frac{\sigma_{cz,i}^2}{(1+z(d_i))^2}.
$$

The prior factorizes as $P(\Delta,D,\sigma_{\mathrm{nl}})=P(\Delta)\,P(D)\,P(\sigma_{\mathrm{nl}})$. The density prior is a Gaussian random field,

$$
P(\Delta)=\prod_k [2\pi P(k)]^{-1}\exp[-|\tilde\delta(k)|^2/P(k)],
$$

where $P(k)$ is the linear matter power spectrum. The distance prior approximates the survey selection function through the histogram of observed redshift-distances, and $P(\sigma_{\mathrm{nl}})$ is taken to be weak, for example uniform or broad Gaussian [2203.02369].

This posterior architecture makes the role of the data model explicit. The density field remains Gaussian at the prior level; the nonlinearity enters through the observational map from modulus and redshift to distance and radial velocity.

## 3. Hamiltonian Monte Carlo formulation

HAMLET bundles the latent variables into a parameter vector

$$
q=(\mathrm{Re}\,\tilde\delta_1,\ldots,\mathrm{Im}\,\tilde\delta_1,\ldots,d_1,\ldots,d_N,\sigma_{\mathrm{nl}}).
$$

The negative log-posterior defines the potential energy,

$$
U(q)=-\ln P(\Delta,D,\sigma_{\mathrm{nl}}|\mu,z).
$$

Introducing conjugate momenta $p$ of matching dimension and a tunable mass matrix $M$, HAMLET defines the Hamiltonian

$$
H(p,q)=K(p)+U(q), \qquad K(p)=\frac12\,p^T M^{-1}p.
$$

The mass matrix is chosen, or periodically adapted, to approximate the covariance of $q$. In the **prior-only** limit, the diagonal blocks can be set using the prior variances, including **$P(k)/2$ for each $\tilde\delta_k$ mode** and an approximate variance **$\simeq(\sigma_{\mathrm{nl}}/H_0)^2$ for each $d_i$**.

Sampling proceeds with the standard **leapfrog integrator** applied to Hamilton’s equations,

$$
\frac{dq}{d\tau}=\frac{\partial K}{\partial p}, \qquad
\frac{dp}{d\tau}=-\frac{\partial U}{\partial q}.
$$

Leapfrog is used because it guarantees **reversibility** and **volume conservation**. The integration error in $H$ is $O(\epsilon^2)$ per step and does not accumulate with the number of steps. The trajectory length is $L=N_{\mathrm{steps}}\times \epsilon$, and the step size $\epsilon$ is tuned to balance proposal quality against traversal efficiency; the implementation targets a Metropolis acceptance rate of approximately **0.65**. Dual averaging can tune $\epsilon$ during warm-up, and the **No-U-Turn Sampler** can optionally adapt $N_{\mathrm{steps}}$ to avoid back-and-forth trajectories [2203.02369].

A common misconception is that HMC in this setting is simply a faster random-walk sampler. HAMLET’s formulation is more specific: the computational gain comes from using posterior gradients of a structured cosmological forward model, so the efficiency improvement is tied to geometry-aware proposals rather than to generic parallelization alone.

## 4. GPU execution and computational profile

HAMLET implements the expensive operations of the sampler through **TensorFlow on GPUs**. The documented GPU-resident components include **FFT**, **the gradient of $U(q)$**, and **leapfrog updates**. The gradient combines analytic derivatives of the log-likelihood and log-prior with an **FFT-transpose** for the $\tilde\delta \rightarrow$ real-space transforms.

For a typical **grouped CF3** problem with approximately **$1.5\times 10^4$ data points** and a **$128^3$ Fourier-cell grid**, HAMLET is reported to run a full HMC chain of approximately **$10^3$–$10^4$ steps** in approximately **10 minutes on a single high-end GPU**, compared with **weeks on one CPU** for the earlier Gibbs-sampler implementation. The corresponding acceleration is stated as approximately **$10^3$–$10^4\times$**. The abstract summarizes the same result as an improvement of **two to four orders of magnitude in CPU time**, attributed both to the higher efficiency of HMC and to GPU rather than CPU execution [2203.02369].

The performance claim is significant because the parameter space is not small. In the mock-catalogue tests, HAMLET is described as exploring approximately **50 times more dimensions** than the prior Gibbs sampler while remaining faster in wall-clock time. This suggests that the principal scalability bottleneck shifts from Markov-chain inefficiency to memory and transform throughput.

The reported memory scaling is correspondingly favorable: the GPU-HMC framework is said to handle approximately **$256^3$ or $512^3$ Fourier grids**—about **$10^7$–$10^8$ modes**—at approximately **1–2 GB GPU memory**. This is presented as sufficient for **Cosmicflows-4**, which is expected to contain approximately **5 times more velocities**, together with higher-resolution modes.

## 5. Validation on Cosmicflows-like mock catalogues

The mock-catalogue campaign used **nine mocks** generated with **$7.5$, $15$, or $30\times10^3$ galaxies** inside **$160\,h^{-1}\,\mathrm{Mpc}$**, with distance-modulus errors set to $\sigma_\mu=\alpha\,\sigma_\mu^{\mathrm{CF3}}$ for $\alpha=\{1,\tfrac12,\tfrac1{10}\}$. The underlying density and velocity fields came from a **$128^3$ linear realization with Planck cosmology**. Chains of approximately **200 warm-up** steps followed by **500–1000 production** steps produced **unbiased estimates** of $\langle\delta(\mathbf r)\rangle$ and $\langle\mathbf v(\mathbf r)\rangle$, and convergence of the volume-averaged $\delta$ reached the **percent level by approximately 100–200 steps** [2203.02369].

Several reconstruction-quality diagnostics were reported. Mean-field slices $\langle\delta\rangle$ reproduced the **zero-contours** of the target field out to approximately **$60$–$100\,h^{-1}\,\mathrm{Mpc}$** for CF3-like errors, and farther for $\alpha=\tfrac12$ or $0.1$. The local fractional uncertainty $\Sigma_\delta(\mathbf r)/\sigma_\delta(\mathbf r)$ increased with radius, while smaller $\alpha$ yielded tighter constraints. The **radial profile of the bulk flow and monopole** matched the target within the mock-boundary zone, **$r\lesssim150\,h^{-1}\,\mathrm{Mpc}$**.

The radial degradation is quantifiable. Pearson correlations between the HAMLET mean field and the target decreased from approximately **0.9** at small radii to approximately **0.5–0.8** at **$r=150\,h^{-1}\,\mathrm{Mpc}$**, with the correlations **higher for velocities than for densities**. This suggests that, within the linear-theory forward model adopted by HAMLET, peculiar-velocity data constrain low-order flow moments more robustly than small-scale density detail.

## 6. Comparative assessment, limitations, and uses

A subsequent comparative study tested HAMLET against **BGc/WF** and an “exact” Wiener filter on Cosmicflows-3-like mocks. In the **nearby regime**, $d\lesssim40\,\mathrm{Mpc}/h$, the two practical methods were found to perform **roughly equally well**. HAMLET performed **slightly better in the intermediate regime**, $40\lesssim d\lesssim120\,\mathrm{Mpc}/h$. The most substantial differences appeared in the **distant regime**, $d\gtrsim120\,\mathrm{Mpc}/h$, near the survey edge: HAMLET outperformed BGc/WF in terms of **better and tighter correlations**, but also yielded a **somewhat biased reconstruction** there, whereas such biases were reported to be absent from BGc/WF [2209.05846].

The distant-regime behavior is central to interpreting HAMLET’s output. The improvement in correlation does not imply uniformly unbiased reconstruction across radius. The testing study explicitly associates the far-edge degradation with a boundary effect, and this suggests that posterior richness alone does not remove the need for careful radial modeling of the survey boundary.

Within those limitations, the algorithm has two immediate applications. First, its acceleration makes possible larger reconstructions from **upcoming Cosmicflows-4 data**. Second, constrained realizations drawn from the HAMLET chain can be used as **initial conditions for zoom-in N-body or hydrodynamic simulations of the Local Universe**, and the abstract also notes the role of the method in setting **constrained initial conditions for cosmological high resolution simulations** [2203.02369].

The framework is also extensible. The parameter vector $q$ may be enlarged to include **cosmological parameters** such as $H_0$, $\sigma_8$, and $\Omega_m$, or **nuisance parameters** such as zero-points for different distance indicators. This suggests a broader role for HAMLET as a joint sampler over cosmography and calibration, rather than only a fixed-cosmology reconstruction engine.

Source: https://www.emergentmind.com/topics/hamlet