What Metrics Does AI Berkshire Use for Financial Rigor?
AI Berkshire validates financial data through a self-contained Financial Rigor Toolkit (tools/financial_rigor.py) that implements six quantitative metrics: market-cap verification, valuation ratios, cross-source validation, Benford's Law analysis, exact arithmetic calculation, and three-scenario valuation modeling.
AI Berkshire is an open-source investment research framework designed to enforce strict data integrity before generating any investment recommendations. At the heart of this system lies the Financial Rigor Toolkit, a dependency-free Python module that executes quantitative validation checks using only Python's standard library. Every metric relies on the Decimal engine to eliminate floating-point drift and ensure exact arithmetic precision throughout the research workflow.
The Financial Rigor Toolkit Architecture
The toolkit is deliberately dependency-free, utilizing only Python standard library modules (decimal, json, math, argparse). This design ensures reproducibility across environments and seamless integration into Claude Code skills. Each metric is implemented as a distinct function in tools/financial_rigor.py that returns structured reports with deviation percentages and explicit pass/fail signals.
Core Metrics for Financial Validation
Market-Cap Verification
The verify_market_cap() function (source lines 61-78) confirms that the product of price and shares outstanding matches the reported market capitalization. The implementation uses exact decimal arithmetic to compute the product, then reports the percentage deviation. The function flags deviations exceeding 5% as warnings and those exceeding 1% as cautions, providing immediate alerts for potential data inconsistencies.
Valuation Ratios Calculation
Implemented in verify_valuation() (source lines 98-160), this metric generates and validates critical equity-valuation multiples:
- PE Ratio: Price divided by EPS
- PB Ratio: Price divided by BVPS (Book Value Per Share)
- ROE: EPS divided by BVPS (when both available)
- P/FCF: Price divided by Free Cash Flow per share
- FCF Yield: Free Cash Flow divided by price
- Dividend Yield: Dividend divided by price
- PS Ratio: Price divided by Revenue per share
All calculations leverage the Decimal engine to prevent floating-point precision errors that could distort investment decisions.
Cross-Source Data Validation
The cross_validate() function (source lines 167-204) checks consistency of a single data point across multiple external sources. The function converts each source value to a Decimal, computes the median as the reference point, and flags any source whose deviation exceeds a configurable tolerance (default 2%). It returns both a consensus value and an "all-consistent" boolean flag, enabling researchers to identify outlier data sources quickly.
Benford's Law Integrity Check
For statistical fraud detection, benford_check() (source lines 108-162) analyzes the leading-digit distribution of numeric series. The function calculates the observed distribution, compares it to the expected Benford distribution, and reports Mean Absolute Deviation (MAD), chi-square statistics, and a qualitative conformity rating. This check requires at least 50 data points to ensure statistical reliability.
Exact Arithmetic Calculator
The exact_calc() function (source lines 88-113) provides a sandboxed calculator for arbitrary arithmetic expressions. It parses expressions, validates allowed characters to prevent injection attacks, evaluates using Python's eval in a restricted environment, and returns exact Decimal results. This utility ensures that intermediate calculations throughout the research workflow maintain precision.
Three-Scenario Valuation Modeling
The three_scenario_valuation() function (source lines 216-258) projects target prices under optimistic, neutral, and pessimistic growth assumptions. For each scenario, the function compounds EPS by the annual growth rate over a configurable time horizon, applies the target PE multiple, and compares the resulting price to the current market price. All compounding operations use the exact decimal context to ensure scenario analyses are mathematically rigorous.
Command-Line Interface Usage
While Claude Code skills invoke these metrics automatically at validation checkpoints, researchers can call the toolkit directly via CLI for debugging or exploration. Each command accepts parameters with proper unit handling and returns formatted JSON reports.
Verify Market-Cap
python3 tools/financial_rigor.py verify-market-cap \
--price 510 --shares 9.11e9 --reported 4.65e12 --currency HKD
Check Valuation Ratios
python3 tools/financial_rigor.py verify-valuation \
--price 510 --eps 23.5 --bvps 120 --fcf-per-share 18 --dividend 2.4
Cross-Validate Data Sources
python3 tools/financial_rigor.py cross-validate \
--field revenue \
--values '{"年报": 7518, "Yahoo": 7500, "StockAnalysis": 7520}' \
--unit 亿
Benford's Law Analysis
python3 tools/financial_rigor.py benford \
--values '[1234, 2345, 3456, 4567, 5678, 6789, 7890, 8912, 9123, …]'
Exact Calculator
python3 tools/financial_rigor.py calc --expr '510 * 9.11e9'
Three-Scenario Valuation
python3 tools/financial_rigor.py three-scenario \
--price 510 --eps 23.5 --shares 9.11 \
--growth 0.15 0.08 0.00 \
--pe 25 20 15 \
--years 3 --currency HKD
Integration with Research Workflows
The financial rigor metrics are embedded within the AI Berkshire research pipeline through Claude Code skills defined in skills/*.md. These skills automatically invoke the toolkit at critical validation checkpoints before finalizing investment recommendations. The scripts/sync-codex-skills.py utility ensures that skill definitions remain synchronized with the latest metric implementations in tools/financial_rigor.py.
Summary
- AI Berkshire employs six rigorous quantitative metrics to validate financial data integrity before generating recommendations.
- All calculations use the
Decimalengine intools/financial_rigor.pyto eliminate floating-point errors. - Market-cap verification flags deviations exceeding 1% (caution) or 5% (warning).
- Cross-source validation defaults to 2% tolerance for identifying data inconsistencies across providers.
- Benford's Law provides statistical manipulation detection for datasets with 50+ observations.
- Three-scenario valuation enables probabilistic price targeting under varying growth assumptions.
- The toolkit is dependency-free, requiring only Python's standard library.
Frequently Asked Questions
What file contains the financial rigor metrics in AI Berkshire?
The core implementation resides in tools/financial_rigor.py, which defines functions including verify_market_cap(), verify_valuation(), and cross_validate(). This module is self-contained and uses only Python standard library dependencies according to the repository source code.
Why does AI Berkshire use Decimal instead of floating-point arithmetic?
The toolkit uses Python's decimal.Decimal engine for all calculations to avoid floating-point drift, which can introduce micro-errors that compound in financial projections. This ensures that valuation ratios, market-cap calculations, and scenario models maintain exact precision as required for rigorous investment analysis.
What is the tolerance threshold for cross-source data validation?
The cross_validate() function defaults to a 2% deviation tolerance when comparing values across multiple data sources (such as annual reports versus Yahoo Finance). This threshold is configurable via command-line arguments to accommodate different data quality requirements.
How does the Benford's Law check detect potential data manipulation?
The benford_check() function calculates the distribution of leading digits in a numeric series and compares it against the expected Benford distribution. It returns Mean Absolute Deviation (MAD) and chi-square statistics along with a conformity rating, flagging datasets that deviate significantly from expected patterns as potential candidates for data manipulation or fabrication.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →