diff --git a/AGENTS.md b/AGENTS.md index f36a503..e0fdb24 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,6 +42,7 @@ src/lib/server/db/ schema.ts (Drizzle) · index.ts (fingerprint, rebuild, src/lib/server/data.ts loads the whole dataset into memory per import; memo() caches analyses src/lib/server/analytics common.ts (CPUE, Fulton's K, recapture pairs…) · env.ts (per-set variables) findings.ts (live findings) · insitu.ts (logger temperature summaries) + population.ts (Schnabel abundance, von Bertalanffy growth) 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 src/lib/stats.ts regression, Spearman, Welch, Wilson, binomial, histograms, box stats diff --git a/src/lib/server/analytics/population.test.ts b/src/lib/server/analytics/population.test.ts new file mode 100644 index 0000000..17577bf --- /dev/null +++ b/src/lib/server/analytics/population.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest'; +import { dataset } from '../data'; +import { poissonCi, schnabel, vonBertalanffy } from './population'; + +describe('population analyses (#43, #44)', () => { + it('gives exact Poisson limits', () => { + // Garwood limits for 0, 3 and 10 events. + expect(poissonCi(0)[0]).toBe(0); + expect(poissonCi(0)[1]).toBeCloseTo(3.689, 2); + expect(poissonCi(3)[0]).toBeCloseTo(0.619, 2); + expect(poissonCi(3)[1]).toBeCloseTo(8.767, 2); + expect(poissonCi(10)[0]).toBeCloseTo(4.795, 2); + expect(poissonCi(10)[1]).toBeCloseTo(18.39, 1); + }); + + it('floors Schnabel estimates at the sharks actually caught', () => { + for (const r of schnabel(dataset(), 1)) { + expect(r.n).toBeGreaterThanOrEqual(r.distinct); + expect(r.lo).toBeLessThanOrEqual(r.n); + expect(r.hi).toBeGreaterThanOrEqual(r.n); + } + }); + + it('returns a growth curve or nothing, never NaN', () => { + const v = vonBertalanffy(dataset()); + if (v) for (const p of v.curve) expect(Number.isFinite(p.y)).toBe(true); + }); +}); diff --git a/src/lib/server/analytics/population.ts b/src/lib/server/analytics/population.ts new file mode 100644 index 0000000..1b7c619 --- /dev/null +++ b/src/lib/server/analytics/population.ts @@ -0,0 +1,195 @@ +/** + * Population analyses from mark–recapture (#43, #44). Exploratory: a nursery is not a closed + * population (pups are born through the season, move and die), so treat the numbers as a rough + * scale, not a census. + */ +import { mean } from '#lib/stats'; +import type { Capture, Dataset } from '../data'; +import { cpueOf, effortSets, fieldCaptures, round } from './common'; + +/** P(X ≤ k) for X ~ Poisson(λ). */ +function poissonCdf(k: number, lambda: number): number { + if (k < 0) return 0; + let term = Math.exp(-lambda); + let sum = term; + for (let i = 1; i <= k; i++) { + term *= lambda / i; + sum += term; + } + return Math.min(1, sum); +} + +/** Exact 95% interval for a Poisson count (Garwood), by bisection. */ +export function poissonCi(k: number): [number, number] { + const solve = (f: (l: number) => number) => { + let lo = 0; + let hi = k * 4 + 20; + for (let i = 0; i < 100; i++) { + const mid = (lo + hi) / 2; + if (f(mid) > 0) lo = mid; + else hi = mid; + } + return (lo + hi) / 2; + }; + const lower = k === 0 ? 0 : solve((l) => (1 - poissonCdf(k - 1, l) - 0.025 < 0 ? 1 : -1)); + const upper = solve((l) => (poissonCdf(k, l) - 0.025 > 0 ? 1 : -1)); + return [lower, upper]; +} + +export interface SchnabelRow { + season: string; + siteId: string; + species: string; + samples: number; + caught: number; + distinct: number; + recaptures: number; + n: number; + lo: number; + hi: number; + cpue: number | null; +} + +/** + * Schnabel estimate (Chapman form, N = ΣCₜMₜ / (R + 1)) per nursery, season and species: each + * set is a sample, marks are individuals already caught there that season. Exact Poisson limits + * on R; estimates are floored at the number of individuals actually caught. + */ +export function schnabel(d: Dataset, minRecaptures = 3): SchnabelRow[] { + const groups = new Map(); + for (const c of fieldCaptures(d)) { + if (!c.setId || !c.individualId || !c.siteId || (c.species !== 'Cm' && c.species !== 'Na')) + continue; + const k = `${c.season}|${c.siteId}|${c.species}`; + const l = groups.get(k); + if (l) l.push(c); + else groups.set(k, [c]); + } + const eff = effortSets(d); + const out: SchnabelRow[] = []; + for (const [k, caps] of groups) { + const [season, siteId, species] = k.split('|'); + const bySet = new Map(); + for (const c of caps) { + const l = bySet.get(c.setId!); + if (l) l.push(c); + else bySet.set(c.setId!, [c]); + } + const samples = [...bySet.values()].sort((a, b) => + (a[0].date ?? '').localeCompare(b[0].date ?? '') + ); + const marked = new Set(); + let sumCM = 0; + let caught = 0; + let recaptures = 0; + for (const s of samples) { + const ids = [...new Set(s.map((c) => c.individualId!))]; + sumCM += ids.length * marked.size; + recaptures += ids.filter((i) => marked.has(i)).length; + caught += ids.length; + for (const i of ids) marked.add(i); + } + if (recaptures < minRecaptures || samples.length < 3) continue; + const [rLo, rHi] = poissonCi(recaptures); + const distinct = marked.size; + const floor = (x: number) => Math.max(distinct, Math.round(x)); + const key = species === 'Cm' ? 'cpueCm' : 'cpueNa'; + out.push({ + season, + siteId, + species, + samples: samples.length, + caught, + distinct, + recaptures, + n: floor(sumCM / (recaptures + 1)), + lo: floor(sumCM / (rHi + 1)), + hi: floor(sumCM / (rLo + 1)), + cpue: round( + cpueOf( + eff.filter((s) => s.season === season && s.siteId === siteId), + key + ) + ) + }); + } + return out.sort((a, b) => a.season.localeCompare(b.season) || a.siteId.localeCompare(b.siteId)); +} + +/** Fabens increment model: ΔL = (L∞ − L₁)(1 − e^(−KΔt)), Δt in years. */ +const fabensSse = (pairs: { l1: number; dl: number; dt: number }[], linf: number, k: number) => + pairs.reduce((s, p) => s + (p.dl - (linf - p.l1) * (1 - Math.exp(-k * p.dt))) ** 2, 0); + +const bestK = (pairs: { l1: number; dl: number; dt: number }[], linf: number) => { + let best = { k: 0, sse: Infinity }; + for (let k = 0.01; k <= 2; k += 0.005) { + const s = fabensSse(pairs, linf, k); + if (s < best.sse) best = { k, sse: s }; + } + return best.k; +}; + +/** + * Von Bertalanffy growth for blacktips: K fitted to recapture increments (Fabens), L∞ from the + * largest adults (mean of the top decile of the adult datasheet) and L₀ from newborns with an + * open umbilical scar. Juvenile increments alone can't pin down L∞, so it comes from the adults. + * 95% interval on K from a bootstrap over recapture pairs (fixed seed, deterministic). + */ +export function vonBertalanffy(d: Dataset) { + const caps = fieldCaptures(d).filter((c) => c.species === 'Cm' && c.individualId && c.tlMm); + const byInd = new Map(); + for (const c of caps) { + const l = byInd.get(c.individualId!); + if (l) l.push(c); + else byInd.set(c.individualId!, [c]); + } + const day = (s: string) => Date.parse(`${s}T00:00:00Z`) / 86_400_000; + const pairs: { l1: number; dl: number; dt: number }[] = []; + for (const l of byInd.values()) { + l.sort((a, b) => (a.date ?? '').localeCompare(b.date ?? '')); + for (let i = 1; i < l.length; i++) { + const a = l[i - 1]; + const b = l[i]; + if (!a.date || !b.date) continue; + const days = day(b.date) - day(a.date); + // A month or more apart, so measurement error doesn't swamp the growth. + if (days >= 30) pairs.push({ l1: a.tlMm!, dl: b.tlMm! - a.tlMm!, dt: days / 365.25 }); + } + } + const adults = d.adults + .map((a) => a.tlMm) + .filter((x): x is number => x != null) + .sort((a, b) => a - b); + const newborn = mean(caps.filter((c) => c.umbilical === 'open').map((c) => c.tlMm)); + if (pairs.length < 20 || adults.length < 10 || newborn == null) return null; + const linf = mean(adults.slice(Math.floor(adults.length * 0.9)))!; + const k = bestK(pairs, linf); + let seed = 1; + const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647; + const ks: number[] = []; + for (let b = 0; b < 200; b++) { + const sample = pairs.map(() => pairs[Math.floor(rand() * pairs.length)]); + ks.push(bestK(sample, linf)); + } + ks.sort((a, b) => a - b); + const kLo = ks[Math.floor(0.025 * ks.length)]; + const kHi = ks[Math.ceil(0.975 * ks.length) - 1]; + const at = (kk: number, t: number) => linf - (linf - newborn) * Math.exp(-kk * t); + const ages = Array.from({ length: 31 }, (_, i) => i * 0.5); + const ageAt = (kk: number, len: number) => + len >= linf ? null : Math.log((linf - newborn) / (linf - len)) / kk; + return { + k: round(k, 3)!, + kLo: round(kLo, 3)!, + kHi: round(kHi, 3)!, + linf: Math.round(linf), + l0: Math.round(newborn), + pairs: pairs.length, + adults: adults.length, + /** Age (years) at 1 m total length on the fitted curve. */ + ageAt1m: round(ageAt(k, 1000), 1), + curve: ages.map((t) => ({ x: t, y: Math.round(at(k, t)) })), + curveLo: ages.map((t) => ({ x: t, y: Math.round(at(kLo, t)) })), + curveHi: ages.map((t) => ({ x: t, y: Math.round(at(kHi, t)) })) + }; +} diff --git a/src/routes/growth/+page.server.ts b/src/routes/growth/+page.server.ts index 90b3757..b1e6a3d 100644 --- a/src/routes/growth/+page.server.ts +++ b/src/routes/growth/+page.server.ts @@ -7,6 +7,7 @@ import { round, siteName } from '#lib/server/analytics/common'; +import { vonBertalanffy } from '#lib/server/analytics/population'; import { histogram, linearFit, mean, median, pairs } from '#lib/stats'; import type { PageServerLoad } from './$types'; @@ -64,6 +65,7 @@ export const load: PageServerLoad = () => const nurseries = activeNurseries(d); return { + vbgf: vonBertalanffy(d), n: within.length, medianCm: round(median(within.filter((g) => g.species === 'Cm').map((g) => g.rate)), 2), medianNa: round(median(within.filter((g) => g.species === 'Na').map((g) => g.rate)), 2), diff --git a/src/routes/growth/+page.svelte b/src/routes/growth/+page.svelte index 4ecda5f..dcf326d 100644 --- a/src/routes/growth/+page.svelte +++ b/src/routes/growth/+page.svelte @@ -3,6 +3,7 @@ import DataTable from '#lib/components/DataTable.svelte'; import BoxPlot from '#lib/components/charts/BoxPlot.svelte'; import Histogram from '#lib/components/charts/Histogram.svelte'; + import LineChart from '#lib/components/charts/LineChart.svelte'; import Scatter from '#lib/components/charts/Scatter.svelte'; import { num, pValue } from '#lib/format'; import { SPECIES } from '#lib/species'; @@ -110,6 +111,40 @@ + {#if data.vbgf} + {@const v = data.vbgf} +
+ + num(x, 0)} + yFormat={(y) => num(y)} + series={[ + { name: 'Fitted curve', color: SPECIES.Cm.color, points: v.curve }, + { + name: 'Slower (K lower limit)', + color: 'var(--ink-2)', + dashed: true, + points: v.curveLo + }, + { + name: 'Faster (K upper limit)', + color: 'var(--ink-2)', + dashed: true, + points: v.curveHi + } + ]} + refs={[{ y: v.linf, label: 'largest adults' }]} + /> + +
+ {/if} + {#if data.acrossSeasons.length}

Sharks seen again in a later season

diff --git a/src/routes/recaptures/+page.server.ts b/src/routes/recaptures/+page.server.ts index efb6814..dcc7baf 100644 --- a/src/routes/recaptures/+page.server.ts +++ b/src/routes/recaptures/+page.server.ts @@ -6,7 +6,8 @@ import { round, siteName } from '#lib/server/analytics/common'; -import { histogram, median } from '#lib/stats'; +import { schnabel } from '#lib/server/analytics/population'; +import { histogram, median, pairs as xy, spearman } from '#lib/stats'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = () => @@ -70,7 +71,20 @@ export const load: PageServerLoad = () => n: d.individuals.filter((i) => (k === 6 ? i.nCaptures >= 6 : i.nCaptures === k)).length })); + // Schnabel estimates per nursery, season and species (#43), against that nursery-season's CPUE. + const est = schnabel(d).map((e) => ({ ...e, site: siteName(d, e.siteId) })); + const rho = (sp: string) => { + const r = spearman( + xy( + est.filter((e) => e.species === sp), + (e) => e.cpue, + (e) => e.n + ) + ); + return r && r.n >= 8 ? { rho: round(r.rho, 2), p: r.p, n: r.n } : null; + }; return { + abundance: { rows: est, cm: rho('Cm'), na: rho('Na') }, bySeason: rateBy((c) => c.season, d.seasons), bySite: rateBy((c) => c.siteId, nurseries).map((r) => ({ ...r, name: siteName(d, r.key) })), bySpecies: rateBy((c) => c.species, ['Cm', 'Na']), diff --git a/src/routes/recaptures/+page.svelte b/src/routes/recaptures/+page.svelte index af6e8a5..4c863eb 100644 --- a/src/routes/recaptures/+page.svelte +++ b/src/routes/recaptures/+page.svelte @@ -5,8 +5,9 @@ import BarChart from '#lib/components/charts/BarChart.svelte'; import Heatmap from '#lib/components/charts/Heatmap.svelte'; import Histogram from '#lib/components/charts/Histogram.svelte'; - import { date, num, pct, season } from '#lib/format'; - import { speciesName } from '#lib/species'; + import Scatter from '#lib/components/charts/Scatter.svelte'; + import { date, num, pct, pValue, season } from '#lib/format'; + import { SPECIES, speciesName } from '#lib/species'; import type { PageProps } from './$types'; let { data }: PageProps = $props(); @@ -144,6 +145,91 @@ />
+ + {#if data.abundance.rows.length} + {@const ab = data.abundance} + {@const top = [...ab.rows].sort((a, b) => b.n - a.n).slice(0, 12)} +
+

How many pups use a nursery?

+

+ Schnabel mark–recapture estimates for each nursery, season and species with at least three + recaptures that season. Each set is a sample; a shark caught earlier the same season at the + same nursery counts as marked. A nursery is not a closed population (pups are born through + the season, move and die), so read these as a rough scale, not a census. +

+
+ + r.cpue != null) + .map((r) => ({ + x: r.cpue!, + y: r.n, + g: speciesName(r.species), + label: `${r.site} ${season(r.season)}: ${num(r.n)} (${num(r.lo)}–${num(r.hi)})` + }))} + groups={[ + { name: SPECIES.Cm.common, color: SPECIES.Cm.color }, + { name: SPECIES.Na.common, color: SPECIES.Na.color } + ]} + xLabel="Sharks / net-hour (that species)" + yLabel="Estimated pups" + xFormat={(v) => num(v, 2)} + yFormat={(v) => num(v)} + /> + + b.n - a.n) + .map((r) => ({ + ...r, + seasonL: season(r.season), + sp: speciesName(r.species), + ci: `${num(r.lo)}–${num(r.hi)}` + })) + }} + > + `${r.site} ${season(r.season)} (${r.species === 'Cm' ? 'blacktip' : 'lemon'})` + )} + series={[ + { name: 'Estimated pups', color: 'var(--series-4)', values: top.map((r) => r.n) } + ]} + errors={top.map((r) => [r.lo, r.hi] as [number, number])} + valueFormat={(v) => num(v)} + /> + +
+
+ {/if}