BESTPRED
USDA's lactation records for every cow, from your AI agent.
Give Claude your herd's test days. Get back 305-day and mature-equivalent milk, fat and protein for every cow.
Quick start · Other apps · How it was checked · Python library
Best prediction is the method USDA has used for official US lactation records since 1998. This is a Python port of USDA-AGIL's BESTPRED 2.0rc7 that gives the same numbers as USDA's own program, and a set of tools any AI agent can call. An agent can't work out a lactation record in its head; with this, it runs USDA's method and reports what comes back.
Quick start
1. Connect
Open claude.ai/directory/bestpred and click Connect. Works in Claude on the web, the desktop app and the phone. Nothing to install.
2. Ask
Attach a test-day export (DHI or DairyComp columns; or try this example herd) and ask:
These are my herd's test days from California. Work out every cow's 305-day milk and mature
equivalent, show me the median by lactation, and tell me which cows are least persistent.
You get every cow's 305-day and 365-day milk, fat and protein, mature equivalent, reliability, persistency and data collection rating, a summary by lactation, and the least persistent cows.
Other apps
| Where | How |
|---|---|
| Claude Code or Cowork | Install the BESTPRED plugin from Claude's plugin directory (the tools plus guidance for Claude on test-day files). Or: claude plugin marketplace add padge01/bestpred-python then claude plugin install bestpred@bestpred |
| Codex | codex plugin marketplace add padge01/bestpred-python, then install BESTPRED from the Plugins tab |
| ChatGPT, Cursor, VS Code, any MCP app | Add the server https://bestpred.dairydatascience.com/mcp |
| On your own computer (files never leave it) | Install uv, then point your app at uvx bestpred-mcp. Setup for each app: mcp/ |
| Python | pip install bestpred, see below |
The hosted server keeps nothing: privacy.
The tools
| Tool | What it does |
|---|---|
| A herd's lactation records | A herd's test-day export to a record for every lactation, a summary by parity and the least persistent cows |
| One cow's lactation record | 305-day and 365-day milk, fat and protein, mature equivalent when her age and state are known, reliability and persistency |
| A cow's expected daily milk | Her expected milk to day 305, to compare with what she is giving |
| Mature-equivalent factors | USDA's age factors for a breed, age at calving, month, parity, state and previous days open |
How it was checked
USDA's code was compiled from source, and both programs ran on the same random lactations: 1 to 15 test days, duplicate tests, LER segments, 3X milking, owner-sampler and unusable supervision codes, missing components, all six breeds, parities 1 to 9, herd means present and absent.
- Every output and the full daily curves agree to 1e-9 relative (typically 1e-12) for 300 lactations under each of 13 settings: the default, single-trait, multi-trait with SCS, the three 3X options, 365-day and 330-day lactations, linear, region, calving-season and region-by-season curves, and kilograms.
- Mature-equivalent factors agree bit for bit (aiplage and adjscs) on 43,200 combinations of breed, state, year, month, parity, previous days open and age base.
- USDA's example record gives the same results line as their program, character for character.
The port that was checked this way follows the Fortran line by line, and is kept as it was in
reference/ to read beside USDA's source. The package runs the same calculation reorganized and
vectorized (a lactation of monthly tests takes about a millisecond), and tests/test_against_reference.py holds the
two to the same answers on random lactations: they differ only in rounding, by about 1e-10.
tests/ holds fixtures from that reference build, so pytest runs without a Fortran compiler. To rebuild the
reference and run the full comparison yourself (needs gcc and gfortran):
tools/reference.sh /tmp/bestpred-ref
python tools/compare.py --driver /tmp/bestpred-ref/driver --n 300
Tested against daily meter weights
600 synthetic lactations shaped like a California Holstein herd. 305-day yield projected from test days drawn monthly from daily weights, compared with the true daily total.
Method, data, table and scripts: validation/.
The Python library
pip install bestpred
Python 3.10 or later, NumPy only. With test days as a DHI test or a parlor export gives them:
from bestpred import TestDay, bestpred
tests = [
TestDay(dim=12, milk=78.0, fat=4.4, protein=3.2, scs=3.1),
TestDay(dim=43, milk=96.5, fat=3.7, protein=2.9, scs=2.0),
TestDay(dim=71, milk=99.0, fat=3.5, protein=3.0, scs=1.8),
TestDay(dim=104, milk=94.2, fat=3.6, protein=3.1, scs=2.4),
]
res = bestpred(tests, herdavg=(26000, 980, 800, 2.8), agefac=(1, 1, 1, 1), cowid='H', parity=2,
fresh='20260301')
res.yields('actual')['milk']['305'] # projected 305-d milk, lb
res.rel_yield[0] # reliability of that projection
res.persistency[0] # milk persistency (SD units, 0 is average)
res.daily_herd[1, 0] + res.daily_bp[1, 0] # the cow's predicted daily milk, days 0..365 (slot 0 unused)
| Argument | Meaning |
|---|---|
herdavg |
Herd 305-d mature-equivalent milk, fat, protein (lb or kg) and mean SCS. 0 for unknown: the breed mean is used. |
agefac |
Factors that turn actual into mature-equivalent yield. bestpred.age_factors() computes USDA's; (1, 1, 1, 1) leaves yields as they are. |
cowid |
Only the first letter matters: breed A, B, G, H, J or M. |
TestDay.super |
Supervision code: 1 supervised, 2 owner-sampler, 0 or 4 unusable, and so on as in Format 4. |
times_milked, times_weighed, times_sampled |
Milkings that day and how many were weighed or sampled (AM-PM testing). |
mrd |
Days averaged into a test (1 for a normal test day; more for LER). |
bestpred.Settings holds the options from USDA's bestpred.par: single- or multi-trait, the 3X adjustment, lactation
length, which standard curves, and pounds or kilograms.
Daily milk weights. There is no limit on the number of test days, so a complete lactation of daily weights comes
back as the sum of its days (supervised weights with every milking weighed: owner-sampler and AM-PM weights carry
error, and are not taken at face value). USDA's program reads only a lactation's first 50 test days:
Settings(maxtd=50) does the same, and warns when test days are left out. As in the original, test days past day 365
and a second test on the same day are not used. Daily weights take more work than monthly tests: about 20 ms a
lactation against 1 or 2, and up to 0.2 s when fat, protein and SCS are daily too.
USDA Format 4 records run straight from the command line and give the program's results_v2.dcr lines, or JSON:
bestpred records.dat
bestpred records.dat --json
Things the port keeps from the original
The aim is the same numbers as USDA's program, so behavior that changes results is kept and marked sic in the code.
Worth knowing:
- When a herd mean is missing, the breed mean is taken from the 1995 table for first lactations and the 2000 table for later ones (the year-group index is the parity group).
- The partial-record 3X factor compares the last test against
end1(capped at 305 days) instead ofend2. - Duplicate test days are dropped (the first is kept); overlapping LER segments are shortened.
- USDA's program reads at most 50 test days from a lactation and ignores the rest. That limit is not kept by default
(a year of daily weights would be predicted from its first 50 days);
Settings(maxtd=50)and the tools'match_usdabring it back. The Format 4 reader takes a record's first 50 segments, as USDA's does, so those records run as before. - USDA's command-line program replaces a record's length with its last test day (
--keep-lengthturns that off here), and its parameter reader quietly resets the calving-season curves (C, T, D, U) to linear. Programs that call the routine directly can use them, and so can this package.
Found while porting:
- With bounds checking on (
-fcheck=all), the original stops when the last usable test day has no usable yield: a plotting loop reads one element before an array. USDA's production build has no bounds checking, and the results don't use that value. - An SCS age adjustment can land exactly on .5. Compilers that fuse multiply-adds (by default on Apple silicon) round it the other way from plain IEEE double arithmetic. This port gives the plain result, as on x86.
References
- VanRaden, P.M. 1997. Lactation yields and accuracies computed from test day yields and (co)variances by best prediction. J. Dairy Sci. 80:3015–3022.
- Cole, J.B., D.J. Null, and P.M. VanRaden. 2009. Best prediction of yields for long lactations. J. Dairy Sci. 92:1796–1810.
- Cole, J.B., and P.M. VanRaden. 2008. A manual for use of BESTPRED: a program for estimation of lactation yield and persistency using best prediction. USDA-ARS-AGIL.
Support
BESTPRED is free and open source. If it saves you time and you would like to support continued development and testing, you can leave an optional tip on Ko-fi.
License
The port is Apache-2.0. The original is public domain, written by employees of the US government. Its notice is reproduced in full in NOTICE, as it asks. This package is not a product of, or endorsed by, USDA.
Ported by Pádraig Lucey (dairydatascience.com).
Metadata
Release files for bestpred 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bestpred-0.3.0.tar.gz | 460.8 kB | With provenance | Details for bestpred-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bestpred-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for bestpred-0.3.0-py3-none-any.whl |
Total release size: 610.2 kB
Release files / bestpred-0.3.0.tar.gz
| Download URL | bestpred-0.3.0.tar.gz |
|---|---|
| Size | 460.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4af08e12889d96fa0eaeb6910d97dd5287b45148dd73f7967d031ffa75d6edf2
|
|
BLAKE2b-256 checksum How to use checksums |
04529ddcab8301a3f18429dd6e7e39f3bb6547da34b670e5837105204f977392
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / bestpred-0.3.0-py3-none-any.whl
| Download URL | bestpred-0.3.0-py3-none-any.whl |
|---|---|
| Size | 149.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
63711a1b4bad15fa5d66b746942d90c0851fc69b5dbcf5a9e3e2816003a9616c
|
|
BLAKE2b-256 checksum How to use checksums |
7336b6556626ad99b7ade95a47e1772630b931edfac82c29718df7a7faebc8a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for bestpred 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bestpred-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.