Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ src/lib/server/analytics common.ts (CPUE, Fulton's K, recapture pairs…) · env
events.ts (cyclones and heavy-rain days vs catches)
glm.ts (NB/Poisson GLM, splines) · catchModel.ts (joint catch model)
glmm.ts (binomial mixed model, crossed random intercepts) · sex.ts
survival.ts (Cormack–Jolly–Seber apparent survival, BFGS)
tests.ts (the statistic behind each chart, worded for ChartCard stats)
src/lib/server/astro.ts sunset/twilight/moon (local formulas) · tides.ts (extrema, interpolation)
src/lib/server/holidays.ts French Polynesia public holidays (Easter-based too), festive window,
Expand Down Expand Up @@ -108,6 +109,12 @@ samples/fixture/ synthetic workbooks for tests/CI (committed)
- `analytics/sex.ts`: female (1) vs male (0) per individual, counted once at its first capture, with the nursery and season of that capture as crossed random intercepts (`glmm.ts`: Laplace approximation, PIRLS as lme4's nAGQ = 0, SDs by Nelder–Mead). Tests fix one SD at 0 and halve the χ²₁ p (boundary).
- Reuse `fitBinomialGlmm` for any yes/no outcome with grouping (fed or empty, recaptured or not) instead of pooling groups; there is no R in CI, so validate new uses against simulated data as `glmm.test.ts` does.

### Apparent survival (#42)

- `analytics/survival.ts`: Cormack–Jolly–Seber per species on 14-day blocks of the season. One history per pup, nursery and season, starting at its first capture there, so pups born later don't bias it. φ is shared, or one per nursery for the nursery test. Capture probability is cloglog(aₙ + β·log net-hours) for that fortnight, with an intercept per nursery and p = 0 in fortnights with no set. Untimed sets count as a typical set, and a catch in a fortnight with no set on record counts as one.
- φ is apparent: death and leaving look the same, and moving to another nursery counts as leaving. Nurseries need ≥ 10 pups seen again in a later fortnight of the same season. The transience model gives the first fortnight after tagging its own φ.
- `fitCjs` is generic and validated on simulated histories in `survival.test.ts`. Reuse it rather than writing another fitter. Wald intervals are on the logit scale and are null at the boundary (φ ≈ 1).

### School holidays (#25)

- `fetch-external.mjs --only=school` writes `external/school-holidays.csv` (first and last day off, local dates). 2013-14 to 2016-17 are transcribed in the script from the Polynesian government's calendar orders for collèges and lycées (arrêtés 0270/CM 2013, 0399, 0401 and 0403/CM 2014), archived from education.pf by the Wayback Machine; the national dataset only starts in 2017-18. From 2017-18 it is the Ministry's `fr-en-calendrier-scolaire` open data (Polynésie, Licence Ouverte 2.0, credit it): pupils only, a day counts when either level is off, `end_date` is the day classes resume.
Expand Down
96 changes: 96 additions & 0 deletions src/lib/server/analytics/survival.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, expect, it } from 'vitest';
import { dataset } from '../data';
import { fitCjs, survival, type CjsGroup, type CjsHistory } from './survival';

/** Seeded uniform draws (mulberry32). */
function rng(seed: number) {
let a = seed;
return () => {
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}

const expit = (v: number) => 1 / (1 + Math.exp(-v));

/**
* Pups arrive through the season and stay each fortnight with chance φ[nursery]; a fortnight
* with h net-hours catches a pup that is there with p = 1 − exp(−q·h). `transient` pups leave
* after their first fortnight whatever φ is. Pups never caught don't enter a CJS data set.
*/
function simulate(seed: number, phi: number[], q: number[], transient = 0) {
const u = rng(seed);
const groups: CjsGroup[] = [];
const histories: CjsHistory[] = [];
for (let n = 0; n < phi.length; n++)
for (let season = 0; season < 8; season++) {
const hours = Array.from({ length: 12 }, () => (u() < 0.2 ? 0 : 1.5 + 2.5 * u()));
const g = groups.length;
groups.push({ nursery: n, hours });
for (let pup = 0; pup < 70; pup++) {
const leavesEarly = u() < transient;
const seen: number[] = [];
let firstSeen = -1;
for (let t = Math.floor(u() * 8); t < 12; t++) {
if (hours[t] > 0 && u() < 1 - Math.exp(-q[n] * hours[t])) {
seen.push(t);
if (firstSeen < 0) firstSeen = t;
}
if (leavesEarly && firstSeen >= 0) break;
if (u() > phi[n]) break;
}
if (seen.length) histories.push({ group: g, seen });
}
}
return { groups, histories };
}

describe('Cormack–Jolly–Seber apparent survival (#42)', () => {
it('recovers a shared φ and the effort slope', () => {
const { groups, histories } = simulate(1, [0.8, 0.8, 0.8], [0.25, 0.15, 0.35]);
const fit = fitCjs(groups, histories, 3)!;
expect(expit(fit.theta[0])).toBeGreaterThan(0.74);
expect(expit(fit.theta[0])).toBeLessThan(0.86);
// p = 1 − exp(−q·h) is the cloglog model with β = 1 and aₙ = log qₙ.
expect(fit.theta[4]).toBeGreaterThan(0.6);
expect(fit.theta[4]).toBeLessThan(1.4);
expect(fit.theta[3]).toBeCloseTo(Math.log(0.35), 0);
expect(fit.se[0]).not.toBeNull();
});

it('tells nurseries apart when φ differs, and not when it does not', () => {
const diff = simulate(2, [0.9, 0.6], [0.25, 0.25]);
const shared = fitCjs(diff.groups, diff.histories, 2)!;
const each = fitCjs(diff.groups, diff.histories, 2, { nurseryPhi: true })!;
expect(expit(each.theta[0])).toBeCloseTo(0.9, 1);
expect(expit(each.theta[1])).toBeCloseTo(0.6, 1);
expect(2 * (each.loglik - shared.loglik)).toBeGreaterThan(10);

const same = simulate(3, [0.8, 0.8], [0.25, 0.25]);
const s0 = fitCjs(same.groups, same.histories, 2)!;
const s1 = fitCjs(same.groups, same.histories, 2, { nurseryPhi: true })!;
expect(2 * (s1.loglik - s0.loglik)).toBeLessThan(6.6); // χ²₁ at p = 0.01
});

it('flags transients as a lower φ in the first fortnight', () => {
const { groups, histories } = simulate(4, [0.85, 0.85], [0.3, 0.3], 0.4);
const shared = fitCjs(groups, histories, 2)!;
const trans = fitCjs(groups, histories, 2, { transience: true })!;
expect(trans.theta[1]).toBeLessThan(0);
expect(expit(trans.theta[0])).toBeCloseTo(0.85, 1);
expect(2 * (trans.loglik - shared.loglik)).toBeGreaterThan(6.6);
});

it('runs on the synthetic fixture without NaN', () => {
for (const sp of ['Cm', 'Na'] as const) {
const r = survival(dataset(), sp);
if (!r) continue;
for (const e of [r.phi, r.month, ...r.nurseries.map((n) => n.phi)]) {
expect(e.est).toBeGreaterThanOrEqual(0);
expect(e.est).toBeLessThanOrEqual(1);
}
}
});
});
Loading
Loading