# AC60 offline analysis: restricted hierarchical inference on public GWTC-3 data

These scripts back the method tutorial of the gravitational-wave population
course. They use real public data: GWTC-3 posterior samples (Zenodo record
5546663) and the O3b BBH sensitivity injections (Zenodo record 5546676),
both released by the LIGO, Virgo and KAGRA collaborations under CC BY 4.0.
The injection-recovery run adds synthetic catalogs built from those
injections; its events are synthetic, not detections.

## Model

- Source-frame primary mass `m1` follows `m1^-alpha` from 5 to a sharp
  maximum `m_max`. The secondary follows `p(m2 | m1) ∝ m2^1.1` on
  `[5, m1]`. Mergers follow `p(z) ∝ dVc/dz (1+z)^1.7` on `[0, 1.9]`.
- Only `alpha` and `m_max` are inferred, on a uniform grid:
  `alpha` in 0-7 (step 0.05), `m_max` in 40-100 solar masses (step 0.5).
  The pairing index 1.1 and `kappa = 2.7` are fixed.
- Cosmology: flat, `H0 = 67.74 km/s/Mpc`, `Omega_m = 0.3089`. This
  reproduces the comoving distances stored with the GWTC-3 PE prior samples
  to about 1e-4.
- PE prior of the `*_cosmo` samples: `pi(m1, m2, z) ∝ (1+z) dVc/dz`
  (uniform in detector-frame masses, uniform merger rate in the source frame).
- Units: solar masses (source frame); redshift is dimensionless.

## Inputs (frozen, committed)

| File | Content | SHA-256 |
| --- | --- | --- |
| `inputs/o3b-bbh-posterior-samples.csv` | 23 O3b BBH candidates with GWOSC FAR below 1 per year and `m2` above 3; 1500 samples each (seed 60) of `mass_1_source`, `mass_2_source`, `redshift` | `11f1e21f10167b7d54e7beb4a478d90d7b4ef8cd2398193fd6fa4b975b0d60f8` |
| `inputs/o3b-bbh-found-injections.csv` | 20000 of the 39069 injections found with FAR below 1 per year in any of the five searches, with their draw density | `2e6cfeaba297971ae88d36d3e13521f79e4c63d3a47bb538dc531173c320d41c` |
| `inputs/gwosc-gwtc3-confident-events.json` | GWOSC event list used to choose the events (MD5 `1c7f6f4fbd46224e390d4e944615da02`) | `e892790dd548e4b41248c771bbe658f9edc8b41cedbfbe0c0ebea7a55f3ccdb7` |
| `inputs/frozen-subset-manifest.json` | labels, sample counts, injection metadata | `c36aa026cf0d49082023cbd88c5d6e38b3984a83bd75ccd01af101dfdb298535` |

`freeze_public_subset.py` made these files. It needs the network and h5py;
nobody has to rerun it. The injection file it read is
`endo3_bbhpop-LIGO-T2100113-v12-1256655642-12905976.hdf5`
(MD5 `408f3eb5c98d62608a4f35cf3d73e3af`). Because only 20000 found
injections are kept, the number of generated injections is scaled to
`33310659 × 20000 / 39069`.

## Environment and run order

Tested with Python 3.9.6, NumPy 2.0.2 and SciPy 1.13.1 on arm64 macOS.
Run each command from this folder.

| Step | Command | Wall clock | Output |
| --- | --- | --- | --- |
| 1 | `python3 hierarchical_mass_inference.py` | 0.3 s | `outputs/hierarchical-inference-results.json` |
| 2 | `python3 injection_recovery.py` | 106 s | `outputs/injection-recovery-results.json` |

Both are seeded and reproduce their outputs byte for byte on the same
NumPy build: step 1 SHA-256 `761339bd80c91b1933ad73ce5da0d8e9a90d34fc520acb1d1890cd64fd8424ec`,
step 2 SHA-256 `941662ac81619cbb7fcc851e7bbc6f8f2e5fbba3adfc6b2da6a42d8187ffc51c`.

## Results (step 1)

- With the selection term: `alpha = 2.56` (90% interval 1.84 to 3.17),
  `m_max = 54` (46 to 75) solar masses.
- Without it: `alpha = 0.26` (0 to 0.76), pressed against the lower prior
  edge. Ignoring selection makes the heavy end look far too common.
