---
title: "Reproducing a two-component kilonova fit to the GW170817 photometry"
description: "Download the Villar et al. table, verify its digest, rerun the fits and checks with fixed versions, and read the saved failure cases."
canonical_url: "https://sandboxphysics.com/stories/kilonovae/reproducing-a-two-component-fit-to-gw170817/"
language: "en"
page_type: "story"
alternate_languages:
  zh-CN: "https://sandboxphysics.com/zh/stories/kilonovae/reproducing-a-two-component-fit-to-gw170817/"
last_modified: "2026-11-14"
---

[Kilonovae: heavy elements and diffusing light](https://sandboxphysics.com/stories/kilonovae/)

# Reproducing a two-component kilonova fit to the GW170817 photometry

Sandbox Physics · 2026-11-14

We fitted a constrained two-component grey kilonova model to a fixed set of 394 GW170817 measurements and saved every input, output and failure case. The best fit has an extra scatter of $0.261$ magnitudes and a red component of $0.0525$ solar masses at $0.152c$ with opacity $3.18\,\mathrm{cm^2\,g^{-1}}$, but the red mass and speed are correlated at $0.888$ and a second solution sits only $1.9$ in $-\ln\mathcal L$ higher. Rerunning the check on our machine reproduced every saved number within $0.2$ percent in about 75 seconds.

[Open the kilonova light-curve Lab](https://sandboxphysics.com/labs/kilonova-light-curves/)

The physics is explained in [fitting GW170817 and the trade-off between ejecta mass and speed](https://sandboxphysics.com/stories/kilonovae/fitting-gw170817-and-parameter-degeneracy/). This guide is the recipe.

## What the authors published

Villar and colleagues compiled the ultraviolet, optical and infrared photometry of GW170817 from 18 papers into one table, which they distribute with the arXiv source of their paper. [Villar and colleagues, arXiv:1710.11576v2, §2 and Table 3](https://arxiv.org/abs/1710.11576v2) They ask that any use of the table cite the original papers as well as their compilation. Our CSV keeps their reference key for every row so that each point can be credited.

**Table 4.1** The fixed inputs.

| Item | Value |
| --- | --- |
| Source archive | arXiv e-print of 1710.11576, file phot\_table.dat |
| Table digest, SHA-256 | a2ec8677f60d096a1347d82f5628377fd206de5596909c161a79d68c70bc6e92 |
| Kept rows | 394 detections in g r i z J H K and Ks, used by the authors, not outliers |
| Output digest, SHA-256 | 6e977704480b16314e4794abecf452216848f41e6682d623f53119de646d52fc |
| Merger time | MJD 57982.528524 |
| Distance | $39.5\,\mathrm{Mpc}$, fixed |
| Milky Way reddening | $E(B-V)_{\mathrm{SFD}}=0.1228$, IRSA dust service, retrieved 2026-11-14 |
| Extinction coefficients | Schlafly and Finkbeiner, Table 6, $R_V=3.1$ |

All of this is also in [tutorial-inputs.json](https://sandboxphysics.com/stories/kilonovae/materials/tutorial-inputs.json). [Schlafly and Finkbeiner, Table 6](https://arxiv.org/abs/1012.4804v2)

## Units

Time is in days since the merger, magnitudes are AB and not corrected for extinction, mass is in solar masses, speed in units of $c$, opacity in $\mathrm{cm^2\,g^{-1}}$, temperature in kelvin and distance in megaparsecs. Inside the code everything is converted to CGS. The model evaluates each filter at one effective wavelength; that approximation is part of what the extra scatter absorbs.

## Step 1: get the table and check it

Keep the download in its own empty folder and the scripts in another.

```
mkdir -p kilonova-fit/author-data kilonova-fit/scripts
cd kilonova-fit/author-data
curl -L -o villar-source.tar.gz https://arxiv.org/e-print/1710.11576
tar -xzf villar-source.tar.gz
shasum -a 256 phot_table.dat
```

The digest should be:

```
a2ec8677f60d096a1347d82f5628377fd206de5596909c161a79d68c70bc6e92  phot_table.dat
```

The e-print link serves the newest version of the source. If the authors ever post a new version, the table may change. Stop and compare before trusting anything below if the digest differs.

## Step 2: extract the fixed subset

Save [extract\_gw170817\_photometry.py](https://sandboxphysics.com/stories/kilonovae/materials/extract_gw170817_photometry.py) in the scripts folder and run it on the table:

```
cd ../scripts
python3 extract_gw170817_photometry.py ../author-data/phot_table.dat
```

It writes gw170817-photometry.csv and prints both digests. The output should match [our CSV](https://sandboxphysics.com/stories/kilonovae/materials/gw170817-photometry.csv), digest 6e9777...52fc. It spans $0.452$ to $25.44$ days.

## Step 3: fix the software

```
python3 -m venv .venv
. .venv/bin/activate
pip install numpy==2.0.2 scipy==1.13.1
```

The versions are listed in [requirements.txt](https://sandboxphysics.com/stories/kilonovae/materials/requirements.txt). We used Python 3.9.6. The fit script writes the versions it ran with into its output.

## Step 4: run the fits and the checks

Save [kilonova\_model.py](https://sandboxphysics.com/stories/kilonovae/materials/kilonova_model.py), [fit\_two\_component.py](https://sandboxphysics.com/stories/kilonovae/materials/fit_two_component.py), [check\_independent\_limits.py](https://sandboxphysics.com/stories/kilonovae/materials/check_independent_limits.py) and [tutorial-inputs.json](https://sandboxphysics.com/stories/kilonovae/materials/tutorial-inputs.json) next to the CSV, then:

```
python3 fit_two_component.py --check
python3 check_independent_limits.py
```

With --check, the fit script recomputes everything and compares it with a saved [fit-outputs.json](https://sandboxphysics.com/stories/kilonovae/materials/fit-outputs.json) in the same folder, allowing a relative difference of $2\times10^{-3}$. Without the flag it overwrites that file. The second script writes [independent-checks.json](https://sandboxphysics.com/stories/kilonovae/materials/independent-checks.json).

## What the fit script does

1. It builds each component from the radioactive heating law, the thermalization fit of Barnes and colleagues, Arnett's diffusion integral with the factor $2/t_d$, and a blackbody photosphere with a temperature floor.
2. It adds the components' flux in each band, converts to AB magnitudes at $39.5\,\mathrm{Mpc}$, and adds Milky Way extinction.
3. It minimises $-\ln\mathcal L$, alternating a bounded least-squares step at fixed scatter with a one-dimensional search over the scatter.
4. It starts from Villar and colleagues' published values and from 16 fixed pseudo-random points, with seed 170817, and keeps every distinct optimum.
5. It repeats the fit for four variants and maps $-\ln\mathcal L$ over a grid of red mass and speed, re-fitting the other parameters at each grid point.

## Results

**Table 4.2** Best two-component fit.

| Quantity | Value | Local error |
| --- | --- | --- |
| Blue mass | $0.0209\,\mathrm M_\odot$ | $0.0005$ |
| Blue speed | $0.385c$ | $0.016$ |
| Blue floor | $5184\,\mathrm K$ | $124$ |
| Red mass | $0.0525\,\mathrm M_\odot$ | $0.0022$ |
| Red speed | $0.152c$ | $0.005$ |
| Red floor | $1014\,\mathrm K$ | $31$ |
| Red opacity | $3.18\,\mathrm{cm^2\,g^{-1}}$ | $0.12$ |
| Extra scatter | $0.261$ mag |  |
| Diffusion times | $1.10$ and $6.96$ days |  |

The local errors come from the curvature at the optimum and ignore the other branches. Villar and colleagues report $0.023$, $0.256c$ and $3983\,\mathrm K$ for blue and $0.050$, $0.149c$, $3.65$ and $1151\,\mathrm K$ for red, with scatter $0.256$. [Villar and colleagues, Table 2](https://arxiv.org/abs/1710.11576v2) The two sets of numbers are different optima of closely related likelihoods; see the failure cases below.

The independent checks pass. Arnett's integral agrees with a direct Radau solution of the energy equation within $4.3\times10^{-4}$. Energy is conserved to $5\times10^{-5}$. The light-curve peaks satisfy $L=Q$ to $2\times10^{-4}$. For pure $t^{-1.3}$ heating the peak falls at $0.7087\,t_d$, the value found by quadrature. The radioactive energy from one to ten days matches the closed form within $10^{-5}$. A blackbody spectrum integrated over frequency returns its luminosity.

## Failure cases and how they are handled

1. The printed light-curve integral. Villar and colleagues' eq. 3 has the kernel $t/t_d$ instead of $2t/t_d^2$. As printed it has units of energy and depends on the time unit; for our best red component it is $3.48$ times $L$ with time in days. We use $2t/t_d^2$ and check it against the energy equation.
2. The printed photosphere law. Eqs. 4 and 5 put $\sigma_{\mathrm{SB}}^2$ inside the fourth root but $\sigma_{\mathrm{SB}}$ in the receded radius. With the square every temperature would be divided by $0.0868$, about $4000\,\mathrm K$ becoming $46000\,\mathrm K$. We use $\sigma_{\mathrm{SB}}$ throughout.
3. The printed likelihood. Eq. 6 does not normalise the Gaussian with variance $\sigma_i^2+\sigma^2$. We use the normalised form. Constant terms do not move the best parameters, but the normalisation does affect the fitted scatter.
4. Several optima. The 17 starts land in four distinct optima, at $63.02$, $64.90$, $82.13$ and $137.28$. Ten of the 17 starts end at $82.13$ and only three reach the best optimum, so a fit with a single start is likely to stop short of it.
5. A blue speed outside the thermalization table. The best blue speed, $0.385c$, exceeds the table's $0.3c$, so its coefficients are held at the edge values. The fit is valid for this model but the blue speed should not be read as a measurement.
6. Extinction convention. Schlafly and Finkbeiner's coefficients multiply the original SFD reddening, $0.1228$. Using the recalibrated $0.1056$ with the same coefficients under-corrects by 14 percent. That variant changes $-\ln\mathcal L$ by only $0.94$ and the red mass by $0.0002$.
7. Rejected models. One component, no temperature floor, and a red opacity forced to $10$ are all worse by more than $200$ in BIC. Their outputs are kept in the file for comparison.
8. The published parameters in our setup. With $\sigma$ refitted they give $-\ln\mathcal L=106.32$ and leave the late $K$ band about $0.20$ magnitudes too bright. We have not separated the effects of data selection, likelihood normalisation and the authors' MCMC sampling.

## What this tutorial does not show

The fit uses only detections and no upper limits, a spherical geometry, one grey opacity per component and blackbody spectra. Villar and colleagues preferred a three-component model, and an asymmetric version changed their masses by about half. Opacities of real r-process mixtures depend on wavelength and change with time. [Tanaka and colleagues, §4.2](https://arxiv.org/abs/1906.08914v2) The numbers here therefore show what this model can and cannot do with these data. They do not measure the ejecta. The Lab draws its curves with a TypeScript port of the same model, which agrees with the Python reference within $3\times10^{-4}$ magnitudes. Compare its default with the data in the [kilonova light curves Lab](https://sandboxphysics.com/labs/kilonova-light-curves/).

## Try it in the Lab

Keep the default, which uses the published Villar et al. values, and look at the K points after ten days: the model curve runs about 0.2 magnitudes brighter than the data. [Open the kilonova light-curve Lab](https://sandboxphysics.com/labs/kilonova-light-curves/)

## Sources

1. [Villar and colleagues, arXiv:1710.11576v2, §2 and Table 3](https://arxiv.org/abs/1710.11576v2)
2. [Schlafly and Finkbeiner, Table 6](https://arxiv.org/abs/1012.4804v2)
3. [Tanaka and colleagues, §4.2](https://arxiv.org/abs/1906.08914v2)

## Keep reading

Kilonovae: heavy elements and diffusing light · Reading path

### From radioactive heat to a blue-then-red flash

1. [Article **How does a neutron-star collision turn nuclear decay into days of coloured light?**](https://sandboxphysics.com/stories/kilonovae/why-a-kilonova-turns-from-blue-to-red/)
2. [Article **Radioactive heat and the time light needs to escape merger debris**](https://sandboxphysics.com/stories/kilonovae/heating-and-diffusion-in-merger-debris/)
3. [Article **From the energy equation to kilonova light curves and colours**](https://sandboxphysics.com/stories/kilonovae/arnett-light-curves-and-the-photosphere/)
4. [Article **Fitting GW170817 and the trade-off between ejecta mass and speed**](https://sandboxphysics.com/stories/kilonovae/fitting-gw170817-and-parameter-degeneracy/)
5. [You are here **Reproducing a two-component kilonova fit to the GW170817 photometry**](https://sandboxphysics.com/stories/kilonovae/reproducing-a-two-component-fit-to-gw170817/)
6. [Article **Reading Tanaka et al. 2020 on r-process opacities in kilonovae**](https://sandboxphysics.com/stories/kilonovae/reading-tanaka-2020-kilonova-opacities/)
