# What Kind of Output Does the `financial_rigor.py` Script Produce?

> Discover the output of the financial_rigor.py script. Get human-readable text reports and Python dictionaries of financial metrics for easy analysis and integration.

- Repository: [Xbt Lin/ai-berkshire](https://github.com/xbtlin/ai-berkshire)
- Tags: internals
- Published: 2026-07-04

---

**The [`financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/financial_rigor.py) script produces human-readable text reports to stdout while simultaneously returning Python dictionaries containing the computed financial metrics for programmatic use.**

The [`financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/financial_rigor.py) script in the `xbtlin/ai-berkshire` repository is a pure-Python command-line utility designed to validate financial data through rigorous calculations. Located at [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py), this self-contained tool uses only the Python standard library to perform exact-decimal arithmetic and cross-validation checks. Each sub-command generates a distinct textual section preceded by separator lines, making the output both readable in the terminal and parsable for downstream automation.

## Human-Readable Console Output

When invoked from the command line, the script prints structured reports to stdout. Each section is preceded by a line of `=` characters (as seen in the `main()` help text at lines 71-78 in [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py)), creating clear visual separation between validation results.

### verify-market-cap

The **`verify-market-cap`** sub-command outputs a header labeled "市值验算 (Market Cap Verification)" followed by the input values for price, shares, and reported capitalization (lines 70-77). It displays the calculated market cap and deviation percentage, then appends a status emoji based on tolerance thresholds (lines 80-90):

- **❌** for deviations greater than 5%
- **⚠️** for deviations between 1% and 5%
- **✅** for deviations less than or equal to 1%

Despite the printed report, the function returns a Python **`bool`** indicating whether the deviation falls within acceptable limits.

### verify-valuation

The **`verify-valuation`** sub-command prints a header "估值指标验算 (Valuation Verification)" (lines 100-106) and processes multiple valuation metrics. For each supplied metric—**PE**, **PB**, **ROE**, **P/FCF**, **FCF Yield**, **Dividend Yield**, and **PS**—the script outputs the formula and calculated result (e.g., `PE (TTM): 510 / 23.5 = 21.70x`) at lines 111-158. The section concludes with confirmation that exact-decimal arithmetic was used (lines 158-160).

Programmatically, this function returns a **`dict`** containing the computed ratios (e.g., `{"PE": 21.7, "PB": 4.25, ...}`).

### cross-validate

The **`cross-validate`** sub-command displays a header "交叉验证: <field> (Cross-Validation)" (lines 68-71) showing the number of sources and median reference value (lines 82-84). Each source then appears on its own line with the value, unit, and deviation percentage, prefixed with **✅** or **❌** indicators (lines 86-93). A final summary line states whether all sources fall within the specified tolerance and reports the consensus value (lines 95-104).

The function returns a **`dict`** with keys `"consensus"` (the median value) and `"all_consistent"` (a boolean indicating consensus).

### benford

The **`benford`** sub-command performs Benford's Law analysis to detect potential data fabrication. It prints a header "Benford定律检测 (Financial Data Fabrication Check)" (lines 18-20) followed by sample size, MAD (Mean Absolute Deviation), χ² statistic, and a textual conformity rating such as "Close (高度符合)" (lines 57-61). A detailed table displays leading-digit frequencies, expected Benford probabilities, and deviations (flagged when exceeding 0.03) at lines 64-71. The section ends with a final verdict (**✅** or **❌**) and brief commentary (lines 74-78).

This function returns a **`dict`** containing `mad`, `chi2`, `conformity`, and `is_conforming` keys.

### calc

The **`calc`** sub-command functions as an exact calculator. It prints a header "精确计算 (Exact Calculator)" (lines 94-96) followed by the original mathematical expression and formatted result (e.g., `结果: 4.62T`) at lines 107-110. For debugging purposes, it also displays the exact `Decimal` value (lines 110-111).

The function returns a **`float`** of the computed value, or `None` if an error occurs.

### three-scenario

The **`three-scenario`** sub-command outputs a "三情景估值模型 (Three-Scenario Valuation)" header (lines 26-28) and an input summary showing price, EPS, and years (lines 39-42). It then generates a table displaying three scenarios—**optimistic (乐观)**, **neutral (中性)**, and **pessimistic (悲观)**—with columns for growth percentage, target PE, projected EPS, target price, and percentage change (lines 44-56). The report confirms exact-decimal arithmetic usage at the conclusion (lines 58-60).

Unlike other commands, this function currently returns no explicit value (printing only), though the implementation could be repurposed to return a list of scenario dictionaries.

## Programmatic Integration

Beyond CLI usage, every verification function in [`financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/financial_rigor.py) is pure-Python and can be imported directly into other scripts. This allows downstream code to consume the returned dictionaries without parsing console output.

```python
from tools.financial_rigor import verify_valuation, cross_validate

# Compute valuation ratios

ratios = verify_valuation(
    price=510,
    eps=23.5,
    bvps=120,
    fcf_per_share=18,
    dividend=2.4,
)
print("PE =", ratios["PE"])

# Cross-validate revenue from three sources

field = "revenue"
values = {"AnnualReport": 7518, "Yahoo": 7500, "StockAnalysis": 7520}
result = cross_validate(field, values, unit="亿")
print("Consensus revenue:", result["consensus"])

```

Command-line usage follows standard Python invocation:

```bash

# Verify a market cap

python3 tools/financial_rigor.py verify-market-cap \
    --price 510 --shares 9.11e9 --reported 4.65e12 --currency HKD

```

## Summary

- **[`financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/financial_rigor.py)** generates dual-output: human-readable text to stdout and machine-readable Python dictionaries for programmatic use.
- Each sub-command produces distinct report sections with bilingual headers (Chinese and English) and visual status indicators (**✅**, **⚠️**, **❌**).
- **Return types vary by function**: `verify-market-cap` returns a boolean; `verify-valuation`, `cross-validate`, and `benford` return dictionaries; `calc` returns a float or `None`; `three-scenario` currently returns nothing.
- The script relies entirely on the Python standard library and uses exact `Decimal` arithmetic to prevent floating-point errors.
- All functions can be imported as a module into other Python scripts, enabling direct access to computed financial metrics without CLI parsing.

## Frequently Asked Questions

### Can I import [`financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/financial_rigor.py) functions into my own Python scripts?

Yes. The script is implemented as a pure-Python module with no external dependencies. You can import functions such as `verify_valuation`, `cross_validate`, and `verify_market_cap` directly from `tools.financial_rigor` to access the returned dictionaries and boolean values without invoking the command line.

### What do the emoji indicators (✅, ⚠️, ❌) mean in the output?

These visual flags indicate deviation tolerances. **❌** appears when the deviation exceeds 5%, **⚠️** appears for deviations between 1% and 5%, and **✅** indicates the deviation is within 1% or that the data conforms to expected values (such as in Benford's Law analysis).

### Does the script support floating-point arithmetic?

No. The script uses Python's `decimal.Decimal` class for all calculations to ensure exact arithmetic and avoid floating-point precision errors. This is confirmed in the output messages of several sub-commands, particularly `verify-valuation` and `three-scenario`.

### Where is the main entry point for the CLI defined?

The command-line interface entry point is the `main()` function in [`tools/financial_rigor.py`](https://github.com/xbtlin/ai-berkshire/blob/main/tools/financial_rigor.py), which handles argument parsing and dispatches to the appropriate sub-commands (lines 71-78). This function coordinates the print statements that generate the human-readable reports while returning the underlying computed values.