Skip to content

Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.

Add analytical-method-validation skill

Timothy Kassis844f4a35ccaa
diff --git a/README.md b/README.md
index 6ab61e5a..430aa441 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE.md)
[![Version](https://img.shields.io/badge/Version-2.57.0-blue.svg)](pyproject.toml)
-[![Skills](https://img.shields.io/badge/Skills-155-brightgreen.svg)](#-whats-included)
+[![Skills](https://img.shields.io/badge/Skills-156-brightgreen.svg)](#-whats-included)
[![Databases](https://img.shields.io/badge/Databases-100%2B-orange.svg)](#-whats-included)
[![Agent Skills](https://img.shields.io/badge/Standard-Agent_Skills-blueviolet.svg)](https://agentskills.io/)
[![Security Scan](https://github.com/K-Dense-AI/scientific-agent-skills/actions/workflows/security-scan.yml/badge.svg)](https://github.com/K-Dense-AI/scientific-agent-skills/actions/workflows/security-scan.yml)
@@ -23,11 +23,11 @@
> **🔔 Claude Scientific Skills is now Scientific Agent Skills.** Same skills, broader compatibility — now works with any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, not just Claude.
-> **New: [K-Dense BYOK](https://github.com/K-Dense-AI/k-dense-byok)** — A free, open-source AI co-scientist that runs on your desktop, powered by Scientific Agent Skills. Bring your own API keys, pick from 40+ models, and get a full research workspace with web search, file handling, 100+ scientific databases, and access to all 155 skills in this repo. Your data stays on your computer, and you can optionally scale to cloud compute via [Modal](https://modal.com/) for heavy workloads. [Get started here.](https://github.com/K-Dense-AI/k-dense-byok)
+> **New: [K-Dense BYOK](https://github.com/K-Dense-AI/k-dense-byok)** — A free, open-source AI co-scientist that runs on your desktop, powered by Scientific Agent Skills. Bring your own API keys, pick from 40+ models, and get a full research workspace with web search, file handling, 100+ scientific databases, and access to all 156 skills in this repo. Your data stays on your computer, and you can optionally scale to cloud compute via [Modal](https://modal.com/) for heavy workloads. [Get started here.](https://github.com/K-Dense-AI/k-dense-byok)
> **Stay up to date:** Follow K-Dense on [X](https://x.com/k_dense_ai), [LinkedIn](https://www.linkedin.com/company/k-dense-inc), and [YouTube](https://www.youtube.com/@K-Dense-Inc) for new skills, release announcements, walkthroughs, research workflow demos, and examples you can use with your own AI agent.
-A comprehensive collection of **155 ready-to-use scientific and research skills** (covering cancer genomics, individual-level 1000 Genomes queries, hosted regulatory-sequence prediction, drug-target binding, molecular dynamics, RNA velocity, geospatial science, time series forecasting, scientific ML resource discovery via Hugging Science, 78+ scientific databases, and more) for any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, created by [K-Dense](https://k-dense.ai). Works with **Cursor, Claude Code, Codex, Google Antigravity, and more**. Transform your AI agent into a research assistant capable of executing complex multi-step scientific workflows across biology, chemistry, medicine, and beyond.
+A comprehensive collection of **156 ready-to-use scientific and research skills** (covering cancer genomics, individual-level 1000 Genomes queries, hosted regulatory-sequence prediction, drug-target binding, molecular dynamics, RNA velocity, geospatial science, time series forecasting, scientific ML resource discovery via Hugging Science, 78+ scientific databases, and more) for any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, created by [K-Dense](https://k-dense.ai). Works with **Cursor, Claude Code, Codex, Google Antigravity, and more**. Transform your AI agent into a research assistant capable of executing complex multi-step scientific workflows across biology, chemistry, medicine, and beyond.
> ⭐ **Help make AI for science easier to discover:** If Scientific Agent Skills saves you time, teaches your agent a workflow, or helps your lab move faster, please [star this repository](https://github.com/K-Dense-AI/scientific-agent-skills). A star is a public signal that these open, reusable research skills are worth maintaining: it helps scientists, engineers, and open-source contributors find the project, shows which agent-skill standards are gaining real adoption, and gives us a clear reason to keep expanding the collection for the community.
@@ -61,13 +61,13 @@ These skills enable your AI agent to seamlessly work with specialized scientific
## 📦 What's Included
-This repository provides **155 scientific and research skills** organized into the following categories:
+This repository provides **156 scientific and research skills** organized into the following categories:
- **100+ Scientific & Financial Databases** - A unified database-lookup skill provides deterministic, provenance-rich access to 78 public databases (PubChem, ChEMBL, UniProt, COSMIC, ClinicalTrials.gov, FRED, USPTO, and more), plus dedicated skills for DepMap, Imaging Data Commons, PrimeKG, U.S. Treasury Fiscal Data, Hugging Science, OneKGPd, and Genomic Intelligence. Multi-database packages like BioServices (~40 bioinformatics services), BioPython (39 NCBI sub-databases via Entrez), and gget (20+ genomics databases) add further coverage
- **70+ Optimized Python Package Skills** - Explicitly defined, version-aware workflows for RDKit, Scanpy, PyTorch Lightning, scikit-learn, PyTDC, PathML, pydicom, NeuroKit2, PufferLib, QuTiP, GeoPandas, pymatgen, BioPython, Qiskit, Molecular Dynamics (OpenMM/MDAnalysis), and others. The agent can still use *any* Python package; these skills provide stronger, safer guidance for the packages listed
- **9 Scientific Integration Skills** - Explicitly defined skills for Benchling, DNAnexus, LatchBio, OMERO, Protocols.io, Open Notebook, Ginkgo Cloud Lab, LabArchives, and Opentrons. Again, the agent is not limited to these — any API or platform reachable from Python is fair game; these skills are the optimized, pre-documented paths
- **30+ Analysis & Communication Tools** - Literature review, evidence-traceable scientific writing, confidential peer review, document processing, Paperzilla, Exa Search, macro-free PPTX posters, slides, schematics, infographics, Mermaid diagrams, and more
-- **10+ Research & Clinical Tools** - Evidence-bounded hypothesis generation, grant writing, aggregate clinical decision-support research, clinician-authored treatment-plan formatting, BIDS, ISO standards-readiness evidence preparation (ISO 13485, ISO 14971, ISO/IEC 17025, ISO 15189), scenario analysis, and workflow-derived skill drafting with Autoskill
+- **10+ Research & Clinical Tools** - Evidence-bounded hypothesis generation, grant writing, aggregate clinical decision-support research, clinician-authored treatment-plan formatting, BIDS, ISO standards-readiness evidence preparation (ISO 13485, ISO 14971, ISO/IEC 17025, ISO 15189), analytical method validation and transfer (ICH Q2(R2)/Q14, ICH M10, USP, CLSI EP), scenario analysis, and workflow-derived skill drafting with Autoskill
Each skill includes:
- ✅ Comprehensive documentation (`SKILL.md`)
@@ -107,7 +107,7 @@ Each skill includes:
- **Multi-Step Workflows** - Execute complex pipelines with a single prompt
### 🎯 **Comprehensive Coverage**
-- **155 Skills** - Extensive coverage across all major scientific domains
+- **156 Skills** - Extensive coverage across all major scientific domains
- **100+ Databases** - Unified access to 78+ databases via database-lookup, plus dedicated data access skills and multi-database packages like BioServices, BioPython, and gget
- **70+ Optimized Python Package Skills** - Current, version-scoped guidance for packages including RDKit, Scanpy, PyTorch Lightning, scikit-learn, PyTDC, pydicom, PufferLib, QuTiP, GeoPandas, pymatgen, Qiskit, Molecular Dynamics (OpenMM/MDAnalysis), scVelo, and TimesFM (the agent can use any Python package; these are the pre-documented paths)
@@ -192,7 +192,7 @@ For Hermes versions that support skill taps, add the repository as a tap:
hermes skills tap add K-Dense-AI/scientific-agent-skills
```
-Every `SKILL.md` has YAML frontmatter, but legacy and community skills vary in `metadata` formatting (block or flow style) and optional extension fields. Repository updates must keep `metadata.version` as a quoted numeric string and pass canonical `skills-ref validate ./skills/<skill-name>` checks. Hosts may interpret optional metadata and credential prompts differently, so verify behavior on the target host. Because 155 skills add up to a lot of standing context, consider installing a topical subset rather than the whole collection.
+Every `SKILL.md` has YAML frontmatter, but legacy and community skills vary in `metadata` formatting (block or flow style) and optional extension fields. Repository updates must keep `metadata.version` as a quoted numeric string and pass canonical `skills-ref validate ./skills/<skill-name>` checks. Hosts may interpret optional metadata and credential prompts differently, so verify behavior on the target host. Because 156 skills add up to a lot of standing context, consider installing a topical subset rather than the whole collection.
> **NemoClaw note:** NemoClaw runs agents inside NVIDIA OpenShell with default-deny outbound networking. Skills are discovered and loaded normally, but any skill that needs the network — package installs via `uv`, or API calls (Exa, Parallel, Benchling, NCBI, Materials Project, …) — only works once the operator pre-approves the relevant domains in the OpenShell TUI.
@@ -428,7 +428,7 @@ networks, and search GEO for similar patterns.
## 📚 Available Skills
-This repository contains **155 scientific and research skills** organized across multiple domains. Each skill provides comprehensive documentation, code examples, and best practices for working with scientific libraries, databases, and tools.
+This repository contains **156 scientific and research skills** organized across multiple domains. Each skill provides comprehensive documentation, code examples, and best practices for working with scientific libraries, databases, and tools.
### Skill Categories
@@ -801,7 +801,7 @@ Recommended practice:
title = {Scientific Agent Skills: A Comprehensive Collection of Scientific Tools for AI Agents},
year = {2026},
url = {https://github.com/K-Dense-AI/scientific-agent-skills},
- note = {155 skills covering databases, packages, integrations, and analysis tools}
+ note = {156 skills covering databases, packages, integrations, and analysis tools}
}
```
diff --git a/docs/examples.md b/docs/examples.md
index 809c2f13..45a83c53 100644
--- a/docs/examples.md
+++ b/docs/examples.md
@@ -5367,6 +5367,95 @@ Expected Output:
---
+### Example 36b: Validating a Stability-Indicating Impurity Method, and Transferring It
+
+**Objective**: Design a validation study for an HPLC related-substances procedure under ICH Q2(R2), evaluate the resulting data with the statistics that actually test the claims, then transfer the procedure to a second site on an equivalence basis. The workflow does not conclude that the procedure is validated — that decision belongs to the analyst and the quality unit.
+
+**Disciplines**: analytical chemistry · pharmaceutical quality control · applied statistics · regulatory documentation
+
+**Skills Used**:
+- `analytical-method-validation` - Framework selection, protocol, and the validation statistics
+- `statistical-analysis` - Supporting diagnostics and assumption checks
+- `scientific-visualization` - Residual plots, recovery plots, Bland-Altman and difference plots
+- `xlsx` - Raw-data and traceability tables
+- `docx` - Formatted protocol and report deliverables
+
+**Starting prompt**:
+
+```text
+Use the analytical-method-validation, statistical-analysis,
+scientific-visualization, xlsx, and docx skills.
+
+Goal: a validation protocol and report for an HPLC related-substances procedure,
+plus a transfer assessment to our second site.
+Context: impurity specification 0.15%, reporting threshold 0.05%, three
+specified impurities, stability-indicating claim required.
+Criteria: state every acceptance criterion before any data is evaluated, and say
+where each one comes from. Use the framework that actually governs and name it.
+Deliver: protocol, evaluated data with the diagnostics that test the model
+(not just r-squared), a transfer equivalence assessment, and a report with raw
+data traceability.
+Report: list anything the data do not support, plainly.
+Do not: declare the procedure validated, set criteria after seeing results,
+reproduce paywalled USP or CLSI text, or invent a threshold from memory.
+```
+
+**Workflow**:
+
+```text
+Step 1: Framework and required characteristics
+- python3 plan_validation.py --framework ich-q2r2 --attribute impurity \
+ --technique hplc --range-use impurity-quantitative
+- Confirm the attribute drives the requirement: a quantitative impurity test needs
+ specificity, response, QL, accuracy, repeatability, and intermediate precision
+- Note that robustness belongs to development under ICH Q14, not to this protocol
+
+Step 2: Protocol with criteria fixed in advance
+- python3 plan_validation.py --framework ich-q2r2 --attribute impurity --protocol
+- Derive each criterion from the 0.15% specification and the 0.05% reporting
+ threshold, and record the derivation next to the number
+- Fix the calibration model and any weighting now, not after seeing the residuals
+
+Step 3: Response across the reportable range
+- python3 check_response.py -i calibration.csv --max-back-calc-error 5 \
+ --weight 1/x
+- Read the lack-of-fit F test and the residual pattern, not the r-squared
+- If the low end is biased, that is the reporting-threshold region — fix the model
+
+Step 4: Accuracy and precision
+- python3 check_accuracy_precision.py -i ap.csv --accuracy-limit 10 \
+ --rsd-limit 5 --design-check impurity
+- Compare repeatability against intermediate precision: if the between-day
+ component dominates, routine performance is the larger number
+- Report recovery with its confidence interval, per Q2(R2) 3.3.1.4
+
+Step 5: Quantitation limit against the reporting threshold
+- python3 check_detection_limits.py --calibration lowrange.csv --blanks blanks.csv \
+ --confirm-ql 0.05 --confirm-data ql_check.csv --reporting-threshold 0.05
+- Name the approach used, and confirm the estimate with real determinations
+- The QL must be at or below 0.05%
+
+Step 6: Transfer to the second site
+- python3 compare_methods.py -i paired.csv --margin 10 --relative \
+ --slope-tolerance 0.10
+- Pre-state the equivalence margin from the specification; TOST, not a t test
+- Deming and Passing-Bablok rather than ordinary least squares, because both
+ sites' results carry error
+
+Step 7: Report and traceability
+- Fill assets/validation-report-template.md; every number traces to raw data
+- Include out-of-criteria individual results rather than dropping them
+- Route to the technical reviewer and quality unit for the actual decision
+```
+
+Expected Output:
+- Validation protocol with pre-stated, derived acceptance criteria
+- Evaluated data with model diagnostics, variance components, and named DL/QL approach
+- Transfer equivalence assessment against a pre-stated margin
+- Validation report with raw-data traceability, and an explicit list of what the data do not support
+
+---
+
## Scientific Communication & Tooling
### Example 37: Publication Packaging — Diagrams, Infographics, and Venue Formatting
@@ -5659,7 +5748,7 @@ OpenAlex, Crossref, Semantic Scholar, CORE, Unpaywall)
**Clinical & regulatory documentation** — all bounded, none clinical decision-making
`clinical-reports` · `clinical-decision-support` · `treatment-plans` · `pyhealth` ·
-`iso-standards-readiness`
+`iso-standards-readiness` · `analytical-method-validation`
**Tooling**
`autoskill` · `pi-agent`
diff --git a/docs/skills.md b/docs/skills.md
index a45534b9..f8987dc6 100644
--- a/docs/skills.md
+++ b/docs/skills.md
@@ -202,6 +202,7 @@
### Regulatory & Standards Evidence Preparation
- **[ISO Standards Readiness](../skills/iso-standards-readiness/)** - Prepare draft scope, controlled-document scaffolds, traceability, and local evidence manifests for ISO 13485 (medical device QMS), ISO 14971 (device risk management), ISO/IEC 17025 (testing and calibration laboratories), and ISO 15189 (medical laboratories), while separating ISO certification from laboratory accreditation, FDA QMSR inspection, CLIA certification, MDSAP, and EU MDR/IVDR evidence boundaries. Per-standard process domains, scope vocabulary, and reference files are selected with a `--standard` profile. Bundled Python 3.11+ standard-library checks use bounded local JSON/Markdown only and make no network calls. The skill does not reproduce the standards, perform an audit or assessment, determine applicability or compliance, certify a QMS, accredit a laboratory, validate a certificate, or promise an outcome; qualified RA/QA, legal, management, laboratory director, assessor, accreditation-body, and certification-body review is required
+- **[Analytical Method Validation](../skills/analytical-method-validation/)** - Plan, execute, and document validation, verification, and transfer of analytical procedures under whichever framework governs: ICH Q2(R2) with Q14, ICH M10 for bioanalysis, USP `<1220>`/`<1225>`/`<1226>`, the CLSI EP series, or ISO/IEC 17025. Encodes the ICH guidelines directly from their openly licensed text — Q2(R2)'s restructured characteristics (range as the parent of response and lower range limits), Table 1 tests by measured attribute, Table 2 reportable ranges, the recommended data minima (5 calibration levels; 9 determinations or 6 at 100% for repeatability), and the 30 Nov 2023 error correction — while USP, CLSI, and ISO documents are cited by designation and scope only because they are paywalled, never reproduced or reconstructed. ICH M10's chromatographic and ligand-binding-assay criteria are kept strictly separate, including the LBA-only total-error limit and the differing ISR tolerances, since conflating them is the most common error in bioanalysis. Six bundled Python 3.11+ standard-library scripts (no numpy or network) do the statistics that actually decide fitness for purpose rather than the ones that look reassuring: `plan_validation.py` selects the framework and emits a protocol whose acceptance criteria must be pre-stated; `check_response.py` tests a calibration model by lack-of-fit F against pure error, residual runs, back-calculated relative error per level, and a heteroscedasticity check for weighting, because r-squared rises with range and misses curvature; `check_accuracy_precision.py` reports recovery with confidence intervals and decomposes precision per level into repeatability and intermediate precision by variance components, exposing between-day variability that a pooled SD hides; `check_detection_limits.py` computes DL and QL by every approach Q2(R2) allows and compares them against the reporting threshold; `check_bioanalytical_run.py` applies M10 run acceptance including the per-level QC rule a passing overall fraction can mask; and `compare_methods.py` uses Deming and Passing-Bablok regression plus TOST equivalence against a pre-stated margin, because a non-significant t test is not evidence of equivalence. Use cases: designing a validation study, evaluating validation data, verifying a compendial procedure, method transfer and co-validation, and OOS investigation support. The skill reports and computes; it does not decide that a procedure is validated, accept or reject a run, or replace the analyst, technical reviewer, quality unit, or regulator
## Scientific Thinking & Analysis
diff --git a/pyproject.toml b/pyproject.toml
index 30d65c78..ba2cc798 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "scientific-agent-skills"
-version = "2.58.0"
+version = "2.59.0"
description = "A set of ready to use Agent Skills for research, science, engineering, analysis, finance and writing."
readme = "README.md"
requires-python = ">=3.13"
diff --git a/skills/analytical-method-validation/SKILL.md b/skills/analytical-method-validation/SKILL.md
new file mode 100644
index 00000000..de386c1b
--- /dev/null
+++ b/skills/analytical-method-validation/SKILL.md
@@ -0,0 +1,299 @@
+---
+name: analytical-method-validation
+description: Plan, execute, and document validation, verification, and transfer of analytical procedures under the governing framework - ICH Q2(R2) and Q14, USP <1220>/<1225>/<1226>, ICH M10 bioanalytical, CLSI EP, or ISO/IEC 17025. Use for HPLC, LC-MS/MS, GC, CE, ICP-MS, dissolution, qNMR, qPCR, NIR, and ligand binding or cell-based assays whenever the question is whether a procedure is fit for its intended purpose. Triggers include "method validation", "analytical method validation", "AMV", "validation protocol", "acceptance criteria", "linearity", "reportable range", "accuracy and precision", "repeatability", "intermediate precision", "recovery", "LOD", "LOQ", "detection limit", "quantitation limit", "specificity", "robustness", "method transfer", "method comparison", "Deming", "Passing-Bablok", "Bland-Altman", "equivalence testing", "OOS investigation", "ICH Q2", "Q2(R2)", "Q14", "USP 1225", "ICH M10", "incurred sample reanalysis", "ISR", "CLSI EP", and any request to show that an assay works.
+license: MIT
+compatibility: Requires Python 3.11+. Scripts use only the standard library - no numpy, scipy, or network access. Statistical distributions are computed from first principles so results are reproducible in any conforming interpreter.
+allowed-tools: Read Write Edit Bash
+metadata:
+ version: "1.0"
+ skill-author: K-Dense Inc.
+ last-reviewed: "2026-07-27"
+---
+
+# Analytical Method Validation
+
+## When to use
+
+Any time the question is whether an analytical procedure is fit for its intended purpose:
+designing a validation study, evaluating validation data, verifying a compendial procedure,
+transferring a procedure to another laboratory or instrument, or defending any of these in a
+report.
+
+## The two rules
+
+**1. Establish which framework governs before designing anything.** The same assay validates
+differently under ICH Q2(R2), USP <1225>, ICH M10, CLSI EP, and ISO/IEC 17025. They differ in
+which characteristics are required, how the studies are laid out, and whether numeric acceptance
+criteria are supplied at all. Blending them produces a protocol that satisfies none of them.
+
+**2. State acceptance criteria before collecting data.** Criteria chosen after seeing results are
+not acceptance criteria, and deciding them post hoc is a standing audit finding. ICH Q2(R2)
+deliberately supplies almost no numeric criteria — they have to come from the specification, the
+analytical target profile (ICH Q14 section 3), or development data. ICH M10 is the exception: it
+supplies explicit numbers, and they differ between chromatographic assays and ligand binding
+assays.
+
+## Scope
+
+This skill plans studies, computes the statistics correctly, and structures the documentation. It
+does **not** decide that a procedure is validated, release a batch, accept or reject a run, close
+an investigation, or substitute for the analyst, the technical reviewer, the quality unit, or the
+regulator. Every script reports; none of them concludes.
+
+## Copyright boundary
+
+ICH guidelines are published openly and licensed for reuse with acknowledgement, so their
+requirements are encoded directly in this skill. **USP general chapters, CLSI EP documents, and
+ISO standards are copyrighted and paywalled.** For those, this skill supplies the designation,
+scope, and where to obtain an authorised copy — never the text, never invented thresholds. Do not
+ask an agent to retrieve, transcribe, or reconstruct their content. If a number matters and it
+lives in a paywalled document, read it from the authorised copy.
+
+## Frameworks
+
+```bash
+cd skills/analytical-method-validation/scripts
+python3 plan_validation.py --list-frameworks
+```
+
+| Key | Governs | Numeric criteria supplied |
+| --- | --- | --- |
+| `ich-q2r2` | Release and stability testing of drug substances and products | Almost none — you derive them |
+| `ich-m10` | Bioanalytical concentration measurement (PK, TK, BE) | Yes, and they differ by modality |
+| `usp-1220` | Compendial procedure lifecycle, three stages | Paywalled |
+| `usp-1225` / `usp-1226` | Validation / verification of compendial procedures | Paywalled |
+| `clsi` | Clinical laboratory measurement procedures (EP series) | Paywalled |
+| `iso-17025` | Lab-developed and modified methods under accreditation | No — "to the extent necessary" |
+
+**Q2(R2) replaced Q2(R1) in November 2023 and restructured the characteristics.** Range is now
+the parent characteristic (section 3.2), containing *response* (linearity) and *validation of
+lower range limits* (DL/QL). Accuracy and precision are section 3.3 and may be evaluated in
+combination against a single criterion. Robustness is treated as a development activity and
+cross-refers to ICH Q14. Multivariate procedures are addressed explicitly (2.5 and 3.2.2.3), and
+Annex 2 adds worked examples for techniques Q2(R1) never covered — quantitative ¹H-NMR, NIR,
+quantitative LC/MS, qPCR, biological assays, and particle size. A Q2(R1)-shaped protocol — a flat
+list of linearity, range, accuracy, precision, specificity, LOD, LOQ, robustness — is out of date.
+Note also the error correction dated 30 November 2023 to Table 5 and Tables 6–11.
+
+## Scripts
+
+```bash
+cd skills/analytical-method-validation/scripts
+```
+
+| Script | Question answered |
+| --- | --- |
+| `plan_validation.py` | Which framework, which characteristics, what study layout, what protocol? |
+| `check_response.py` | Does the calibration model actually hold across the range? |
+| `check_accuracy_precision.py` | What is the recovery, and how much of the variability is between days? |
+| `check_detection_limits.py` | What are DL and QL by each allowed approach, and do they serve the reporting threshold? |
+| `check_bioanalytical_run.py` | Does this run meet ICH M10 for its modality? |
+| `compare_methods.py` | Are two procedures equivalent, at a pre-stated margin? |
+
+All take `--format table|tsv|json`. Provenance, guideline citations, and caveats go to stderr;
+data goes to stdout, so `> out.tsv` keeps them separate. Exit code is `0` for no findings, `1`
+when findings were raised, `2` for bad input — so any of them can gate a workflow.
+
+## Workflow
+
+### 1. Fix the framework and the required characteristics
+
+```bash
+python3 plan_validation.py --framework ich-q2r2 --attribute assay --technique hplc --range-use assay
+```
+
+Q2(R2) Table 1 decides what is required from the *measured attribute*, not from the technique. For
+an assay: specificity, response, accuracy, repeatability, intermediate precision. For a limit
+test: specificity and DL only. For an identity test: specificity alone. Attributes accepted include
+`assay`, `impurity` (quantitative), `impurity-limit`, and `identity`.
+
+Reportable range comes from the specification. Q2(R2) Table 2 gives worked examples — 80–120% of
+declared content for an assay, 70–130% for content uniformity, reporting threshold to 120% of the
+specification for an impurity.
+
+### 2. Generate the protocol and fill in the criteria
+
+```bash
+python3 plan_validation.py --framework ich-q2r2 --attribute impurity --protocol > protocol.md
+```
+
+Every bracketed field is a decision to make and record *before* data collection. The protocol
+skeleton deliberately refuses to pre-fill acceptance criteria for Q2(R2) work, because there is no
+defensible default.
+
+### 3. Evaluate the response
+
+```bash
+python3 check_response.py -i calibration.csv --max-back-calc-error 2
+```
+
+Input is `level,response`, one row per injection; repeated rows at the same level are replicates,
+and supplying them is what makes the linearity test possible.
+
+Real output from a curve that a coefficient of determination would wave through:
+
+```
+statistic value
+distinct levels 5
+slope 166.6000
+intercept 2495.0000
+intercept CI includes 0 no
+coefficient of determination (r2) 0.9830
+lack-of-fit F 469.5294
+lack-of-fit p 1.5139e-06
+runs test p 0.0492
+
+level n mean_response mean_back_calculated relative_error_pct
+50.0000 2 10075.0000 45.4982 -9.0036
+75.0000 2 15150.0000 75.9604 1.2805
+100.0000 2 20050.0000 105.3721 5.3721
+125.0000 2 24050.0000 129.3818 3.5054
+150.0000 2 26450.0000 143.7875 -4.1417
+```
+
+r² = 0.983 and the model is unusable: −9.0% back-calculated error at the bottom of the range,
+lack-of-fit p = 1.5 × 10⁻⁶, non-random residual signs. **r² is not evidence of linearity** — it
+rises with range and is nearly insensitive to curvature. The lack-of-fit F test against pure error
+and the residual pattern are the evidence, which is why Q2(R2) 3.2.2.1 asks for an analysis of the
+deviation of points from the line rather than a correlation coefficient alone.
+
+Add `--weight 1/x2` for a wide-range curve. The script flags heteroscedasticity when the residual
+variance in the top third of the range exceeds the bottom third by more than 10×, because an
+unweighted fit then biases exactly the low end where a reporting threshold lives.
+
+### 4. Evaluate accuracy and precision
+
+```bash
+python3 check_accuracy_precision.py -i ap.csv --accuracy-limit 2 --rsd-limit 1.0 --design-check assay
+```
+
+Input is `level,measured,group`, where `group` is the intermediate-precision factor — day, analyst,
+or instrument.
+
+```
+level component sd rsd_pct df ci90_low_sd ci90_high_sd
+100 repeatability (within group) 0.0707 0.0707 3 0.0438 0.2065
+100 between-group 1.6515 1.6515 2 n/a n/a
+100 intermediate precision (total) 1.6530 1.6530 2.0037 0.9554 7.2821
+```
+
+Repeatability of 0.07% RSD looks superb; intermediate precision is 1.65%, twenty-three times
+larger, because the variability lives entirely between days. Reporting the within-day figure as
+the procedure's precision would understate routine performance by more than an order of magnitude.
+This is why the script fits a one-way random-effects model rather than pooling.
+
+Two traps the script handles for you:
+
+- **Precision is estimated within each level, never pooled across levels.** Pooling 80/100/120%
+ results into one standard deviation turns the range itself into apparent imprecision. The script
+ reports per level, plus a level-independent view as percent of nominal.
+- **`--require-ci-within-limit`** enforces that the whole confidence interval sits inside the
+ limit, not just the mean. Q2(R2) 3.3.1.4 asks for the interval to be *compatible with* the
+ criterion; a mean that scrapes inside on six replicates has not demonstrated much.
+
+### 5. Establish DL and QL, and confirm them
+
+```bash
+python3 check_detection_limits.py --calibration lowcal.csv --blanks blanks.csv \
+ --confirm-ql 0.05 --confirm-data ql_check.csv --reporting-threshold 0.05
+```
+
+```
+approach sigma slope DL QL
+sd-and-slope (sigma = residual SD of regression) 7.2816 5033.3490 0.0048 0.0145
+sd-and-slope (sigma = SD of y-intercept) 4.3303 5033.3490 0.0028 0.0086
+sd-and-slope (sigma = SD of 8 blanks) 3.7702 5033.3490 0.0025 0.0075
+```
+
+The same data give QL estimates spanning 1.9×, purely from the choice of σ. Q2(R2) 3.2.3.5
+therefore requires the limit **and the approach used to determine it** to be reported, and an
+estimated limit to be confirmed with samples at or near it. For an impurity procedure the QL must
+be at or below the reporting threshold. Reaching for `3.3σ/slope` reflexively, reporting one number
+with no named approach, and never confirming it are three separate findings.
+
+### 6. Bioanalytical runs under ICH M10
+
+```bash
+python3 check_bioanalytical_run.py --modality chromatographic --run run1.csv
+python3 check_bioanalytical_run.py --modality lba --isr isr.csv
+python3 check_bioanalytical_run.py --modality lba --criteria
+```
+
+`--modality` is mandatory and has no default, because the criteria genuinely differ:
+
+| | Chromatographic | Ligand binding assay |
+| --- | --- | --- |
+| Calibration tolerance | ±15%, ±20% at LLOQ | ±20%, ±25% at LLOQ and ULOQ |
+| Accuracy / precision | ±15% / ≤15% CV (±20% / ≤20% at LLOQ) | ±20% / ≤20% CV (±25% / ≤25% at LLOQ and ULOQ) |
+| A&P design | 4 QC levels, 5 replicates/run, ≥3 runs over ≥2 days | 5 QC levels, 3 replicates/run, ≥6 runs over ≥2 days |
+| Total error | no such criterion | ≤30%, ≤40% at LLOQ and ULOQ |
+| ISR agreement | ±20% for ≥2/3 of repeats | ±30% for ≥2/3 of repeats |
+
+Applying the ±15% chromatographic numbers to a ligand binding assay, or importing the LBA total-error
+criterion into a chromatographic method, are both common and both wrong.
+
+The run check enforces the per-level rule that gets missed: at least 2/3 of *all* QCs **and** at
+least 50% at *each* level. A run can pass the overall fraction while a single level fails
+completely.
+
+```
+finding: QC level high: 0/2 within tolerance (0%); M10 requires at least 50% at each level
+```
+
+### 7. Transfer and method comparison
+
+```bash
+python3 compare_methods.py -i paired.csv --margin 2 --relative --slope-tolerance 0.05
+```
+
+```
+mean difference (%) 1.4646
+TOST margin 2.0000
+TOST p-value 1.0528e-13
+90% CI (TOST) 1.44127 to 1.48797
+equivalent at stated margin yes
+--- for contrast only ---
+paired t-test p (NOT equivalence) 0.0000
+OLS slope (biased here) 1.0396
+Deming slope 1.0398
+Passing-Bablok slope 1.0351
+```
+
+Two errors this replaces:
+
+- **"p > 0.05, no significant difference, therefore the methods are equivalent."** Failing to
+ detect a difference is not evidence of equivalence, and on a small transfer dataset that outcome
+ is close to guaranteed. TOST tests the hypothesis that matters — that the true difference lies
+ inside a pre-stated margin. Here the t test says the difference is highly significant *and* TOST
+ says the methods are equivalent at ±2%; both are true, and only one answers the question.
+- **Ordinary least squares for method comparison.** OLS assumes the reference values carry no
+ error, which is false when comparing two procedures, and biases the slope toward zero. Deming
+ (with a stated error-variance ratio) and Passing–Bablok (non-parametric, outlier-resistant) are
+ the appropriate regressions and are reported side by side with OLS for contrast.
+
+The script also flags proportional bias — when the difference trends with concentration, a single
+mean bias and its limits of agreement are misleading regardless of how tight they look.
+
+## What this skill exists to prevent
+
+1. Validating against ICH Q2(R1)'s structure three years after Q2(R2) replaced it.
+2. Acceptance criteria written after the data were seen.
+3. r² presented as evidence of linearity.
+4. Repeatability reported as the procedure's precision, with the between-day component invisible.
+5. One DL/QL number with no named approach and no confirmation.
+6. Chromatographic M10 criteria applied to a ligand binding assay, or the reverse.
+7. A t test's non-significance presented as equivalence at a method transfer.
+
+## References
+
+- `references/framework-selection.md` — which framework governs, and the questions that decide it
+- `references/ich-q2r2.md` — structure, Table 1 and Table 2, per-characteristic recommended data
+- `references/ich-m10-bioanalytical.md` — the full chromatographic and LBA criteria side by side
+- `references/compendial-and-clsi.md` — USP, CLSI and ISO designations, scope, and how to cite them
+- `references/statistics.md` — the statistical methods, why each one, and the common errors
+- `references/source-ledger.md` — provenance and research dates for every claim in this skill
+
+## Assets
+
+- `assets/validation-protocol-template.md` — protocol structure with criteria stated up front
+- `assets/validation-report-template.md` — report structure with raw-data traceability
diff --git a/skills/analytical-method-validation/assets/validation-protocol-template.md b/skills/analytical-method-validation/assets/validation-protocol-template.md
new file mode 100644
index 00000000..99f64fdc
--- /dev/null
+++ b/skills/analytical-method-validation/assets/validation-protocol-template.md
@@ -0,0 +1,106 @@
+# Analytical Procedure Validation Protocol
+
+> Every bracketed field is a decision to make and record **before** data collection.
+> `plan_validation.py --protocol` generates a framework-specific version of this document with the
+> required characteristics already listed.
+
+| Field | Value |
+| --- | --- |
+| Protocol number / version | [ ] |
+| Analytical procedure identifier and version | [ ] |
+| Product / analyte / matrix | [ ] |
+| Measured quality attribute | [ ] assay / impurity (quantitative) / impurity (limit) / identity / other |
+| Governing framework and section | [ ] |
+| Regional expectation confirmed with | [ ] |
+| Related development report (ICH Q14) | [ ] |
+| Author / date | [ ] |
+| Technical reviewer / date | [ ] |
+| Quality unit approval / date | [ ] |
+
+## 1. Intended purpose and analytical target profile
+
+- Measurand and reporting unit: [ ]
+- Decision the result supports: [ ] release / stability / in-process / clinical / other
+- Specification or reporting limits served: [ ]
+- Required reportable range, derived from the specification: [ ]
+- Performance characteristics and criteria (the ATP): [ ]
+
+## 2. Pre-stated acceptance criteria
+
+State a numeric criterion and its justification for every characteristic to be validated. A
+criterion with no justification traceable to the specification, the ATP, or development data is not
+defensible.
+
+| Characteristic | Criterion | Justification | Framework reference |
+| --- | --- | --- | --- |
+| Specificity / selectivity | [ ] | [ ] | [ ] |
+| Response (calibration model) | [ ] | [ ] | [ ] |
+| Lower range limit (DL / QL) | [ ] | [ ] | [ ] |
+| Accuracy | [ ] | [ ] | [ ] |
+| Repeatability | [ ] | [ ] | [ ] |
+| Intermediate precision | [ ] | [ ] | [ ] |
+| Combined accuracy and precision, if used | [ ] | [ ] | [ ] |
+
+- Interval to be reported alongside accuracy and precision: [ ] confidence level [ ]
+- Does the criterion apply to the point estimate or to the whole interval? [ ]
+
+## 3. Study design
+
+| Characteristic | Levels | Replicates | Runs / days / analysts / instruments |
+| --- | --- | --- | --- |
+| Response | [ ] (minimum 5 for ICH Q2(R2)) | [ ] | [ ] |
+| Accuracy | [ ] | [ ] | [ ] |
+| Repeatability | [ ] | [ ] | [ ] |
+| Intermediate precision | [ ] | [ ] | [ ] |
+| Lower range limit | [ ] | [ ] | [ ] |
+
+- Replicate count matches the routine reportable result: [ ] yes / [ ] justified deviation: [ ]
+- Calibration model and weighting, fixed in advance: [ ] unweighted / 1/x / 1/x² / non-linear / multivariate
+- Randomisation and run order: [ ]
+- Prior knowledge or development data used in place of a test, with justification: [ ]
+
+## 4. Materials
+
+| Item | Identity / grade | Lot | Assigned value and uncertainty | Expiry |
+| --- | --- | --- | --- | --- |
+| Reference material | [ ] | [ ] | [ ] | [ ] |
+| Impurity standards | [ ] | [ ] | [ ] | [ ] |
+| Blank / placebo matrix | [ ] | [ ] | — | [ ] |
+
+## 5. Sample and solution handling
+
+- Preparation procedure and dilution scheme: [ ]
+- Solution stability window to be demonstrated: [ ]
+- Storage conditions: [ ]
+
+## 6. Specificity and stability-indicating properties
+
+- Interferences to be challenged: [ ]
+- Forced degradation conditions, if a stability-indicating claim is made: [ ]
+- Orthogonal procedure, if used, and its accuracy: [ ]
+
+## 7. Robustness (normally development, ICH Q14)
+
+| Parameter | Nominal | Range varied | Effect assessed on |
+| --- | --- | --- | --- |
+| [ ] | [ ] | [ ] | [ ] |
+
+## 8. Statistical treatment
+
+- Software, version, and how calculations are verified: [ ]
+- Handling of outliers, stated in advance: [ ]
+- Scripts to be used and their output retained as records: [ ]
+
+## 9. Deviations and data integrity
+
+- Deviation identification, assessment and approval route: [ ]
+- All results will be reported, including out-of-criteria values: [ ] confirmed
+- Raw data location, audit trail, and review: [ ]
+
+## 10. Approvals
+
+| Role | Name | Signature | Date |
+| --- | --- | --- | --- |
+| Author | | | |
+| Technical reviewer | | | |
+| Quality unit | | | |
diff --git a/skills/analytical-method-validation/assets/validation-report-template.md b/skills/analytical-method-validation/assets/validation-report-template.md
new file mode 100644
index 00000000..ea91d19c
--- /dev/null
+++ b/skills/analytical-method-validation/assets/validation-report-template.md
@@ -0,0 +1,116 @@
+# Analytical Procedure Validation Report
+
+> Reports the outcome against criteria stated in the approved protocol. If a criterion here differs
+> from the protocol, that is a deviation to be documented, not an edit to be made.
+
+| Field | Value |
+| --- | --- |
+| Report number / version | [ ] |
+| Protocol number / version executed | [ ] |
+| Analytical procedure identifier and version | [ ] |
+| Governing framework | [ ] |
+| Execution dates | [ ] |
+| Analysts and instruments | [ ] |
+| Author / date | [ ] |
+| Technical reviewer / date | [ ] |
+| Quality unit approval / date | [ ] |
+
+## 1. Summary of outcome
+
+| Characteristic | Criterion (from protocol) | Result | Interval reported | Met |
+| --- | --- | --- | --- | --- |
+| Specificity / selectivity | [ ] | [ ] | — | [ ] |
+| Response | [ ] | [ ] | [ ] | [ ] |
+| Lower range limit (DL / QL) | [ ] | [ ] | — | [ ] |
+| Accuracy | [ ] | [ ] | [ ] | [ ] |
+| Repeatability | [ ] | [ ] | [ ] | [ ] |
+| Intermediate precision | [ ] | [ ] | [ ] | [ ] |
+
+- Validated reportable range: [ ]
+- Statement of fitness for the intended purpose, and who is making it: [ ]
+
+## 2. Response
+
+- Levels and replicates actually run: [ ]
+- Calibration model and weighting: [ ]
+- Slope, intercept, and their confidence intervals: [ ]
+- Coefficient of determination: [ ]
+- **Analysis of deviation from the regression line** (residual plot, lack-of-fit test, back-calculated
+ relative error per level): [ ]
+
+## 3. Accuracy
+
+| Level | n | Mean recovery (%) | Bias (%) | Confidence interval | Met |
+| --- | --- | --- | --- | --- | --- |
+| [ ] | [ ] | [ ] | [ ] | [ ] | [ ] |
+
+- Approach used: [ ] reference material / spiking / orthogonal comparison
+- For impurities, basis of determination: [ ] w/w / area %
+
+## 4. Precision
+
+| Level | Component | SD | %RSD | df | Interval | Met |
+| --- | --- | --- | --- | --- | --- | --- |
+| [ ] | repeatability | [ ] | [ ] | [ ] | [ ] | [ ] |
+| [ ] | between-group | [ ] | [ ] | [ ] | — | — |
+| [ ] | intermediate precision | [ ] | [ ] | [ ] | [ ] | [ ] |
+
+- Intermediate precision factors varied: [ ] days / analysts / instruments / environment
+- Reproducibility, if performed: [ ]
+
+## 5. Lower range limits
+
+- DL, and **the approach used to determine it**: [ ]
+- QL, and **the approach used to determine it**: [ ]
+- Confirmation of the estimated limit with samples at or near it: [ ]
+- For impurity procedures, QL relative to the reporting threshold: [ ]
+
+## 6. Specificity and stability-indicating properties
+
+- Interference results: [ ]
+- Forced degradation results and peak purity / mass balance: [ ]
+- Relative response factors, and any correction factor applied: [ ]
+
+## 7. Robustness
+
+| Parameter | Range varied | Effect on the reportable result | Conclusion |
+| --- | --- | --- | --- |
+| [ ] | [ ] | [ ] | [ ] |
+
+- Solution stability demonstrated over: [ ]
+
+## 8. Deviations
+
+| # | Description | Assessment of impact | Disposition | Approved by |
+| --- | --- | --- | --- | --- |
+| [ ] | [ ] | [ ] | [ ] | [ ] |
+
+- Out-of-criteria individual results, and whether they were included in the reported statistics: [ ]
+
+## 9. Raw data traceability
+
+Every reported number must be traceable to a retained record. A report whose numbers cannot be
+reproduced from the raw data is the finding that costs the most to remediate.
+
+| Reported item | Raw data location | Instrument / system | Acquisition date | Reviewed by |
+| --- | --- | --- | --- | --- |
+| [ ] | [ ] | [ ] | [ ] | [ ] |
+
+- Software and version used for calculations: [ ]
+- Calculation verification method: [ ]
+- Script outputs retained as records: [ ]
+
+## 10. Conclusion and lifecycle
+
+- Conclusion against the ATP / intended purpose: [ ]
+- Conditions or limitations on use: [ ]
+- Ongoing performance monitoring planned: [ ]
+- Revalidation triggers identified: [ ]
+
+## 11. Approvals
+
+| Role | Name | Signature | Date |
+| --- | --- | --- | --- |
+| Author | | | |
+| Technical reviewer | | | |
+| Quality unit | | | |
diff --git a/skills/analytical-method-validation/references/compendial-and-clsi.md b/skills/analytical-method-validation/references/compendial-and-clsi.md
new file mode 100644
index 00000000..c1f76b91
--- /dev/null
+++ b/skills/analytical-method-validation/references/compendial-and-clsi.md
@@ -0,0 +1,96 @@
+# Compendial, CLSI, and ISO Sources (No Standard Text)
+
+Research basis: **2026-07-27**. This reference identifies documents, their scope, and where to obtain
+them. **It does not reproduce their requirements, thresholds, or study designs**, because they are
+copyrighted and paywalled.
+
+## Copyright boundary
+
+USP–NF general chapters, CLSI documents, and ISO/IEC standards are copyrighted works sold by their
+publishers. Do not ask an agent to retrieve, transcribe, summarise clause-by-clause, reconstruct, or
+store their text. Vendor application notes and training decks that quote them are equally
+constrained, and a paraphrase that carries the same numbers is still a reproduction of the
+substantive content.
+
+The practical consequence: **when a numeric criterion or a study design lives in one of these
+documents, read it from the authorised copy.** An agent asked for "the USP <621> tailing factor
+limit" or "the CLSI EP15 number of days" will produce a plausible number. Plausible is not the same
+as correct, and the difference is discovered at audit.
+
+Record publisher, title, designation, edition, amendments, authorised location, access date, and
+review date in the laboratory's controlled source register.
+
+## USP–NF general chapters
+
+| Chapter | Title | Scope |
+| --- | --- | --- |
+| `<1220>` | Analytical Procedure Life Cycle | Three-stage lifecycle: procedure design (Stage 1), performance qualification (Stage 2), ongoing performance verification (Stage 3), organised around an analytical target profile. Official 1 May 2022 (incorporated into USP–NF 2022 Issue 1 on 1 Nov 2021). Integrates the concepts previously spread across `<1224>`, `<1225>`, and `<1226>`. |
+| `<1225>` | Validation of Compendial Procedures | Validation of non-compendial procedures, and of compendial procedures used outside their stated scope. Stage 2 activity under `<1220>`. |
+| `<1226>` | Verification of Compendial Procedures | Assessment of selected performance characteristics showing a compendial procedure works under actual conditions of use. **Verification is not revalidation** and does not repeat the full validation. |
+| `<1224>` | Transfer of Analytical Procedures | Transfer between laboratories. |
+| `<1010>` | Analytical Data — Interpretation and Treatment | Statistical treatment of analytical data. |
+| `<621>` | Chromatography | System suitability and chromatographic operating parameters, including the extent to which a compendial procedure may be adjusted without triggering revalidation. |
+| `<711>` / `<1092>` | Dissolution / The Dissolution Procedure | Dissolution testing and development/validation of the procedure. |
+
+Obtain from the USP–NF (<https://www.uspnf.com/>). Regional pharmacopoeias — Ph. Eur., JP, ChP —
+carry their own general chapters; check which pharmacopoeia the specification cites, because
+adjustment allowances and system suitability requirements differ between them.
+
+**The `<1226>` decision.** Verification applies when using a compendial procedure as written and
+within its scope. Two situations push you back to `<1225>` validation: using the procedure outside
+its stated scope (a different matrix, a different dosage form, a concentration range it does not
+cover), or modifying it beyond the adjustments the relevant chapter permits. Getting this wrong in
+either direction is expensive — unnecessary full validation, or an unsupported claim of verification.
+
+## CLSI EP series
+
+Designations and titles below were taken from clsi.org listings and secondary sources on the
+research date. **Editions change; confirm the current edition on <https://clsi.org/> before designing
+a study.** Marked `[confirm]` where the edition was not read from the publisher directly.
+
+| Designation | Subject | Note |
+| --- | --- | --- |
+| EP05 | Evaluation of precision of quantitative measurement procedures | Establishment of precision; the multi-day/multi-run designs. `[confirm edition]` |
+| EP06 | Evaluation of linearity of quantitative measurement procedures | 2nd edition reported. `[confirm edition]` |
+| EP07 | Interference testing in clinical chemistry | Screening, quantifying and confirming interferents; verifying manufacturer interference claims. 3rd edition reported. `[confirm edition]` |
+| EP09 | Measurement procedure comparison and bias estimation using patient samples | The method-comparison document. 3rd edition reported. `[confirm edition]` |
+| EP15 | User verification of precision and estimation of bias | The short study a laboratory runs to verify a manufacturer's claims. 3rd edition reported. `[confirm edition]` |
+| EP17 | Evaluation of detection capability | Limit of blank, limit of detection, limit of quantitation; verification of manufacturer claims. `[confirm edition]` |
+| EP25 | Evaluation of stability of in vitro diagnostic reagents | `[confirm edition]` |
+| EP28 | Defining, establishing, and verifying reference intervals | Formerly designated C28. An implementation guide (EP28IG) also exists. `[confirm edition]` |
+
+**Vocabulary.** CLSI distinguishes *limit of blank*, *limit of detection*, and *limit of quantitation*
+as three separate quantities with separate protocols. This is not the same taxonomy as ICH Q2(R2)'s
+detection limit and quantitation limit, and the two should not be translated into each other
+casually — the underlying definitions and the experiments differ.
+
+**Verification versus establishment.** For an FDA-cleared or CE-marked assay used as intended, a
+laboratory *verifies* the manufacturer's performance claims — a bounded study. For a
+laboratory-developed test, or an assay used off-label, the laboratory *establishes* performance,
+which is a much larger exercise. Under CLIA the distinction has direct regulatory consequences and
+also depends on test complexity. Determine which applies before designing anything.
+
+## ISO standards
+
+| Standard | Relevance |
+| --- | --- |
+| ISO/IEC 17025:2017 | Clause 7.2 selection, verification and validation of methods; clause 7.6 measurement uncertainty. Validation "to the extent necessary" for the intended application — no characteristic list, no numeric criteria. |
+| ISO 15189 | Medical laboratories: quality and competence. The clinical-laboratory counterpart to 17025. |
+| ISO 21748 / ISO 5725 series | Using repeatability, reproducibility and trueness estimates in measurement uncertainty; accuracy of measurement methods. |
+
+Obtain from ISO (<https://www.iso.org/>) or a national member body. A laboratory is **accredited** to
+ISO/IEC 17025 by an accreditation body — it is not "17025 certified", and writing "certified" is a
+substantive error assessors notice.
+
+For accreditation readiness, the quality manual, and the surrounding management system, use this
+repository's `iso-standards-readiness` skill. This skill stays at the level of the individual
+procedure.
+
+## Environmental, food, and forensic method systems
+
+Where a prescribed method system governs — a published EPA method, an AOAC Official Method, a
+standard method for water or food analysis — the validation and quality-control requirements are
+written into the method or the programme, and they take precedence. Do not substitute a
+pharmaceutical framework. Common differences: matrix spike and duplicate requirements per batch,
+prescribed calibration-verification frequencies, method detection limit procedures that differ from
+both ICH and CLSI, and mandatory participation in proficiency testing schemes.
diff --git a/skills/analytical-method-validation/references/framework-selection.md b/skills/analytical-method-validation/references/framework-selection.md
new file mode 100644
index 00000000..c6cb6e91
--- /dev/null
+++ b/skills/analytical-method-validation/references/framework-selection.md
@@ -0,0 +1,87 @@
+# Which Framework Governs
+
+Research basis: **2026-07-27**. Confirm every date and edition against the official source before
+relying on it; see `source-ledger.md`.
+
+Framework selection is the first decision and the one most often skipped. Getting it wrong
+invalidates the protocol regardless of how well the studies are executed, because each framework
+requires a different set of characteristics, a different study layout, and a different treatment of
+acceptance criteria.
+
+## The deciding questions, in order
+
+**1. Is the measurand a drug concentration in a biological matrix, supporting a nonclinical or
+clinical study?**
+→ **ICH M10.** This covers pharmacokinetics, toxicokinetics, and bioequivalence. M10 supplies
+explicit numeric criteria, and they differ between chromatographic assays and ligand binding
+assays. Q2(R2) does not govern here.
+
+**2. Is it a quality attribute of a drug substance or drug product — assay, potency, impurity,
+identity, dissolution, content uniformity?**
+→ **ICH Q2(R2)** for validation, with **ICH Q14** for development, robustness, the analytical
+target profile, and lifecycle change management. If the procedure is compendial and being used as
+written, see question 3 first.
+
+**3. Is the procedure a compendial (pharmacopoeial) procedure?**
+→ **USP <1226> verification** if it is used as written and within its stated scope. Verification
+assesses selected characteristics to show the procedure works under actual conditions of use; it is
+not revalidation and does not repeat the full study. → **USP <1225> validation** if the procedure
+is non-compendial, or compendial but used outside its scope. Both sit inside the **USP <1220>**
+three-stage lifecycle. Regional pharmacopoeias (Ph. Eur., JP) have their own general chapters —
+check which pharmacopoeia the specification cites.
+
+**4. Is it a clinical laboratory measurement procedure reporting patient results?**
+→ **CLSI EP series**, inside a CLIA/CAP or ISO 15189 quality system. The vocabulary differs from
+pharmaceutical work: *verification* of a manufacturer's claims for an FDA-cleared assay is a much
+smaller exercise than *establishment* of performance for a laboratory-developed test, and the
+distinction is regulatory, not stylistic.
+
+**5. Is the laboratory accredited to ISO/IEC 17025 and the method non-standard, laboratory-developed,
+or a modified standard method?**
+→ **ISO/IEC 17025 clause 7.2.2** requires validation as extensive as necessary to meet the needs of
+the intended application, plus measurement uncertainty under clause 7.6. It sets no characteristic
+list and no numeric criteria; the laboratory justifies both.
+
+**6. Is it an environmental, food, or forensic method under a prescribed method system?**
+→ The method system governs (for example a published EPA method, an AOAC Official Method, or a
+regulator's prescribed procedure), usually with its own validation and QC requirements written into
+the method itself. Do not substitute a pharmaceutical framework.
+
+## More than one can apply
+
+Common and legitimate. A contract laboratory accredited to ISO/IEC 17025 running a compendial assay
+for a pharmaceutical client satisfies <1226> for the procedure and 17025 clause 7.2 for the
+accreditation scope, with the client's specification supplying the criteria. Record which framework
+each requirement traces to, so a later change can be assessed against the right one.
+
+## Do not blend them
+
+The failure mode is a protocol that mixes Q2(R1)-era characteristic names, an M10 numeric tolerance
+imported because it was memorable, and a CLSI study layout. It satisfies none of the three and is
+hard to defend because no single source can be cited for any of it. If a requirement is in the
+protocol, name the framework and section it comes from.
+
+## Where the numbers come from
+
+| Framework | Numeric acceptance criteria |
+| --- | --- |
+| ICH Q2(R2) | Almost none. Derive from the specification, the ATP, or development data, and justify. |
+| ICH Q14 | None. It supplies the ATP concept and the development/robustness framework. |
+| ICH M10 | Explicit, and modality-dependent. Use them as written. |
+| USP <1225>/<1226>/<1220> | Consult the authorised text. |
+| CLSI EP | Consult the authorised text; many EP documents supply study designs rather than limits. |
+| ISO/IEC 17025 | None. The laboratory sets and justifies them. |
+
+Q2(R2)'s reticence is deliberate: a criterion that is not tied to what the result is used for is
+arbitrary. An assay releasing product against a 95.0–105.0% specification needs different precision
+than one supporting a 70–130% content-uniformity limit. Deriving the criterion from the decision the
+result supports is the substance of the exercise, not paperwork around it.
+
+## Related skills in this repository
+
+- `iso-standards-readiness` — the surrounding quality system (ISO/IEC 17025, ISO 15189
+ accreditation readiness, quality manual, CAPA). That skill operates at the laboratory level; this
+ one operates at the level of a single procedure.
+- `statistical-analysis`, `statistical-power` — general inference and study sizing.
+- `uncertainty-and-units` — unit handling and measurement uncertainty propagation, which ISO/IEC
+ 17025 clause 7.6 requires alongside validation.
diff --git a/skills/analytical-method-validation/references/ich-m10-bioanalytical.md b/skills/analytical-method-validation/references/ich-m10-bioanalytical.md
new file mode 100644
index 00000000..de95df1e
--- /dev/null
+++ b/skills/analytical-method-validation/references/ich-m10-bioanalytical.md
@@ -0,0 +1,123 @@
+# ICH M10 — Bioanalytical Criteria, by Modality
+
+Research basis: **2026-07-27**, read from the ICH Harmonised Guideline *Bioanalytical Method
+Validation and Study Sample Analysis M10*, Step 4 dated 24 May 2022. ICH licenses its documents for
+reuse with acknowledgement. Confirm the current text and your region's implementation at
+<https://database.ich.org/sites/default/files/M10_Guideline_Step4_2022_0524.pdf>.
+
+M10 harmonised what had been separate FDA and EMA bioanalytical guidance for studies in its scope:
+methods quantifying drug and metabolite concentrations in biological matrices supporting nonclinical
+and clinical studies, plus the analysis of study samples.
+
+## The distinction that matters most
+
+**Chromatographic assays (section 3) and ligand binding assays (section 4) have different numeric
+criteria throughout.** They are not stylistic variants of one set. Applying chromatographic
+tolerances to an LBA is the most common error in this area, and importing the LBA total-error
+criterion into a chromatographic method is its mirror image.
+
+| | Chromatographic | Ligand binding assay |
+| --- | --- | --- |
+| Calibration levels (minimum) | 6, including LLOQ | 6, including LLOQ |
+| Calibration standard tolerance | ±15% | ±20% |
+| … at LLOQ | ±20% | ±25% |
+| … at ULOQ | ±15% | ±25% |
+| Calibration standards that must pass | ≥75% | ≥75%, excluding anchor points |
+| Accuracy | ±15% | ±20% |
+| … at limits | ±20% at LLOQ | ±25% at LLOQ **and** ULOQ |
+| Precision (%CV) | ≤15% | ≤20% |
+| … at limits | ≤20% at LLOQ | ≤25% at LLOQ **and** ULOQ |
+| A&P QC levels | minimum 4 | 5 (LLOQ, low, medium, high, ULOQ) |
+| A&P replicates per level per run | ≥5 (within-run) | ≥3 |
+| A&P runs | ≥3 runs over ≥2 days | ≥6 runs over ≥2 days |
+| **Total error** | **no such criterion** | **≤30%; ≤40% at LLOQ and ULOQ** |
+| Routine run QC tolerance | ±15% | ±20% |
+| Routine run QC pass rule | ≥2/3 of all QCs **and** ≥50% at each level | same rule, ±20% |
+| Dilution integrity | mean within ±15% | mean within ±20% |
+| Stability | mean at each QC level within ±15% | mean within ±20% |
+| ISR agreement | within ±20% for ≥2/3 of repeats | within ±30% for ≥2/3 of repeats |
+| Selectivity sources/lots | ≥6 individual sources | ≥6 individual sources |
+| Carry-over in blank | ≤20% of LLOQ analyte response and ≤5% of IS response | per guideline |
+
+Verify any figure against the guideline before using it in a protocol; regional implementation and
+subsequent revisions can change the picture.
+
+## Chromatographic QC placement (section 3)
+
+Accuracy and precision validation QCs at a minimum of **four** concentration levels:
+
+- the **LLOQ**
+- **low QC** — within three times the LLOQ
+- **medium QC** — around 30–50% of the calibration curve range
+- **high QC** — at least 75% of the ULOQ
+
+For runs that are not accuracy-and-precision runs, low, medium and high QCs may be analysed in
+duplicate; these plus the calibration standards form the basis for accepting or rejecting the run.
+
+Calibration standards and QCs should be prepared from **separate stock solutions**, to avoid a bias
+that is not a property of the analytical performance. If a single stock must serve both, verify the
+accuracy and stability of that stock. A single source of blank matrix may be used if it is free of
+interference and matrix effects.
+
+Calibration curves for accuracy and precision assessment should use freshly spiked standards in at
+least one run; if other runs use frozen standards, demonstrate their stability.
+
+## Reporting obligations that catch people out
+
+**Report everything.** Validation data and the determination of accuracy and precision must include
+*all* results obtained, including individual QCs outside the acceptance criteria — except cases where
+errors are obvious and documented. Silently dropping an out-of-criteria QC is a data integrity
+problem, not a rounding decision.
+
+**Within-run accuracy and precision are reported per run.** If the within-run criteria are not met in
+every run, calculate an overall estimate of within-run accuracy and precision for each QC level.
+Between-run (intermediate) accuracy and precision combine data from all runs.
+
+**Trend within a run.** It is recommended to demonstrate accuracy and precision over at least one run
+sized like a prospective study-sample run, so time-dependent drift is visible.
+
+## Incurred sample reanalysis (section 5)
+
+ISR repeats the analysis of a subset of study samples in separate runs, to verify that measured
+concentrations in real samples are reproducible. It is not a substitute for QCs — QCs are spiked,
+incurred samples are not, and only incurred samples can reveal metabolite back-conversion, protein
+binding effects, or matrix instability.
+
+- The extent depends on the analyte and the samples and should be justified.
+- Objective criteria for choosing the subset should be **predefined**; selecting samples around
+ Cmax and the elimination phase is recommended.
+- **Do not pool samples** — pooling masks anomalous findings.
+- ISR samples and QCs are processed and analysed in the same manner as the original analysis.
+- Percent difference is `(repeat value - initial value) / mean value x 100` -- assessed
+ against the **mean of the two**, not against the initial value.
+- Repeats are performed within the analyte's stability window, but **not on the same day**
+ as the original analysis.
+- Acceptance: within ±20% for at least 2/3 of repeats (chromatographic), or within ±30% for at least
+ 2/3 (LBA).
+
+For nonclinical studies in scope, ISR should in general be performed; the guideline notes incurred
+samples need only be included if available, so inclusion was not felt to be mandatory in every case.
+Confirm the situations requiring ISR against the guideline text for your study type.
+
+## Study sample reanalysis is a separate thing
+
+ISR is a method-reliability check. *Reanalysis of study samples* for a reportable-value decision is
+different, and the reasons for reanalysis, the number of replicates, and the criteria for selecting
+the value to report must be **predefined in the protocol, study plan, or SOP before study sample
+analysis begins.** Deciding after the fact which of two values to report is the classic finding.
+
+## Partial and cross validation
+
+M10 addresses partial validation (a change to a validated method — matrix, anticoagulant, species,
+instrument, or a range change) and cross validation (comparing data from two methods or two
+laboratories contributing to the same study). Both are scoped by the change and the risk; consult
+the guideline for what each requires. For a cross validation between sites or methods, the
+statistics in `compare_methods.py` — equivalence testing against a pre-stated margin, and a
+regression that allows error in both measurements — are the appropriate treatment.
+
+## Biomarkers and other contexts
+
+M10's scope centres on drug and metabolite concentration measurement. Biomarker assays, immunogenicity
+assays, and diagnostic measurements are addressed differently or fall outside scope; do not assume the
+concentration-assay criteria transfer. Where a biomarker assay supports a regulatory decision, the
+fit-for-purpose framework and the applicable regional guidance govern the extent of validation.
diff --git a/skills/analytical-method-validation/references/ich-q2r2.md b/skills/analytical-method-validation/references/ich-q2r2.md
new file mode 100644
index 00000000..60819028
--- /dev/null
+++ b/skills/analytical-method-validation/references/ich-q2r2.md
@@ -0,0 +1,229 @@
+# ICH Q2(R2) — Structure and Recommended Data
+
+Research basis: **2026-07-27**, read from the ICH Harmonised Guideline *Validation of Analytical
+Procedures Q2(R2)*, Final Version adopted 1 November 2023, with the error correction dated
+30 November 2023. ICH licenses its documents for reuse with acknowledgement, so requirements are
+summarised here directly. Confirm the current text and your region's implementation date at
+<https://database.ich.org/sites/default/files/ICH_Q2%28R2%29_Guideline_2023_1130.pdf>.
+
+## Document history that matters
+
+| Version | Date | Note |
+| --- | --- | --- |
+| Q2A | Oct 1994 | Text |
+| Q2B | Nov 1996 | Methodology |
+| Q2(R1) | Nov 2005 | Q2B merged into the parent guideline |
+| Q2(R2) | 1 Nov 2023 | Complete revision, aligned with the new Q14 |
+| Q2(R2) correction | 30 Nov 2023 | Table 5 reportable-range linearity formulae; Tables 6–11 |
+
+If a protocol cites "ICH Q2(R1)" or lists characteristics in the R1 order, it is working from the
+superseded structure. The error correction is easy to miss and applies to the dissolution example
+and to Annex 2 Tables 6–11.
+
+## The restructure
+
+Q2(R1) presented a flat list. Q2(R2) groups methodology under section 3 by performance
+characteristic:
+
+```
+3.1 Specificity/Selectivity
+ 3.1.1 General considerations (absence of interference, orthogonal comparison,
+ technology-inherent justification)
+3.2 Range <-- parent characteristic
+ 3.2.2 Response
+ 3.2.2.1 Linear response <-- what R1 called "linearity"
+ 3.2.2.2 Non-linear response
+ 3.2.2.3 Multivariate calibration
+ 3.2.3 Validation of lower range limits <-- what R1 called LOD and LOQ
+3.3 Accuracy and Precision
+ 3.3.1 Accuracy
+ 3.3.2 Precision (repeatability, intermediate precision, reproducibility)
+ 3.3.3 Combined approaches for accuracy and precision <-- new
+3.4 Robustness --> largely a development activity, see ICH Q14
+```
+
+Section 2 carries the general considerations, including two concepts absent from R1: **reportable
+range** (2.3) and **considerations for multivariate analytical procedures** (2.5).
+
+## Table 1 — which tests for which measured attribute
+
+Required tests follow the *measured quality attribute*, not the instrument.
+
+| Characteristic | Identity | Impurity: quantitative | Impurity: limit test | Assay (content/potency) |
+| --- | --- | --- | --- | --- |
+| Specificity test | yes | yes | yes | yes |
+| Response (calibration model) | no | yes | no | yes |
+| Lower range limit | no | QL† | DL | no |
+| Accuracy test | no | yes | no | yes |
+| Repeatability test | no | yes | no | yes |
+| Intermediate precision test | no | yes‡ | no | yes‡ |
+
+† In some complex cases DL may also be evaluated.
+‡ Not required independently where reproducibility has been performed and intermediate precision
+can be derived from that dataset.
+
+Further notes from Table 1: other quantitative measurements follow the impurity scheme when the
+range limit is close to DL/QL, and the assay scheme when it is not. Some characteristics may be
+substituted by technology-inherent justification for physicochemical properties. Lack of specificity
+in one procedure should be compensated by one or more supporting procedures unless justified.
+
+## Table 2 — reportable range examples
+
+The reportable range derives from the specification and must include the upper and lower
+specification or reporting limits. Other ranges are acceptable if justified; at low amounts a wider
+upper range may be more practical.
+
+| Use | Low end | High end |
+| --- | --- | --- |
+| Assay of a product | 80% of declared content, or 80% of the lower specification limit | 120% of declared content, or 120% of the upper specification limit |
+| Potency | lowest specification limit −20% | highest specification limit +20% |
+| Content uniformity | 70% of declared content | 130% of declared content |
+| Dissolution, IR, one point | Q − 45% of the lowest strength specification | per specification |
+| Dissolution, IR, multi-point | lower limit as justified, or QL | 130% of declared content of the highest strength |
+| Dissolution, modified release | lower limit as justified, or QL | per specification |
+| Impurity | reporting threshold | 120% of the specification limit |
+| Purity (area %) | 80% of the lower specification limit | upper specification limit, or 100% |
+
+Where assay and impurity run as a single test with one standard, linearity must be shown both at the
+impurity reporting level and up to 120% of the assay specification limit.
+
+**Reportable range vs working range.** The reportable range is the interval of *reported results*.
+A working range is what is presented to the instrument, and may differ because of dilution or other
+sample preparation. They can be identical. Mathematical calculation normally links the two.
+
+## Recommended data, by characteristic
+
+**Specificity (3.1).** Demonstrate absence of relevant interference, or compare against an
+orthogonal procedure, or justify from the technology. For a stability-indicating claim (2.4),
+include samples containing relevant degradation products: spiked with target analytes and known
+interferences, stressed physically and chemically, and aged or stress-stored product samples.
+
+**Response — linear (3.2.2.1).** Evaluate across the range. **A minimum of five concentrations,
+appropriately distributed, is recommended.** Report the plot, the correlation coefficient or
+coefficient of determination, the y-intercept, the slope, and *an analysis of the deviation of the
+actual data points from the regression line* — for a linear response, assess the impact of any
+non-random pattern in the residual plot. Data may be transformed (for example logarithmically) if
+necessary. Other approaches require justification.
+
+**Response — non-linear (3.2.2.2).** Some procedures are legitimately non-linear; immunoassays and
+cell-based assays commonly give an S-shaped curve, typically modelled with four- or five-parameter
+logistic functions. For these, **linearity of the concentration–response relationship is not
+required.** Assess the model by non-linear regression, and evaluate whether results are proportional
+to the true values across the range.
+
+**Response — multivariate (3.2.2.3).** Algorithms may be linear or non-linear. Accuracy depends on
+the distribution of calibration samples across the range and on the reference procedure's error.
+Assess how the residuals change across the calibration range, graphically.
+
+**Lower range limits (3.2.3).** Four approaches:
+
+| Approach | DL | QL |
+| --- | --- | --- |
+| Visual evaluation (3.2.3.1) | lowest reliably detected | lowest reliably quantitated |
+| Signal-to-noise (3.2.3.2) | S/N 3:1 generally acceptable | S/N at least 10:1 |
+| SD of response and slope (3.2.3.3) | 3.3σ / S | 10σ / S |
+| Accuracy and precision at the limit (3.2.3.4) | — | validated directly, not estimated |
+
+σ may come from the SD of blank responses, the residual SD of the regression line, or the SD of
+y-intercepts of regression lines. S is the calibration slope. Signal-to-noise applies only where
+there is baseline noise, and the noise region should sit around where the peak would appear.
+
+Reporting (3.2.3.5): give the limit **and the approach used**. An estimated limit should then be
+validated by analysing a suitable number of samples at or near it. **For impurity tests the QL must
+be at or below the reporting threshold.** Where the QL is well below the reporting limit — roughly
+ten times lower — the confirmatory validation may be omitted with justification.
+
+**Accuracy (3.3.1).** Establish across the reportable range under regular test conditions, including
+the sample matrix and the described preparation steps. Three routes: comparison against a reference
+material of known purity, a spiking study into matrix, or comparison against an orthogonal
+procedure. Accuracy can be inferred once precision, response within the range, and specificity are
+established.
+
+Recommended data (3.3.1.4): an appropriate number of determinations and levels across the reportable
+range — **for example 3 concentrations × 3 replicates of the full procedure.** Report as mean percent
+recovery of a known added amount, or as the difference between the mean and the accepted true value,
+**together with an appropriate 100(1−α)% confidence interval** or justified alternative interval. The
+observed interval should be compatible with the accuracy criterion. For impurities, state whether
+the determination is weight/weight or area percent. For quantitative multivariate procedures use
+RMSEP, compared against an acceptable RMSEC.
+
+**Precision (3.3.2).** Use authentic homogeneous samples, or artificially prepared ones if
+unavailable.
+
+- *Repeatability (3.3.2.1)*: **a minimum of 9 determinations covering the reportable range** (for
+ example 3 concentrations × 3 replicates), **or a minimum of 6 determinations at 100% of the test
+ concentration.**
+- *Intermediate precision (3.3.2.2)*: establish the effects of random events — typically different
+ days, environmental conditions, analysts, and equipment. **Studying these effects individually is
+ not necessary**, and design of experiments is encouraged. The extent should be justified from
+ development understanding and risk assessment (ICH Q14).
+- *Reproducibility (3.3.2.3)*: an inter-laboratory trial. **Usually not required for a regulatory
+ submission**, but consider it for pharmacopoeial standardisation or multi-site procedures.
+
+Recommended data (3.3.2.4): report the standard deviation, the relative standard deviation, and an
+appropriate 100(1−α)% confidence interval.
+
+**Combined accuracy and precision (3.3.3).** Instead of separate criteria, assess total impact
+against a single combined criterion, using a prediction interval, a tolerance interval, or a
+confidence interval. Report the combined value, describe the approach, and supply the individual
+results as supplemental information where they help justify suitability.
+
+**Robustness (3.4).** Deliberate variation of procedure parameters, plus stability of sample
+preparations and reagents over the duration of the procedure. Considered during development; may be
+submitted as development data case-by-case or made available on request. See ICH Q14 section 5.
+
+## Lifecycle, transfer, and prior knowledge
+
+Section 2.1 permits suitable development data (ICH Q14) to form part of the validation data, and
+allows abbreviated validation testing for an established platform procedure used for a new purpose,
+with scientific justification. A validation protocol must exist before the study, stating the
+intended purpose, the characteristics to be validated, and the associated criteria; where prior
+knowledge is used, justify it. Results are summarised in a validation report.
+
+The experimental design should reflect the number of replicates used in routine analysis to generate
+a reportable result, unless a different number is justified.
+
+Section 2.2 covers change: partial or full revalidation may be needed, decided on science and risk,
+and scoped to the characteristics the change affects. **Transfer** to another laboratory calls for
+partial or full revalidation and/or comparative analysis of representative samples; not performing
+transfer experiments requires justification. **Co-validation** across multiple sites can demonstrate
+the criteria are met and can simultaneously satisfy transfer at the participating sites.
+
+## Annex 2 — illustrative technique examples
+
+Non-mandatory worked examples, useful as a starting point for the robustness parameter list:
+
+| Table | Technique |
+| --- | --- |
+| 3 | Quantitative separation techniques (HPLC, GC, CE) for impurities or assay, and relative-area quantitation |
+| 4 | Elemental impurities by ICP-OES or ICP-MS |
+| 5 | Dissolution with HPLC as product performance test (corrected 30 Nov 2023) |
+| 6 | Quantitative ¹H-NMR for assay of a drug substance |
+| 7 | Biological assays |
+| 8 | Quantitative PCR |
+| 9 | Particle size measurement |
+| 10 | NIR analytical procedure |
+| 11 | Quantitative LC/MS |
+
+From Table 3, a detail worth carrying forward: **relative response factors.** Where the analyte
+responds differently from the reference material, calculate the RRF from the appropriate ratio of
+responses under final procedure conditions and document it. **If the RRF falls outside 0.8–1.2,
+apply a correction factor.** Where an impurity is overestimated, omitting the correction may be
+acceptable.
+
+## Multivariate procedures (2.5)
+
+Results come from a model relating many input variables to the property of interest. Validate in two
+phases:
+
+1. **Model development** — calibration plus internal testing. Test data may be a separate set or
+ part of the calibration set used rotationally, and are used to estimate performance and tune
+ parameters such as the number of PLS latent variables. See ICH Q14.
+2. **Model validation** — an independent validation set. For identification libraries, analyse
+ challenge samples *not* represented in the library to demonstrate discriminative ability.
+
+Samples need reference values or categories, normally from a validated or pharmacopoeial reference
+procedure whose performance **equals or exceeds** the expected performance of the multivariate
+procedure. Reference measurement and multivariate data collection should be on the same samples
+within a period short enough to assure sample and measurement stability. Describe any correlation or
+unit conversion, and any assumptions.
diff --git a/skills/analytical-method-validation/references/source-ledger.md b/skills/analytical-method-validation/references/source-ledger.md
new file mode 100644
index 00000000..be0d3cfc
--- /dev/null
+++ b/skills/analytical-method-validation/references/source-ledger.md
@@ -0,0 +1,125 @@
+# Official Source Ledger
+
+**Research date: 2026-07-27.** Every framework claim in this skill traces to an entry below.
+Re-check each source before operational use — guidelines are revised, editions change, and regional
+implementation dates differ from adoption dates.
+
+This ledger is a version baseline. It is not legal advice, an applicability determination, or a
+substitute for a controlled copy held under the laboratory's document control.
+
+## Documents read directly
+
+These were downloaded and read as full text on the research date, so the requirements encoded in
+`scripts/_catalog.py` and summarised in `references/ich-q2r2.md` and
+`references/ich-m10-bioanalytical.md` come from the primary source rather than from secondary
+summaries.
+
+### ICH Q2(R2) Validation of Analytical Procedures
+
+- Source read: <https://database.ich.org/sites/default/files/ICH_Q2%28R2%29_Guideline_2023_1130.pdf>
+- Verified metadata: Final Version, adopted by the ICH Assembly Regulatory Members under Step 4 on
+ **1 November 2023**. Step 2 endorsement 24 March 2022. Supersedes Q2(R1) (November 2005).
+- Verified detail: an **error correction dated 30 November 2023** covers Table 5 (dissolution with
+ HPLC, reportable range linearity formulae, page 25) and Tables 6–11 (pages 26–32).
+- Content taken: section structure; Table 1 (tests by measured attribute); Table 2 (reportable range
+ examples); recommended data for specificity, response, lower range limits, accuracy, precision,
+ and robustness; sections 2.1–2.5; Annex 1 and Annex 2 table inventory; the relative response factor
+ 0.8–1.2 rule from Annex 2 Table 3.
+- Licence: ICH permits use, reproduction, adaptation and distribution under a public licence provided
+ ICH's copyright is acknowledged. Acknowledged here and in `scripts/_catalog.py`.
+- Limitation: **adoption is not implementation.** Confirm the date from which your regional regulator
+ expects Q2(R2) with that regulator.
+
+### ICH M10 Bioanalytical Method Validation and Study Sample Analysis
+
+- Source read: <https://database.ich.org/sites/default/files/M10_Guideline_Step4_2022_0524.pdf>
+- Verified metadata: Step 4, dated **24 May 2022**.
+- Content taken: chromatographic criteria (section 3) — calibration levels and tolerances, QC
+ placement at four levels with the low/medium/high definitions, within-run and between-run accuracy
+ and precision design and criteria, routine-run QC pass rules, carry-over, selectivity source count,
+ dilution integrity, stability; ligand binding assay criteria (section 4) — calibration tolerances
+ including anchor point exclusion, five QC levels, run and replicate structure, accuracy and
+ precision criteria at LLOQ and ULOQ, and the total error criterion; incurred sample reanalysis
+ (section 5) including the percent-difference basis and the pass fractions.
+- Verified distinction: the **total error criterion (≤30%, ≤40% at LLOQ and ULOQ) appears for ligand
+ binding assays**. No equivalent criterion was found for chromatographic assays.
+- Licence: as for Q2(R2).
+- Limitation: regional implementation dates differ. Confirm with the regional regulator.
+
+### ICH Q14 Analytical Procedure Development
+
+- Source read: <https://database.ich.org/sites/default/files/ICH_Q14_Guideline_2023_1116.pdf>
+- Content taken: section structure; the minimal versus enhanced approaches (section 2.1); the
+ analytical target profile (section 3) and that its formal documentation and submission is
+ **optional**; robustness and parameter ranges (section 5); established conditions (section 6.1);
+ lifecycle management and post-approval change (section 7); multivariate procedures (section 8).
+- Adopted alongside Q2(R2) by the ICH Assembly in the same session.
+- Licence: as for Q2(R2).
+
+## Documents identified but not read (paywalled)
+
+Designation, title, and scope only. **No requirement, threshold, or study design from any of these is
+reproduced anywhere in this skill.** Where a numeric criterion is needed, read it from an authorised
+copy.
+
+### USP–NF general chapters
+
+- Official pages: `<1220>` <https://doi.usp.org/USPNF/USPNF_M10975_02_01.html>;
+ `<1225>` <https://doi.usp.org/USPNF/USPNF_M99945_40101_01.html>;
+ `<1226>` <https://doi.usp.org/USPNF/USPNF_M870_03_01.html>
+- Verified metadata for `<1220>`: incorporated into USP–NF 2022 Issue 1 on **1 November 2021**,
+ **official 1 May 2022**. It brings the concepts of `<1224>`, `<1225>` and `<1226>` into a single
+ three-stage lifecycle. `<1225>` covers validation, particularly Stage 2 activities under `<1220>`;
+ `<1226>` covers verification of compendial procedures.
+- Provenance limitation: this metadata came from **secondary sources** (publisher notices and trade
+ press) rather than from the USP–NF text, which is behind subscription. Marked
+ **[confirm in USP–NF]**. Confirm the current official text, revision, and any subsequent change.
+- Chapters referenced by designation only, not read: `<1224>`, `<1010>`, `<621>`, `<711>`, `<1092>`.
+
+### CLSI EP series
+
+- Publisher: <https://clsi.org/standards/products/method-evaluation/>
+- Designations and subjects recorded in `references/compendial-and-clsi.md`: EP05, EP06, EP07, EP09,
+ EP15, EP17, EP25, EP28 (formerly C28), plus the EP17IG and EP28IG implementation guides.
+- Provenance limitation: designations, titles and edition numbers were taken from **clsi.org product
+ listings and secondary sources** on the research date, not read from the documents. Every edition
+ number carries **[confirm edition]** in the reference file. Editions change; verify on clsi.org
+ before designing a study.
+
+### ISO standards
+
+- ISO/IEC 17025:2017 — <https://www.iso.org/standard/66912.html>. Edition 3; supersedes the 2005
+ edition. Relevant clauses: 7.2 (selection, verification and validation of methods), 7.6
+ (measurement uncertainty). Not read; identified by catalogue metadata.
+- ISO 15189, ISO 21748, ISO 5725 series — referenced by designation and scope only.
+- Provenance limitation: ISO catalogue pages have historically refused automated access. Confirm
+ edition and status on iso.org or with a national member body. **[confirm on iso.org]**
+- See this repository's `iso-standards-readiness` skill and its own source ledger for the
+ accreditation-level treatment of these standards.
+
+## Statistical methods
+
+The statistical procedures in `references/statistics.md` and `scripts/_common.py` are standard
+published methods, not requirements of any framework:
+
+- Incomplete beta and gamma function implementations follow the standard continued-fraction and series
+ algorithms; the t, chi-square and F distributions are derived from them.
+- Lack-of-fit F test against pure error: standard regression ANOVA.
+- Wald–Wolfowitz runs test: standard non-parametric test of randomness in a sequence of signs.
+- One-way random-effects variance components with the standard unbalanced expected-mean-square
+ coefficient; Satterthwaite approximation for effective degrees of freedom of the total.
+- Deming regression with jackknife standard errors; Passing–Bablok with the rank-based slope interval.
+- Bland–Altman bias and limits of agreement.
+- Two one-sided tests (TOST) for equivalence.
+
+Implementations are verified against published quantiles and hand-checkable cases in
+`tests/analytical-method-validation/test_scripts.py`. Where a framework prescribes a specific
+statistical treatment, the framework governs — these are the general-purpose tools.
+
+## What is deliberately absent
+
+- No numeric acceptance criteria are supplied for ICH Q2(R2) work. The guideline does not set them and
+ neither does this skill; they come from the specification, the analytical target profile, or
+ development data.
+- No text, table, threshold, or study design from any USP, CLSI, or ISO document.
+- No claim that a procedure is validated, a run acceptable, or an investigation closed.
diff --git a/skills/analytical-method-validation/references/statistics.md b/skills/analytical-method-validation/references/statistics.md
new file mode 100644
index 00000000..dfa649b8
--- /dev/null
+++ b/skills/analytical-method-validation/references/statistics.md
@@ -0,0 +1,209 @@
+# The Statistics, and Why Each One
+
+Every method in this file is implemented in `scripts/_common.py` using only the standard library.
+Distribution functions are computed from the regularised incomplete beta and gamma functions, and the
+implementations are checked against published quantiles in `tests/analytical-method-validation/`.
+
+## Calibration response
+
+### r² is not evidence of linearity
+
+The coefficient of determination measures how much of the variance in response the model explains. It
+rises with the width of the calibration range and is nearly insensitive to curvature. A quadratic
+response measured over a decade of concentration routinely gives r² > 0.99 while the back-calculated
+result at the bottom of the range is 10% wrong.
+
+ICH Q2(R2) 3.2.2.1 asks for r or r², the slope, the intercept, the plot, **and an analysis of the
+deviation of the actual data points from the regression line**. The last item is the one that
+detects a bad model. Report r² because the guideline asks for it, not because it demonstrates
+anything.
+
+### Lack-of-fit F test
+
+The correct test of a linear calibration model, and it requires replicates at some levels.
+
+Partition the residual sum of squares into **pure error** (scatter among replicates at the same
+level, which no model can explain) and **lack of fit** (systematic deviation of level means from the
+line):
+
+```
+F = MS_lack-of-fit / MS_pure-error, df = (k - 2, n - k)
+```
+
+for `k` distinct levels and `n` total points. A significant F says the straight line fails to
+describe the data beyond what replicate scatter explains. Without replicates the partition is
+impossible and no linearity test exists — which is a good reason to replicate at least one level, and
+a reason `check_response.py` says so explicitly when it cannot run the test.
+
+### Residual pattern: runs test
+
+Curvature makes residual signs cluster: all negative at the ends and positive in the middle, or the
+reverse. The Wald–Wolfowitz runs test counts sign changes and compares against the number expected
+if signs were random. Too few runs is evidence of systematic misfit. It complements the F test and
+works when replicates are absent, though it needs at least eight points with both signs present.
+
+### Heteroscedasticity and weighting
+
+Chromatographic response variance usually scales with concentration. Unweighted least squares
+minimises absolute squared residuals, so the high-concentration points — which have the largest
+absolute residuals — dominate the fit. The result is a curve that is accurate at the top of the range
+and biased at the bottom, which is exactly where an impurity reporting threshold or an LLOQ sits.
+
+`check_response.py` compares residual variance in the top and bottom thirds of the range. A ratio
+above roughly 10× with an unweighted fit is flagged; `1/x` or `1/x²` weighting is the usual remedy.
+State the weighting in the protocol before validation — switching to weighting after seeing the data
+to make the low end pass is not a statistical decision.
+
+### Back-calculated relative error
+
+The practical criterion: invert the fitted line, compute the concentration each response implies,
+and compare against nominal at each level. This is what the procedure will actually report, and it
+exposes a bad model in units an analyst and an assessor both understand. Bioanalytical work has
+required it for decades; it belongs in small-molecule QC validation too.
+
+## Precision
+
+### Estimate within each level, never pooled across levels
+
+Pooling results from 80%, 100% and 120% levels into one standard deviation makes the range itself
+appear as imprecision. The number produced is meaningless and always too large.
+`check_accuracy_precision.py` estimates precision within each level, and separately provides a
+level-independent view by converting to percent of nominal first.
+
+### Repeatability and intermediate precision are different quantities
+
+A one-way random-effects model on the intermediate-precision factor — day, analyst, or instrument:
+
+```
+observation = grand mean + group effect + residual
+```
+
+with `MS_within` and `MS_between` from the ANOVA table:
+
+```
+s²_repeatability = MS_within
+s²_between = max(0, (MS_between - MS_within) / n_effective)
+s²_intermediate = s²_repeatability + s²_between
+```
+
+For a balanced design `n_effective` is the replicates per group; unbalanced designs use the standard
+expected-mean-square coefficient, which the script reports when it applies.
+
+The between-group variance is truncated at zero because a negative variance estimate is not
+meaningful — it means the data cannot distinguish the groups. The script says so when it happens
+rather than silently reporting zero.
+
+Why this matters: a procedure can show 0.07% RSD within a day and 1.65% RSD across days. The
+within-day figure is real, and reporting it as the procedure's precision understates routine
+performance by more than twenty-fold. Q2(R2) 3.3.2.2 exists precisely because the between-day
+component is the one that bites in routine use.
+
+### Confidence intervals on a standard deviation
+
+A precision estimate from six or nine determinations is imprecise, and Q2(R2) 3.3.2.4 asks for an
+interval alongside it. For a variance with `ν` degrees of freedom:
+
+```
+s · sqrt(ν / χ²_{1-α/2, ν}) < σ < s · sqrt(ν / χ²_{α/2, ν})
+```
+
+These intervals are wide, and that is the point. With ν = 5 the upper bound is roughly twice the
+point estimate. An RSD that lands just inside a limit on six replicates has not demonstrated that the
+procedure meets the limit. For the total (intermediate) SD, which is a sum of variance components,
+the effective degrees of freedom come from the Satterthwaite approximation.
+
+## Accuracy
+
+Report mean percent recovery, or the difference from the accepted true value, **with a confidence
+interval** — Q2(R2) 3.3.1.4 is explicit, and a bare mean is not sufficient. The interval is
+`mean ± t_{1-α/2, n-1} · s/√n` at each level.
+
+The stricter reading, available as `--require-ci-within-limit`, asks that the whole interval sit
+inside the acceptance limit rather than just the point estimate. Q2(R2) says the observed interval
+should be *compatible with* the criterion. Which reading applies is a decision to make and justify in
+the protocol, before the data exist.
+
+### Combined accuracy and precision
+
+Q2(R2) 3.3.3 permits a single combined criterion assessed with a prediction interval, a tolerance
+interval, or a confidence interval, instead of separate accuracy and precision criteria. This is
+often the more honest framing — what matters is whether a future reportable result will be close
+enough to the truth, which is a tolerance-interval question. If you use it, describe the approach and
+supply the individual results as supporting information.
+
+## Detection and quantitation limits
+
+The `3.3σ/S` and `10σ/S` formulae are estimates whose value depends entirely on which σ you choose.
+On the same calibration data, σ from the residual SD of the regression, from the SD of the
+y-intercept, and from the SD of blank responses commonly give limits spanning a factor of two or
+more. None is wrong; they answer slightly different questions.
+
+Consequences for practice:
+
+- Report the limit **and the approach**, per Q2(R2) 3.2.3.5. A number alone is not reportable.
+- Confirm an estimated limit with real determinations at or near it. `3.2.3.4` allows skipping the
+ estimate entirely and validating the QL directly by accuracy and precision, which is cleaner.
+- For impurity procedures, the QL must be at or below the reporting threshold.
+- Signal-to-noise scaling assumes noise is constant with concentration. It usually is not; confirm at
+ the resulting level.
+- CLSI's limit of blank / limit of detection / limit of quantitation are defined differently again,
+ with their own protocols. Do not translate between the schemes casually.
+
+## Method comparison and transfer
+
+### Ordinary least squares is the wrong regression here
+
+OLS assumes the x values are known without error. In a method comparison both procedures have
+measurement error, and ignoring the error in x biases the slope toward zero — a regression-dilution
+effect that manufactures apparent proportional bias where none exists.
+
+**Deming regression** accounts for error in both variables given `λ`, the ratio of error variances.
+With `λ = 1` (equal precision) it reduces to orthogonal regression. Standard errors here come from a
+jackknife, which avoids distributional assumptions about the slope.
+
+**Passing–Bablok** is non-parametric: the slope is a shifted median of all pairwise slopes, with a
+rank-based confidence interval. It assumes no distribution, tolerates outliers, and is the usual
+choice in clinical method comparison. Its confidence intervals are wider, honestly reflecting what
+the data support.
+
+Report both. Agreement between them is reassuring; disagreement points to outliers or to a
+distributional problem worth understanding before concluding anything.
+
+### Bland–Altman answers a different question
+
+Regression asks whether the relationship is proportional. Bland–Altman asks how far apart two
+procedures are on the same sample: mean difference (bias) and limits of agreement at
+`bias ± 1.96·SD`. Both matter, and neither substitutes for the other.
+
+Two cautions. The limits of agreement are themselves estimates with confidence intervals, which are
+wide for small n — the script reports the half-width. And if the difference trends with
+concentration, a single mean bias and its limits are misleading no matter how tight they look; the
+script tests for that trend and flags it.
+
+### Equivalence: TOST, not a t test
+
+The default reflex at a transfer is a two-sample or paired t test, and `p > 0.05` written up as "no
+significant difference, methods equivalent". This inverts the logic. A non-significant result means
+the data were insufficient to detect a difference — and on a transfer dataset of ten or twenty
+samples, that outcome is close to guaranteed regardless of whether the procedures agree. The test
+rewards small studies.
+
+**Two one-sided tests** invert the hypotheses to match the question. Given a pre-stated margin `δ`,
+test both `H01: difference ≤ -δ` and `H02: difference ≥ +δ`. Rejecting both concludes equivalence.
+Operationally: the `(1-2α)` confidence interval on the difference must lie entirely inside `±δ`.
+
+A worked contrast from `compare_methods.py`: a transfer with a consistent +1.46% bias gives a paired
+t-test p-value below 0.0001 — a highly significant difference — while TOST establishes equivalence at
+a ±2% margin. Both are correct. The difference is real and it is small enough not to matter. Only
+TOST answers the question the transfer actually asks.
+
+The margin must be pre-stated, from the specification or the analytical target profile. A margin
+chosen after seeing the data is not an acceptance criterion, and this is the single most common way
+equivalence testing gets misused.
+
+## What none of this does
+
+These are computations. They do not establish that a procedure is fit for purpose. That conclusion
+requires the intended purpose, the specification, product and process knowledge, the laboratory's
+history with the technique, and the judgement of people who are accountable for it. A script that
+reported "validated" would be lying about what it can know.
diff --git a/skills/analytical-method-validation/scripts/_catalog.py b/skills/analytical-method-validation/scripts/_catalog.py
new file mode 100644
index 00000000..29115549
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/_catalog.py
@@ -0,0 +1,499 @@
+#!/usr/bin/env python3
+"""Framework catalogue for analytical method validation.
+
+Content sourced 2026-07-27 from the freely published ICH guidelines, which ICH
+licenses for reuse with acknowledgement. Compendial (USP) and CLSI documents are
+copyrighted and paywalled: they are referenced here by designation, title, and
+scope only. No proprietary text is reproduced.
+
+See ../references/source-ledger.md for the provenance of every entry.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+RESEARCH_DATE = "2026-07-27"
+
+# --------------------------------------------------------------------------
+# Frameworks
+# --------------------------------------------------------------------------
+
+FRAMEWORKS: dict[str, dict[str, Any]] = {
+ "ich-q2r2": {
+ "title": "ICH Q2(R2) Validation of Analytical Procedures",
+ "adopted": "2023-11-01",
+ "effective_note": (
+ "Adopted by the ICH Assembly 1 Nov 2023; an error correction to Table 5 and "
+ "Tables 6-11 is dated 30 Nov 2023. Confirm the adoption/implementation date for "
+ "your region with the regional regulator."
+ ),
+ "supersedes": "ICH Q2(R1) (2005)",
+ "url": "https://database.ich.org/sites/default/files/ICH_Q2%28R2%29_Guideline_2023_1130.pdf",
+ "reproducible": True,
+ "scope": (
+ "Analytical procedures for release and stability testing of commercial drug "
+ "substances and products; applicable to other control-strategy procedures on a "
+ "risk basis, and phase-appropriately during clinical development."
+ ),
+ "governs": ["assay", "potency", "purity", "impurity-quantitative",
+ "impurity-limit", "identity", "dissolution", "content-uniformity"],
+ "companion": "ICH Q14 (analytical procedure development, robustness, lifecycle)",
+ },
+ "ich-m10": {
+ "title": "ICH M10 Bioanalytical Method Validation and Study Sample Analysis",
+ "adopted": "2022-05-24",
+ "effective_note": (
+ "Step 4 adopted 24 May 2022. Regional implementation dates differ; confirm with "
+ "the regional regulator."
+ ),
+ "supersedes": (
+ "Harmonises region-specific bioanalytical guidance (e.g., FDA 2018 BMV, "
+ "EMA 2011); what it replaces depends on the region's implementation"
+ ),
+ "url": "https://database.ich.org/sites/default/files/M10_Guideline_Step4_2022_0524.pdf",
+ "reproducible": True,
+ "scope": (
+ "Bioanalytical methods quantifying drug/metabolite concentrations in biological "
+ "matrices supporting nonclinical and clinical studies, plus study sample analysis."
+ ),
+ "governs": ["pk-concentration", "toxicokinetics", "bioequivalence", "biomarker-selected"],
+ "companion": "Distinct criteria for chromatographic methods vs ligand binding assays",
+ },
+ "usp-1220": {
+ "title": "USP General Chapter <1220> Analytical Procedure Life Cycle",
+ "adopted": "official 2022-05-01",
+ "effective_note": (
+ "Incorporated into USP-NF 2022 Issue 1 (1 Nov 2021), official 1 May 2022. "
+ "Confirm the current official text and any revision in the USP-NF."
+ ),
+ "supersedes": "integrates the concepts of <1224>, <1225>, and <1226> into a lifecycle",
+ "url": "https://doi.usp.org/USPNF/USPNF_M10975_02_01.html",
+ "reproducible": False,
+ "scope": (
+ "Three-stage lifecycle: procedure design (Stage 1), performance qualification "
+ "(Stage 2), ongoing performance verification (Stage 3), organised around an "
+ "analytical target profile."
+ ),
+ "governs": ["compendial-lifecycle"],
+ "companion": "<1225> validation, <1226> verification, <1224> transfer, <1010> data treatment",
+ },
+ "usp-1225": {
+ "title": "USP General Chapter <1225> Validation of Compendial Procedures",
+ "adopted": "see current USP-NF",
+ "effective_note": "Confirm the current official text and revision in the USP-NF.",
+ "supersedes": "",
+ "url": "https://doi.usp.org/USPNF/USPNF_M99945_40101_01.html",
+ "reproducible": False,
+ "scope": (
+ "Validation of non-compendial procedures and of compendial procedures used "
+ "outside their stated scope; Stage 2 activities under <1220>."
+ ),
+ "governs": ["assay", "impurity-quantitative", "impurity-limit", "identity"],
+ "companion": "<1226> when verifying a compendial procedure as written",
+ },
+ "usp-1226": {
+ "title": "USP General Chapter <1226> Verification of Compendial Procedures",
+ "adopted": "see current USP-NF",
+ "effective_note": "Confirm the current official text and revision in the USP-NF.",
+ "supersedes": "",
+ "url": "https://doi.usp.org/USPNF/USPNF_M870_03_01.html",
+ "reproducible": False,
+ "scope": (
+ "Assessment of selected performance characteristics to show a compendial "
+ "procedure works under actual conditions of use. Verification is not "
+ "revalidation and does not repeat the full validation."
+ ),
+ "governs": ["compendial-verification"],
+ "companion": "<1225> when the procedure is used outside its compendial scope",
+ },
+ "clsi": {
+ "title": "CLSI EP series (clinical laboratory measurement procedures)",
+ "adopted": "per document",
+ "effective_note": (
+ "Editions change; the designations below were taken from clsi.org listings and "
+ "secondary sources on the research date and are marked [confirm on clsi.org]. "
+ "Verify the current edition before designing a study."
+ ),
+ "supersedes": "",
+ "url": "https://clsi.org/standards/products/method-evaluation/",
+ "reproducible": False,
+ "scope": (
+ "Establishment and user verification of performance for clinical laboratory "
+ "measurement procedures, under CLIA/CAP and ISO 15189 quality systems."
+ ),
+ "governs": ["clinical-verification", "clinical-establishment"],
+ "companion": "ISO 15189 for the surrounding medical laboratory quality system",
+ },
+ "iso-17025": {
+ "title": "ISO/IEC 17025:2017 (testing and calibration laboratory competence)",
+ "adopted": "2017",
+ "effective_note": "Copyrighted. Obtain an authorised copy from ISO or a national member.",
+ "supersedes": "ISO/IEC 17025:2005",
+ "url": "https://www.iso.org/standard/66912.html",
+ "reproducible": False,
+ "scope": (
+ "Clause 7.2 covers selection, verification and validation of methods; clause 7.6 "
+ "covers measurement uncertainty. Method validation is required to the extent "
+ "necessary for the intended use."
+ ),
+ "governs": ["nonstandard-method", "lab-developed-method", "modified-standard-method"],
+ "companion": (
+ "The repo's iso-standards-readiness skill covers the surrounding quality system; "
+ "this skill covers the individual procedure."
+ ),
+ },
+}
+
+# --------------------------------------------------------------------------
+# ICH Q2(R2) Table 1 -- which validation tests for which measured attribute
+# Source: ICH Q2(R2), Table 1. "+" normally conducted, "-" not normally conducted.
+# --------------------------------------------------------------------------
+
+Q2R2_TESTS_BY_ATTRIBUTE: dict[str, dict[str, str]] = {
+ "identity": {
+ "specificity": "required",
+ "response": "not-normally",
+ "lower-range-limit": "not-normally",
+ "accuracy": "not-normally",
+ "repeatability": "not-normally",
+ "intermediate-precision": "not-normally",
+ },
+ "impurity-quantitative": {
+ "specificity": "required",
+ "response": "required",
+ "lower-range-limit": "required-QL",
+ "accuracy": "required",
+ "repeatability": "required",
+ "intermediate-precision": "required-unless-reproducibility",
+ },
+ "impurity-limit": {
+ "specificity": "required",
+ "response": "not-normally",
+ "lower-range-limit": "required-DL",
+ "accuracy": "not-normally",
+ "repeatability": "not-normally",
+ "intermediate-precision": "not-normally",
+ },
+ "assay": {
+ "specificity": "required",
+ "response": "required",
+ "lower-range-limit": "not-normally",
+ "accuracy": "required",
+ "repeatability": "required",
+ "intermediate-precision": "required-unless-reproducibility",
+ },
+}
+
+ATTRIBUTE_ALIASES = {
+ "content": "assay",
+ "potency": "assay",
+ "assay": "assay",
+ "identification": "identity",
+ "identity": "identity",
+ "id": "identity",
+ "impurity": "impurity-quantitative",
+ "impurities": "impurity-quantitative",
+ "related-substances": "impurity-quantitative",
+ "purity": "impurity-quantitative",
+ "impurity-quantitative": "impurity-quantitative",
+ "impurity-limit": "impurity-limit",
+ "limit-test": "impurity-limit",
+}
+
+# ICH Q2(R2) Table 2 -- examples of reportable ranges.
+Q2R2_REPORTABLE_RANGE: dict[str, dict[str, str]] = {
+ "assay": {
+ "low": "80% of declared content, or 80% of the lower specification acceptance criterion",
+ "high": "120% of declared content, or 120% of the upper specification acceptance criterion",
+ },
+ "potency": {
+ "low": "lowest specification acceptance criterion -20%",
+ "high": "highest specification acceptance criterion +20%",
+ },
+ "content-uniformity": {
+ "low": "70% of declared content",
+ "high": "130% of declared content",
+ },
+ "dissolution-ir-one-point": {
+ "low": "Q - 45% of the lowest strength specification",
+ "high": "(per specification; see ICH Q2(R2) Table 2)",
+ },
+ "dissolution-ir-multi-point": {
+ "low": "lower limit of reportable range as justified by the specification, or QL",
+ "high": "130% of declared content of the highest strength",
+ },
+ "dissolution-modified-release": {
+ "low": "lower limit of reportable range as justified by the specification, or QL",
+ "high": "(per specification; see ICH Q2(R2) Table 2)",
+ },
+ "impurity-quantitative": {
+ "low": "reporting threshold",
+ "high": "120% of the specification acceptance criterion",
+ },
+ "purity-area-percent": {
+ "low": "80% of the lower specification acceptance criterion",
+ "high": "upper specification acceptance criterion, or 100%",
+ },
+}
+
+# ICH Q2(R2) recommended data, section 3.
+Q2R2_STUDY_DESIGN: dict[str, dict[str, str]] = {
+ "response": {
+ "requirement": "minimum of 5 concentrations appropriately distributed across the range",
+ "reference": "Q2(R2) 3.2.2.1",
+ "report": (
+ "plot of the data, correlation coefficient or coefficient of determination, "
+ "y-intercept, slope, and an analysis of deviation of points from the line "
+ "(residual pattern)"
+ ),
+ },
+ "accuracy": {
+ "requirement": (
+ "appropriate number of determinations and levels across the reportable range "
+ "(e.g., 3 concentrations / 3 replicates each of the full procedure)"
+ ),
+ "reference": "Q2(R2) 3.3.1.4",
+ "report": (
+ "mean percent recovery of a known added amount, or difference between mean and "
+ "accepted true value, with a 100(1-alpha)% confidence interval"
+ ),
+ },
+ "repeatability": {
+ "requirement": (
+ "minimum 9 determinations covering the reportable range (e.g., 3 concentrations "
+ "/ 3 replicates), or minimum 6 determinations at 100% of the test concentration"
+ ),
+ "reference": "Q2(R2) 3.3.2.1",
+ "report": "standard deviation, relative standard deviation, and a 100(1-alpha)% CI",
+ },
+ "intermediate-precision": {
+ "requirement": (
+ "effects of random events -- typically different days, environmental conditions, "
+ "analysts, equipment. Studying effects individually is not necessary; DoE is "
+ "encouraged. Extent justified by development understanding and risk (ICH Q14)"
+ ),
+ "reference": "Q2(R2) 3.3.2.2",
+ "report": "standard deviation, relative standard deviation, and a 100(1-alpha)% CI",
+ },
+ "reproducibility": {
+ "requirement": (
+ "inter-laboratory trial; usually NOT required for a regulatory submission, but "
+ "consider for pharmacopoeial standardisation or multi-site procedures"
+ ),
+ "reference": "Q2(R2) 3.3.2.3",
+ "report": "standard deviation, relative standard deviation, and a 100(1-alpha)% CI",
+ },
+ "specificity": {
+ "requirement": (
+ "absence of interference, orthogonal procedure comparison, or technology-inherent "
+ "justification. For a stability-indicating claim, include samples containing "
+ "relevant degradation products (spiked, stressed, or aged)"
+ ),
+ "reference": "Q2(R2) 3.1, 2.4",
+ "report": "interference data, resolution/peak purity, or orthogonal comparison",
+ },
+ "lower-range-limit": {
+ "requirement": (
+ "DL/QL by visual evaluation, signal-to-noise, standard deviation of the response "
+ "and slope, or direct accuracy and precision at the lower limit. An estimated "
+ "limit should then be confirmed with samples at or near that limit"
+ ),
+ "reference": "Q2(R2) 3.2.3",
+ "report": "the limit and the approach used to determine it",
+ },
+ "robustness": {
+ "requirement": (
+ "deliberate variation of procedure parameters plus solution stability. Normally "
+ "performed during development under ICH Q14; submitted case-by-case or available "
+ "on request"
+ ),
+ "reference": "Q2(R2) 3.4, ICH Q14 section 5",
+ "report": "parameters varied, ranges, and the effect on the reportable result",
+ },
+}
+
+# ICH Q2(R2) 3.2.3.2/3.2.3.3 -- the estimation approaches and their constants.
+DL_QL_APPROACHES = {
+ "visual": {
+ "dl": "lowest level reliably detected by analysis of known concentrations",
+ "ql": "lowest level reliably quantitated by analysis of known concentrations",
+ "note": "acceptable for both non-instrumental and instrumental procedures",
+ },
+ "signal-to-noise": {
+ "dl": "S/N of 3:1 generally acceptable",
+ "ql": "S/N of at least 10:1 acceptable",
+ "note": "only for procedures exhibiting baseline noise; define the noise region",
+ },
+ "sd-and-slope": {
+ "dl": "DL = 3.3 * sigma / S",
+ "ql": "QL = 10 * sigma / S",
+ "note": (
+ "sigma from the SD of blank responses, the residual SD of the regression line, "
+ "or the SD of y-intercepts of regression lines; S is the calibration slope"
+ ),
+ },
+ "accuracy-precision": {
+ "dl": "not applicable",
+ "ql": "QL validated directly by accuracy and precision at the lower range limit",
+ "note": "avoids relying on an estimate; Q2(R2) 3.2.3.4",
+ },
+}
+
+# --------------------------------------------------------------------------
+# ICH M10 acceptance criteria. Verified against the Step 4 guideline text.
+# Chromatographic (CC) and ligand binding assay (LBA) criteria differ and are
+# the single most commonly conflated pair in bioanalysis.
+# --------------------------------------------------------------------------
+
+M10_CRITERIA: dict[str, dict[str, Any]] = {
+ "chromatographic": {
+ "label": "Chromatographic assays (ICH M10 section 3)",
+ "calibration_min_levels": 6,
+ "calibration_tolerance_pct": 15.0,
+ "calibration_tolerance_lloq_pct": 20.0,
+ "calibration_tolerance_uloq_pct": 15.0,
+ "calibration_min_pass_fraction": 0.75,
+ "accuracy_tolerance_pct": 15.0,
+ "accuracy_tolerance_lloq_pct": 20.0,
+ "precision_cv_pct": 15.0,
+ "precision_cv_lloq_pct": 20.0,
+ "limit_levels": "LLOQ",
+ "qc_levels_accuracy_precision": 4,
+ "qc_levels_routine_run": 3,
+ "ap_replicates_per_run": 5,
+ "ap_min_runs": 3,
+ "ap_min_days": 2,
+ "qc_run_pass_fraction": 2.0 / 3.0,
+ "qc_run_pass_fraction_per_level": 0.50,
+ "qc_run_tolerance_pct": 15.0,
+ "total_error_pct": None,
+ "total_error_pct_at_limits": None,
+ "isr_tolerance_pct": 20.0,
+ "isr_pass_fraction": 2.0 / 3.0,
+ "carryover_blank_pct_of_lloq": 20.0,
+ "carryover_blank_pct_of_is": 5.0,
+ "selectivity_min_sources": 6,
+ "dilution_tolerance_pct": 15.0,
+ "stability_tolerance_pct": 15.0,
+ "notes": (
+ "Accuracy/precision validation QCs at a minimum of 4 levels: LLOQ, low QC within "
+ "3x the LLOQ, medium QC around 30-50% of the calibration range, and high QC at "
+ "least 75% of the ULOQ. Within-run uses at least 5 replicates per level per run; "
+ "between-run uses each level in at least 3 runs over at least 2 days. Routine "
+ "(non-accuracy-and-precision) runs may use low, medium and high QCs in duplicate. "
+ "M10 states no explicit total-error criterion for chromatographic assays."
+ ),
+ },
+ "lba": {
+ "label": "Ligand binding assays (ICH M10 section 4)",
+ "calibration_min_levels": 6,
+ "calibration_tolerance_pct": 20.0,
+ "calibration_tolerance_lloq_pct": 25.0,
+ "calibration_tolerance_uloq_pct": 25.0,
+ "calibration_min_pass_fraction": 0.75,
+ "accuracy_tolerance_pct": 20.0,
+ "accuracy_tolerance_lloq_pct": 25.0,
+ "precision_cv_pct": 20.0,
+ "precision_cv_lloq_pct": 25.0,
+ "limit_levels": "LLOQ and ULOQ",
+ "qc_levels_accuracy_precision": 5,
+ "qc_levels_routine_run": 3,
+ "ap_replicates_per_run": 3,
+ "ap_min_runs": 6,
+ "ap_min_days": 2,
+ "qc_run_pass_fraction": 2.0 / 3.0,
+ "qc_run_pass_fraction_per_level": 0.50,
+ "qc_run_tolerance_pct": 20.0,
+ "total_error_pct": 30.0,
+ "total_error_pct_at_limits": 40.0,
+ "isr_tolerance_pct": 30.0,
+ "isr_pass_fraction": 2.0 / 3.0,
+ "carryover_blank_pct_of_lloq": None,
+ "carryover_blank_pct_of_is": None,
+ "selectivity_min_sources": 6,
+ "dilution_tolerance_pct": 20.0,
+ "stability_tolerance_pct": 20.0,
+ "notes": (
+ "Anchor points outside the quantitation range are excluded from the calibration "
+ "pass count. Accuracy and precision are evaluated at 5 QC levels (LLOQ, low, "
+ "medium, high, ULOQ) with at least 3 replicates per level per run in at least 6 "
+ "runs over 2 or more days. LBAs carry an additional total-error criterion: the "
+ "sum of absolute accuracy (%) and precision (%) must not exceed 30%, or 40% at "
+ "the LLOQ and ULOQ. Chromatographic assays have no such criterion."
+ ),
+ },
+}
+
+# --------------------------------------------------------------------------
+# Technique notes distilled from ICH Q2(R2) Annex 2 (illustrative, not mandatory)
+# --------------------------------------------------------------------------
+
+TECHNIQUE_NOTES: dict[str, dict[str, str]] = {
+ "hplc": {
+ "annex_table": "Table 3 (quantitative separation techniques)",
+ "robustness": (
+ "extraction volume/time/temperature, dilution, column or capillary lot, mobile "
+ "phase and buffer composition and pH, column temperature, flow rate, detection "
+ "wavelength; plus stability of sample and reference preparations"
+ ),
+ "special": (
+ "Relative response factors: if the RRF falls outside 0.8-1.2, apply a correction "
+ "factor. If an impurity is overestimated it may be acceptable to omit the "
+ "correction. Determine RRF under final procedure conditions and document it."
+ ),
+ },
+ "gc": {"annex_table": "Table 3 (quantitative separation techniques)",
+ "robustness": "as for HPLC, plus inlet temperature, split ratio, carrier flow, oven ramp",
+ "special": "same relative response factor 0.8-1.2 consideration as HPLC"},
+ "ce": {"annex_table": "Table 3 (quantitative separation techniques)",
+ "robustness": "capillary lot, buffer composition and pH, capillary temperature, voltage",
+ "special": "same relative response factor consideration as HPLC"},
+ "icp": {"annex_table": "Table 4 (elemental impurities by ICP-OES or ICP-MS)",
+ "robustness": "plasma conditions, sample introduction, internal standard, matrix matching",
+ "special": "spectral and non-spectral interference; ICH Q3D drives which elements matter"},
+ "dissolution": {"annex_table": "Table 5 (dissolution with HPLC as product performance test)",
+ "robustness": "medium composition and volume, deaeration, agitation, sinker, filter",
+ "special": (
+ "Table 5 was corrected on 30 Nov 2023 (reportable range linearity "
+ "formulae). Use the corrected text."
+ )},
+ "qnmr": {"annex_table": "Table 6 (quantitative 1H-NMR for assay of a drug substance)",
+ "robustness": "pulse angle, relaxation delay, number of scans, temperature, shimming",
+ "special": "internal standard purity and signal selection dominate accuracy"},
+ "bioassay": {"annex_table": "Table 7 (biological assays)",
+ "robustness": "cell passage, incubation time and temperature, reagent lot, plate layout",
+ "special": (
+ "Non-linear (4- or 5-parameter logistic) response is expected. Linearity "
+ "of the concentration-response relationship is NOT required; evaluate "
+ "proportionality of results to expected values instead."
+ )},
+ "qpcr": {"annex_table": "Table 8 (quantitative PCR)",
+ "robustness": "primer/probe lot, master mix, cycling parameters, template input",
+ "special": "amplification efficiency and specificity of amplicon detection"},
+ "particle-size": {"annex_table": "Table 9 (particle size measurement)",
+ "robustness": "dispersion medium, sonication, pump speed, obscuration",
+ "special": "technology-inherent justification may substitute for some characteristics"},
+ "nir": {"annex_table": "Table 10 (NIR analytical procedure)",
+ "robustness": "instrument, probe, sample presentation, temperature, humidity",
+ "special": (
+ "Multivariate: validate in two phases (calibration plus internal testing, "
+ "then an independent validation set). Report RMSEP against RMSEC. Reference "
+ "procedure performance must equal or exceed the multivariate procedure's."
+ )},
+ "lcms": {"annex_table": "Table 11 (quantitative LC/MS)",
+ "robustness": "source conditions, mobile phase additives, column lot, matrix lots",
+ "special": (
+ "Matrix effects and ion suppression need explicit evaluation. For a "
+ "bioanalytical purpose, ICH M10 governs instead of Q2(R2)."
+ )},
+}
+
+
+def resolve_attribute(name: str) -> str:
+ key = name.strip().lower().replace("_", "-")
+ if key in ATTRIBUTE_ALIASES:
+ return ATTRIBUTE_ALIASES[key]
+ raise KeyError(
+ f"unknown attribute {name!r}; choose from: {', '.join(sorted(set(ATTRIBUTE_ALIASES)))}"
+ )
diff --git a/skills/analytical-method-validation/scripts/_common.py b/skills/analytical-method-validation/scripts/_common.py
new file mode 100644
index 00000000..50332f25
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/_common.py
@@ -0,0 +1,955 @@
+#!/usr/bin/env python3
+"""Shared statistics and I/O for analytical method validation checks.
+
+Standard library only. Every distribution function here is implemented from the
+regularised incomplete beta and gamma functions so the scripts run in any
+Python 3.11+ interpreter without numpy or scipy.
+
+These helpers compute and report. They never decide that a procedure is
+validated, fit for purpose, or acceptable to a regulator -- that judgement
+belongs to the analyst and the quality unit.
+"""
+
+from __future__ import annotations
+
+import argparse
+import csv
+import io
+import json
+import math
+import sys
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any, Iterable, Sequence
+
+# --------------------------------------------------------------------------
+# Limits and exit codes
+# --------------------------------------------------------------------------
+
+MAX_INPUT_BYTES = 5_000_000
+MAX_ROWS = 20_000
+
+EXIT_OK = 0
+EXIT_FINDINGS = 1
+EXIT_INPUT_ERROR = 2
+
+TINY = 1e-300
+
+
+class InputError(Exception):
+ """Raised for malformed or out-of-bounds user input."""
+
+
+# --------------------------------------------------------------------------
+# Special functions
+# --------------------------------------------------------------------------
+
+
+def _betacf(a: float, b: float, x: float, itmax: int = 400, eps: float = 3e-16) -> float:
+ """Continued fraction for the incomplete beta function (Lentz's method)."""
+ qab, qap, qam = a + b, a + 1.0, a - 1.0
+ c = 1.0
+ d = 1.0 - qab * x / qap
+ if abs(d) < TINY:
+ d = TINY
+ d = 1.0 / d
+ h = d
+ for m in range(1, itmax + 1):
+ m2 = 2 * m
+ aa = m * (b - m) * x / ((qam + m2) * (a + m2))
+ d = 1.0 + aa * d
+ if abs(d) < TINY:
+ d = TINY
+ c = 1.0 + aa / c
+ if abs(c) < TINY:
+ c = TINY
+ d = 1.0 / d
+ h *= d * c
+ aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
+ d = 1.0 + aa * d
+ if abs(d) < TINY:
+ d = TINY
+ c = 1.0 + aa / c
+ if abs(c) < TINY:
+ c = TINY
+ d = 1.0 / d
+ delta = d * c
+ h *= delta
+ if abs(delta - 1.0) < eps:
+ break
+ return h
+
+
+def betainc(a: float, b: float, x: float) -> float:
+ """Regularised incomplete beta function I_x(a, b)."""
+ if x <= 0.0:
+ return 0.0
+ if x >= 1.0:
+ return 1.0
+ log_front = (
+ math.lgamma(a + b)
+ - math.lgamma(a)
+ - math.lgamma(b)
+ + a * math.log(x)
+ + b * math.log1p(-x)
+ )
+ front = math.exp(log_front)
+ if x < (a + 1.0) / (a + b + 2.0):
+ return front * _betacf(a, b, x) / a
+ return 1.0 - front * _betacf(b, a, 1.0 - x) / b
+
+
+def gammainc_lower(a: float, x: float) -> float:
+ """Regularised lower incomplete gamma P(a, x)."""
+ if x <= 0.0:
+ return 0.0
+ if x < a + 1.0:
+ # Series representation.
+ term = 1.0 / a
+ total = term
+ n = a
+ for _ in range(1000):
+ n += 1.0
+ term *= x / n
+ total += term
+ if abs(term) < abs(total) * 1e-16:
+ break
+ return total * math.exp(-x + a * math.log(x) - math.lgamma(a))
+ # Continued fraction for Q(a, x), then complement.
+ b = x + 1.0 - a
+ c = 1.0 / TINY
+ d = 1.0 / b
+ h = d
+ for i in range(1, 1000):
+ an = -i * (i - a)
+ b += 2.0
+ d = an * d + b
+ if abs(d) < TINY:
+ d = TINY
+ c = b + an / c
+ if abs(c) < TINY:
+ c = TINY
+ d = 1.0 / d
+ delta = d * c
+ h *= delta
+ if abs(delta - 1.0) < 1e-16:
+ break
+ q = math.exp(-x + a * math.log(x) - math.lgamma(a)) * h
+ return 1.0 - q
+
+
+def _bisect_ppf(cdf, target: float, lo: float, hi: float, tol: float = 1e-12) -> float:
+ """Invert a monotone CDF by bisection."""
+ for _ in range(300):
+ mid = 0.5 * (lo + hi)
+ if cdf(mid) < target:
+ lo = mid
+ else:
+ hi = mid
+ if hi - lo < tol * max(1.0, abs(mid)):
+ break
+ return 0.5 * (lo + hi)
+
+
+def t_cdf(t: float, df: float) -> float:
+ """CDF of Student's t with df degrees of freedom."""
+ if df <= 0:
+ raise InputError("t distribution needs df > 0")
+ x = df / (df + t * t)
+ half = 0.5 * betainc(0.5 * df, 0.5, x)
+ return half if t <= 0 else 1.0 - half
+
+
+def t_ppf(p: float, df: float) -> float:
+ """Quantile of Student's t."""
+ if not 0.0 < p < 1.0:
+ raise InputError("t_ppf needs 0 < p < 1")
+ return _bisect_ppf(lambda t: t_cdf(t, df), p, -1e4, 1e4)
+
+
+def chi2_cdf(x: float, df: float) -> float:
+ """CDF of the chi-square distribution."""
+ if x <= 0:
+ return 0.0
+ return gammainc_lower(0.5 * df, 0.5 * x)
+
+
+def chi2_ppf(p: float, df: float) -> float:
+ """Quantile of the chi-square distribution."""
+ if not 0.0 < p < 1.0:
+ raise InputError("chi2_ppf needs 0 < p < 1")
+ return _bisect_ppf(lambda x: chi2_cdf(x, df), p, 1e-12, 1e6)
+
+
+def f_cdf(x: float, df1: float, df2: float) -> float:
+ """CDF of the F distribution."""
+ if x <= 0:
+ return 0.0
+ return betainc(0.5 * df1, 0.5 * df2, df1 * x / (df1 * x + df2))
+
+
+def f_sf(x: float, df1: float, df2: float) -> float:
+ """Upper tail of the F distribution (the p-value for an F test).
+
+ Computed from the complementary incomplete beta rather than as 1 - cdf,
+ which underflows to exactly 0 for large F and would print a lack-of-fit
+ p-value of 0 in a validation report.
+ """
+ if x <= 0:
+ return 1.0
+ return betainc(0.5 * df2, 0.5 * df1, df2 / (df1 * x + df2))
+
+
+def z_ppf(p: float) -> float:
+ """Standard normal quantile."""
+ from statistics import NormalDist
+
+ return NormalDist().inv_cdf(p)
+
+
+# --------------------------------------------------------------------------
+# Descriptive helpers
+# --------------------------------------------------------------------------
+
+
+def mean(values: Sequence[float]) -> float:
+ if not values:
+ raise InputError("mean of an empty sequence")
+ return math.fsum(values) / len(values)
+
+
+def sample_sd(values: Sequence[float]) -> float:
+ n = len(values)
+ if n < 2:
+ return float("nan")
+ m = mean(values)
+ return math.sqrt(math.fsum((v - m) ** 2 for v in values) / (n - 1))
+
+
+def rsd_percent(values: Sequence[float]) -> float:
+ """Relative standard deviation (%CV). NaN when the mean is ~0."""
+ m = mean(values)
+ if abs(m) < 1e-15:
+ return float("nan")
+ return 100.0 * sample_sd(values) / abs(m)
+
+
+def median(values: Sequence[float]) -> float:
+ if not values:
+ raise InputError("median of an empty sequence")
+ s = sorted(values)
+ n = len(s)
+ mid = n // 2
+ return s[mid] if n % 2 else 0.5 * (s[mid - 1] + s[mid])
+
+
+def sd_confidence_interval(sd: float, df: float, level: float = 0.90) -> tuple[float, float]:
+ """Chi-square confidence interval for a standard deviation."""
+ if df <= 0 or not math.isfinite(sd):
+ return (float("nan"), float("nan"))
+ alpha = 1.0 - level
+ lo_chi = chi2_ppf(1.0 - alpha / 2.0, df)
+ hi_chi = chi2_ppf(alpha / 2.0, df)
+ return (sd * math.sqrt(df / lo_chi), sd * math.sqrt(df / hi_chi))
+
+
+# --------------------------------------------------------------------------
+# Regression
+# --------------------------------------------------------------------------
+
+
+@dataclass
+class LinearFit:
+ """Weighted least-squares straight-line fit and its diagnostics."""
+
+ n: int
+ slope: float
+ intercept: float
+ se_slope: float
+ se_intercept: float
+ residual_sd: float
+ r_squared: float
+ r: float
+ df: int
+ residuals: list[float] = field(default_factory=list)
+ fitted: list[float] = field(default_factory=list)
+ weights: list[float] = field(default_factory=list)
+
+ def predict(self, x: float) -> float:
+ return self.intercept + self.slope * x
+
+ def slope_ci(self, level: float = 0.95) -> tuple[float, float]:
+ t = t_ppf(0.5 + level / 2.0, self.df)
+ return (self.slope - t * self.se_slope, self.slope + t * self.se_slope)
+
+ def intercept_ci(self, level: float = 0.95) -> tuple[float, float]:
+ t = t_ppf(0.5 + level / 2.0, self.df)
+ return (self.intercept - t * self.se_intercept, self.intercept + t * self.se_intercept)
+
+
+def fit_linear(
+ xs: Sequence[float], ys: Sequence[float], weights: Sequence[float] | None = None
+) -> LinearFit:
+ """Fit y = a + b*x by (optionally weighted) least squares."""
+ n = len(xs)
+ if n != len(ys):
+ raise InputError("x and y must be the same length")
+ if n < 3:
+ raise InputError("a regression needs at least 3 points")
+ w = [1.0] * n if weights is None else [float(v) for v in weights]
+ if len(w) != n:
+ raise InputError("weights must match the number of points")
+ if any(v < 0 for v in w):
+ raise InputError("weights must be non-negative")
+
+ sw = math.fsum(w)
+ swx = math.fsum(wi * xi for wi, xi in zip(w, xs))
+ swy = math.fsum(wi * yi for wi, yi in zip(w, ys))
+ swxx = math.fsum(wi * xi * xi for wi, xi in zip(w, xs))
+ swxy = math.fsum(wi * xi * yi for wi, xi, yi in zip(w, xs, ys))
+ denom = sw * swxx - swx * swx
+ if abs(denom) < 1e-300:
+ raise InputError("x values are collinear or identical; slope is undefined")
+
+ slope = (sw * swxy - swx * swy) / denom
+ intercept = (swy - slope * swx) / sw
+ fitted = [intercept + slope * xi for xi in xs]
+ residuals = [yi - fi for yi, fi in zip(ys, fitted)]
+ df = n - 2
+ ss_res = math.fsum(wi * ri * ri for wi, ri in zip(w, residuals))
+ residual_sd = math.sqrt(ss_res / df)
+ se_slope = residual_sd * math.sqrt(sw / denom)
+ se_intercept = residual_sd * math.sqrt(swxx / denom)
+
+ ybar_w = swy / sw
+ ss_tot = math.fsum(wi * (yi - ybar_w) ** 2 for wi, yi in zip(w, ys))
+ r_squared = 1.0 - ss_res / ss_tot if ss_tot > 0 else float("nan")
+ r = math.copysign(math.sqrt(max(0.0, r_squared)), slope)
+
+ return LinearFit(
+ n=n,
+ slope=slope,
+ intercept=intercept,
+ se_slope=se_slope,
+ se_intercept=se_intercept,
+ residual_sd=residual_sd,
+ r_squared=r_squared,
+ r=r,
+ df=df,
+ residuals=residuals,
+ fitted=fitted,
+ weights=w,
+ )
+
+
+def runs_test(residuals: Sequence[float]) -> dict[str, Any]:
+ """Wald-Wolfowitz runs test on residual signs.
+
+ ICH Q2(R2) 3.2.2.1 asks for the impact of any non-random pattern in the
+ residual plot to be assessed. Curvature shows up as too few runs.
+ """
+ signs = [1 if r >= 0 else -1 for r in residuals if r != 0]
+ n = len(signs)
+ n_pos = sum(1 for s in signs if s > 0)
+ n_neg = n - n_pos
+ runs = 1 + sum(1 for i in range(1, n) if signs[i] != signs[i - 1]) if n else 0
+ if n_pos < 1 or n_neg < 1 or n < 8:
+ return {
+ "runs": runs,
+ "n_pos": n_pos,
+ "n_neg": n_neg,
+ "z": float("nan"),
+ "p_value": float("nan"),
+ "note": "too few points for a meaningful runs test (need n>=8 with both signs)",
+ }
+ exp = 2.0 * n_pos * n_neg / n + 1.0
+ var = (2.0 * n_pos * n_neg * (2.0 * n_pos * n_neg - n)) / (n * n * (n - 1.0))
+ if var <= 0:
+ return {"runs": runs, "n_pos": n_pos, "n_neg": n_neg, "z": float("nan"),
+ "p_value": float("nan"), "note": "degenerate variance"}
+ z = (runs - exp) / math.sqrt(var)
+ from statistics import NormalDist
+
+ p = 2.0 * NormalDist().cdf(-abs(z))
+ return {"runs": runs, "n_pos": n_pos, "n_neg": n_neg, "expected_runs": exp,
+ "z": z, "p_value": p, "note": ""}
+
+
+def lack_of_fit(xs: Sequence[float], ys: Sequence[float], fit: LinearFit) -> dict[str, Any]:
+ """ANOVA lack-of-fit F test, which needs replicate y at some x levels.
+
+ This is the statistically meaningful test of a linear calibration model.
+ r-squared is not: it rises with range and is insensitive to curvature.
+ """
+ groups: dict[float, list[float]] = {}
+ for x, y in zip(xs, ys):
+ groups.setdefault(round(float(x), 12), []).append(float(y))
+ k = len(groups)
+ n = len(xs)
+ replicated = sum(1 for vals in groups.values() if len(vals) > 1)
+ df_pe = n - k
+ df_lof = k - 2
+ if df_pe < 1 or df_lof < 1:
+ return {
+ "applicable": False,
+ "levels": k,
+ "replicated_levels": replicated,
+ "reason": "needs replicates at >=1 level and >=3 distinct levels",
+ }
+ ss_pe = math.fsum(
+ math.fsum((v - mean(vals)) ** 2 for v in vals) for vals in groups.values()
+ )
+ ss_res = math.fsum(r * r for r in fit.residuals)
+ ss_lof = max(0.0, ss_res - ss_pe)
+ ms_pe = ss_pe / df_pe
+ ms_lof = ss_lof / df_lof
+ if ms_pe <= 0:
+ return {"applicable": False, "levels": k, "replicated_levels": replicated,
+ "reason": "zero pure-error variance; replicates are identical"}
+ f_stat = ms_lof / ms_pe
+ return {
+ "applicable": True,
+ "levels": k,
+ "replicated_levels": replicated,
+ "df_lack_of_fit": df_lof,
+ "df_pure_error": df_pe,
+ "ms_lack_of_fit": ms_lof,
+ "ms_pure_error": ms_pe,
+ "f_statistic": f_stat,
+ "p_value": f_sf(f_stat, df_lof, df_pe),
+ }
+
+
+def heteroscedasticity(xs: Sequence[float], residuals: Sequence[float]) -> dict[str, Any]:
+ """Compare residual spread in the lowest and highest thirds of the range.
+
+ A large ratio means unweighted least squares over-weights the top of the
+ curve, which biases back-calculated results at the bottom -- exactly where
+ an impurity reporting threshold or an LLOQ lives.
+ """
+ pairs = sorted(zip(xs, residuals), key=lambda p: p[0])
+ n = len(pairs)
+ if n < 6:
+ return {"applicable": False, "reason": "needs at least 6 points"}
+ cut = max(2, n // 3)
+ low = [r for _, r in pairs[:cut]]
+ high = [r for _, r in pairs[-cut:]]
+ var_low = math.fsum(r * r for r in low) / len(low)
+ var_high = math.fsum(r * r for r in high) / len(high)
+ if var_low <= 0:
+ return {"applicable": False, "reason": "zero residual variance in the low third"}
+ ratio = var_high / var_low
+ return {
+ "applicable": True,
+ "n_low": len(low),
+ "n_high": len(high),
+ "variance_ratio_high_over_low": ratio,
+ "sd_ratio": math.sqrt(ratio),
+ }
+
+
+# --------------------------------------------------------------------------
+# Variance components (precision)
+# --------------------------------------------------------------------------
+
+
+@dataclass
+class PrecisionComponents:
+ """One-way random-effects decomposition of precision."""
+
+ grand_mean: float
+ n_total: int
+ n_groups: int
+ ms_between: float
+ ms_within: float
+ df_between: int
+ df_within: int
+ sd_repeatability: float
+ sd_between: float
+ sd_intermediate: float
+ balanced: bool
+ n_effective: float
+
+ def rsd(self, sd: float) -> float:
+ if abs(self.grand_mean) < 1e-15:
+ return float("nan")
+ return 100.0 * sd / abs(self.grand_mean)
+
+ def satterthwaite_df(self) -> float:
+ """Effective df for the total (intermediate) SD."""
+ var_total = self.sd_intermediate ** 2
+ if var_total <= 0:
+ return float("nan")
+ n = self.n_effective
+ c1 = 1.0 / n
+ c2 = (n - 1.0) / n
+ num = var_total ** 2
+ den = 0.0
+ if self.df_between > 0:
+ den += (c1 * self.ms_between) ** 2 / self.df_between
+ if self.df_within > 0:
+ den += (c2 * self.ms_within) ** 2 / self.df_within
+ return num / den if den > 0 else float("nan")
+
+
+def one_way_components(groups: dict[str, Sequence[float]]) -> PrecisionComponents:
+ """Decompose precision into within-group and between-group components.
+
+ Groups are the intermediate-precision factor -- day, analyst, instrument,
+ or a combined run. Within-group scatter estimates repeatability; the total
+ estimates intermediate precision.
+ """
+ clean = {k: [float(v) for v in vals] for k, vals in groups.items() if len(vals) >= 1}
+ if len(clean) < 2:
+ raise InputError("intermediate precision needs at least 2 groups")
+ if all(len(v) < 2 for v in clean.values()):
+ raise InputError("at least one group needs >=2 replicates to estimate repeatability")
+
+ counts = [len(v) for v in clean.values()]
+ n_total = sum(counts)
+ k = len(clean)
+ all_values = [v for vals in clean.values() for v in vals]
+ grand = mean(all_values)
+
+ ss_within = math.fsum(
+ math.fsum((v - mean(vals)) ** 2 for v in vals) for vals in clean.values()
+ )
+ ss_between = math.fsum(len(vals) * (mean(vals) - grand) ** 2 for vals in clean.values())
+ df_within = n_total - k
+ df_between = k - 1
+ ms_within = ss_within / df_within if df_within > 0 else float("nan")
+ ms_between = ss_between / df_between if df_between > 0 else float("nan")
+
+ balanced = len(set(counts)) == 1
+ if balanced:
+ n_eff = float(counts[0])
+ else:
+ # Standard unbalanced coefficient for the expected mean square.
+ n_eff = (n_total - math.fsum(c * c for c in counts) / n_total) / (k - 1)
+
+ var_within = max(0.0, ms_within) if math.isfinite(ms_within) else 0.0
+ var_between = 0.0
+ if math.isfinite(ms_between) and math.isfinite(ms_within) and n_eff > 0:
+ var_between = max(0.0, (ms_between - ms_within) / n_eff)
+
+ return PrecisionComponents(
+ grand_mean=grand,
+ n_total=n_total,
+ n_groups=k,
+ ms_between=ms_between,
+ ms_within=ms_within,
+ df_between=df_between,
+ df_within=df_within,
+ sd_repeatability=math.sqrt(var_within),
+ sd_between=math.sqrt(var_between),
+ sd_intermediate=math.sqrt(var_within + var_between),
+ balanced=balanced,
+ n_effective=n_eff,
+ )
+
+
+# --------------------------------------------------------------------------
+# Method comparison
+# --------------------------------------------------------------------------
+
+
+def deming(
+ xs: Sequence[float], ys: Sequence[float], lambda_ratio: float = 1.0
+) -> dict[str, Any]:
+ """Deming regression: errors in both variables.
+
+ `lambda_ratio` is var(error in y) / var(error in x) -- the variance of the
+ random error in the TEST (y) procedure over that in the COMPARATIVE (x) one.
+ Check the direction against the limits, which are unambiguous: as
+ lambda -> infinity the fit converges on the ordinary least-squares slope of
+ y on x (x treated as error-free), and as lambda -> 0 it converges on the
+ inverse regression (y treated as error-free). lambda = 1 means equal error
+ variances and reduces to orthogonal regression.
+
+ In practice lambda is estimated as (SD of x replicates / SD of y replicates)
+ squared, so equal-precision procedures give 1.
+
+ Ordinary least squares assumes x is error-free, which is false when
+ comparing two measurement procedures, and biases the slope toward zero.
+ """
+ n = len(xs)
+ if n != len(ys):
+ raise InputError("x and y must be the same length")
+ if n < 3:
+ raise InputError("Deming regression needs at least 3 points")
+ if lambda_ratio <= 0:
+ raise InputError("lambda_ratio must be > 0")
+
+ def _fit(xv: Sequence[float], yv: Sequence[float]) -> tuple[float, float]:
+ xb, yb = mean(xv), mean(yv)
+ sxx = math.fsum((x - xb) ** 2 for x in xv)
+ syy = math.fsum((y - yb) ** 2 for y in yv)
+ sxy = math.fsum((x - xb) * (y - yb) for x, y in zip(xv, yv))
+ if abs(sxy) < 1e-300:
+ raise InputError("zero covariance; Deming slope is undefined")
+ term = syy - lambda_ratio * sxx
+ slope = (term + math.sqrt(term * term + 4.0 * lambda_ratio * sxy * sxy)) / (
+ 2.0 * sxy
+ )
+ return slope, yb - slope * xb
+
+ slope, intercept = _fit(xs, ys)
+
+ # Jackknife standard errors.
+ slopes, intercepts = [], []
+ for i in range(n):
+ xv = list(xs[:i]) + list(xs[i + 1 :])
+ yv = list(ys[:i]) + list(ys[i + 1 :])
+ try:
+ s, a = _fit(xv, yv)
+ except InputError:
+ continue
+ slopes.append(s)
+ intercepts.append(a)
+ if len(slopes) > 2:
+ m = len(slopes)
+ se_slope = math.sqrt((m - 1) / m * math.fsum((s - mean(slopes)) ** 2 for s in slopes))
+ se_int = math.sqrt(
+ (m - 1) / m * math.fsum((a - mean(intercepts)) ** 2 for a in intercepts)
+ )
+ df = m - 2
+ else:
+ se_slope = se_int = float("nan")
+ df = 1
+
+ t = t_ppf(0.975, df) if df > 0 else float("nan")
+ return {
+ "n": n,
+ "lambda_ratio": lambda_ratio,
+ "slope": slope,
+ "intercept": intercept,
+ "se_slope": se_slope,
+ "se_intercept": se_int,
+ "slope_ci95": (slope - t * se_slope, slope + t * se_slope),
+ "intercept_ci95": (intercept - t * se_int, intercept + t * se_int),
+ "df": df,
+ }
+
+
+def passing_bablok(xs: Sequence[float], ys: Sequence[float]) -> dict[str, Any]:
+ """Passing-Bablok regression: non-parametric, no distributional assumption.
+
+ Robust to outliers and does not assume a known error-variance ratio, which
+ is why CLSI EP09-style method comparison work often prefers it.
+ """
+ n = len(xs)
+ if n != len(ys):
+ raise InputError("x and y must be the same length")
+ if n < 5:
+ raise InputError("Passing-Bablok needs at least 5 points")
+
+ slopes: list[float] = []
+ for i in range(n):
+ for j in range(i + 1, n):
+ dx = xs[j] - xs[i]
+ dy = ys[j] - ys[i]
+ if dx == 0 and dy == 0:
+ continue
+ if dx == 0:
+ continue # vertical pair carries no finite slope
+ slopes.append(dy / dx)
+ if not slopes:
+ raise InputError("no usable pairwise slopes")
+
+ slopes.sort()
+ n_slopes = len(slopes)
+ shift = sum(1 for s in slopes if s < -1.0)
+
+ def _shifted_median(offset: int) -> float:
+ idx = n_slopes // 2 + offset
+ if n_slopes % 2:
+ return slopes[min(max(idx, 0), n_slopes - 1)]
+ lo = slopes[min(max(idx - 1, 0), n_slopes - 1)]
+ hi = slopes[min(max(idx, 0), n_slopes - 1)]
+ return 0.5 * (lo + hi)
+
+ slope = _shifted_median(shift)
+ intercept = median([y - slope * x for x, y in zip(xs, ys)])
+
+ # Rank-based 95% CI on the slope. M1 and M2 are 1-based order statistics of
+ # the shifted slope list, so both convert to 0-based with the same -1.
+ c = z_ppf(0.975) * math.sqrt(n * (n - 1.0) * (2.0 * n + 5.0) / 18.0)
+ m1 = int(round((n_slopes - c) / 2.0))
+ m2 = n_slopes - m1 + 1
+ lo_idx = min(max(m1 + shift - 1, 0), n_slopes - 1)
+ hi_idx = min(max(m2 + shift - 1, 0), n_slopes - 1)
+ slope_lo, slope_hi = slopes[lo_idx], slopes[hi_idx]
+ int_lo = median([y - slope_hi * x for x, y in zip(xs, ys)])
+ int_hi = median([y - slope_lo * x for x, y in zip(xs, ys)])
+
+ return {
+ "n": n,
+ "n_slopes": n_slopes,
+ "slope": slope,
+ "intercept": intercept,
+ "slope_ci95": (slope_lo, slope_hi),
+ "intercept_ci95": (int_lo, int_hi),
+ }
+
+
+def bland_altman(
+ xs: Sequence[float], ys: Sequence[float], relative: bool = False
+) -> dict[str, Any]:
+ """Bias and limits of agreement between paired measurements."""
+ n = len(xs)
+ if n != len(ys):
+ raise InputError("x and y must be the same length")
+ if n < 3:
+ raise InputError("Bland-Altman needs at least 3 pairs")
+ means = [0.5 * (x + y) for x, y in zip(xs, ys)]
+ if relative:
+ diffs = []
+ for x, y, m in zip(xs, ys, means):
+ if abs(m) < 1e-15:
+ raise InputError("relative differences need non-zero pair means")
+ diffs.append(100.0 * (y - x) / m)
+ else:
+ diffs = [y - x for x, y in zip(xs, ys)]
+
+ bias = mean(diffs)
+ sd = sample_sd(diffs)
+ t = t_ppf(0.975, n - 1)
+ se_bias = sd / math.sqrt(n)
+ loa_lo, loa_hi = bias - 1.96 * sd, bias + 1.96 * sd
+ se_loa = sd * math.sqrt(1.0 / n + (1.96 ** 2) / (2.0 * (n - 1)))
+
+ # Proportional-bias check: does the difference trend with the mean?
+ trend = None
+ try:
+ tf = fit_linear(means, diffs)
+ t_stat = tf.slope / tf.se_slope if tf.se_slope > 0 else float("nan")
+ trend = {
+ "slope": tf.slope,
+ "p_value": 2.0 * (1.0 - t_cdf(abs(t_stat), tf.df)) if math.isfinite(t_stat) else float("nan"),
+ }
+ except InputError:
+ trend = None
+
+ return {
+ "n": n,
+ "relative": relative,
+ "bias": bias,
+ "sd_differences": sd,
+ "bias_ci95": (bias - t * se_bias, bias + t * se_bias),
+ "loa_lower": loa_lo,
+ "loa_upper": loa_hi,
+ "loa_ci95_halfwidth": t * se_loa,
+ "proportional_bias": trend,
+ }
+
+
+def tost_paired(
+ diffs: Sequence[float], margin: float, alpha: float = 0.05
+) -> dict[str, Any]:
+ """Two one-sided tests for equivalence on paired differences.
+
+ Absence of a significant difference is not evidence of equivalence. TOST
+ tests the hypothesis that actually matters at a method transfer: that the
+ true difference lies inside +/- margin.
+ """
+ n = len(diffs)
+ if n < 2:
+ raise InputError("TOST needs at least 2 differences")
+ if margin <= 0:
+ raise InputError("margin must be > 0")
+ d = mean(diffs)
+ sd = sample_sd(diffs)
+ se = sd / math.sqrt(n)
+ df = n - 1
+ if se <= 0:
+ raise InputError("zero variability; TOST is undefined")
+ t_lower = (d + margin) / se
+ t_upper = (d - margin) / se
+ p_lower = 1.0 - t_cdf(t_lower, df)
+ p_upper = t_cdf(t_upper, df)
+ p = max(p_lower, p_upper)
+ t_crit = t_ppf(1.0 - alpha, df)
+ ci = (d - t_crit * se, d + t_crit * se)
+ return {
+ "n": n,
+ "mean_difference": d,
+ "sd_difference": sd,
+ "margin": margin,
+ "alpha": alpha,
+ "p_lower": p_lower,
+ "p_upper": p_upper,
+ "p_value": p,
+ "ci_1_minus_2alpha": ci,
+ "equivalent": bool(ci[0] > -margin and ci[1] < margin),
+ }
+
+
+# --------------------------------------------------------------------------
+# I/O
+# --------------------------------------------------------------------------
+
+
+def read_input(path: str | None) -> str:
+ """Read a bounded amount of text from a path or stdin."""
+ if path in (None, "-"):
+ # Reading a terminal would block forever with no indication why, so an
+ # omitted --input becomes an error rather than an apparent hang.
+ if path is None and sys.stdin.isatty():
+ raise InputError("no input given; pass --input FILE, or '-' to read stdin")
+ data = sys.stdin.read(MAX_INPUT_BYTES + 1)
+ else:
+ p = Path(path)
+ if not p.is_file():
+ raise InputError(f"not a file: {path}")
+ if p.stat().st_size > MAX_INPUT_BYTES:
+ raise InputError(f"input larger than {MAX_INPUT_BYTES} bytes")
+ data = p.read_text(encoding="utf-8", errors="replace")
+ if len(data) > MAX_INPUT_BYTES:
+ raise InputError(f"input larger than {MAX_INPUT_BYTES} bytes")
+ return data
+
+
+def parse_rows(text: str, path_hint: str | None = None) -> list[dict[str, str]]:
+ """Parse CSV, TSV, or a JSON array of objects into a list of dicts."""
+ stripped = text.lstrip()
+ if stripped.startswith("[") or stripped.startswith("{"):
+ try:
+ payload = json.loads(stripped)
+ except json.JSONDecodeError as exc:
+ raise InputError(f"invalid JSON: {exc}") from exc
+ if isinstance(payload, dict):
+ payload = payload.get("rows", payload.get("data"))
+ if not isinstance(payload, list):
+ raise InputError("JSON input must be an array of objects, or {\"rows\": [...]}")
+ # Refuse rather than truncate: silently dropping validation data would
+ # produce a clean-looking result computed on part of the study.
+ if len(payload) > MAX_ROWS:
+ raise InputError(f"more than {MAX_ROWS} rows")
+ rows = []
+ for item in payload:
+ if not isinstance(item, dict):
+ raise InputError("JSON rows must be objects")
+ rows.append({str(k): "" if v is None else str(v) for k, v in item.items()})
+ if not rows:
+ raise InputError("no data rows found")
+ return rows
+
+ delimiter = "\t" if (path_hint or "").endswith((".tsv", ".tab")) else None
+ if delimiter is None:
+ first = text.splitlines()[0] if text.splitlines() else ""
+ delimiter = "\t" if first.count("\t") > first.count(",") else ","
+ reader = csv.DictReader(io.StringIO(text), delimiter=delimiter)
+ rows = []
+ for i, row in enumerate(reader):
+ if i >= MAX_ROWS:
+ raise InputError(f"more than {MAX_ROWS} rows")
+ rows.append({(k or "").strip(): (v or "").strip() for k, v in row.items()})
+ if not rows:
+ raise InputError("no data rows found")
+ return rows
+
+
+def require_columns(rows: list[dict[str, str]], columns: Iterable[str]) -> None:
+ present = set(rows[0].keys())
+ missing = [c for c in columns if c not in present]
+ if missing:
+ raise InputError(
+ f"missing required column(s): {', '.join(missing)}; found: {', '.join(sorted(present))}"
+ )
+
+
+def to_float(value: str, column: str, row_index: int) -> float:
+ try:
+ return float(str(value).strip())
+ except (TypeError, ValueError) as exc:
+ raise InputError(
+ f"row {row_index + 1}: column '{column}' is not numeric: {value!r}"
+ ) from exc
+
+
+def fmt(value: Any, digits: int = 4) -> str:
+ """Format a number for a table cell."""
+ if value is None:
+ return ""
+ if isinstance(value, bool):
+ return "yes" if value else "no"
+ if isinstance(value, float):
+ if math.isnan(value):
+ return "n/a"
+ if math.isinf(value):
+ return "inf"
+ if value != 0 and (abs(value) < 1e-4 or abs(value) >= 1e6):
+ return f"{value:.{digits}e}"
+ return f"{value:.{digits}f}"
+ return str(value)
+
+
+def emit_table(rows: list[dict[str, Any]], stream=None) -> None:
+ """Print aligned columns."""
+ stream = stream or sys.stdout
+ if not rows:
+ print("(no rows)", file=stream)
+ return
+ headers = list(rows[0].keys())
+ cells = [[fmt(r.get(h)) for h in headers] for r in rows]
+ widths = [
+ max(len(h), *(len(c[i]) for c in cells)) if cells else len(h)
+ for i, h in enumerate(headers)
+ ]
+ print(" ".join(h.ljust(w) for h, w in zip(headers, widths)).rstrip(), file=stream)
+ for c in cells:
+ print(" ".join(v.ljust(w) for v, w in zip(c, widths)).rstrip(), file=stream)
+
+
+def emit(rows: list[dict[str, Any]], fmt_name: str, stream=None) -> None:
+ """Print rows as a table, TSV, or JSON."""
+ stream = stream or sys.stdout
+ if fmt_name == "json":
+ json.dump(rows, stream, indent=2, default=_json_default)
+ print(file=stream)
+ elif fmt_name == "tsv":
+ if not rows:
+ return
+ headers = list(rows[0].keys())
+ print("\t".join(headers), file=stream)
+ for r in rows:
+ print("\t".join(fmt(r.get(h)) for h in headers), file=stream)
+ else:
+ emit_table(rows, stream=stream)
+
+
+def _json_default(obj: Any) -> Any:
+ if isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)):
+ return None
+ if isinstance(obj, tuple):
+ return list(obj)
+ raise TypeError(f"not JSON serialisable: {type(obj)!r}")
+
+
+def note(message: str) -> None:
+ """Write provenance and caveats to stderr so stdout stays parseable."""
+ print(f"note: {message}", file=sys.stderr)
+
+
+def finding(message: str) -> None:
+ print(f"finding: {message}", file=sys.stderr)
+
+
+def add_common_args(parser: argparse.ArgumentParser) -> None:
+ parser.add_argument(
+ "--format",
+ choices=("table", "tsv", "json"),
+ default="table",
+ help="output format (default: table)",
+ )
+
+
+def run_cli(main_func) -> None:
+ """Wrap a main() so InputError becomes a clean exit code 2."""
+ try:
+ sys.exit(main_func())
+ except InputError as exc:
+ print(f"error: {exc}", file=sys.stderr)
+ sys.exit(EXIT_INPUT_ERROR)
+ except BrokenPipeError:
+ sys.exit(EXIT_OK)
diff --git a/skills/analytical-method-validation/scripts/check_accuracy_precision.py b/skills/analytical-method-validation/scripts/check_accuracy_precision.py
new file mode 100644
index 00000000..302b9654
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/check_accuracy_precision.py
@@ -0,0 +1,328 @@
+#!/usr/bin/env python3
+"""Evaluate accuracy and precision per ICH Q2(R2) 3.3.
+
+Accuracy is reported as mean recovery with a confidence interval, which is what
+Q2(R2) 3.3.1.4 asks for -- a bare mean is not sufficient. Precision is
+decomposed into repeatability and intermediate precision by a one-way
+random-effects model, because pooling all results into a single standard
+deviation understates the day-to-day variability the procedure will actually
+show in routine use.
+
+ python3 check_accuracy_precision.py --input ap.csv
+ python3 check_accuracy_precision.py -i ap.csv --accuracy-limit 2 --rsd-limit 2
+ python3 check_accuracy_precision.py -i ap.csv --design-check assay --format json
+
+Input columns:
+ level nominal / added concentration (groups the accuracy analysis)
+ measured measured or recovered concentration
+ group optional: day, analyst, instrument or run -- the intermediate
+ precision factor. Without it only repeatability is estimated.
+
+Exit codes: 0 no findings, 1 findings raised, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import math
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _common import ( # noqa: E402
+ EXIT_FINDINGS,
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ emit,
+ finding,
+ mean,
+ note,
+ one_way_components,
+ parse_rows,
+ read_input,
+ require_columns,
+ rsd_percent,
+ run_cli,
+ sample_sd,
+ sd_confidence_interval,
+ t_ppf,
+ to_float,
+)
+
+# ICH Q2(R2) 3.3.2.1 minima, used only to comment on the design. Either option
+# is sufficient on its own.
+DESIGN_MINIMA = {
+ "assay": {
+ "range_determinations": 9,
+ "range_levels": 3,
+ "single_level_determinations": 6,
+ "note": "9 determinations across the range (3x3), or 6 at 100% of test concentration",
+ },
+ "impurity": {
+ "range_determinations": 9,
+ "range_levels": 3,
+ "single_level_determinations": 6,
+ "note": "9 determinations across the range (3x3), or 6 at 100% of test concentration",
+ },
+}
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(
+ description="Check accuracy and precision from validation data."
+ )
+ parser.add_argument("--input", "-i", help="CSV/TSV/JSON file, or '-' for stdin")
+ parser.add_argument("--accuracy-limit", type=float, default=None,
+ help="flag a level whose mean recovery deviates more than this %% "
+ "from 100%%")
+ parser.add_argument("--rsd-limit", type=float, default=None,
+ help="flag repeatability or intermediate precision %%RSD above this")
+ parser.add_argument("--ci-level", type=float, default=0.95,
+ help="confidence level for accuracy intervals (default 0.95)")
+ parser.add_argument("--sd-ci-level", type=float, default=0.90,
+ help="confidence level for SD intervals (default 0.90)")
+ parser.add_argument("--design-check", choices=sorted(DESIGN_MINIMA), default=None,
+ help="comment on the design against Q2(R2) recommended minima")
+ parser.add_argument("--require-ci-within-limit", action="store_true",
+ help="require the whole accuracy CI inside the limit, not just the mean")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ if not 0.5 <= args.ci_level < 1.0:
+ raise InputError("--ci-level must be in [0.5, 1)")
+
+ rows = parse_rows(read_input(args.input), args.input)
+ require_columns(rows, ["level", "measured"])
+ has_group = "group" in rows[0]
+
+ records = []
+ for i, r in enumerate(rows):
+ records.append(
+ {
+ "level": to_float(r["level"], "level", i),
+ "measured": to_float(r["measured"], "measured", i),
+ "group": (r.get("group") or "all").strip() or "all",
+ }
+ )
+
+ findings: list[str] = []
+
+ # ---------------- Accuracy ----------------
+ levels = sorted({rec["level"] for rec in records})
+ accuracy_rows = []
+ all_recoveries: list[float] = []
+ for level in levels:
+ vals = [rec["measured"] for rec in records if rec["level"] == level]
+ if level == 0:
+ raise InputError("a nominal level of 0 cannot be used for recovery")
+ recoveries = [100.0 * v / level for v in vals]
+ all_recoveries.extend(recoveries)
+ m = mean(recoveries)
+ n = len(recoveries)
+ if n >= 2:
+ sd = sample_sd(recoveries)
+ half = t_ppf(0.5 + args.ci_level / 2.0, n - 1) * sd / math.sqrt(n)
+ lo, hi = m - half, m + half
+ else:
+ sd, lo, hi = float("nan"), float("nan"), float("nan")
+ accuracy_rows.append(
+ {
+ "level": level,
+ "n": n,
+ "mean_measured": mean(vals),
+ "mean_recovery_pct": m,
+ "bias_pct": m - 100.0,
+ "sd_recovery_pct": sd,
+ f"ci{int(args.ci_level * 100)}_low": lo,
+ f"ci{int(args.ci_level * 100)}_high": hi,
+ }
+ )
+ if args.accuracy_limit is not None:
+ if abs(m - 100.0) > args.accuracy_limit:
+ findings.append(
+ f"level {level:g}: mean recovery {m:.2f}% is {m - 100.0:+.2f}% from nominal, "
+ f"outside +/-{args.accuracy_limit:g}%"
+ )
+ elif args.require_ci_within_limit and math.isfinite(lo):
+ if lo < 100.0 - args.accuracy_limit or hi > 100.0 + args.accuracy_limit:
+ findings.append(
+ f"level {level:g}: mean recovery {m:.2f}% is inside "
+ f"+/-{args.accuracy_limit:g}% but its "
+ f"{int(args.ci_level * 100)}% CI ({lo:.2f}, {hi:.2f}) is not -- the data "
+ "do not demonstrate accuracy at this limit"
+ )
+
+ # ---------------- Precision ----------------
+ # Precision is estimated WITHIN each concentration level. Pooling levels
+ # together would let the 80/100/120 spread masquerade as imprecision.
+ pct = int(args.sd_ci_level * 100)
+ precision_rows: list[dict] = []
+
+ def precision_block(label: str, groups: dict[str, list[float]], reference: float | None):
+ """Append rows for one level (or for the normalised all-levels view)."""
+ try:
+ comp = one_way_components(groups)
+ except InputError as exc:
+ vals = [v for vs in groups.values() for v in vs]
+ if len(vals) < 2:
+ note(f"{label}: too few values for a precision estimate")
+ return None
+ sd = sample_sd(vals)
+ base = reference if reference else abs(mean(vals))
+ rsd = 100.0 * sd / base if base else float("nan")
+ lo, hi = sd_confidence_interval(sd, len(vals) - 1, args.sd_ci_level)
+ precision_rows.append(
+ {"level": label, "component": "repeatability only", "sd": sd, "rsd_pct": rsd,
+ "df": len(vals) - 1, f"ci{pct}_low_sd": lo, f"ci{pct}_high_sd": hi}
+ )
+ note(f"{label}: intermediate precision not estimated ({exc})")
+ return None
+
+ base = reference if reference else abs(comp.grand_mean)
+
+ def as_rsd(sd: float) -> float:
+ return 100.0 * sd / base if base else float("nan")
+
+ sr_lo, sr_hi = sd_confidence_interval(
+ comp.sd_repeatability, comp.df_within, args.sd_ci_level
+ )
+ sat_df = comp.satterthwaite_df()
+ si_lo, si_hi = sd_confidence_interval(comp.sd_intermediate, sat_df, args.sd_ci_level)
+ precision_rows.extend(
+ [
+ {"level": label, "component": "repeatability (within group)",
+ "sd": comp.sd_repeatability, "rsd_pct": as_rsd(comp.sd_repeatability),
+ "df": comp.df_within, f"ci{pct}_low_sd": sr_lo, f"ci{pct}_high_sd": sr_hi},
+ {"level": label, "component": "between-group", "sd": comp.sd_between,
+ "rsd_pct": as_rsd(comp.sd_between), "df": comp.df_between,
+ f"ci{pct}_low_sd": float("nan"), f"ci{pct}_high_sd": float("nan")},
+ {"level": label, "component": "intermediate precision (total)",
+ "sd": comp.sd_intermediate, "rsd_pct": as_rsd(comp.sd_intermediate),
+ "df": sat_df, f"ci{pct}_low_sd": si_lo, f"ci{pct}_high_sd": si_hi},
+ ]
+ )
+ if comp.sd_between == 0.0:
+ note(
+ f"{label}: between-group variance estimated as zero (MS_between <= MS_within); "
+ "the groups are indistinguishable at this level"
+ )
+ if not comp.balanced:
+ note(f"{label}: unbalanced design, effective group size {comp.n_effective:.3f}")
+ if args.rsd_limit is not None:
+ for name, value in (
+ ("repeatability", as_rsd(comp.sd_repeatability)),
+ ("intermediate precision", as_rsd(comp.sd_intermediate)),
+ ):
+ if math.isfinite(value) and value > args.rsd_limit:
+ findings.append(
+ f"{label}: {name} {value:.3f}% RSD exceeds the stated "
+ f"{args.rsd_limit:g}% limit"
+ )
+ return comp
+
+ if has_group:
+ for level in levels:
+ groups: dict[str, list[float]] = {}
+ for rec in records:
+ if rec["level"] == level:
+ groups.setdefault(rec["group"], []).append(rec["measured"])
+ precision_block(f"{level:g}", groups, reference=abs(level))
+
+ # Level-independent view: recovery as % of nominal, pooled across levels.
+ norm_groups: dict[str, list[float]] = {}
+ for rec in records:
+ norm_groups.setdefault(rec["group"], []).append(
+ 100.0 * rec["measured"] / rec["level"]
+ )
+ if len(norm_groups) >= 2:
+ precision_block("all (% of nominal)", norm_groups, reference=100.0)
+ else:
+ for level in levels:
+ vals = [rec["measured"] for rec in records if rec["level"] == level]
+ if len(vals) < 2:
+ continue
+ sd = sample_sd(vals)
+ lo, hi = sd_confidence_interval(sd, len(vals) - 1, args.sd_ci_level)
+ rsd = 100.0 * sd / abs(level)
+ precision_rows.append(
+ {"level": f"{level:g}", "component": "repeatability (no group column)",
+ "sd": sd, "rsd_pct": rsd, "df": len(vals) - 1,
+ f"ci{pct}_low_sd": lo, f"ci{pct}_high_sd": hi}
+ )
+ if args.rsd_limit is not None and rsd > args.rsd_limit:
+ findings.append(
+ f"{level:g}: repeatability {rsd:.3f}% RSD exceeds {args.rsd_limit:g}%"
+ )
+ note(
+ "no `group` column, so only repeatability was estimated. Q2(R2) 3.3.2.2 expects "
+ "intermediate precision from different days, analysts or equipment"
+ )
+
+ naive = rsd_percent([rec["measured"] for rec in records])
+ if len(levels) > 1:
+ note(
+ f"a single SD over every result regardless of level would report {naive:.3f}% RSD, "
+ "which is a range effect and not precision. Precision is reported per level below"
+ )
+
+ # ---------------- Design commentary ----------------
+ if args.design_check:
+ # Q2(R2) 3.3.2.1 offers two alternatives, and either one is sufficient:
+ # (a) >=9 determinations covering the reportable range (e.g. 3 levels x 3), or
+ # (b) >=6 determinations at 100% of the test concentration.
+ # Flag only when neither holds. Requiring 9 unconditionally would raise a
+ # finding against a design the guideline explicitly permits.
+ spec = DESIGN_MINIMA[args.design_check]
+ option_a = len(records) >= spec["range_determinations"] and len(levels) >= spec["range_levels"]
+ per_level = {lv: sum(1 for r in records if r["level"] == lv) for lv in levels}
+ best_single = max(per_level.values())
+ option_b = best_single >= spec["single_level_determinations"]
+ if option_a:
+ note(
+ f"design satisfies Q2(R2) 3.3.2.1 option (a): {len(records)} determinations "
+ f"across {len(levels)} levels"
+ )
+ elif option_b:
+ note(
+ f"design satisfies Q2(R2) 3.3.2.1 option (b): {best_single} determinations at a "
+ "single level. Note that option (b) gives no information about precision "
+ "across the range"
+ )
+ else:
+ findings.append(
+ f"repeatability design meets neither Q2(R2) 3.3.2.1 option: "
+ f"{len(records)} determinations across {len(levels)} level(s), with at most "
+ f"{best_single} at any one level. Option (a) needs "
+ f"{spec['range_determinations']} across at least {spec['range_levels']} levels; "
+ f"option (b) needs {spec['single_level_determinations']} at 100% of the test "
+ "concentration"
+ )
+
+ if args.format == "json":
+ emit([{"accuracy": accuracy_rows, "precision": precision_rows,
+ "overall_mean_recovery_pct": mean(all_recoveries),
+ "findings": findings}], "json")
+ else:
+ emit(accuracy_rows, args.format)
+ print()
+ emit(precision_rows, args.format)
+
+ note(
+ "Q2(R2) 3.3.1.4: report accuracy as mean percent recovery, or the difference from the "
+ "accepted true value, with a 100(1-alpha)% confidence interval"
+ )
+ note(
+ "Q2(R2) 3.3.2.2: intermediate precision covers days, environmental conditions, analysts "
+ "and equipment; the effects need not be studied individually"
+ )
+ for f in findings:
+ finding(f)
+ if not findings:
+ note("no findings against the checks that were run")
+ note("this tool does not decide that accuracy or precision is acceptable")
+ return EXIT_FINDINGS if findings else EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/skills/analytical-method-validation/scripts/check_bioanalytical_run.py b/skills/analytical-method-validation/scripts/check_bioanalytical_run.py
new file mode 100644
index 00000000..aecd2e72
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/check_bioanalytical_run.py
@@ -0,0 +1,293 @@
+#!/usr/bin/env python3
+"""Apply ICH M10 acceptance criteria to a bioanalytical run or an ISR dataset.
+
+Chromatographic assays and ligand binding assays carry DIFFERENT numeric
+criteria in ICH M10, and conflating them is the most common error in this area.
+--modality is therefore mandatory: nothing here has a default.
+
+ # calibration standards and QCs from one analytical run
+ python3 check_bioanalytical_run.py --modality chromatographic --run run1.csv
+
+ # incurred sample reanalysis
+ python3 check_bioanalytical_run.py --modality lba --isr isr.csv
+
+ # LBA total-error criterion from accuracy/precision validation data
+ python3 check_bioanalytical_run.py --modality lba --total-error ap.csv
+
+Input for --run: columns `type` (calibrator|qc), `nominal`, `measured`, and an
+optional `label` (e.g. LLOQ, low, medium, high, ULOQ, or ANCHOR for LBA anchor
+points, which are excluded from the calibration pass count).
+
+Input for --isr: columns `original` and `repeat`.
+
+Input for --total-error: columns `label`, `accuracy_pct`, `precision_pct`.
+
+Exit codes: 0 all applied criteria met, 1 criteria not met, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import math
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _catalog import M10_CRITERIA # noqa: E402
+from _common import ( # noqa: E402
+ EXIT_FINDINGS,
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ emit,
+ finding,
+ note,
+ parse_rows,
+ read_input,
+ require_columns,
+ run_cli,
+ to_float,
+)
+
+LIMIT_LABELS = {"lloq", "uloq"}
+
+
+def calibrator_tolerance(label: str, crit: dict) -> float:
+ """Tolerance for a calibration standard at this label.
+
+ Routine-run QCs are not routed through here: M10 applies one flat tolerance
+ to every QC level for run acceptance, which check_run uses directly.
+ """
+ low = label.strip().lower()
+ if low == "lloq":
+ return crit["calibration_tolerance_lloq_pct"]
+ if low == "uloq":
+ return crit["calibration_tolerance_uloq_pct"]
+ return crit["calibration_tolerance_pct"]
+
+
+def check_run(path: str, crit: dict) -> tuple[list[dict], list[str]]:
+ rows = parse_rows(read_input(path), path)
+ require_columns(rows, ["type", "nominal", "measured"])
+ findings: list[str] = []
+ detail: list[dict] = []
+
+ cal_pass = cal_total = 0
+ cal_levels: set[float] = set()
+ qc_by_level: dict[str, list[bool]] = {}
+ qc_pass = qc_total = 0
+
+ for i, r in enumerate(rows):
+ kind = (r["type"] or "").strip().lower()
+ label = (r.get("label") or "").strip()
+ nominal = to_float(r["nominal"], "nominal", i)
+ measured = to_float(r["measured"], "measured", i)
+ if nominal == 0:
+ raise InputError(f"row {i + 1}: nominal of 0 cannot be used")
+ dev = 100.0 * (measured - nominal) / nominal
+
+ if kind in ("calibrator", "cal", "standard", "std"):
+ if label.lower() == "anchor":
+ detail.append({"type": "calibrator", "label": "ANCHOR", "nominal": nominal,
+ "measured": measured, "deviation_pct": dev,
+ "tolerance_pct": float("nan"), "within": "excluded"})
+ continue
+ tol = calibrator_tolerance(label, crit)
+ ok = abs(dev) <= tol
+ cal_total += 1
+ cal_pass += int(ok)
+ cal_levels.add(round(nominal, 12))
+ detail.append({"type": "calibrator", "label": label or "-", "nominal": nominal,
+ "measured": measured, "deviation_pct": dev,
+ "tolerance_pct": tol, "within": ok})
+ elif kind in ("qc", "quality-control"):
+ tol = crit["qc_run_tolerance_pct"]
+ ok = abs(dev) <= tol
+ key = label or f"{nominal:g}"
+ qc_by_level.setdefault(key, []).append(ok)
+ qc_total += 1
+ qc_pass += int(ok)
+ detail.append({"type": "qc", "label": key, "nominal": nominal,
+ "measured": measured, "deviation_pct": dev,
+ "tolerance_pct": tol, "within": ok})
+ else:
+ raise InputError(
+ f"row {i + 1}: type must be calibrator or qc, got {r['type']!r}"
+ )
+
+ # Calibration curve criteria.
+ if cal_total:
+ n_levels = len(cal_levels)
+ need_levels = crit["calibration_min_levels"]
+ if n_levels < need_levels:
+ findings.append(
+ f"calibration curve has {n_levels} concentration levels; M10 requires a "
+ f"minimum of {need_levels}"
+ )
+ frac = cal_pass / cal_total
+ need = crit["calibration_min_pass_fraction"]
+ if frac < need:
+ findings.append(
+ f"{cal_pass}/{cal_total} calibration standards within tolerance "
+ f"({frac * 100:.1f}%); M10 requires at least {need * 100:.0f}%"
+ )
+
+ # Routine run QC criteria: both the overall fraction and per-level fraction.
+ if qc_total:
+ n_qc_levels = len(qc_by_level)
+ if n_qc_levels < crit["qc_levels_routine_run"]:
+ findings.append(
+ f"{n_qc_levels} QC levels present; M10 expects at least "
+ f"{crit['qc_levels_routine_run']} for run acceptance"
+ )
+ frac = qc_pass / qc_total
+ need = crit["qc_run_pass_fraction"]
+ if frac < need:
+ findings.append(
+ f"{qc_pass}/{qc_total} QCs within +/-{crit['qc_run_tolerance_pct']:.0f}% "
+ f"({frac * 100:.1f}%); M10 requires at least {need * 100:.0f}% of the total"
+ )
+ for level, flags in sorted(qc_by_level.items()):
+ level_frac = sum(flags) / len(flags)
+ if level_frac < crit["qc_run_pass_fraction_per_level"]:
+ findings.append(
+ f"QC level {level}: {sum(flags)}/{len(flags)} within tolerance "
+ f"({level_frac * 100:.0f}%); M10 requires at least "
+ f"{crit['qc_run_pass_fraction_per_level'] * 100:.0f}% at each level"
+ )
+ return detail, findings
+
+
+def check_isr(path: str, crit: dict) -> tuple[list[dict], list[str]]:
+ rows = parse_rows(read_input(path), path)
+ require_columns(rows, ["original", "repeat"])
+ tol = crit["isr_tolerance_pct"]
+ detail, findings = [], []
+ passes = 0
+ for i, r in enumerate(rows):
+ original = to_float(r["original"], "original", i)
+ repeat = to_float(r["repeat"], "repeat", i)
+ mean_val = 0.5 * (original + repeat)
+ if mean_val == 0:
+ raise InputError(f"row {i + 1}: mean of original and repeat is zero")
+ # M10 defines the ISR percent difference against the mean of the two.
+ diff = 100.0 * (repeat - original) / mean_val
+ ok = abs(diff) <= tol
+ passes += int(ok)
+ detail.append({"sample": r.get("sample", str(i + 1)), "original": original,
+ "repeat": repeat, "mean": mean_val, "percent_difference": diff,
+ "tolerance_pct": tol, "within": ok})
+ frac = passes / len(rows)
+ if frac < crit["isr_pass_fraction"]:
+ findings.append(
+ f"ISR: {passes}/{len(rows)} repeats within +/-{tol:.0f}% ({frac * 100:.1f}%); "
+ f"M10 requires at least {crit['isr_pass_fraction'] * 100:.0f}%"
+ )
+ return detail, findings
+
+
+def check_total_error(path: str, crit: dict) -> tuple[list[dict], list[str]]:
+ if crit["total_error_pct"] is None:
+ raise InputError(
+ "ICH M10 states a total-error criterion for ligand binding assays only; "
+ "use --modality lba, or omit --total-error for a chromatographic assay"
+ )
+ rows = parse_rows(read_input(path), path)
+ require_columns(rows, ["label", "accuracy_pct", "precision_pct"])
+ detail, findings = [], []
+ for i, r in enumerate(rows):
+ label = (r["label"] or "").strip()
+ acc = to_float(r["accuracy_pct"], "accuracy_pct", i)
+ prec = to_float(r["precision_pct"], "precision_pct", i)
+ total = abs(acc) + abs(prec)
+ limit = (
+ crit["total_error_pct_at_limits"]
+ if label.lower() in LIMIT_LABELS
+ else crit["total_error_pct"]
+ )
+ ok = total <= limit
+ detail.append({"label": label or "-", "accuracy_pct": acc, "precision_pct": prec,
+ "total_error_pct": total, "limit_pct": limit, "within": ok})
+ if not ok:
+ findings.append(
+ f"{label or 'level ' + str(i + 1)}: total error {total:.2f}% exceeds "
+ f"{limit:.0f}%"
+ )
+ return detail, findings
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(
+ description="Apply ICH M10 acceptance criteria to bioanalytical data."
+ )
+ parser.add_argument("--modality", required=True, choices=sorted(M10_CRITERIA),
+ help="chromatographic or lba -- the criteria differ and there is no default")
+ parser.add_argument("--run", help="calibrators and QCs from an analytical run")
+ parser.add_argument("--isr", help="incurred sample reanalysis data")
+ parser.add_argument("--total-error", dest="total_error",
+ help="accuracy/precision per level (ligand binding assays only)")
+ parser.add_argument("--criteria", action="store_true",
+ help="print the criteria that would be applied and exit")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ crit = M10_CRITERIA[args.modality]
+
+ if args.criteria:
+ rows = [
+ {"criterion": k, "value": ("n/a" if v is None else v)}
+ for k, v in crit.items()
+ if k not in ("label", "notes")
+ ]
+ emit(rows, args.format)
+ note(crit["label"])
+ note(crit["notes"])
+ return EXIT_OK
+
+ if not any((args.run, args.isr, args.total_error)):
+ raise InputError("supply at least one of --run, --isr, --total-error, or --criteria")
+
+ detail: list[dict] = []
+ findings: list[str] = []
+ sections: dict[str, list[dict]] = {}
+
+ if args.run:
+ d, f = check_run(args.run, crit)
+ sections["run"] = d
+ findings += f
+ if args.isr:
+ d, f = check_isr(args.isr, crit)
+ sections["isr"] = d
+ findings += f
+ if args.total_error:
+ d, f = check_total_error(args.total_error, crit)
+ sections["total_error"] = d
+ findings += f
+
+ if args.format == "json":
+ emit([{"modality": args.modality, **sections, "findings": findings}], "json")
+ else:
+ for name, rows in sections.items():
+ print(f"[{name}]")
+ emit(rows, args.format)
+ print()
+
+ note(crit["label"])
+ note(crit["notes"])
+ note(
+ "chromatographic and ligand binding assay criteria differ throughout M10; this run was "
+ f"assessed as: {args.modality}"
+ )
+ for f in findings:
+ finding(f)
+ if not findings:
+ note("all applied criteria met")
+ note(
+ "run acceptance is a documented decision by the analyst; this tool applies stated "
+ "criteria and does not accept or reject a run"
+ )
+ return EXIT_FINDINGS if findings else EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/skills/analytical-method-validation/scripts/check_detection_limits.py b/skills/analytical-method-validation/scripts/check_detection_limits.py
new file mode 100644
index 00000000..7deebcb3
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/check_detection_limits.py
@@ -0,0 +1,310 @@
+#!/usr/bin/env python3
+"""Estimate DL and QL by every approach ICH Q2(R2) 3.2.3 allows, and compare them.
+
+The four approaches routinely disagree by a factor of two or more on the same
+data. Reporting one number without naming the approach is the finding an
+assessor raises, so this script computes all of the applicable ones side by side
+and checks the answer against the reporting threshold it has to serve.
+
+ # sigma from the calibration line, slope from the same fit
+ python3 check_detection_limits.py --calibration calib.csv
+
+ # sigma from blank responses
+ python3 check_detection_limits.py --calibration calib.csv --blanks blanks.csv
+
+ # confirm an estimated QL with real data at that level
+ python3 check_detection_limits.py --calibration calib.csv \
+ --confirm-ql 0.05 --confirm-data ql_check.csv --reporting-threshold 0.05
+
+Input:
+ --calibration CSV with `level` and `response` (low-range calibration curve)
+ --blanks CSV with `response` (blank measurements)
+ --confirm-data CSV with `measured` (results at or near the claimed QL)
+
+Exit codes: 0 no findings, 1 findings raised, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import math
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _catalog import DL_QL_APPROACHES # noqa: E402
+from _common import ( # noqa: E402
+ EXIT_FINDINGS,
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ emit,
+ finding,
+ fit_linear,
+ mean,
+ note,
+ parse_rows,
+ read_input,
+ require_columns,
+ rsd_percent,
+ run_cli,
+ sample_sd,
+ to_float,
+)
+
+DL_FACTOR = 3.3
+QL_FACTOR = 10.0
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(
+ description="Estimate detection and quantitation limits per ICH Q2(R2) 3.2.3."
+ )
+ parser.add_argument("--calibration", required=True,
+ help="CSV/TSV/JSON with `level` and `response`")
+ parser.add_argument("--blanks", help="CSV/TSV/JSON with `response` for blank samples")
+ parser.add_argument("--signal-to-noise", type=float, default=None,
+ help="measured S/N at a stated concentration (use with --sn-level)")
+ parser.add_argument("--sn-level", type=float, default=None,
+ help="the concentration at which --signal-to-noise was measured")
+ parser.add_argument("--confirm-ql", type=float, default=None,
+ help="the QL being claimed, to be confirmed with --confirm-data")
+ parser.add_argument("--confirm-data", help="CSV/TSV/JSON with `measured` at/near the QL")
+ parser.add_argument("--confirm-accuracy-limit", type=float, default=20.0,
+ help="max %% bias allowed when confirming the QL (default 20)")
+ parser.add_argument("--confirm-rsd-limit", type=float, default=20.0,
+ help="max %%RSD allowed when confirming the QL (default 20)")
+ parser.add_argument("--reporting-threshold", type=float, default=None,
+ help="impurity reporting threshold the QL must be at or below")
+ parser.add_argument("--weight", choices=("none", "1/x", "1/x2"), default="none")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ rows = parse_rows(read_input(args.calibration), args.calibration)
+ require_columns(rows, ["level", "response"])
+ xs = [to_float(r["level"], "level", i) for i, r in enumerate(rows)]
+ ys = [to_float(r["response"], "response", i) for i, r in enumerate(rows)]
+ weights = None
+ if args.weight != "none":
+ if any(x == 0 for x in xs):
+ raise InputError("weighting needs non-zero levels")
+ power = 1 if args.weight == "1/x" else 2
+ weights = [1.0 / (abs(x) ** power) for x in xs]
+ fit = fit_linear(xs, ys, weights)
+ slope = fit.slope
+ if slope == 0:
+ raise InputError("calibration slope is zero; DL/QL cannot be computed")
+
+ findings: list[str] = []
+ results: list[dict] = []
+
+ # Approach 3.2.3.3, sigma = residual SD of the regression line.
+ results.append(
+ {
+ "approach": "sd-and-slope (sigma = residual SD of regression)",
+ "sigma": fit.residual_sd,
+ "slope": slope,
+ "DL": DL_FACTOR * fit.residual_sd / abs(slope),
+ "QL": QL_FACTOR * fit.residual_sd / abs(slope),
+ "reference": "Q2(R2) 3.2.3.3",
+ }
+ )
+
+ # Approach 3.2.3.3, sigma = SD of the y-intercept.
+ results.append(
+ {
+ "approach": "sd-and-slope (sigma = SD of y-intercept)",
+ "sigma": fit.se_intercept,
+ "slope": slope,
+ "DL": DL_FACTOR * fit.se_intercept / abs(slope),
+ "QL": QL_FACTOR * fit.se_intercept / abs(slope),
+ "reference": "Q2(R2) 3.2.3.3",
+ }
+ )
+
+ # Approach 3.2.3.3, sigma = SD of blank responses.
+ if args.blanks:
+ brows = parse_rows(read_input(args.blanks), args.blanks)
+ require_columns(brows, ["response"])
+ blanks = [to_float(r["response"], "response", i) for i, r in enumerate(brows)]
+ if len(blanks) < 3:
+ raise InputError("blank SD needs at least 3 blank measurements")
+ bsd = sample_sd(blanks)
+ results.append(
+ {
+ "approach": f"sd-and-slope (sigma = SD of {len(blanks)} blanks)",
+ "sigma": bsd,
+ "slope": slope,
+ "DL": DL_FACTOR * bsd / abs(slope),
+ "QL": QL_FACTOR * bsd / abs(slope),
+ "reference": "Q2(R2) 3.2.3.3",
+ }
+ )
+ note(f"blank mean response {mean(blanks):.6g}, SD {bsd:.6g}, n={len(blanks)}")
+
+ # Approach 3.2.3.2, signal-to-noise.
+ if args.signal_to_noise is not None:
+ if args.sn_level is None:
+ raise InputError("--signal-to-noise needs --sn-level")
+ if args.signal_to_noise <= 0 or args.sn_level <= 0:
+ raise InputError("--signal-to-noise and --sn-level must be > 0")
+ per_unit = args.signal_to_noise / args.sn_level
+ results.append(
+ {
+ "approach": f"signal-to-noise (S/N {args.signal_to_noise:g} at {args.sn_level:g})",
+ "sigma": float("nan"),
+ "slope": slope,
+ "DL": 3.0 / per_unit,
+ "QL": 10.0 / per_unit,
+ "reference": "Q2(R2) 3.2.3.2",
+ }
+ )
+ note(
+ "signal-to-noise DL uses the 3:1 ratio and QL the 10:1 ratio from Q2(R2) 3.2.3.2, "
+ "scaled linearly from the measured S/N. Linear scaling of noise is an assumption -- "
+ "confirm at the resulting level"
+ )
+
+ # Spread across approaches: the point of computing all of them.
+ qls = [r["QL"] for r in results if math.isfinite(r["QL"])]
+ spread_note = ""
+ if len(qls) >= 2:
+ ratio = max(qls) / min(qls) if min(qls) > 0 else float("inf")
+ spread_note = (
+ f"QL estimates span {min(qls):.6g} to {max(qls):.6g} ({ratio:.2f}x) across "
+ f"{len(qls)} approaches"
+ )
+ if ratio > 2.0:
+ findings.append(
+ spread_note
+ + ": name the approach used in the report, and confirm the claimed limit with "
+ "real data at that level (Q2(R2) 3.2.3.5)"
+ )
+
+ # QL confirmation with real data (Q2(R2) 3.2.3.4 / 3.2.3.5).
+ confirm_rows: list[dict] = []
+ if args.confirm_data:
+ if args.confirm_ql is None:
+ raise InputError("--confirm-data needs --confirm-ql")
+ crows = parse_rows(read_input(args.confirm_data), args.confirm_data)
+ require_columns(crows, ["measured"])
+ measured = [to_float(r["measured"], "measured", i) for i, r in enumerate(crows)]
+ if len(measured) < 3:
+ raise InputError("QL confirmation needs at least 3 determinations")
+ m = mean(measured)
+ bias = 100.0 * (m - args.confirm_ql) / args.confirm_ql
+ rsd = rsd_percent(measured)
+ confirm_rows = [
+ {"metric": "claimed QL", "value": args.confirm_ql},
+ {"metric": "n determinations", "value": len(measured)},
+ {"metric": "mean measured", "value": m},
+ {"metric": "bias vs claimed QL (%)", "value": bias},
+ {"metric": "RSD (%)", "value": rsd},
+ {"metric": "accuracy limit (%)", "value": args.confirm_accuracy_limit},
+ {"metric": "RSD limit (%)", "value": args.confirm_rsd_limit},
+ ]
+ if abs(bias) > args.confirm_accuracy_limit:
+ findings.append(
+ f"QL confirmation: bias {bias:+.2f}% at the claimed QL exceeds "
+ f"+/-{args.confirm_accuracy_limit:g}%"
+ )
+ if math.isfinite(rsd) and rsd > args.confirm_rsd_limit:
+ findings.append(
+ f"QL confirmation: {rsd:.2f}% RSD at the claimed QL exceeds "
+ f"{args.confirm_rsd_limit:g}%"
+ )
+ elif args.confirm_ql is not None:
+ note(
+ "a QL was claimed but no confirmation data supplied. Q2(R2) 3.2.3.5 asks that an "
+ "estimated limit be validated by analysing samples at or near it"
+ )
+
+ # The requirement that actually gates an impurity method.
+ if args.reporting_threshold is not None:
+ # With no claimed QL, use the LARGEST estimate. Taking the smallest would
+ # let the check pass on the most flattering choice of sigma, which is the
+ # wrong direction to err on a compliance requirement.
+ conservative = max(qls) if qls else float("nan")
+ claimed = args.confirm_ql if args.confirm_ql is not None else conservative
+ if args.confirm_ql is None and qls:
+ note(
+ f"no --confirm-ql given, so the reporting-threshold check uses the most "
+ f"conservative estimate ({conservative:.6g}), not the most favourable "
+ f"({min(qls):.6g})"
+ )
+ if min(qls) <= args.reporting_threshold < conservative:
+ findings.append(
+ f"the QL estimates straddle the reporting threshold "
+ f"{args.reporting_threshold:.6g}: {min(qls):.6g} would pass and "
+ f"{conservative:.6g} would not. Whether this procedure meets Q2(R2) "
+ "3.2.3.5 depends on which approach is chosen, so choose it, justify it, "
+ "and confirm the claimed limit with data at that level"
+ )
+ if math.isfinite(claimed) and claimed > args.reporting_threshold:
+ findings.append(
+ f"QL {claimed:.6g} is above the reporting threshold "
+ f"{args.reporting_threshold:.6g}. Q2(R2) 3.2.3.5 requires the QL for an "
+ "impurity procedure to be at or below the reporting threshold"
+ )
+ elif math.isfinite(claimed):
+ ratio = args.reporting_threshold / claimed if claimed > 0 else float("inf")
+ if math.isclose(ratio, 1.0, rel_tol=1e-9):
+ note(
+ f"QL {claimed:.6g} sits exactly at the reporting threshold "
+ f"{args.reporting_threshold:.6g}, which satisfies Q2(R2) 3.2.3.5 with no "
+ "margin -- any drift puts the procedure out of compliance"
+ )
+ else:
+ note(
+ f"QL {claimed:.6g} is {ratio:.1f}x below the reporting threshold "
+ f"{args.reporting_threshold:.6g}"
+ )
+ if ratio >= 10:
+ note(
+ "the QL is roughly 10x or more below the reporting limit, so Q2(R2) 3.2.3.5 "
+ "allows the confirmatory validation to be omitted with justification"
+ )
+
+ if args.format == "json":
+ emit(
+ [
+ {
+ "calibration": {
+ "n": fit.n,
+ "slope": slope,
+ "intercept": fit.intercept,
+ "residual_sd": fit.residual_sd,
+ "weighting": args.weight,
+ },
+ "estimates": results,
+ "confirmation": confirm_rows,
+ "findings": findings,
+ }
+ ],
+ "json",
+ )
+ else:
+ emit(results, args.format)
+ if confirm_rows:
+ print()
+ emit(confirm_rows, args.format)
+
+ if not args.confirm_data:
+ note(
+ f"approach 'accuracy-precision' ({DL_QL_APPROACHES['accuracy-precision']['note']}) "
+ "validates the QL directly rather than estimating it -- supply --confirm-data "
+ "to use it"
+ )
+ if spread_note and not any(spread_note in f for f in findings):
+ note(spread_note)
+ note("Q2(R2) 3.2.3.5: report the limit AND the approach used to determine it")
+ for f in findings:
+ finding(f)
+ if not findings:
+ note("no findings against the checks that were run")
+ note("this tool does not decide that a detection or quantitation limit is acceptable")
+ return EXIT_FINDINGS if findings else EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/skills/analytical-method-validation/scripts/check_response.py b/skills/analytical-method-validation/scripts/check_response.py
new file mode 100644
index 00000000..9b7c0411
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/check_response.py
@@ -0,0 +1,243 @@
+#!/usr/bin/env python3
+"""Evaluate a calibration response (linearity) the way ICH Q2(R2) 3.2.2 asks.
+
+Reports what the guideline asks to be reported -- slope, intercept, coefficient
+of determination, and an analysis of the deviation of points from the line --
+and adds the diagnostics that actually detect an unsuitable model: a lack-of-fit
+F test against pure error, a runs test on residual signs, back-calculated
+relative error per level, and a heteroscedasticity check that tells you whether
+weighting is needed.
+
+ python3 check_response.py --input calib.csv
+ python3 check_response.py --input calib.csv --weight 1/x2 --levels-required 5
+ python3 check_response.py --input calib.csv --max-back-calc-error 5 --format json
+
+Input columns: `level` (nominal concentration or %) and `response` (signal).
+An optional `replicate` column is ignored -- replicates are simply repeated rows
+at the same level, which is what enables the lack-of-fit test.
+
+Exit codes: 0 no findings, 1 findings raised, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import math
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _common import ( # noqa: E402
+ EXIT_FINDINGS,
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ emit,
+ finding,
+ fit_linear,
+ heteroscedasticity,
+ lack_of_fit,
+ mean,
+ note,
+ parse_rows,
+ read_input,
+ require_columns,
+ run_cli,
+ runs_test,
+ to_float,
+)
+
+WEIGHT_SCHEMES = {
+ "none": lambda x: 1.0,
+ "1/x": lambda x: 1.0 / abs(x) if x != 0 else 0.0,
+ "1/x2": lambda x: 1.0 / (x * x) if x != 0 else 0.0,
+}
+
+
+def build_weights(xs: list[float], scheme: str) -> list[float] | None:
+ if scheme == "none":
+ return None
+ fn = WEIGHT_SCHEMES[scheme]
+ if any(x == 0 for x in xs):
+ raise InputError(f"weighting {scheme} needs non-zero levels; a zero level was supplied")
+ return [fn(x) for x in xs]
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description="Check a calibration response for linearity.")
+ parser.add_argument("--input", "-i", help="CSV/TSV/JSON file, or '-' for stdin")
+ parser.add_argument("--weight", choices=sorted(WEIGHT_SCHEMES), default="none",
+ help="calibration weighting (default: none)")
+ parser.add_argument("--levels-required", type=int, default=5,
+ help="minimum distinct levels expected (Q2(R2) recommends 5)")
+ parser.add_argument("--max-back-calc-error", type=float, default=None,
+ help="flag any level whose mean back-calculated error exceeds this %%")
+ parser.add_argument("--alpha", type=float, default=0.05,
+ help="significance level for lack-of-fit and runs tests")
+ parser.add_argument("--through-origin-tolerance", type=float, default=None,
+ help="flag when the intercept exceeds this %% of the response at the "
+ "highest level (a proxy for the y-intercept significance check)")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ rows = parse_rows(read_input(args.input), args.input)
+ require_columns(rows, ["level", "response"])
+ xs = [to_float(r["level"], "level", i) for i, r in enumerate(rows)]
+ ys = [to_float(r["response"], "response", i) for i, r in enumerate(rows)]
+
+ weights = build_weights(xs, args.weight)
+ fit = fit_linear(xs, ys, weights)
+ distinct = sorted({round(x, 12) for x in xs})
+
+ findings: list[str] = []
+
+ # Q2(R2) 3.2.2.1: a minimum of five concentrations is recommended.
+ if len(distinct) < args.levels_required:
+ findings.append(
+ f"{len(distinct)} distinct levels; Q2(R2) 3.2.2.1 recommends at least "
+ f"{args.levels_required} appropriately distributed across the range"
+ )
+
+ lof = lack_of_fit(xs, ys, fit)
+ runs = runs_test(fit.residuals)
+ het = heteroscedasticity(xs, fit.residuals)
+
+ if lof.get("applicable") and lof["p_value"] < args.alpha:
+ findings.append(
+ f"lack-of-fit F={lof['f_statistic']:.3f} on {lof['df_lack_of_fit']}/"
+ f"{lof['df_pure_error']} df, p={lof['p_value']:.4g}: the straight line does not "
+ "describe the data beyond replicate scatter"
+ )
+ if math.isfinite(runs.get("p_value", float("nan"))) and runs["p_value"] < args.alpha:
+ findings.append(
+ f"residual signs are non-random (runs={runs['runs']}, expected "
+ f"{runs['expected_runs']:.1f}, p={runs['p_value']:.4g}): inspect the residual plot "
+ "for curvature"
+ )
+ if het.get("applicable") and het["variance_ratio_high_over_low"] > 10 and args.weight == "none":
+ findings.append(
+ f"residual variance is {het['variance_ratio_high_over_low']:.1f}x larger in the top "
+ "third of the range than the bottom, and the fit is unweighted: back-calculated "
+ "results at the low end are biased. Consider 1/x or 1/x2 weighting"
+ )
+
+ # Back-calculated relative error per level -- the practical test of the model.
+ level_rows = []
+ by_level: dict[float, list[float]] = {}
+ for x, y in zip(xs, ys):
+ by_level.setdefault(round(x, 12), []).append(y)
+ for level in distinct:
+ responses = by_level[level]
+ back = [
+ (r - fit.intercept) / fit.slope if fit.slope != 0 else float("nan")
+ for r in responses
+ ]
+ mean_back = mean(back)
+ rel_err = 100.0 * (mean_back - level) / level if level != 0 else float("nan")
+ level_rows.append(
+ {
+ "level": level,
+ "n": len(responses),
+ "mean_response": mean(responses),
+ "mean_back_calculated": mean_back,
+ "relative_error_pct": rel_err,
+ }
+ )
+ if (
+ args.max_back_calc_error is not None
+ and math.isfinite(rel_err)
+ and abs(rel_err) > args.max_back_calc_error
+ ):
+ findings.append(
+ f"level {level:g}: back-calculated mean deviates {rel_err:+.2f}% from nominal, "
+ f"outside the stated +/-{args.max_back_calc_error:g}%"
+ )
+
+ if args.through_origin_tolerance is not None:
+ top_response = fit.predict(max(distinct))
+ if top_response != 0:
+ pct = 100.0 * abs(fit.intercept) / abs(top_response)
+ if pct > args.through_origin_tolerance:
+ findings.append(
+ f"intercept is {pct:.2f}% of the response at the highest level, above the "
+ f"stated {args.through_origin_tolerance:g}% tolerance"
+ )
+
+ slope_lo, slope_hi = fit.slope_ci()
+ int_lo, int_hi = fit.intercept_ci()
+ summary = [
+ {"statistic": "n points", "value": fit.n},
+ {"statistic": "distinct levels", "value": len(distinct)},
+ {"statistic": "weighting", "value": args.weight},
+ {"statistic": "slope", "value": fit.slope},
+ {"statistic": "slope 95% CI", "value": f"{slope_lo:.6g} to {slope_hi:.6g}"},
+ {"statistic": "intercept", "value": fit.intercept},
+ {"statistic": "intercept 95% CI", "value": f"{int_lo:.6g} to {int_hi:.6g}"},
+ {"statistic": "intercept CI includes 0", "value": int_lo <= 0.0 <= int_hi},
+ {"statistic": "coefficient of determination (r2)", "value": fit.r_squared},
+ {"statistic": "correlation coefficient (r)", "value": fit.r},
+ {"statistic": "residual SD", "value": fit.residual_sd},
+ {"statistic": "residual df", "value": fit.df},
+ ]
+ if lof.get("applicable"):
+ summary += [
+ {"statistic": "lack-of-fit F", "value": lof["f_statistic"]},
+ {"statistic": "lack-of-fit p", "value": lof["p_value"]},
+ ]
+ else:
+ summary.append(
+ {"statistic": "lack-of-fit test", "value": f"not run ({lof.get('reason', '')})"}
+ )
+ summary += [
+ {"statistic": "runs test p", "value": runs.get("p_value", float("nan"))},
+ {
+ "statistic": "residual SD ratio (high/low third)",
+ "value": het.get("sd_ratio", float("nan")),
+ },
+ ]
+
+ if args.format == "json":
+ emit(
+ [
+ {
+ "summary": {r["statistic"]: r["value"] for r in summary},
+ "levels": level_rows,
+ "findings": findings,
+ }
+ ],
+ "json",
+ )
+ else:
+ emit(summary, args.format)
+ print()
+ emit(level_rows, args.format)
+
+ note(
+ "Q2(R2) 3.2.2.1 asks for the plot, r or r-squared, intercept, slope, and an analysis of "
+ "the deviation of points from the line"
+ )
+ note(
+ "r-squared alone does not demonstrate linearity: it rises with range and is insensitive "
+ "to curvature. The lack-of-fit test and the residual pattern are the evidence"
+ )
+ if args.weight != "none" and lof.get("applicable"):
+ note(
+ f"the lack-of-fit F test is computed on unweighted residuals while the fit used "
+ f"{args.weight} weighting, so its null distribution is approximate here. Read it "
+ "alongside the back-calculated error per level, which is unaffected"
+ )
+ if not lof.get("applicable"):
+ note(
+ "no replicates at any level, so pure error could not be separated from lack of fit. "
+ "Replicating at least one level makes the linearity test possible"
+ )
+ for f in findings:
+ finding(f)
+ if not findings:
+ note("no findings against the checks that were run")
+ note("this tool does not decide that the calibration model is acceptable")
+ return EXIT_FINDINGS if findings else EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/skills/analytical-method-validation/scripts/compare_methods.py b/skills/analytical-method-validation/scripts/compare_methods.py
new file mode 100644
index 00000000..88bc5933
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/compare_methods.py
@@ -0,0 +1,215 @@
+#!/usr/bin/env python3
+"""Compare two analytical procedures for a transfer, bridging, or bias study.
+
+Uses the regressions that belong to method comparison -- Deming and
+Passing-Bablok, which allow error in both measurements -- rather than ordinary
+least squares, which assumes the comparative procedure is error-free and biases
+the slope toward zero. Adds Bland-Altman agreement and, most importantly, a TOST
+equivalence test.
+
+The default reflex at a method transfer is a t test, and "p > 0.05, no
+significant difference" is then written up as evidence of equivalence. It is not:
+failing to detect a difference is not the same as showing there is none, and with
+a small transfer dataset that outcome is close to guaranteed. TOST tests the
+hypothesis that matters -- that the true difference lies inside a pre-stated
+acceptance margin.
+
+ python3 compare_methods.py --input paired.csv --margin 2
+ python3 compare_methods.py -i paired.csv --margin 2 --relative --lambda 1.0
+
+Input columns: `reference` and `test` (paired results on the same samples).
+An optional `sample` column labels the rows.
+
+Exit codes: 0 equivalence demonstrated at the stated margin, 1 not demonstrated
+or other findings, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import math
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _common import ( # noqa: E402
+ EXIT_FINDINGS,
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ bland_altman,
+ deming,
+ emit,
+ finding,
+ fit_linear,
+ mean,
+ note,
+ parse_rows,
+ passing_bablok,
+ read_input,
+ require_columns,
+ run_cli,
+ t_cdf,
+ to_float,
+ tost_paired,
+)
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(
+ description="Compare two procedures with the statistics method comparison requires."
+ )
+ parser.add_argument("--input", "-i", help="CSV/TSV/JSON with `reference` and `test`")
+ parser.add_argument("--margin", type=float, required=True,
+ help="pre-stated equivalence margin for TOST, in the units of the "
+ "difference (absolute, or %% when --relative is used)")
+ parser.add_argument("--relative", action="store_true",
+ help="work in percent differences relative to the pair mean")
+ parser.add_argument("--lambda", dest="lambda_ratio", type=float, default=1.0,
+ help="Deming error-variance ratio var(y error)/var(x error) -- test "
+ "procedure over comparative procedure. Default 1.0 means equal "
+ "precision. Estimate it as (SD of x replicates / SD of y "
+ "replicates) squared")
+ parser.add_argument("--alpha", type=float, default=0.05,
+ help="one-sided alpha for TOST (default 0.05)")
+ parser.add_argument("--slope-tolerance", type=float, default=None,
+ help="flag when the slope CI excludes 1 +/- this amount")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ rows = parse_rows(read_input(args.input), args.input)
+ require_columns(rows, ["reference", "test"])
+ ref = [to_float(r["reference"], "reference", i) for i, r in enumerate(rows)]
+ test = [to_float(r["test"], "test", i) for i, r in enumerate(rows)]
+ n = len(ref)
+ if n < 3:
+ raise InputError("method comparison needs at least 3 paired results")
+
+ findings: list[str] = []
+
+ # Agreement.
+ ba = bland_altman(ref, test, relative=args.relative)
+ unit = "%" if args.relative else "units"
+
+ # TOST on the differences that Bland-Altman used.
+ if args.relative:
+ diffs = [
+ 100.0 * (t - r) / (0.5 * (r + t)) for r, t in zip(ref, test)
+ ]
+ else:
+ diffs = [t - r for r, t in zip(ref, test)]
+ tost = tost_paired(diffs, args.margin, args.alpha)
+
+ # The naive test, computed only to show what it does not establish.
+ d_mean = mean(diffs)
+ sd = ba["sd_differences"]
+ se = sd / math.sqrt(n) if sd > 0 else float("nan")
+ t_stat = d_mean / se if se and math.isfinite(se) and se > 0 else float("nan")
+ p_naive = (
+ 2.0 * (1.0 - t_cdf(abs(t_stat), n - 1)) if math.isfinite(t_stat) else float("nan")
+ )
+
+ # Regressions.
+ ols = fit_linear(ref, test)
+ dem = deming(ref, test, args.lambda_ratio)
+ try:
+ pb = passing_bablok(ref, test)
+ except InputError as exc:
+ pb = None
+ note(f"Passing-Bablok not computed: {exc}")
+
+ summary = [
+ {"statistic": "n pairs", "value": n},
+ {"statistic": f"mean difference ({unit})", "value": ba["bias"]},
+ {"statistic": "difference 95% CI", "value":
+ f"{ba['bias_ci95'][0]:.6g} to {ba['bias_ci95'][1]:.6g}"},
+ {"statistic": f"SD of differences ({unit})", "value": sd},
+ {"statistic": "limits of agreement", "value":
+ f"{ba['loa_lower']:.6g} to {ba['loa_upper']:.6g}"},
+ {"statistic": "LoA 95% CI half-width", "value": ba["loa_ci95_halfwidth"]},
+ {"statistic": "--- equivalence ---", "value": ""},
+ {"statistic": "TOST margin", "value": args.margin},
+ {"statistic": "TOST p-value", "value": tost["p_value"]},
+ {"statistic": f"{100 * (1 - 2 * args.alpha):.0f}% CI (TOST)", "value":
+ f"{tost['ci_1_minus_2alpha'][0]:.6g} to {tost['ci_1_minus_2alpha'][1]:.6g}"},
+ {"statistic": "equivalent at stated margin", "value": tost["equivalent"]},
+ {"statistic": "--- for contrast only ---", "value": ""},
+ {"statistic": "paired t-test p (NOT equivalence)", "value": p_naive},
+ {"statistic": "--- regressions ---", "value": ""},
+ {"statistic": "OLS slope (biased here)", "value": ols.slope},
+ {"statistic": "Deming slope", "value": dem["slope"]},
+ {"statistic": "Deming slope 95% CI", "value":
+ f"{dem['slope_ci95'][0]:.6g} to {dem['slope_ci95'][1]:.6g}"},
+ {"statistic": "Deming intercept", "value": dem["intercept"]},
+ ]
+ if pb:
+ summary += [
+ {"statistic": "Passing-Bablok slope", "value": pb["slope"]},
+ {"statistic": "Passing-Bablok slope 95% CI", "value":
+ f"{pb['slope_ci95'][0]:.6g} to {pb['slope_ci95'][1]:.6g}"},
+ {"statistic": "Passing-Bablok intercept", "value": pb["intercept"]},
+ ]
+
+ prop = ba.get("proportional_bias")
+ if prop and math.isfinite(prop.get("p_value", float("nan"))):
+ summary.append(
+ {"statistic": "proportional bias p (difference vs mean)", "value": prop["p_value"]}
+ )
+ if prop["p_value"] < 0.05:
+ findings.append(
+ f"the difference trends with concentration (slope {prop['slope']:.4g}, "
+ f"p={prop['p_value']:.4g}): a single mean bias does not describe the "
+ "disagreement, and limits of agreement are misleading"
+ )
+
+ if not tost["equivalent"]:
+ findings.append(
+ f"equivalence NOT demonstrated at +/-{args.margin:g} {unit}: the "
+ f"{100 * (1 - 2 * args.alpha):.0f}% CI "
+ f"({tost['ci_1_minus_2alpha'][0]:.4g}, {tost['ci_1_minus_2alpha'][1]:.4g}) is not "
+ f"contained in the margin"
+ )
+
+ if args.slope_tolerance is not None:
+ lo, hi = dem["slope_ci95"]
+ target_lo, target_hi = 1.0 - args.slope_tolerance, 1.0 + args.slope_tolerance
+ if lo < target_lo or hi > target_hi:
+ findings.append(
+ f"Deming slope 95% CI ({lo:.4g}, {hi:.4g}) is not contained in "
+ f"({target_lo:g}, {target_hi:g})"
+ )
+
+ if args.format == "json":
+ emit([{"summary": {r["statistic"]: r["value"] for r in summary
+ if not r["statistic"].startswith("---")},
+ "bland_altman": ba, "deming": dem,
+ "passing_bablok": pb, "tost": tost,
+ "ols_slope": ols.slope, "paired_t_p_value": p_naive,
+ "findings": findings}], "json")
+ else:
+ emit(summary, args.format)
+
+ if math.isfinite(p_naive) and p_naive > 0.05 and not tost["equivalent"]:
+ note(
+ f"the paired t-test gives p={p_naive:.4g}, which would often be written up as "
+ "'no significant difference'. TOST shows equivalence is NOT established at the "
+ "stated margin. Absence of a detected difference is not evidence of equivalence"
+ )
+ note(
+ "ordinary least squares assumes the reference values carry no error, which is false in a "
+ "method comparison; the OLS slope is shown only for contrast"
+ )
+ note(
+ "the equivalence margin must be pre-stated from the specification or the analytical "
+ "target profile, never chosen after seeing the data"
+ )
+ for f in findings:
+ finding(f)
+ if not findings:
+ note("no findings against the checks that were run")
+ note("this tool does not decide that a transfer or comparison passes")
+ return EXIT_FINDINGS if findings else EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/skills/analytical-method-validation/scripts/plan_validation.py b/skills/analytical-method-validation/scripts/plan_validation.py
new file mode 100644
index 00000000..d54ce45e
--- /dev/null
+++ b/skills/analytical-method-validation/scripts/plan_validation.py
@@ -0,0 +1,381 @@
+#!/usr/bin/env python3
+"""Design a validation study: which framework, which tests, what study layout.
+
+Answers the first question of any validation exercise -- what am I required to
+demonstrate, and with how much data -- before any sample is injected.
+
+ python3 plan_validation.py --framework ich-q2r2 --attribute assay --technique hplc
+ python3 plan_validation.py --framework ich-m10 --modality lba
+ python3 plan_validation.py --list-frameworks
+ python3 plan_validation.py --framework ich-q2r2 --attribute impurity --protocol > protocol.md
+
+Exit codes: 0 plan produced, 2 bad input.
+"""
+
+from __future__ import annotations
+
+import argparse
+import sys
+
+sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent))
+
+from _catalog import ( # noqa: E402
+ DL_QL_APPROACHES,
+ FRAMEWORKS,
+ M10_CRITERIA,
+ Q2R2_REPORTABLE_RANGE,
+ Q2R2_STUDY_DESIGN,
+ Q2R2_TESTS_BY_ATTRIBUTE,
+ RESEARCH_DATE,
+ TECHNIQUE_NOTES,
+ resolve_attribute,
+)
+from _common import ( # noqa: E402
+ EXIT_OK,
+ InputError,
+ add_common_args,
+ emit,
+ note,
+ run_cli,
+)
+
+STATUS_TEXT = {
+ "required": "conduct",
+ "required-QL": "conduct (QL; DL too in some complex cases)",
+ "required-DL": "conduct (DL)",
+ "required-unless-reproducibility": (
+ "conduct, unless intermediate precision can be derived from a reproducibility dataset"
+ ),
+ "not-normally": "not normally conducted",
+}
+
+
+def plan_q2r2(attribute: str, technique: str | None, range_use: str | None) -> list[dict]:
+ tests = Q2R2_TESTS_BY_ATTRIBUTE[attribute]
+ rows = []
+ for characteristic, status in tests.items():
+ design = Q2R2_STUDY_DESIGN.get(characteristic, {})
+ rows.append(
+ {
+ "characteristic": characteristic,
+ "required": STATUS_TEXT[status],
+ "study_design": design.get("requirement", ""),
+ "report": design.get("report", ""),
+ "reference": design.get("reference", ""),
+ }
+ )
+ # Robustness sits in development under Q14 but belongs in the plan.
+ rows.append(
+ {
+ "characteristic": "robustness",
+ "required": "development activity (ICH Q14); available on request",
+ "study_design": Q2R2_STUDY_DESIGN["robustness"]["requirement"],
+ "report": Q2R2_STUDY_DESIGN["robustness"]["report"],
+ "reference": Q2R2_STUDY_DESIGN["robustness"]["reference"],
+ }
+ )
+ if technique:
+ tn = TECHNIQUE_NOTES.get(technique)
+ if tn:
+ rows.append(
+ {
+ "characteristic": f"technique note ({technique})",
+ "required": f"see Q2(R2) Annex 2 {tn['annex_table']}",
+ "study_design": f"robustness parameters: {tn['robustness']}",
+ "report": tn["special"],
+ "reference": "Q2(R2) Annex 2",
+ }
+ )
+ if range_use:
+ rr = Q2R2_REPORTABLE_RANGE.get(range_use)
+ if rr:
+ rows.append(
+ {
+ "characteristic": f"reportable range ({range_use})",
+ "required": "confirm response, accuracy and precision across this range",
+ "study_design": f"low: {rr['low']}",
+ "report": f"high: {rr['high']}",
+ "reference": "Q2(R2) 2.3, Table 2",
+ }
+ )
+ return rows
+
+
+def plan_m10(modality: str) -> list[dict]:
+ crit = M10_CRITERIA[modality]
+ rows = [
+ {
+ "item": "calibration curve",
+ "requirement": (
+ f"minimum {crit['calibration_min_levels']} concentration levels including "
+ f"the LLOQ; at least "
+ f"{crit['calibration_min_pass_fraction'] * 100:.0f}% of standards must pass"
+ ),
+ "tolerance": (
+ f"+/-{crit['calibration_tolerance_pct']:.0f}% nominal; "
+ f"+/-{crit['calibration_tolerance_lloq_pct']:.0f}% at LLOQ; "
+ f"+/-{crit['calibration_tolerance_uloq_pct']:.0f}% at ULOQ"
+ ),
+ },
+ {
+ "item": "accuracy and precision QC levels",
+ "requirement": (
+ f"{crit['qc_levels_accuracy_precision']} levels; "
+ f"{crit['ap_replicates_per_run']} replicates per level per run; "
+ f"at least {crit['ap_min_runs']} runs over at least {crit['ap_min_days']} days"
+ ),
+ "tolerance": (
+ f"accuracy +/-{crit['accuracy_tolerance_pct']:.0f}% "
+ f"(+/-{crit['accuracy_tolerance_lloq_pct']:.0f}% at {crit['limit_levels']}); "
+ f"precision CV <={crit['precision_cv_pct']:.0f}% "
+ f"(<={crit['precision_cv_lloq_pct']:.0f}% at {crit['limit_levels']})"
+ ),
+ },
+ {
+ "item": "routine run acceptance",
+ "requirement": (
+ f"{crit['qc_levels_routine_run']} QC levels; at least "
+ f"{crit['qc_run_pass_fraction'] * 100:.0f}% of all QCs and at least "
+ f"{crit['qc_run_pass_fraction_per_level'] * 100:.0f}% at each level must pass"
+ ),
+ "tolerance": f"+/-{crit['qc_run_tolerance_pct']:.0f}% nominal",
+ },
+ {
+ "item": "selectivity",
+ "requirement": f"at least {crit['selectivity_min_sources']} individual matrix sources/lots",
+ "tolerance": (
+ f"interference <={crit['carryover_blank_pct_of_lloq']:.0f}% of LLOQ analyte "
+ f"response and <={crit['carryover_blank_pct_of_is']:.0f}% of IS response"
+ if crit["carryover_blank_pct_of_lloq"] is not None
+ else "per guideline; evaluate interference in each source"
+ ),
+ },
+ {
+ "item": "dilution integrity",
+ "requirement": "validate the dilution factors used in study sample analysis",
+ "tolerance": f"mean within +/-{crit['dilution_tolerance_pct']:.0f}% nominal",
+ },
+ {
+ "item": "stability",
+ "requirement": "cover the conditions and durations study samples actually experience",
+ "tolerance": f"mean at each QC level within +/-{crit['stability_tolerance_pct']:.0f}% nominal",
+ },
+ {
+ "item": "incurred sample reanalysis",
+ "requirement": (
+ f"repeat a predefined subset in separate runs; at least "
+ f"{crit['isr_pass_fraction'] * 100:.0f}% of repeats must agree"
+ ),
+ "tolerance": f"percent difference within +/-{crit['isr_tolerance_pct']:.0f}%",
+ },
+ ]
+ if crit["total_error_pct"] is not None:
+ rows.append(
+ {
+ "item": "total error",
+ "requirement": "sum of absolute accuracy (%) and precision (%)",
+ "tolerance": (
+ f"<={crit['total_error_pct']:.0f}%, "
+ f"<={crit['total_error_pct_at_limits']:.0f}% at LLOQ and ULOQ"
+ ),
+ }
+ )
+ return rows
+
+
+def render_protocol(framework: str, attribute: str | None, modality: str | None,
+ technique: str | None, range_use: str | None) -> str:
+ fw = FRAMEWORKS[framework]
+ lines = [
+ "# Analytical Procedure Validation Protocol",
+ "",
+ "> Draft skeleton. Every bracketed field is a decision the analyst and quality unit",
+ "> must make and record BEFORE data collection. Acceptance criteria set after seeing",
+ "> data are not acceptance criteria.",
+ "",
+ "## 1. Identification",
+ "",
+ "| Field | Value |",
+ "| --- | --- |",
+ "| Protocol number / version | [ ] |",
+ "| Analytical procedure identifier | [ ] |",
+ "| Product / analyte / matrix | [ ] |",
+ "| Governing framework | " + fw["title"] + " |",
+ "| Regional expectation confirmed | [ ] " + fw["effective_note"] + " |",
+ "| Author / date | [ ] |",
+ "| Reviewed / approved by (quality unit) | [ ] |",
+ "",
+ "## 2. Intended purpose and analytical target profile",
+ "",
+ "- Measurand and reporting unit: [ ]",
+ "- Decision the result supports (release, stability, in-process, clinical): [ ]",
+ "- Specification or reporting limits the procedure must serve: [ ]",
+ "- Required reportable range, derived from the specification: [ ]",
+ "- Performance characteristics and criteria (the ATP, ICH Q14 section 3): [ ]",
+ "",
+ "## 3. Pre-stated acceptance criteria",
+ "",
+ "| Characteristic | Criterion | Justification | Source |",
+ "| --- | --- | --- | --- |",
+ ]
+ if framework == "ich-m10" and modality:
+ for row in plan_m10(modality):
+ lines.append(
+ f"| {row['item']} | {row['tolerance']} | guideline default | ICH M10 |"
+ )
+ elif attribute:
+ for row in plan_q2r2(attribute, technique, range_use):
+ crit = "[ ] state a numeric criterion"
+ lines.append(
+ f"| {row['characteristic']} | {crit} | [ ] | {row['reference'] or 'ICH Q2(R2)'} |"
+ )
+ lines += [
+ "",
+ "> ICH Q2(R2) deliberately does not set numeric acceptance criteria for most",
+ "> characteristics. A criterion has to come from the specification, the ATP, product",
+ "> knowledge, or development data -- not from a remembered default.",
+ "",
+ "## 4. Study design",
+ "",
+ "| Characteristic | Levels | Replicates | Runs / days / analysts / instruments |",
+ "| --- | --- | --- | --- |",
+ "| [ ] | [ ] | [ ] | [ ] |",
+ "",
+ "- Reference materials and their documented identity/purity: [ ]",
+ "- Number of replicates matches the routine reportable result: [ ] yes / [ ] justified",
+ "- Prior knowledge or development data used in place of a test, with justification: [ ]",
+ "",
+ "## 5. Sample and solution handling",
+ "",
+ "- Preparation, storage, and solution stability window: [ ]",
+ "- Blank, placebo, and spiked matrix definitions: [ ]",
+ "",
+ "## 6. Statistical treatment",
+ "",
+ "- Calibration model and weighting, stated in advance: [ ]",
+ "- Interval to be reported with accuracy and precision (confidence level): [ ]",
+ "- Software, version, and calculation verification: [ ]",
+ "",
+ "## 7. Deviations and data integrity",
+ "",
+ "- Deviation handling and reporting: [ ]",
+ "- All results reported, including out-of-criteria values: [ ] confirmed",
+ "- Raw data location and audit trail: [ ]",
+ "",
+ "## 8. Approvals",
+ "",
+ "| Role | Name | Signature | Date |",
+ "| --- | --- | --- | --- |",
+ "| Author | | | |",
+ "| Technical reviewer | | | |",
+ "| Quality unit | | | |",
+ "",
+ f"_Framework metadata researched {RESEARCH_DATE}. Confirm the current guideline text "
+ f"and regional expectation before use: {fw['url']}_",
+ ]
+ return "\n".join(lines)
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(
+ description="Plan an analytical procedure validation study.",
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ )
+ parser.add_argument("--framework", help="governing framework key")
+ parser.add_argument("--attribute", help="measured attribute (assay, impurity, identity, ...)")
+ parser.add_argument("--modality", choices=sorted(M10_CRITERIA),
+ help="ICH M10 only: chromatographic or lba")
+ parser.add_argument("--technique", choices=sorted(TECHNIQUE_NOTES),
+ help="analytical technique, for Annex 2 notes")
+ parser.add_argument("--range-use", choices=sorted(Q2R2_REPORTABLE_RANGE),
+ help="reportable range example to include")
+ parser.add_argument("--protocol", action="store_true",
+ help="emit a validation protocol skeleton in Markdown")
+ parser.add_argument("--list-frameworks", action="store_true")
+ parser.add_argument("--list-dl-ql", action="store_true",
+ help="list the DL/QL estimation approaches")
+ add_common_args(parser)
+ args = parser.parse_args()
+
+ if args.list_frameworks:
+ rows = [
+ {
+ "key": key,
+ "title": fw["title"],
+ "adopted": fw["adopted"],
+ "text_reusable": "yes" if fw["reproducible"] else "no (paywalled)",
+ "scope": fw["scope"],
+ }
+ for key, fw in FRAMEWORKS.items()
+ ]
+ emit(rows, args.format)
+ note(f"framework metadata researched {RESEARCH_DATE}; confirm before relying on a date")
+ return EXIT_OK
+
+ if args.list_dl_ql:
+ rows = [
+ {"approach": k, "detection_limit": v["dl"], "quantitation_limit": v["ql"],
+ "note": v["note"]}
+ for k, v in DL_QL_APPROACHES.items()
+ ]
+ emit(rows, args.format)
+ note("source: ICH Q2(R2) 3.2.3")
+ return EXIT_OK
+
+ if not args.framework:
+ raise InputError("--framework is required (or use --list-frameworks)")
+ if args.framework not in FRAMEWORKS:
+ raise InputError(
+ f"unknown framework {args.framework!r}; choose from: {', '.join(FRAMEWORKS)}"
+ )
+ fw = FRAMEWORKS[args.framework]
+
+ attribute = None
+ if args.attribute:
+ try:
+ attribute = resolve_attribute(args.attribute)
+ except KeyError as exc:
+ raise InputError(str(exc)) from exc
+
+ if args.framework == "ich-m10":
+ if not args.modality:
+ raise InputError("ICH M10 needs --modality chromatographic|lba")
+ rows = plan_m10(args.modality)
+ elif args.framework == "ich-q2r2":
+ if not attribute:
+ raise InputError("ICH Q2(R2) needs --attribute (assay, impurity, identity, ...)")
+ rows = plan_q2r2(attribute, args.technique, args.range_use)
+ else:
+ note(
+ f"{fw['title']} is copyrighted and not reproduced here. This skill reports its "
+ "designation, scope, and where to obtain it; the study design must come from the "
+ "authorised text."
+ )
+ rows = [
+ {
+ "framework": args.framework,
+ "title": fw["title"],
+ "scope": fw["scope"],
+ "obtain_from": fw["url"],
+ "companion": fw["companion"],
+ }
+ ]
+
+ if args.protocol:
+ print(render_protocol(args.framework, attribute, args.modality,
+ args.technique, args.range_use))
+ note("protocol skeleton written; every bracketed field needs a decision before data")
+ return EXIT_OK
+
+ emit(rows, args.format)
+ note(f"framework: {fw['title']}")
+ note(f"regional applicability: {fw['effective_note']}")
+ if fw["companion"]:
+ note(f"companion: {fw['companion']}")
+ note(f"catalogue researched {RESEARCH_DATE}; confirm the current text at {fw['url']}")
+ note("this tool reports requirements; it does not decide fitness for purpose")
+ return EXIT_OK
+
+
+if __name__ == "__main__":
+ run_cli(main)
diff --git a/tests/analytical-method-validation/fixtures/ap_between_day.csv b/tests/analytical-method-validation/fixtures/ap_between_day.csv
new file mode 100644
index 00000000..8ed660cc
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/ap_between_day.csv
@@ -0,0 +1,19 @@
+level,measured,group
+80,80.4,day1
+100,100.5,day1
+120,120.6,day1
+80,80.3,day1
+100,100.4,day1
+120,120.5,day1
+80,78.9,day2
+100,98.6,day2
+120,118.4,day2
+80,79.0,day2
+100,98.7,day2
+120,118.5,day2
+80,81.5,day3
+100,101.9,day3
+120,122.3,day3
+80,81.6,day3
+100,102.0,day3
+120,122.4,day3
diff --git a/tests/analytical-method-validation/fixtures/ap_no_group.csv b/tests/analytical-method-validation/fixtures/ap_no_group.csv
new file mode 100644
index 00000000..44d765b1
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/ap_no_group.csv
@@ -0,0 +1,7 @@
+level,measured
+100,100.2
+100,99.8
+100,100.4
+100,99.9
+100,100.1
+100,100.3
diff --git a/tests/analytical-method-validation/fixtures/ap_thin_design.csv b/tests/analytical-method-validation/fixtures/ap_thin_design.csv
new file mode 100644
index 00000000..991ee2c0
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/ap_thin_design.csv
@@ -0,0 +1,4 @@
+level,measured
+100,100.2
+100,99.8
+120,120.1
diff --git a/tests/analytical-method-validation/fixtures/ap_zero_level.csv b/tests/analytical-method-validation/fixtures/ap_zero_level.csv
new file mode 100644
index 00000000..a585716d
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/ap_zero_level.csv
@@ -0,0 +1,4 @@
+level,measured
+0,0.5
+100,100.2
+100,99.8
diff --git a/tests/analytical-method-validation/fixtures/bad_columns.csv b/tests/analytical-method-validation/fixtures/bad_columns.csv
new file mode 100644
index 00000000..3619f463
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/bad_columns.csv
@@ -0,0 +1,4 @@
+concentration,signal
+50,10100
+100,20100
+150,30100
diff --git a/tests/analytical-method-validation/fixtures/blanks.csv b/tests/analytical-method-validation/fixtures/blanks.csv
new file mode 100644
index 00000000..bbc6cdfe
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/blanks.csv
@@ -0,0 +1,9 @@
+response
+4
+-3
+6
+2
+-5
+3
+1
+-2
diff --git a/tests/analytical-method-validation/fixtures/calibration_curved.csv b/tests/analytical-method-validation/fixtures/calibration_curved.csv
new file mode 100644
index 00000000..90759f51
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_curved.csv
@@ -0,0 +1,11 @@
+level,response
+50,10100
+50,10050
+75,15100
+75,15200
+100,20000
+100,20100
+125,24000
+125,24100
+150,26500
+150,26400
diff --git a/tests/analytical-method-validation/fixtures/calibration_good.csv b/tests/analytical-method-validation/fixtures/calibration_good.csv
new file mode 100644
index 00000000..089b8221
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_good.csv
@@ -0,0 +1,16 @@
+level,response
+50,10120
+50,10080
+50,10150
+75,15200
+75,15100
+75,15250
+100,20150
+100,20250
+100,20050
+125,25200
+125,25100
+125,25300
+150,30250
+150,30150
+150,30350
diff --git a/tests/analytical-method-validation/fixtures/calibration_good.json b/tests/analytical-method-validation/fixtures/calibration_good.json
new file mode 100644
index 00000000..098495fc
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_good.json
@@ -0,0 +1,62 @@
+[
+ {
+ "level": 50.0,
+ "response": 10120.0
+ },
+ {
+ "level": 50.0,
+ "response": 10080.0
+ },
+ {
+ "level": 50.0,
+ "response": 10150.0
+ },
+ {
+ "level": 75.0,
+ "response": 15200.0
+ },
+ {
+ "level": 75.0,
+ "response": 15100.0
+ },
+ {
+ "level": 75.0,
+ "response": 15250.0
+ },
+ {
+ "level": 100.0,
+ "response": 20150.0
+ },
+ {
+ "level": 100.0,
+ "response": 20250.0
+ },
+ {
+ "level": 100.0,
+ "response": 20050.0
+ },
+ {
+ "level": 125.0,
+ "response": 25200.0
+ },
+ {
+ "level": 125.0,
+ "response": 25100.0
+ },
+ {
+ "level": 125.0,
+ "response": 25300.0
+ },
+ {
+ "level": 150.0,
+ "response": 30250.0
+ },
+ {
+ "level": 150.0,
+ "response": 30150.0
+ },
+ {
+ "level": 150.0,
+ "response": 30350.0
+ }
+]
\ No newline at end of file
diff --git a/tests/analytical-method-validation/fixtures/calibration_good.tsv b/tests/analytical-method-validation/fixtures/calibration_good.tsv
new file mode 100644
index 00000000..8927307d
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_good.tsv
@@ -0,0 +1,16 @@
+level response
+50 10120
+50 10080
+50 10150
+75 15200
+75 15100
+75 15250
+100 20150
+100 20250
+100 20050
+125 25200
+125 25100
+125 25300
+150 30250
+150 30150
+150 30350
diff --git a/tests/analytical-method-validation/fixtures/calibration_nonnumeric.csv b/tests/analytical-method-validation/fixtures/calibration_nonnumeric.csv
new file mode 100644
index 00000000..2d49894b
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_nonnumeric.csv
@@ -0,0 +1,4 @@
+level,response
+50,10100
+100,not-a-number
+150,30100
diff --git a/tests/analytical-method-validation/fixtures/calibration_three_levels.csv b/tests/analytical-method-validation/fixtures/calibration_three_levels.csv
new file mode 100644
index 00000000..1f79e9d7
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/calibration_three_levels.csv
@@ -0,0 +1,7 @@
+level,response
+50,10100
+50,10050
+100,20100
+100,20000
+150,30100
+150,30050
diff --git a/tests/analytical-method-validation/fixtures/lowrange_calibration.csv b/tests/analytical-method-validation/fixtures/lowrange_calibration.csv
new file mode 100644
index 00000000..0fc9f797
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/lowrange_calibration.csv
@@ -0,0 +1,11 @@
+level,response
+0.02,105
+0.02,98
+0.05,255
+0.05,262
+0.10,510
+0.10,498
+0.15,760
+0.15,772
+0.20,1010
+0.20,1002
diff --git a/tests/analytical-method-validation/fixtures/m10_isr.csv b/tests/analytical-method-validation/fixtures/m10_isr.csv
new file mode 100644
index 00000000..8d0d61ec
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/m10_isr.csv
@@ -0,0 +1,7 @@
+sample,original,repeat
+1,100,128.6
+2,50,64.3
+3,200,257.1
+4,75,96.4
+5,150,192.9
+6,120,154.3
diff --git a/tests/analytical-method-validation/fixtures/m10_run_bad_type.csv b/tests/analytical-method-validation/fixtures/m10_run_bad_type.csv
new file mode 100644
index 00000000..d2728949
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/m10_run_bad_type.csv
@@ -0,0 +1,3 @@
+type,label,nominal,measured
+standard_curve_point,LLOQ,1,1.15
+qc,low,3,3.1
diff --git a/tests/analytical-method-validation/fixtures/m10_run_high_qc_fails.csv b/tests/analytical-method-validation/fixtures/m10_run_high_qc_fails.csv
new file mode 100644
index 00000000..2b4a34bf
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/m10_run_high_qc_fails.csv
@@ -0,0 +1,13 @@
+type,label,nominal,measured
+calibrator,LLOQ,1,1.15
+calibrator,,2,2.05
+calibrator,,5,5.1
+calibrator,,20,19.5
+calibrator,,100,101
+calibrator,ULOQ,500,495
+qc,low,3,3.1
+qc,low,3,3.05
+qc,medium,50,52
+qc,medium,50,49
+qc,high,400,470
+qc,high,400,465
diff --git a/tests/analytical-method-validation/fixtures/m10_run_lba_anchor.csv b/tests/analytical-method-validation/fixtures/m10_run_lba_anchor.csv
new file mode 100644
index 00000000..dc1259f7
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/m10_run_lba_anchor.csv
@@ -0,0 +1,12 @@
+type,label,nominal,measured
+calibrator,ANCHOR,0.5,0.9
+calibrator,LLOQ,1,1.15
+calibrator,,2,2.1
+calibrator,,5,5.2
+calibrator,,20,19.0
+calibrator,,100,103
+calibrator,ULOQ,500,480
+calibrator,ANCHOR,800,700
+qc,low,3,3.1
+qc,medium,50,52
+qc,high,400,410
diff --git a/tests/analytical-method-validation/fixtures/m10_total_error.csv b/tests/analytical-method-validation/fixtures/m10_total_error.csv
new file mode 100644
index 00000000..3ae886f6
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/m10_total_error.csv
@@ -0,0 +1,6 @@
+label,accuracy_pct,precision_pct
+LLOQ,18,20
+low,10,12
+medium,8,11
+high,9,10
+ULOQ,20,19
diff --git a/tests/analytical-method-validation/fixtures/ql_confirmation.csv b/tests/analytical-method-validation/fixtures/ql_confirmation.csv
new file mode 100644
index 00000000..95363b25
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/ql_confirmation.csv
@@ -0,0 +1,7 @@
+measured
+0.048
+0.052
+0.049
+0.055
+0.047
+0.051
diff --git a/tests/analytical-method-validation/fixtures/transfer_paired.csv b/tests/analytical-method-validation/fixtures/transfer_paired.csv
new file mode 100644
index 00000000..aa0a17d5
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/transfer_paired.csv
@@ -0,0 +1,13 @@
+sample,reference,test
+1,98.2,99.6
+2,101.4,102.9
+3,99.7,101.2
+4,100.3,101.8
+5,97.9,99.3
+6,102.1,103.7
+7,100.8,102.3
+8,99.1,100.5
+9,101.6,103.1
+10,98.6,100.0
+11,100.2,101.7
+12,99.4,100.9
diff --git a/tests/analytical-method-validation/fixtures/transfer_two_pairs.csv b/tests/analytical-method-validation/fixtures/transfer_two_pairs.csv
new file mode 100644
index 00000000..1245408c
--- /dev/null
+++ b/tests/analytical-method-validation/fixtures/transfer_two_pairs.csv
@@ -0,0 +1,3 @@
+sample,reference,test
+1,100,101
+2,102,103
diff --git a/tests/analytical-method-validation/test_scripts.py b/tests/analytical-method-validation/test_scripts.py
new file mode 100644
index 00000000..e8ccdf3b
--- /dev/null
+++ b/tests/analytical-method-validation/test_scripts.py
@@ -0,0 +1,949 @@
+"""Unit tests for the analytical-method-validation skill scripts.
+
+Everything runs offline; the scripts make no network calls. The statistical
+implementations are checked against published quantiles and hand-checkable
+cases, because the whole value of the skill rests on them being right.
+
+ uv run --with pytest python -m pytest tests/analytical-method-validation -q
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import json
+import math
+import subprocess
+import sys
+import unittest
+from pathlib import Path
+
+SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "analytical-method-validation"
+SCRIPTS_DIR = SKILL_ROOT / "scripts"
+FIXTURES = Path(__file__).resolve().parent / "fixtures"
+
+
+def _load_script(name: str):
+ """Load a bundled script as a module regardless of cwd."""
+ if str(SCRIPTS_DIR) not in sys.path:
+ sys.path.insert(0, str(SCRIPTS_DIR))
+ spec = importlib.util.spec_from_file_location(name, SCRIPTS_DIR / f"{name}.py")
+ assert spec and spec.loader
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[name] = module
+ spec.loader.exec_module(module)
+ return module
+
+
+common = _load_script("_common")
+catalog = _load_script("_catalog")
+
+
+def run_script(name: str, *args: str) -> subprocess.CompletedProcess:
+ """Invoke a script as a subprocess and capture its streams."""
+ return subprocess.run(
+ [sys.executable, str(SCRIPTS_DIR / f"{name}.py"), *args],
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+
+
+# ---------------------------------------------------------------- structure
+
+
+class TestSkillStructure(unittest.TestCase):
+ def test_skill_md_frontmatter(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ self.assertTrue(text.startswith("---\n"))
+ end = text.index("\n---\n", 4)
+ front = text[4:end]
+ self.assertIn("name: analytical-method-validation", front)
+ self.assertIn('version: "1.0"', front)
+ # allowed-tools must be a space-separated string, not a YAML list
+ for line in front.splitlines():
+ if line.startswith("allowed-tools:"):
+ self.assertNotIn(",", line)
+ self.assertNotIn("[", line)
+
+ def test_skill_md_under_500_lines(self):
+ lines = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8").splitlines()
+ self.assertLess(len(lines), 500, f"SKILL.md has {len(lines)} lines")
+
+ def test_referenced_files_exist(self):
+ text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
+ for rel in ("references/framework-selection.md", "references/ich-q2r2.md",
+ "references/ich-m10-bioanalytical.md", "references/compendial-and-clsi.md",
+ "references/statistics.md", "references/source-ledger.md",
+ "assets/validation-protocol-template.md",
+ "assets/validation-report-template.md"):
+ self.assertIn(rel, text, f"{rel} not referenced from SKILL.md")
+ self.assertTrue((SKILL_ROOT / rel).is_file(), f"{rel} missing")
+
+ def test_no_bytecode_shipped(self):
+ self.assertEqual(list(SKILL_ROOT.rglob("*.pyc")), [])
+ self.assertEqual(list(SKILL_ROOT.rglob("__pycache__")), [])
+
+ def test_no_tests_inside_skill(self):
+ self.assertEqual(list(SKILL_ROOT.rglob("test_*.py")), [])
+
+ def test_no_techniques_attributed_to_q2r2_that_it_never_mentions(self):
+ """Guard against secondary-source claims leaking in as primary-source facts.
+
+ Trade summaries of the Q2(R1) -> Q2(R2) revision commonly list Raman
+ spectroscopy. The adopted guideline text does not contain the word, so the
+ skill must not attribute it to Q2(R2).
+ """
+ for path in SKILL_ROOT.rglob("*.md"):
+ text = path.read_text(encoding="utf-8")
+ self.assertNotIn("Raman", text, f"{path.name} attributes Raman to ICH guidance")
+
+ def test_paywalled_standards_are_not_quoted_with_numbers(self):
+ """USP/CLSI/ISO content must be referenced, never reproduced.
+
+ A numeric threshold sitting next to a paywalled designation is the shape of
+ an accidental reproduction, so the reference file states scope only.
+ """
+ text = (SKILL_ROOT / "references" / "compendial-and-clsi.md").read_text(encoding="utf-8")
+ self.assertIn("does not reproduce", text)
+ for banned in ("tailing factor limit", "%RSD limit of", "must not exceed 2.0"):
+ self.assertNotIn(banned, text)
+
+
+# ---------------------------------------------------------------- distributions
+
+
+class TestDistributions(unittest.TestCase):
+ def test_t_quantiles(self):
+ for p, df, want in [
+ (0.975, 1, 12.706205),
+ (0.975, 5, 2.570582),
+ (0.975, 9, 2.262157),
+ (0.95, 10, 1.812461),
+ (0.995, 20, 2.845340),
+ (0.975, 100, 1.983972),
+ ]:
+ self.assertAlmostEqual(common.t_ppf(p, df), want, places=4)
+
+ def test_t_cdf_symmetry(self):
+ self.assertAlmostEqual(common.t_cdf(0.0, 7), 0.5, places=10)
+ self.assertAlmostEqual(
+ common.t_cdf(-1.3, 12) + common.t_cdf(1.3, 12), 1.0, places=10
+ )
+
+ def test_chi2_quantiles(self):
+ for p, df, want in [
+ (0.025, 9, 2.700389),
+ (0.975, 9, 19.022768),
+ (0.95, 1, 3.841459),
+ (0.05, 20, 10.850811),
+ (0.5, 4, 3.356694),
+ ]:
+ self.assertAlmostEqual(common.chi2_ppf(p, df), want, places=3)
+
+ def test_f_distribution(self):
+ # F(1,1) upper 5% point is 161.4476
+ self.assertAlmostEqual(common.f_sf(161.4476, 1, 1), 0.05, places=5)
+ # median-ish symmetry check
+ self.assertAlmostEqual(common.f_sf(1.0, 5, 5), 0.5, places=6)
+ self.assertAlmostEqual(common.f_sf(4.0, 2, 10), 0.052922, places=5)
+ self.assertAlmostEqual(common.f_sf(0.0, 3, 5), 1.0, places=12)
+
+ def test_f_sf_does_not_underflow_to_zero(self):
+ """A validation report must not print a lack-of-fit p-value of exactly 0.
+
+ Computing 1 - cdf underflows well before the true tail probability does.
+ """
+ for f, df1, df2 in [(1e8, 3, 5), (1e3, 1, 200), (1e6, 10, 100)]:
+ tail = common.f_sf(f, df1, df2)
+ self.assertGreater(tail, 0.0, f"F={f} df=({df1},{df2}) underflowed")
+ self.assertLess(tail, 1e-15)
+ # And it still agrees with 1 - cdf where that is accurate.
+ for f, df1, df2 in [(4.0, 2, 10), (2.5, 3, 12), (1.0, 5, 5)]:
+ self.assertAlmostEqual(
+ common.f_sf(f, df1, df2), 1.0 - common.f_cdf(f, df1, df2), places=12
+ )
+
+ def test_betainc_edges(self):
+ self.assertEqual(common.betainc(2.0, 3.0, 0.0), 0.0)
+ self.assertEqual(common.betainc(2.0, 3.0, 1.0), 1.0)
+ # I_x(a,b) + I_(1-x)(b,a) == 1
+ self.assertAlmostEqual(
+ common.betainc(2.5, 4.5, 0.3) + common.betainc(4.5, 2.5, 0.7), 1.0, places=12
+ )
+
+ def test_bad_probabilities_raise(self):
+ with self.assertRaises(common.InputError):
+ common.t_ppf(0.0, 5)
+ with self.assertRaises(common.InputError):
+ common.chi2_ppf(1.0, 5)
+
+
+# ---------------------------------------------------------------- regression
+
+
+class TestRegression(unittest.TestCase):
+ def test_exact_line(self):
+ fit = common.fit_linear([1, 2, 3, 4], [3, 5, 7, 9])
+ self.assertAlmostEqual(fit.slope, 2.0, places=12)
+ self.assertAlmostEqual(fit.intercept, 1.0, places=12)
+ self.assertAlmostEqual(fit.r_squared, 1.0, places=12)
+ self.assertAlmostEqual(fit.residual_sd, 0.0, places=10)
+
+ def test_known_least_squares(self):
+ fit = common.fit_linear([1, 2, 3, 4, 5], [2.1, 4.0, 6.2, 7.9, 10.1])
+ self.assertAlmostEqual(fit.slope, 1.99, places=10)
+ self.assertAlmostEqual(fit.intercept, 0.09, places=10)
+ lo, hi = fit.slope_ci()
+ self.assertLess(lo, 1.99)
+ self.assertGreater(hi, 1.99)
+
+ def test_weighting_changes_the_fit(self):
+ xs = [1, 2, 5, 10, 20]
+ ys = [1.0, 2.1, 5.2, 9.5, 21.0]
+ unweighted = common.fit_linear(xs, ys)
+ weighted = common.fit_linear(xs, ys, [1.0 / (x * x) for x in xs])
+ self.assertNotAlmostEqual(unweighted.slope, weighted.slope, places=6)
+
+ def test_too_few_points(self):
+ with self.assertRaises(common.InputError):
+ common.fit_linear([1, 2], [1, 2])
+
+ def test_collinear_x_raises(self):
+ with self.assertRaises(common.InputError):
+ common.fit_linear([3, 3, 3], [1, 2, 3])
+
+ def test_lack_of_fit_detects_curvature(self):
+ xs = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
+ quad = [x * x for x in xs]
+ ys = [q + (0.05 if i % 2 else -0.05) for i, q in enumerate(quad)]
+ lof = common.lack_of_fit(xs, ys, common.fit_linear(xs, ys))
+ self.assertTrue(lof["applicable"])
+ self.assertLess(lof["p_value"], 0.001)
+
+ def test_lack_of_fit_passes_linear_data(self):
+ xs = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
+ ys = [2 * x + (0.02 if i % 2 else -0.02) for i, x in enumerate(xs)]
+ lof = common.lack_of_fit(xs, ys, common.fit_linear(xs, ys))
+ self.assertTrue(lof["applicable"])
+ self.assertGreater(lof["p_value"], 0.05)
+
+ def test_lack_of_fit_needs_replicates(self):
+ xs = [1, 2, 3, 4, 5]
+ ys = [2, 4, 6, 8, 10.1]
+ lof = common.lack_of_fit(xs, ys, common.fit_linear(xs, ys))
+ self.assertFalse(lof["applicable"])
+ self.assertIn("replicates", lof["reason"])
+
+ def test_runs_test_flags_curvature(self):
+ xs = list(range(1, 13))
+ ys = [x * x for x in xs]
+ runs = common.runs_test(common.fit_linear(xs, ys).residuals)
+ self.assertLess(runs["p_value"], 0.05)
+
+ def test_runs_test_small_sample_is_honest(self):
+ runs = common.runs_test([1.0, -1.0, 1.0])
+ self.assertTrue(math.isnan(runs["p_value"]))
+ self.assertIn("too few", runs["note"])
+
+ def test_heteroscedasticity_ratio(self):
+ xs = list(range(1, 13))
+ resid = [0.01 * (1 if i % 2 else -1) for i in range(6)] + [
+ 1.0 * (1 if i % 2 else -1) for i in range(6)
+ ]
+ het = common.heteroscedasticity(xs, resid)
+ self.assertTrue(het["applicable"])
+ self.assertGreater(het["variance_ratio_high_over_low"], 100)
+
+
+# ---------------------------------------------------------------- precision
+
+
+class TestVarianceComponents(unittest.TestCase):
+ def test_balanced_hand_check(self):
+ groups = {"d1": [10.0, 10.2, 9.8], "d2": [10.5, 10.6, 10.4], "d3": [9.6, 9.5, 9.7]}
+ comp = common.one_way_components(groups)
+ self.assertEqual(comp.n_total, 9)
+ self.assertEqual(comp.n_groups, 3)
+ self.assertTrue(comp.balanced)
+ self.assertAlmostEqual(comp.n_effective, 3.0, places=12)
+ self.assertAlmostEqual(comp.ms_within, 0.02, places=12)
+ self.assertAlmostEqual(comp.ms_between, 0.61, places=12)
+ # s2_between = (0.61 - 0.02)/3
+ self.assertAlmostEqual(comp.sd_between ** 2, (0.61 - 0.02) / 3.0, places=12)
+ self.assertAlmostEqual(
+ comp.sd_intermediate ** 2, 0.02 + (0.61 - 0.02) / 3.0, places=12
+ )
+ self.assertGreater(comp.sd_intermediate, comp.sd_repeatability)
+
+ def test_between_variance_truncated_at_zero(self):
+ # Groups deliberately indistinguishable: MS_between < MS_within
+ groups = {"a": [1.0, 3.0, 2.0], "b": [2.0, 1.0, 3.0], "c": [3.0, 2.0, 1.0]}
+ comp = common.one_way_components(groups)
+ self.assertEqual(comp.sd_between, 0.0)
+ self.assertAlmostEqual(comp.sd_intermediate, comp.sd_repeatability, places=12)
+
+ def test_unbalanced_effective_n(self):
+ groups = {"a": [1.0, 1.1, 0.9, 1.05], "b": [2.0, 2.1], "c": [3.0, 3.1, 2.9]}
+ comp = common.one_way_components(groups)
+ self.assertFalse(comp.balanced)
+ self.assertGreater(comp.n_effective, 1.0)
+ self.assertLess(comp.n_effective, 4.0)
+
+ def test_needs_two_groups(self):
+ with self.assertRaises(common.InputError):
+ common.one_way_components({"only": [1.0, 2.0, 3.0]})
+
+ def test_sd_confidence_interval_brackets(self):
+ lo, hi = common.sd_confidence_interval(1.0, 9, 0.90)
+ self.assertLess(lo, 1.0)
+ self.assertGreater(hi, 1.0)
+ # chi2 interval for sd=1, df=9 at 90%
+ self.assertAlmostEqual(lo, math.sqrt(9 / common.chi2_ppf(0.95, 9)), places=10)
+
+
+# ---------------------------------------------------------------- comparison
+
+
+class TestMethodComparison(unittest.TestCase):
+ def test_deming_recovers_unit_slope(self):
+ xs = list(range(1, 21))
+ ys = [x + (0.05 if i % 2 else -0.05) for i, x in enumerate(xs)]
+ res = common.deming(xs, ys, 1.0)
+ self.assertAlmostEqual(res["slope"], 1.0, places=2)
+ self.assertAlmostEqual(res["intercept"], 0.0, places=1)
+
+ def test_deming_lambda_affects_slope(self):
+ xs = [1, 2, 3, 4, 5, 6, 7, 8]
+ ys = [1.2, 1.9, 3.4, 3.8, 5.5, 5.9, 7.6, 7.9]
+ a = common.deming(xs, ys, 1.0)["slope"]
+ b = common.deming(xs, ys, 4.0)["slope"]
+ self.assertNotAlmostEqual(a, b, places=6)
+
+ def test_deming_lambda_direction_matches_documentation(self):
+ """lambda = var(y error)/var(x error), pinned by the unambiguous limits.
+
+ Large lambda means x is effectively error-free, so the fit must converge
+ on the OLS slope of y on x. Small lambda means y is error-free, so it must
+ converge on the inverse regression. Documenting this backwards would send
+ a user with unequal precision in exactly the wrong direction.
+ """
+ xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ ys = [1.2, 1.9, 3.4, 3.8, 5.5, 5.9, 7.6, 7.9, 9.3, 10.4]
+ xb, yb = common.mean(xs), common.mean(ys)
+ sxx = math.fsum((x - xb) ** 2 for x in xs)
+ syy = math.fsum((y - yb) ** 2 for y in ys)
+ sxy = math.fsum((x - xb) * (y - yb) for x, y in zip(xs, ys))
+ ols_y_on_x = sxy / sxx
+ inverse = syy / sxy
+ self.assertAlmostEqual(common.deming(xs, ys, 1e9)["slope"], ols_y_on_x, places=6)
+ self.assertAlmostEqual(common.deming(xs, ys, 1e-9)["slope"], inverse, places=6)
+ # lambda = 1 is orthogonal regression, between the two.
+ orth = common.deming(xs, ys, 1.0)["slope"]
+ self.assertLess(ols_y_on_x, orth)
+ self.assertLess(orth, inverse)
+
+ def test_deming_rejects_bad_lambda(self):
+ with self.assertRaises(common.InputError):
+ common.deming([1, 2, 3], [1, 2, 3], 0.0)
+
+ def test_passing_bablok_exact_line(self):
+ xs = [1, 2, 3, 4, 5, 6, 7]
+ ys = [2 * x + 1 for x in xs]
+ res = common.passing_bablok(xs, ys)
+ self.assertAlmostEqual(res["slope"], 2.0, places=10)
+ self.assertAlmostEqual(res["intercept"], 1.0, places=10)
+
+ def test_passing_bablok_resists_one_outlier(self):
+ xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ ys = [float(x) for x in xs]
+ ys[4] = 50.0 # gross outlier
+ pb = common.passing_bablok(xs, ys)
+ ols = common.fit_linear(xs, ys)
+ self.assertAlmostEqual(pb["slope"], 1.0, places=6)
+ self.assertGreater(abs(ols.slope - 1.0), abs(pb["slope"] - 1.0))
+
+ def test_passing_bablok_needs_five_points(self):
+ with self.assertRaises(common.InputError):
+ common.passing_bablok([1, 2, 3], [1, 2, 3])
+
+ def test_passing_bablok_ci_order_statistics(self):
+ """Pin the 1-based-to-0-based conversion of the M1/M2 order statistics.
+
+ M1 and M2 are both 1-based positions in the shifted slope list, so both
+ convert with the same -1. Applying the offset to only one end silently
+ narrows the interval on the low side.
+ """
+ xs = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
+ deltas = [1.2, -0.8, 0.5, -1.5, 2.0, -0.3, 1.1, -2.2, 0.9, -1.0, 1.8, -0.6]
+ ys = [1.05 * x + 2 + d for x, d in zip(xs, deltas)]
+ res = common.passing_bablok(xs, ys)
+
+ slopes = sorted(
+ (ys[j] - ys[i]) / (xs[j] - xs[i])
+ for i in range(len(xs))
+ for j in range(i + 1, len(xs))
+ if xs[j] != xs[i]
+ )
+ n_slopes = len(slopes)
+ shift = sum(1 for s in slopes if s < -1.0)
+ c = common.z_ppf(0.975) * math.sqrt(
+ len(xs) * (len(xs) - 1.0) * (2.0 * len(xs) + 5.0) / 18.0
+ )
+ m1 = int(round((n_slopes - c) / 2.0))
+ m2 = n_slopes - m1 + 1
+ self.assertAlmostEqual(res["slope_ci95"][0], slopes[m1 + shift - 1], places=12)
+ self.assertAlmostEqual(res["slope_ci95"][1], slopes[m2 + shift - 1], places=12)
+ self.assertLessEqual(res["slope_ci95"][0], res["slope"])
+ self.assertLessEqual(res["slope"], res["slope_ci95"][1])
+
+ def test_passing_bablok_ci_contains_estimate_across_shapes(self):
+ for n in (7, 8, 11, 12, 19, 20):
+ xs = [10.0 + 5 * i for i in range(n)]
+ ys = [1.03 * x + 1 + (0.7 if i % 3 == 0 else -0.4) for i, x in enumerate(xs)]
+ res = common.passing_bablok(xs, ys)
+ lo, hi = res["slope_ci95"]
+ self.assertLessEqual(lo, res["slope"], f"n={n}")
+ self.assertLessEqual(res["slope"], hi, f"n={n}")
+
+ def test_bland_altman_bias_and_loa(self):
+ xs = [10.0] * 6
+ ys = [11.0, 11.0, 11.0, 11.0, 11.0, 11.0]
+ ba = common.bland_altman(xs, ys)
+ self.assertAlmostEqual(ba["bias"], 1.0, places=12)
+ self.assertAlmostEqual(ba["sd_differences"], 0.0, places=12)
+
+ def test_bland_altman_relative(self):
+ ba = common.bland_altman([100.0, 200.0, 400.0], [110.0, 220.0, 440.0], relative=True)
+ # Each pair is a 10% increase: (110-100)/105 = 9.5238% of the pair mean.
+ self.assertAlmostEqual(ba["bias"], 9.523809523, places=6)
+ self.assertAlmostEqual(ba["sd_differences"], 0.0, places=9)
+
+ def test_bland_altman_needs_three_pairs(self):
+ with self.assertRaises(common.InputError):
+ common.bland_altman([100.0, 200.0], [110.0, 220.0])
+
+ def test_tost_equivalent(self):
+ res = common.tost_paired([0.1, -0.2, 0.05, 0.0, 0.15], margin=2.0)
+ self.assertTrue(res["equivalent"])
+ self.assertLess(res["p_value"], 0.05)
+
+ def test_tost_not_equivalent_when_biased(self):
+ res = common.tost_paired([1.9, 2.1, 2.0, 1.95, 2.05], margin=2.0)
+ self.assertFalse(res["equivalent"])
+ self.assertGreater(res["p_value"], 0.05)
+
+ def test_tost_ci_is_1_minus_2alpha(self):
+ diffs = [0.5, 0.6, 0.4, 0.55, 0.45, 0.5]
+ res = common.tost_paired(diffs, margin=1.0, alpha=0.05)
+ lo, hi = res["ci_1_minus_2alpha"]
+ sd = common.sample_sd(diffs)
+ se = sd / math.sqrt(len(diffs))
+ expected_half = common.t_ppf(0.95, len(diffs) - 1) * se
+ self.assertAlmostEqual(hi - res["mean_difference"], expected_half, places=10)
+ self.assertAlmostEqual(res["mean_difference"] - lo, expected_half, places=10)
+
+ def test_tost_rejects_bad_margin(self):
+ with self.assertRaises(common.InputError):
+ common.tost_paired([0.1, 0.2], margin=0.0)
+
+
+# ---------------------------------------------------------------- catalogue
+
+
+class TestCatalogue(unittest.TestCase):
+ def test_m10_modalities_differ(self):
+ cc = catalog.M10_CRITERIA["chromatographic"]
+ lba = catalog.M10_CRITERIA["lba"]
+ self.assertEqual(cc["accuracy_tolerance_pct"], 15.0)
+ self.assertEqual(lba["accuracy_tolerance_pct"], 20.0)
+ self.assertEqual(cc["accuracy_tolerance_lloq_pct"], 20.0)
+ self.assertEqual(lba["accuracy_tolerance_lloq_pct"], 25.0)
+ self.assertEqual(cc["isr_tolerance_pct"], 20.0)
+ self.assertEqual(lba["isr_tolerance_pct"], 30.0)
+
+ def test_total_error_is_lba_only(self):
+ self.assertIsNone(catalog.M10_CRITERIA["chromatographic"]["total_error_pct"])
+ self.assertEqual(catalog.M10_CRITERIA["lba"]["total_error_pct"], 30.0)
+ self.assertEqual(catalog.M10_CRITERIA["lba"]["total_error_pct_at_limits"], 40.0)
+
+ def test_m10_run_structure(self):
+ cc = catalog.M10_CRITERIA["chromatographic"]
+ lba = catalog.M10_CRITERIA["lba"]
+ self.assertEqual(cc["qc_levels_accuracy_precision"], 4)
+ self.assertEqual(lba["qc_levels_accuracy_precision"], 5)
+ self.assertEqual(cc["ap_replicates_per_run"], 5)
+ self.assertEqual(lba["ap_replicates_per_run"], 3)
+ self.assertEqual(cc["ap_min_runs"], 3)
+ self.assertEqual(lba["ap_min_runs"], 6)
+ for crit in (cc, lba):
+ self.assertEqual(crit["calibration_min_levels"], 6)
+ self.assertAlmostEqual(crit["calibration_min_pass_fraction"], 0.75)
+ self.assertAlmostEqual(crit["isr_pass_fraction"], 2 / 3)
+
+ def test_q2r2_table1_limit_test_needs_dl_not_ql(self):
+ limit = catalog.Q2R2_TESTS_BY_ATTRIBUTE["impurity-limit"]
+ quant = catalog.Q2R2_TESTS_BY_ATTRIBUTE["impurity-quantitative"]
+ self.assertEqual(limit["lower-range-limit"], "required-DL")
+ self.assertEqual(quant["lower-range-limit"], "required-QL")
+ self.assertEqual(limit["accuracy"], "not-normally")
+ self.assertEqual(quant["accuracy"], "required")
+
+ def test_q2r2_identity_only_needs_specificity(self):
+ ident = catalog.Q2R2_TESTS_BY_ATTRIBUTE["identity"]
+ self.assertEqual(ident["specificity"], "required")
+ for key, value in ident.items():
+ if key != "specificity":
+ self.assertEqual(value, "not-normally")
+
+ def test_attribute_aliases_resolve(self):
+ self.assertEqual(catalog.resolve_attribute("Content"), "assay")
+ self.assertEqual(catalog.resolve_attribute("related-substances"), "impurity-quantitative")
+ self.assertEqual(catalog.resolve_attribute("limit-test"), "impurity-limit")
+ with self.assertRaises(KeyError):
+ catalog.resolve_attribute("nonsense")
+
+ def test_paywalled_frameworks_marked_not_reproducible(self):
+ for key in ("usp-1220", "usp-1225", "usp-1226", "clsi", "iso-17025"):
+ self.assertFalse(catalog.FRAMEWORKS[key]["reproducible"], key)
+ for key in ("ich-q2r2", "ich-m10"):
+ self.assertTrue(catalog.FRAMEWORKS[key]["reproducible"], key)
+
+ def test_q2r2_response_requires_five_levels(self):
+ self.assertIn("5", catalog.Q2R2_STUDY_DESIGN["response"]["requirement"])
+
+ def test_dl_ql_factors_documented(self):
+ sd_slope = catalog.DL_QL_APPROACHES["sd-and-slope"]
+ self.assertIn("3.3", sd_slope["dl"])
+ self.assertIn("10", sd_slope["ql"])
+
+
+# ---------------------------------------------------------------- CLI
+
+
+class TestPlanValidation(unittest.TestCase):
+ def test_list_frameworks(self):
+ res = run_script("plan_validation.py".removesuffix(".py"), "--list-frameworks",
+ "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ rows = json.loads(res.stdout)
+ keys = {r["key"] for r in rows}
+ self.assertIn("ich-q2r2", keys)
+ self.assertIn("ich-m10", keys)
+
+ def test_q2r2_assay_plan(self):
+ res = run_script("plan_validation", "--framework", "ich-q2r2",
+ "--attribute", "assay", "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ rows = json.loads(res.stdout)
+ chars = {r["characteristic"] for r in rows}
+ self.assertIn("specificity", chars)
+ self.assertIn("intermediate-precision", chars)
+ self.assertIn("robustness", chars)
+
+ def test_identity_plan_marks_most_tests_not_required(self):
+ res = run_script("plan_validation", "--framework", "ich-q2r2",
+ "--attribute", "identity", "--format", "json")
+ rows = {r["characteristic"]: r["required"] for r in json.loads(res.stdout)}
+ self.assertEqual(rows["specificity"], "conduct")
+ self.assertIn("not normally", rows["accuracy"])
+
+ def test_m10_requires_modality(self):
+ res = run_script("plan_validation", "--framework", "ich-m10")
+ self.assertEqual(res.returncode, 2)
+ self.assertIn("modality", res.stderr)
+
+ def test_m10_lba_plan_includes_total_error(self):
+ res = run_script("plan_validation", "--framework", "ich-m10",
+ "--modality", "lba", "--format", "json")
+ items = {r["item"] for r in json.loads(res.stdout)}
+ self.assertIn("total error", items)
+
+ def test_m10_chromatographic_plan_excludes_total_error(self):
+ res = run_script("plan_validation", "--framework", "ich-m10",
+ "--modality", "chromatographic", "--format", "json")
+ items = {r["item"] for r in json.loads(res.stdout)}
+ self.assertNotIn("total error", items)
+
+ def test_unknown_framework_exits_2(self):
+ res = run_script("plan_validation", "--framework", "nope")
+ self.assertEqual(res.returncode, 2)
+
+ def test_protocol_skeleton_has_pre_stated_criteria_section(self):
+ res = run_script("plan_validation", "--framework", "ich-q2r2",
+ "--attribute", "impurity", "--protocol")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ self.assertIn("Pre-stated acceptance criteria", res.stdout)
+ self.assertIn("BEFORE data collection", res.stdout)
+
+ def test_paywalled_framework_refuses_to_supply_design(self):
+ res = run_script("plan_validation", "--framework", "usp-1225", "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ self.assertIn("copyrighted", res.stderr)
+ rows = json.loads(res.stdout)
+ self.assertIn("obtain_from", rows[0])
+
+
+class TestCheckResponse(unittest.TestCase):
+ def test_good_curve_no_findings(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.csv"),
+ "--max-back-calc-error", "2")
+ self.assertEqual(res.returncode, 0, res.stderr)
+
+ def test_curved_response_flagged(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_curved.csv"),
+ "--max-back-calc-error", "2")
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("lack-of-fit", res.stderr)
+
+ def test_curved_response_has_high_r_squared(self):
+ """The whole point: r-squared passes while the model is unusable."""
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_curved.csv"),
+ "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ self.assertGreater(payload["summary"]["coefficient of determination (r2)"], 0.98)
+ self.assertLess(payload["summary"]["lack-of-fit p"], 0.001)
+ worst = max(abs(r["relative_error_pct"]) for r in payload["levels"])
+ self.assertGreater(worst, 5.0)
+
+ def test_too_few_levels_flagged(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_three_levels.csv"))
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("recommends at least 5", res.stderr)
+
+ def test_missing_column_exits_2(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "bad_columns.csv"))
+ self.assertEqual(res.returncode, 2)
+ self.assertIn("missing required column", res.stderr)
+
+ def test_weighting_option_accepted(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.csv"),
+ "--weight", "1/x2", "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ self.assertEqual(payload["summary"]["weighting"], "1/x2")
+
+
+class TestCheckAccuracyPrecision(unittest.TestCase):
+ def test_between_day_shift_inflates_intermediate_precision(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_between_day.csv"),
+ "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ rows = payload["precision"]
+ rep = [r for r in rows if r["level"] == "100"
+ and r["component"].startswith("repeatability")][0]
+ inter = [r for r in rows if r["level"] == "100"
+ and r["component"].startswith("intermediate")][0]
+ self.assertGreater(inter["rsd_pct"], 10 * rep["rsd_pct"])
+
+ def test_precision_is_reported_per_level(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_between_day.csv"),
+ "--format", "json")
+ rows = json.loads(res.stdout)[0]["precision"]
+ levels = {r["level"] for r in rows}
+ self.assertIn("80", levels)
+ self.assertIn("100", levels)
+ self.assertIn("120", levels)
+ # Per-level RSD must not be inflated by the 80/100/120 range itself.
+ for r in rows:
+ if r["component"].startswith("repeatability"):
+ self.assertLess(r["rsd_pct"], 5.0)
+
+ def test_accuracy_reports_confidence_interval(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_between_day.csv"),
+ "--format", "json")
+ acc = json.loads(res.stdout)[0]["accuracy"]
+ for row in acc:
+ self.assertIn("ci95_low", row)
+ self.assertIn("ci95_high", row)
+ self.assertLess(row["ci95_low"], row["mean_recovery_pct"])
+ self.assertGreater(row["ci95_high"], row["mean_recovery_pct"])
+
+ def test_require_ci_within_limit_is_stricter(self):
+ lenient = run_script("check_accuracy_precision", "-i",
+ str(FIXTURES / "ap_between_day.csv"), "--accuracy-limit", "1.0")
+ strict = run_script("check_accuracy_precision", "-i",
+ str(FIXTURES / "ap_between_day.csv"), "--accuracy-limit", "1.0",
+ "--require-ci-within-limit")
+ # The mean is inside +/-1%, so lenient passes; the CI is not, so strict fails.
+ self.assertEqual(lenient.returncode, 0, lenient.stderr)
+ self.assertEqual(strict.returncode, 1)
+ self.assertIn("CI", strict.stderr)
+
+ def test_no_group_column_only_repeatability(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_no_group.csv"),
+ "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ rows = json.loads(res.stdout)[0]["precision"]
+ self.assertTrue(all("repeatability" in r["component"] for r in rows))
+ self.assertIn("intermediate precision", res.stderr)
+
+ def test_zero_level_rejected(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_zero_level.csv"))
+ self.assertEqual(res.returncode, 2)
+
+ def test_six_at_one_level_satisfies_option_b(self):
+ """Q2(R2) 3.3.2.1 offers two alternatives; either is sufficient.
+
+ Six determinations at 100% of the test concentration is option (b).
+ Demanding nine unconditionally raises a finding against a design the
+ guideline explicitly permits.
+ """
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_no_group.csv"),
+ "--design-check", "assay")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ self.assertIn("option (b)", res.stderr)
+
+ def test_nine_across_range_satisfies_option_a(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_between_day.csv"),
+ "--design-check", "assay")
+ self.assertIn("option (a)", res.stderr)
+
+ def test_design_meeting_neither_option_is_flagged(self):
+ res = run_script("check_accuracy_precision", "-i", str(FIXTURES / "ap_thin_design.csv"),
+ "--design-check", "assay")
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("neither", res.stderr)
+
+
+class TestCheckDetectionLimits(unittest.TestCase):
+ def test_multiple_approaches_reported(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--blanks", str(FIXTURES / "blanks.csv"), "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ approaches = [e["approach"] for e in payload["estimates"]]
+ self.assertGreaterEqual(len(approaches), 3)
+ self.assertTrue(any("blanks" in a for a in approaches))
+ self.assertTrue(any("residual SD" in a for a in approaches))
+
+ def test_dl_ql_factor_relationship(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"), "--format", "json")
+ for est in json.loads(res.stdout)[0]["estimates"]:
+ if math.isfinite(est["sigma"]):
+ self.assertAlmostEqual(est["QL"] / est["DL"], 10.0 / 3.3, places=6)
+
+ def test_ql_above_reporting_threshold_flagged(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--confirm-ql", "0.10", "--reporting-threshold", "0.05")
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("reporting threshold", res.stderr)
+
+ def test_ql_confirmation_evaluated(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--confirm-ql", "0.05",
+ "--confirm-data", str(FIXTURES / "ql_confirmation.csv"),
+ "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ metrics = {r["metric"]: r["value"] for r in payload["confirmation"]}
+ self.assertIn("bias vs claimed QL (%)", metrics)
+ self.assertIn("RSD (%)", metrics)
+
+ def test_confirm_data_without_claim_exits_2(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--confirm-data", str(FIXTURES / "ql_confirmation.csv"))
+ self.assertEqual(res.returncode, 2)
+
+ def test_threshold_check_uses_the_conservative_estimate(self):
+ """With no claimed QL, the compliance check must not pick the flattering estimate.
+
+ The fixture's QL estimates straddle 0.010. Taking the smallest would report
+ no findings on a procedure whose conservative limit fails.
+ """
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--blanks", str(FIXTURES / "blanks.csv"),
+ "--reporting-threshold", "0.010", "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ qls = [e["QL"] for e in payload["estimates"]]
+ self.assertLess(min(qls), 0.010)
+ self.assertGreater(max(qls), 0.010)
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("straddle", res.stderr)
+ self.assertIn("conservative", res.stderr)
+
+ def test_explicit_claim_overrides_the_conservative_default(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--blanks", str(FIXTURES / "blanks.csv"),
+ "--confirm-ql", "0.005", "--reporting-threshold", "0.010")
+ self.assertNotIn("straddle", res.stderr)
+ self.assertEqual(res.returncode, 0, res.stderr)
+
+ def test_signal_to_noise_requires_level(self):
+ res = run_script("check_detection_limits", "--calibration",
+ str(FIXTURES / "lowrange_calibration.csv"),
+ "--signal-to-noise", "12")
+ self.assertEqual(res.returncode, 2)
+
+
+class TestCheckBioanalyticalRun(unittest.TestCase):
+ def test_per_level_qc_rule_catches_failing_level(self):
+ res = run_script("check_bioanalytical_run", "--modality", "chromatographic",
+ "--run", str(FIXTURES / "m10_run_high_qc_fails.csv"))
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("50% at each level", res.stderr)
+
+ def test_same_run_passes_lba_tolerances(self):
+ """The identical run passes under LBA tolerances -- the modality matters."""
+ cc = run_script("check_bioanalytical_run", "--modality", "chromatographic",
+ "--run", str(FIXTURES / "m10_run_high_qc_fails.csv"))
+ lba = run_script("check_bioanalytical_run", "--modality", "lba",
+ "--run", str(FIXTURES / "m10_run_high_qc_fails.csv"))
+ self.assertEqual(cc.returncode, 1)
+ self.assertEqual(lba.returncode, 0, lba.stderr)
+
+ def test_modality_is_mandatory(self):
+ res = run_script("check_bioanalytical_run", "--run",
+ str(FIXTURES / "m10_run_high_qc_fails.csv"))
+ self.assertNotEqual(res.returncode, 0)
+ self.assertIn("modality", res.stderr.lower())
+
+ def test_isr_chromatographic_vs_lba(self):
+ cc = run_script("check_bioanalytical_run", "--modality", "chromatographic",
+ "--isr", str(FIXTURES / "m10_isr.csv"))
+ lba = run_script("check_bioanalytical_run", "--modality", "lba",
+ "--isr", str(FIXTURES / "m10_isr.csv"))
+ # Differences sit between 20% and 30%, so this fails CC and passes LBA.
+ self.assertEqual(cc.returncode, 1)
+ self.assertEqual(lba.returncode, 0, lba.stderr)
+
+ def test_total_error_rejected_for_chromatographic(self):
+ res = run_script("check_bioanalytical_run", "--modality", "chromatographic",
+ "--total-error", str(FIXTURES / "m10_total_error.csv"))
+ self.assertEqual(res.returncode, 2)
+ self.assertIn("ligand binding", res.stderr)
+
+ def test_total_error_applied_for_lba(self):
+ res = run_script("check_bioanalytical_run", "--modality", "lba",
+ "--total-error", str(FIXTURES / "m10_total_error.csv"),
+ "--format", "json")
+ rows = json.loads(res.stdout)[0]["total_error"]
+ lloq = [r for r in rows if r["label"].lower() == "lloq"][0]
+ mid = [r for r in rows if r["label"].lower() == "medium"][0]
+ self.assertEqual(lloq["limit_pct"], 40.0)
+ self.assertEqual(mid["limit_pct"], 30.0)
+
+ def test_anchor_points_excluded_from_calibration_count(self):
+ res = run_script("check_bioanalytical_run", "--modality", "lba",
+ "--run", str(FIXTURES / "m10_run_lba_anchor.csv"),
+ "--format", "json")
+ rows = json.loads(res.stdout)[0]["run"]
+ anchors = [r for r in rows if r["label"] == "ANCHOR"]
+ self.assertTrue(anchors)
+ self.assertEqual(anchors[0]["within"], "excluded")
+
+ def test_criteria_listing(self):
+ res = run_script("check_bioanalytical_run", "--modality", "lba", "--criteria",
+ "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+ names = {r["criterion"] for r in json.loads(res.stdout)}
+ self.assertIn("total_error_pct", names)
+
+ def test_bad_type_column_exits_2(self):
+ res = run_script("check_bioanalytical_run", "--modality", "chromatographic",
+ "--run", str(FIXTURES / "m10_run_bad_type.csv"))
+ self.assertEqual(res.returncode, 2)
+
+
+class TestCompareMethods(unittest.TestCase):
+ def test_equivalent_at_two_percent(self):
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_paired.csv"),
+ "--margin", "2", "--relative", "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ self.assertTrue(payload["tost"]["equivalent"])
+
+ def test_not_equivalent_at_one_percent(self):
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_paired.csv"),
+ "--margin", "1", "--relative")
+ self.assertEqual(res.returncode, 1)
+ self.assertIn("equivalence NOT demonstrated", res.stderr)
+
+ def test_t_test_significant_while_tost_equivalent(self):
+ """The core teaching case: both statements are true and only one answers the question."""
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_paired.csv"),
+ "--margin", "2", "--relative", "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ self.assertLess(payload["paired_t_p_value"], 0.05)
+ self.assertTrue(payload["tost"]["equivalent"])
+
+ def test_deming_and_ols_both_reported(self):
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_paired.csv"),
+ "--margin", "5", "--relative", "--format", "json")
+ payload = json.loads(res.stdout)[0]
+ self.assertIn("ols_slope", payload)
+ self.assertIn("slope", payload["deming"])
+ self.assertIn("slope", payload["passing_bablok"])
+
+ def test_margin_is_required(self):
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_paired.csv"))
+ self.assertEqual(res.returncode, 2)
+
+ def test_too_few_pairs_exits_2(self):
+ res = run_script("compare_methods", "-i", str(FIXTURES / "transfer_two_pairs.csv"),
+ "--margin", "2")
+ self.assertEqual(res.returncode, 2)
+
+
+# ---------------------------------------------------------------- I/O
+
+
+class TestInputHandling(unittest.TestCase):
+ def test_json_input_accepted(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.json"),
+ "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+
+ def test_tsv_input_accepted(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.tsv"),
+ "--format", "json")
+ self.assertEqual(res.returncode, 0, res.stderr)
+
+ def test_non_numeric_value_names_the_row(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_nonnumeric.csv"))
+ self.assertEqual(res.returncode, 2)
+ self.assertIn("not numeric", res.stderr)
+
+ def test_missing_file_exits_2(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "does_not_exist.csv"))
+ self.assertEqual(res.returncode, 2)
+
+ def test_oversize_json_is_refused_not_truncated(self):
+ """Silently dropping rows would give a clean result computed on part of the study."""
+ payload = [{"level": 50 + (i % 5) * 25, "response": 10000 + i} for i in range(common.MAX_ROWS + 5)]
+ with self.assertRaises(common.InputError) as ctx:
+ common.parse_rows(json.dumps(payload))
+ self.assertIn("more than", str(ctx.exception))
+
+ def test_json_row_cap_boundary_is_accepted(self):
+ payload = [{"level": 50 + (i % 5) * 25, "response": 10000 + i} for i in range(common.MAX_ROWS)]
+ rows = common.parse_rows(json.dumps(payload))
+ self.assertEqual(len(rows), common.MAX_ROWS)
+
+ def test_empty_json_array_rejected(self):
+ with self.assertRaises(common.InputError):
+ common.parse_rows("[]")
+
+ def test_omitted_input_does_not_block_on_a_terminal(self):
+ """An omitted --input must error, not wait forever on a tty."""
+ import unittest.mock as mock
+
+ with mock.patch.object(sys, "stdin") as fake:
+ fake.isatty.return_value = True
+ with self.assertRaises(common.InputError) as ctx:
+ common.read_input(None)
+ self.assertIn("--input", str(ctx.exception))
+ fake.read.assert_not_called()
+
+ def test_stdout_carries_data_stderr_carries_notes(self):
+ res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.csv"),
+ "--format", "tsv")
+ self.assertIn("statistic", res.stdout)
+ self.assertNotIn("note:", res.stdout)
+ self.assertIn("note:", res.stderr)
+
+
+if __name__ == "__main__":
+ unittest.main()