# dipcheck

Find transit-like dips in a light curve, vet them, count every try, and seal your predictions before the new data arrives.

`dipcheck` is one JavaScript file with no dependencies. It runs in Node 18+ and in the browser.

```
npm test                         # 21 checks, about 4 seconds
node bin/dipcheck.js help
```

## Why it exists

In October 2026 an independent researcher worked with an AI coding agent through years of public TESS photometry of a nearby K dwarf and reported a 3.18-day, roughly 500 ppm transit-like signal: a planet candidate of about 1.4 Earth radii, not a confirmed planet. Two things about that story are worth building tools for:

1. **An agent can try a thousand things.** Over a thousand scripts were reported. Every extra search is another chance for noise to look like a planet. `dipcheck` keeps a hash-chained ledger of every search and turns the number of tries into a false-alarm figure.
2. **The predictions were written down before the new data.** The follow-up was announced with the expected transits and four possible outcomes: recovered, inconsistent, excluded, insufficient data. `dipcheck` seals those predictions in a hashed record and later judges new data against exactly what was sealed.

## Use

```js
const dipcheck = require("./src/dipcheck.js");          // or <script src="dipcheck.js"> -> window.dipcheck

const lc = dipcheck.parseCSV(text);                      // time, flux[, flux_err]; headers and comments are fine
const tries = dipcheck.ledger();                         // every search gets counted here
const r = dipcheck.search(lc, { star: { radius: 0.58, mass: 0.62 }, ledger: tries });

r.candidate     // { period, t0, duration, depth, depthErr, snr, transits, sde }
r.ephemeris     // { period, sigmaP, t0, sigmaT0, times } from fitted transit times
r.vetting       // { verdict, planetRadius, checks: [...] }

const sealed = dipcheck.seal({ target: "my star", ephemeris: r.ephemeris, depth: r.candidate.depth,
                               duration: r.candidate.duration, from: 3120, to: 3147 });
// ...weeks later, with the new light curve:
dipcheck.judge(sealed, dipcheck.detrend(dipcheck.clean(newLc))).category
// "recovered" | "inconsistent" | "excluded" | "insufficient data"
```

## What each step does

| Step | Function | What happens |
|---|---|---|
| Read | `parseCSV(text)` | Finds the time, flux and error columns (TIME, BTJD, PDCSAP_FLUX, FLUX_ERR …), drops bad rows, sorts by time. |
| Clean | `clean(lc, {sigma: 4})` | Divides by the median and clips high outliers only. Dips are what we look for, so low points stay. |
| Flatten | `detrend(lc, {window: 0.75})` | Divides out slow changes with a moving median (window in days). Dips much shorter than the window survive. |
| Search | `bls(lc, {pmin, pmax, durations})` | Box least squares (Kovács, Zucker & Mazeh 2002) over a period grid fine enough for the star's expected transit duration. Returns the periodogram and the best box with its signal detection efficiency (SDE). |
| Fit | `fit(lc, cand)` | Depth, depth error, SNR and the number of transits actually covered by data. |
| Times | `ephemeris(lc, cand)` | Fits each transit's mid-time, then a straight line through them: period and reference time with uncertainties from the scatter. |
| Vet | `vet(lc, cand, {star, trials})` | Ten checks (below) and a verdict: "planet candidate", "needs more data" or "likely false positive". |
| Luck | `nullSearch(lc, {n})`, `nullScale(sdes)`, `afterTries(p1, k)` | Shuffles the data in time and searches each shuffle. `pValue(sde)` is the empirical chance that noise alone reaches your SDE (never below 1/(n+1)); `tail(sde)` extends it with a Gumbel fit to the shuffled maxima. `nullScale` builds the same scale from scores you collected in pieces. Then: the chance after k searches. |
| Count | `ledger()`, `verifyLedger(doc)` | Append-only log, each entry hashed with the previous one (SHA-256). Editing or deleting an entry breaks the chain. |
| Predict | `predict(eph, {from, to})` | Transit mid-times in a future window, each with its growing uncertainty. |
| Seal | `seal({...})`, `verifySeal(rec)` | Freezes the ephemeris, depth, duration, window, predictions and the success rules into one hashed record. |
| Judge | `judge(sealed, newLc)` | Measures the new data at the sealed times only and returns one of the four outcomes. |
| Simulate | `synth(opts)`, `preset(name)` | Seeded light curves with noise, data gaps every 13.7 days, stellar variability, instrument ramps, planets and eclipsing binaries. |

## The ten checks

