Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A “global” normalizing flow on the sphere S^2

Geophysical and astronomical data live on the sphere. This notebook builds a normalizing flow on S2⊂R3S^2 \subset \mathbb{R}^3 via stereographic projection: we map the sphere to the plane (with an explicit Jacobian), fit a flowjax spline coupling on the plane, and pull the density back to S2S^2 to plot it in a Mollweide projection.

The target is a synthetic stand-in for “global hotspots”: a mixture of three von Mises–Fisher (VMF) distributions concentrated at three different points on the sphere.

Limitations. Stereographic projection has a singularity at the north pole: points near it map to large R2\mathbb{R}^2 values and the Jacobian blows up. We pick the projection point so that the data clusters lie away from the singularity. Tangent-space (expmap) flows avoid this at the cost of losing the global bijection to R2\mathbb{R}^2 — a topic for a future notebook.

/home/azureuser/localfiles/geo_repos/geo_ml/gauss_flows-fit/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

1. A VMF-mixture target on S2S^2

Three modes at chosen unit vectors with concentration κ=30\kappa = 30 (tight clusters). We use north-pole stereographic projection (singularity at x3=1x_3 = 1, see section 2). The mode means are placed in the southern hemisphere (x3<0x_3 < 0) so the data maps to a compact region near the origin in R2\mathbb{R}^2 — well away from the singularity.

data shape: (3000, 3)
on-sphere check max |‖x‖ - 1|: 1.1920928955078125e-07

2. Stereographic projection S2↔R2S^2 \leftrightarrow \mathbb{R}^2

Project from the north pole N=(0,0,1)N = (0, 0, 1). For a point x=(x1,x2,x3)x = (x_1, x_2, x_3) on S2S^2 with x3≠1x_3 \neq 1 the forward map and its inverse are

σ(x)=11−x3 (x1, x2),σ−1(u)=11+∥u∥2 (2u1, 2u2, ∥u∥2−1).\sigma(x) = \frac{1}{1 - x_3}\,(x_1,\, x_2), \qquad \sigma^{-1}(u) = \frac{1}{1 + \|u\|^2}\,(2u_1,\, 2u_2,\, \|u\|^2 - 1).

The Jacobian determinant of σ\sigma from the canonical area form on S2S^2 to the Lebesgue measure on R2\mathbb{R}^2 is

∣det⁡Jσ(x)∣=1(1−x3)2,∣det⁡Jσ−1(u)∣=4(1+∥u∥2)2.\bigl|\det J_\sigma(x)\bigr| = \frac{1}{(1 - x_3)^2}, \qquad \bigl|\det J_{\sigma^{-1}}(u)\bigr| = \frac{4}{(1 + \|u\|^2)^2}.

Stereographic projection is a chart between manifolds of different ambient dimensions (3 → 2 and back), so it can’t be expressed as a flowjax AbstractBijection (which requires matching shapes). We use plain JAX functions and assemble the sphere flow functionally below.

Validate the projection

Round-trip and Jacobian-vs-autodiff checks. Because the image is 2D-embedded-in-3D we compare against 12log⁡det⁡(J⊤J)\tfrac{1}{2}\log\det(J^\top J), which is the area-element formula.

sphere round-trip max error : 5.96e-08
closed-form log-det at u₀   : +0.800955
autodiff log-det at u₀      : +0.800955

3. Flatten data, train an R2\mathbb{R}^2 flow

We map the sphere samples to the plane via stereographic projection, then fit a RQSplineCoupling chain wrapped in a Transformed(Normal, ...) model.

plane data shape: (3000, 2)
plane data range: [-1.3057652711868286, 0.890555739402771]
WARNING:matplotlib.font_manager:findfont: Failed to find font weight semibold for DejaVu Sans, now using 700.
<Figure size 660x440 with 1 Axes>

4. Push samples back to the sphere

The change-of-variables formula gives the sphere density:

log⁡pS2(x)  =  log⁡pplane(σ(x))  +  log⁡ ∣det⁡Jσ(x)∣  =  log⁡pplane(σ(x))−2log⁡(1−x3).\log p_{S^2}(x) \;=\; \log p_\text{plane}\bigl(\sigma(x)\bigr) \;+\; \log\,|\det J_\sigma(x)| \;=\; \log p_\text{plane}\bigl(\sigma(x)\bigr) - 2\log(1 - x_3).

Sampling: draw u∼pplaneu \sim p_\text{plane} from the trained flow, then x=σ−1(u)x = \sigma^{-1}(u) lands on S2S^2 with the right density automatically.

sphere sample shape: (2000, 3)
on-sphere check max |‖y‖ - 1|: 1.7881393432617188e-07

5. Visualise on a Mollweide projection

Convert (x1,x2,x3)(x_1, x_2, x_3) on S2S^2 to (longitude, latitude) and plot data and flow samples side by side. We also draw a density heatmap by evaluating sphere_dist.log_prob on a (lon, lat) grid.

WARNING:matplotlib.font_manager:findfont: Failed to find font weight semibold for DejaVu Sans, now using 700.
<Figure size 1540x1320 with 4 Axes>

The flow samples cluster around the same three hotspots as the training data, and the Mollweide density heatmap recovers the three modes. Density values away from the modes are tiny (the colormap is dominated by the peaks).

6. Sanity check: the log-density integrates to ≈ 1 on the sphere

A correct density on S2S^2 should integrate to 1 against the surface area element cos⁡(lat) dlon dlat\cos(\text{lat})\,d\text{lon}\,d\text{lat}. We approximate this on the same (lon, lat) grid.

∫_S² p(x) dA  ≈  0.9899

We don’t expect exactly 1 because the grid skips a thin equatorial band near the projection-pole singularity and uses a coarse mesh — but the integral should land in the ballpark of 1, confirming the Jacobian bookkeeping is right.

7. What’s going on under the hood

  • Plane flow: a standard Transformed(Normal, RQSplineCoupling chain) trained on stereographically-projected data. Nothing special.

  • Sphere log-density: composed manually via change of variables. sphere_log_prob(x) = plane.log_prob(σ(x)) + log|det J_σ(x)|.

  • Sphere sampling: u∼pplaneu \sim p_\text{plane}, then x=σ−1(u)x = \sigma^{-1}(u).

  • Where stereographic helps: any flat-space flow (RQSplineCoupling here, but FFJORD, MatrixExponential, gaussianization_flow, … all work) can be reused without writing a sphere-native bijection.

  • Where it bites: data near the projection pole maps to large ∥u∥\|u\|, the spline coupling has finite support, and the Jacobian ∝1/(1+∥u∥2)2\propto 1/(1 + \|u\|^2)^2 collapses. For globally distributed data you’d want a two-chart (north + south) construction or a tangent-space (expmap) flow — see the tangent_basis / expmap_sphere utilities for the building blocks.