- A power law fitted to the detected median masses gives `alpha = 0.75`.
- Detectable fraction at the posterior peak: about `1.0e-3`; Farr's
  selection effective sample size there is about 9800, far above
  `4 N_obs = 92`.

## Checks and failure cases

- Split-half selection check: even- and odd-indexed injections give the same
  detectable fraction within 0.6 sigma at five test points.
- Farr's criterion holds on the whole grid with 20000 injections (smallest
  effective size 142 at `alpha = 7`). With 2000 injections it fails from
  `alpha = 4.25`, with 500 from `alpha = 3.15`.
- Per-event effective samples: GW191109_010717 (`m1` about 65) falls below
  20 when `m_max` cuts into its posterior. 36% of the reported posterior
  mass lies in such cells, so the lower end of `m_max` is set by too few
  samples of one event. A hard cut on those cells gives `alpha = 2.68`
  (2.06 to 3.24) and `m_max = 58` (52 to 79); it is shown, not used,
  because a cut that depends on the data moves the posterior.
- GW191127_050227 has 5.4% of its samples above 100 solar masses, where
  the injections stop; the model gives them zero weight.

## Injection recovery (step 2)

Even-indexed found injections are resampled to a known population
(`alpha = 3.5`, `m_max = 70`) and become synthetic detections; odd-indexed
injections estimate the selection term. Each synthetic event gets a mock
posterior that is Gaussian in log detector-frame chirp mass (width 0.04),
log mass ratio (0.3, truncated at 1) and log redshift (0.3). 100 catalogs
per row; the binomial standard error of a 90% coverage is 0.03.

| Events | Selection term | `alpha` 68% / 90% coverage | `m_max` 68% / 90% | Posterior mass in flagged cells |
| --- | --- | --- | --- | --- |
| 23 | yes | 0.61 / 0.91 | 0.79 / 0.95 | 0.28 |
| 23 | no | 0.00 / 0.00 | 0.73 / 0.93 | 0.23 |
| 92 | yes | 0.51 / 0.83 | 0.52 / 0.82 | 0.76 |
| 92 | no | 0.00 / 0.00 | 0.47 / 0.74 | 0.67 |

Without the selection term the true slope is never inside the interval. With
it, 23-event catalogs cover close to nominal, although a KS test of the truth
percentiles still detects a small shift (p = 0.008). At 92 events the
coverage drops: most posterior mass then sits in cells where some event has
fewer than 20 effective samples, usually a binary close to the sharp 5 solar
mass edge, and the Monte Carlo error of the event terms grows with the
number of events. More samples per event or a smooth lower edge would be
needed for larger catalogs.

## Limits

Two inferred parameters only; no peak, no spins, no mass-ratio or redshift
freedom; a sharp lower edge at 5 instead of a smooth turn-on. The results are
a worked example of the method, not a replacement for the GWTC-3 population
analysis (Abbott et al. 2023, PRX 13, 011048), whose models and priors differ.

## Replay the three catalog cuts

The browser’s O3b comparison is [outputs/o3b-observed-comparison.json](outputs/o3b-observed-comparison.json).
Run `python3 -B observed_replay.py` in this directory. The replay checks the original
inputs and the new `inputs/o3b-bbh-found-far.csv` by SHA-256. The latter's checksum
is `00d928169fa2d769428adfd964a158a5a97b04557cc46181a217133b69ad178c`.
Each row stores the original HDF5 row index and the minimum reported false-alarm
rate across the five listed searches, in events per year.

To extract that CSV again from the official file, use
`python freeze_o3b_far.py /path/to/endo3_bbhpop-LIGO-T2100113-v12-1256655642-12905976.hdf5`.
The extractor verifies the released SHA-256
`ac194f6360534066d8e455dab8dca98651edf6fd52e423d1ef43bc21b38ff6a6`
and regenerates the masses, redshifts and proposal densities for all 20,000 rows.
It confirms that those coordinates match the original frozen CSV exactly before
recording any search scores. Extraction was checked with Python 3.12.12, NumPy
2.4.4 and h5py 3.16.0. The normal replay just reads the two CSVs and the event
posteriors, so it uses the original Python 3.9.6 environment above.

At each threshold the inference uses the retained events and retained
injections. The effective number generated stays fixed. The two model curves
average the latent source distributions over the fitted grid posterior.
The observed bars combine published event posteriors with equal event weight
and their original PE priors. The comparison is descriptive. A formal
posterior predictive check would also simulate and analyze the noisy
measurement for each predicted detection.