| Check | Passes when | Catches |
|---|---|---|
| Signal-to-noise | SNR ≥ 7.1 (5–7.1 is weak) | noise |
| Transits covered | at least 3 dips with data | one-off events |
| Odd and even dips match | difference < 2σ | two stars eclipsing each other, at twice the period |
| No dip at half phase | < 2σ | secondary eclipses |
| No single dip carries it | the largest dip holds < 40% of the signal | one bad stretch of data |
| Duration fits the star | 0.2–1.3 × the central-transit duration for the given star | blends, giant stars, wrong period |
| Planet-sized | radius ≤ 15 Earth radii (≤ 22.4 is weak) | stars |
| Flat-bottomed | edge depth ≥ 0.5 × centre depth | grazing eclipses |
| Not stuck to data gaps | < 34% of dips within a day of a gap | instrument ramps after downlinks |
| Not the spacecraft's rhythm | period not within 3% of 13.7 d, its half or double | the telescope's orbit |

With `trials: { tries, p1 }` an eleventh check reports the chance that noise produced the signal after all those tries.

The checks are screens, not proof. They do not replace pixel-level centroid checks, Gaia neighbours, high-resolution imaging, radial velocities or a statistical validation tool such as TRICERATOPS (Giacalone et al. 2021). A planet transiting an unresolved companion star passes most of these checks; only those other tools can address it.

## Outcomes when you open the envelope

| Outcome | When |
|---|---|
| recovered | the stacked depth at the sealed times is ≥ 3σ and within 3σ of the sealed depth |
| excluded | nothing at the sealed times, and the sealed depth is ruled out at 3σ |
| inconsistent | something is there, but not what was sealed (wrong depth) |
| insufficient data | fewer than half of the predicted transits were observed, or the data are too noisy (error > half the sealed depth) |

The rules (`detect: 3`, `depthMatch: 3`, `minCoverage: 0.5`) are inside the seal, so they cannot be changed after the fact either.

## Command line

```
node bin/dipcheck.js synth --preset small-planet > lc.csv
node bin/dipcheck.js search lc.csv --rstar 0.58 --mstar 0.62 --ledger tries.json
node bin/dipcheck.js null lc.csv --n 20
node bin/dipcheck.js seal lc.csv --from 3120 --to 3147 --rstar 0.58 --mstar 0.62 > sealed.json
node bin/dipcheck.js judge sealed.json new.csv
node bin/dipcheck.js ledger tries.json
```

On the simulated "small-planet" light curve (81 days, 30-minute points, 350 ppm noise) the search finds P = 3.1799 d, a 1.5-hour dip of 507 ± 40 ppm at SNR 12.6 and SDE 12.2, covered 24 times, 1.42 Earth radii for a star of 0.58 solar radii, and all ten checks pass.

## Presets (all simulated, none is real data)

| Name | What it is |
|---|---|
| `small-planet` | a quiet K dwarf with a 3.18-day, 500 ppm planet |
| `two-stars` | an eclipsing binary whose eclipses alternate in depth |
| `wobbly-noise` | no planet: noise, stellar variability and ramps after each data gap |
| `too-long` | a dip far too long for its star (a blended binary) |

## Limits

- Box-shaped search: fine for finding dips, rough for shapes. Fit a physical transit model before quoting a radius.
- The period grid assumes the star you give it; with no star it uses 0.8 solar radii and 0.8 solar masses.
- Years-long baselines with big gaps need very fine period grids; `maxPeriods` caps the grid at 30,000 periods by default.
- `nullSearch` shuffles in short blocks; it keeps short correlations but not long ones, so it is a floor on the false-alarm chance, not a ceiling. Shuffling also destroys instrument systematics, so a ramp or a spacecraft rhythm can look "significant" here: that is what the vetting checks are for.
- `tail()` extrapolates far beyond the shuffles you ran. Treat numbers below 1/(n+1) as estimates, and very small ones as "far beyond these shuffles", not as exact odds.
- `afterTries` treats the tries as independent. Searches with nearly the same settings are not, so it overstates the penalty for near-duplicates and is right for genuinely different searches.

## References

- Kovács, G., Zucker, S. & Mazeh, T. 2002, A&A 391, 369 — the box least squares search.
- Hippke, M. & Heller, R. 2019, A&A 623, A39 — transit least squares and SDE thresholds.
- Seager, S. & Mallén-Ornelas, G. 2003, ApJ 585, 1038 — transit durations and shapes.
- Ricker, G. R. et al. 2015, JATIS 1, 014003 — the TESS mission.
- Giacalone, S. et al. 2021, AJ 161, 24 — TRICERATOPS, statistical validation.
- Nosek, B. A. et al. 2018, PNAS 115, 2600 — the preregistration revolution.

MIT licence.
