Skip to content

Personal PK calibration: per-route levels, absorption speed, MCMC - #306

Merged
SmirnovaOyama merged 2 commits into
mainfrom
claude/project-thread-772trk
Nov 11, 2026
Merged

SmirnovaOyama merged 2 commits into
mainfrom
claude/project-thread-772trk

Conversation

@SmirnovaOyama

Copy link
Copy Markdown
Collaborator

Requested by Аня · project thread

What changed 修改内容

A sixth calibration method, Personal PK / 个人药代 (mcmc), next to Full Bayes. It is not the default.

Instead of "the model times one correction", each person gets their own pharmacokinetics:

E2(t) = Σ_r exp(s + u_r) · C_r(t; k, a)
  • A level per route (injection, oral, sublingual, gel, patch). The E2 model is linear in dose, so splitting it by route is exact: the per-route parts add up to the simulation to 1e-9 (routeCurves.ts, tested). Route offsets u_r are hierarchical around one personal level s, so routes borrow strength from each other. With one route the model is MIPD's plus absorption.
  • A personal absorption speed a: every first-order uptake rate (injection depot release, gut, mucosa, skin) is multiplied together. It moves peaks and troughs, not the average. Zero-order patches are unaffected.
  • A personal clearance k, as in MIPD.
  • Real MCMC: 4 chains of adaptive Metropolis (Haario et al. 2001), proposal covariance adapted during burn-in only, step size tuned to 0.234 acceptance, 2000 kept draws, split R-hat reported. Seeded from the data, so the same labs always give the same curve.
  • The curve is the posterior mean of exactly the model the draws were scored on (bilinear on the same (k, a) grid); factorSdFn gives the forecast band; latestInterval is the 5–95% quantile at the latest lab.
  • Same rules as the other methods for few labs: clearance moves from 3 lab clusters, absorption from 4; Gaussian likelihood below 3 labs, Student-t from there. Per-lab SD is Full Bayes's (now shared as labLogSd).
  • Settings: new card with a hand-drawn samples glyph; the current fit shows absorption speed and a row per route (replacing the single amplitude row when there are several routes). Nine languages. Backup, sync and the backup test vectors accept mcmc.
  • Algorithm Explanation §16, README (both), wiki updated.

Why 原因

Asked for in the project thread: personalised absorption rate, per-route calibration (e.g. sublingual vs injection each with its own bias), and real sampling instead of the Laplace approximation. Per-route levels also give each person their own sublingual factor, which is the open question in #22.

How it was tested 测试方式

  • tests/mcmcCalibration.test.ts (10 tests): per-route curves sum to the simulation; labs made from a known person (sublingual ×0.5, injection ×1.3) are recovered; injection depot release ×2 and ×0.5 are recovered; daily oral absorption barely shows (about 10%, inside lab noise) and the prior keeps most of its say, which is documented; one lab shrinks and pins k and a; an outlier is discounted; forward mode never rewrites the past; deterministic; split R-hat flags disagreeing chains. Whole suite about 6 s.
  • Sync roundtrip test for mcmc.
  • tsc, lint (0 errors), format:check, 915 unit tests and build pass locally.
  • Screenshot of the settings page (zh, light card and dark stats) attached in the project thread.

Not yet validated against real timed labs (#22): the repo has no timed personal lab data. That is why it ships as an option and not the default.

Checklist 自查清单

  • npx tsc --noEmit, npm run lint, npm run format:check, npm test and npm run build pass locally
  • New interface text is in both zh and en (and the other seven)
  • Model changes cite their source
  • Database changes: none
  • No health data leaves the device
  • The version number is unchanged

https://claude.ai/code/session_01PYdxvXGCTGpTnsjZgUDcnD


Generated by Claude Code

A new calibration method next to Full Bayes, not the default yet. Each person
gets their own pharmacokinetics: a level per route (hierarchical around one
personal level), a personal clearance and a personal absorption rate, sampled
with four chains of adaptive Metropolis and checked with split R-hat. The
curve is the posterior mean of exactly the model the draws were scored on.

- routeCurves.ts splits the E2 model by route; the parts add up to the
  simulation to 1e-9, so per-route factors are exact
- mcmc.ts: (clearance, absorption) grid at the lab times, seeded sampler,
  posterior mean curve and band, latest-lab interval, discounted labs
- Full Bayes's per-lab SD moved into labLogSd and shared
- Settings card with a new hand-drawn icon, absorption and per-route rows,
  nine languages; backup and sync accept 'mcmc'
- Algorithm Explanation §16, README, wiki and backup test vectors updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PYdxvXGCTGpTnsjZgUDcnD
Copilot AI balanced review requested due to automatic review settings November 11, 2026 14:06
@SmirnovaOyama SmirnovaOyama self-assigned this Nov 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Copy link
Copy Markdown
Collaborator Author

Browser E2E failed before any test ran: installing Playwright's system fonts hit Temporary failure resolving 'azure.archive.ubuntu.com' on the runner (apt exit 100). That is a runner DNS problem, not this PR's code, so there is nothing to port. I re-ran the failed job once.


Generated by Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://claude-project-thread-772trk-hrt-tracker.oyamasmirnona.workers.dev (commit 9c24f1f)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://fefd2821-hrt-tracker.oyamasmirnona.workers.dev 9c24f1f 2026-11-11T14:39:04.587Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://3c2a3512-hrt-tracker.oyamasmirnona.workers.dev 07ddf95 2026-11-11T14:07:36.300Z Visit the dashboard ↗

- New page one level below calibration settings: a chart you can move
  (method, lab level, a later change, number of labs, one wrong lab),
  computed by computeCalibration on a runSimulation curve, with the 90%
  range where the method gives one
- Each method with a small hand-drawn picture and its formula (KaTeX),
  plus how much each lab counts
- ModelChart can wash a range between two lines
- The Personal PK card uses a pair of dice, as picked
- Nine languages

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PYdxvXGCTGpTnsjZgUDcnD
@SmirnovaOyama
SmirnovaOyama merged commit 86665f2 into main Nov 11, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants