Library/Fundamental Analysis and Valuation/Statement Ratios/DuPont Decomposition

D18-F01-A01 / Complete engineering topic

DuPont Decomposition

Decompose ROE into a multiplication identity that reconciles exactly and makes denominator alignment auditable.

DuPont Decomposition turns aligned statement facts into an auditable diagnostic with visible definition boundariesD18 / D18-F01

Decompose ROE into a multiplication identity that reconciles exactly and makes denominator alignment auditable.

The decision this tutorial makes visible

A single ROE number hides whether the result came from profitability, asset turnover, or a larger assets-to-equity relationship.

The precise question is: Which combination of margin, asset use, and accounting leverage produced return on equity?

An analyst needs to see what changed in the business; a builder needs every source fact, clock, unit, sign, average, adjustment, and reason code needed to reproduce the diagnostic.

Intuition before notation

The inserted revenue and asset terms cancel only when the exact same amounts and periods are reused, turning reconciliation into a data-quality test.

This package selects one explicit statement-ratio convention and compares material alternatives. It does not label issuer-defined measures as universal or hide non-meaningful denominator states.

Scope and nearby methods

Canonical three-factor DuPont: net income/revenue × revenue/average assets × average assets/average equity. Five-factor, parent-only, and sector variants are compared but excluded.

VariantDefinitionBest useMain limitation
Three-factor packageMargin × turnover × equity multiplierTransparent ROE bridgeBroad factors can hide tax and financing detail
Five-factor DuPontTax burden × interest burden × EBIT margin × turnover × leverageSeparating tax and financing effectsFragile near zero or sign changes
Parent-common ROEParent-attributable income / average parent equityCommon-shareholder perspectiveCannot mix with consolidated assets without a bridge

What is sourced, selected, synthetic, and derived

RoleMaterial claimEvidenceBoundary
Sourced factBalance sheets describe a point in time, while income and cash-flow statements describe a period; notes are part of interpreting the reported amounts.S1 and supporting sourcesIt does not prescribe the package ratios, XBRL mapping, normalization choices, or cross-company conclusions.
Implementation choiceCanonical three-factor DuPont: net income/revenue × revenue/average assets × average assets/average equity. Five-factor, parent-only, and sector variants are compared but excluded.Frozen package definition contractNearby variants remain named and separate.
Synthetic teaching inputAll company amounts, periods, scenarios, and calculated teaching paths are repository-authored synthetic data.datasets/canonical-input.json and scenario-results.jsonNo value is represented as a live provider observation or filed issuer fact.
Author-derived calculationSynthetic NI 96, revenue 1,200, average assets 1,000, and average equity 400 produce 8% margin × 1.2× turnover × 2.5× multiplier = 24% ROE, exactly matching 96/400.Formula, shared fixture, independent arithmetic, and Python/TypeScript parityArithmetic fidelity does not prove an analytical or investment conclusion.
Scope boundaryThe output does not establish investment quality, solvency, valuation, peer superiority, forecast accuracy, profitability, covenant compliance, or investment advice.No empirical or advisory claim is testedUse the result as one documented diagnostic.

Primary regulator, accounting-body, institution, paper, and filed-entity sources establish reporting and definition boundaries. The fixtures, calculations, scenarios, and conclusions about those fixtures are repository-authored and synthetic.

Formula, symbols, and numerical policy

Plain text
ROE = (NI / Revenue) × (Revenue / average Assets) × (average Assets / average Equity) = NI / average Equity
SymbolMeaningUnitPolicy
NIPeriod net incomecurrencySame attributable/consolidation scope throughout
SPeriod revenuecurrencyExact same amount in margin and turnover
Two-point average assetscurrencySame amount in turnover and multiplier
ĒTwo-point average equitycurrencyMust be positive for package interpretation
  • Use one currency and scale for every amount in a calculation; ratios are dimensionless and days use the declared period day count.
  • Do not round inputs or intermediate averages; round displayed percentages, days, and multiples only after calculation.
  • Return a diagnostic null for structurally valid but economically uninterpretable denominators; reject malformed schemas and non-finite numbers.
  • Preserve filed signs in common-size statements and normalize expense/debt signs explicitly for ratios that require positive denominators.

Read the formula in the same order as the algorithm. Validate identity, ordering, units, and supported state first. Apply the selected denominator, equality, and mapping rules second. Calculate with unrounded numeric values. Round only at the declared presentation boundary, and preserve null as a diagnostic rather than coercing it to zero.

Build the algorithm

  1. Validate one aligned period and two surrounding balance dates.
  2. Average assets and equity without rounding.
  3. Calculate margin, turnover, and equity multiplier.
  4. Multiply factors and reconcile to direct ROE.

Production-minded operational checklist

  1. Align entity and period
  2. Freeze income/equity attribution
  3. Reuse identical cancellation terms
  4. Audit the identity gap

A precise ratio is unsafe when its framework, period, unit, source fact, numerator, denominator, average, sign, or adjustment policy is ambiguous. Stop and map the evidence before calculating.

Worked synthetic example

The canonical fixture is synthetic teaching data, not a filed issuer statement or provider observation. Its primary author-derived output, dupont_roe, is 0.24. The complete input and output are in datasets/canonical-input.json and datasets/expected-output.json.

Synthetic NI 96, revenue 1,200, average assets 1,000, and average equity 400 produce 8% margin × 1.2× turnover × 2.5× multiplier = 24% ROE, exactly matching 96/400.

Counterfactual checkpoint

Same ROE, different engine. Lower margin while increasing turnover or multiplier. The output changes because A product can be equal even when its factors differ.

The structured result retains state and diagnostics in addition to the primary number. That makes the calculation independently reviewable and prevents a partial, null, rejected, or definition-bounded outcome from being mistaken for an unqualified value.

Boundary and counterexample workbook

The playground computes every scenario at 61 deterministic parameter states. The table uses the declared focus step and states whether that focus reproduces the canonical fixture. The full state ledger and compressed transition segments are in datasets/scenario-results.json.

ScenarioReview focusPurposeStatePrimary outputDiagnosticDecision segments
Canonical profit sweepStep 30 · canonical fixtureMove net income through the canonical value.calculated24.00%calculated · three-factor-identity-reconciles1
Revenue and turnoverStep 30 · canonical fixtureChange revenue while assets and equity stay fixed.calculated24.00%calculated · three-factor-identity-reconciles1
Asset intensityStep 30 · canonical fixtureChange the asset base and observe turnover.calculated24.00%calculated · three-factor-identity-reconciles1
Equity multiplierStep 30 · canonical fixtureChange average equity and the accounting leverage factor.calculated24.00%calculated · three-factor-identity-reconciles1
Loss stateStep 30 · comparison focusMove net income from a loss toward break-even.loss-12.50%loss · three-factor-identity-reconciles2
Zero-revenue boundaryStep 30 · comparison focusApproach and leave the zero-revenue not-meaningful state.calculated24.00%calculated · three-factor-identity-reconciles2
Nonpositive-equity boundaryStep 30 · comparison focusCross the package's average-equity interpretation gate.calculated56.47%calculated · three-factor-identity-reconciles2

These rows are not backtest observations. They are controlled counterexamples that expose how one driver changes the state, output, or reason code while the rest of the contract stays fixed.

Visualize the boundary

DuPont Decomposition annotated teaching map

Open this SVG at full size, or use the guided playground to compare the seven topic-specific canonical, boundary, policy, and failure scenarios.

The Mermaid flow answers where the selected calculation sits in the processing sequence. The SVG keeps the formula, output, decision boundary, and invariant visible together. The lab lets the reader step through the same structured states without changing the underlying definition.

Five coordinated teaching views

DuPont Decomposition learning promise

The hero fixes the practitioner question and the builder contract before any ratio is interpreted.

DuPont Decomposition formula anatomy

The formula view keeps units, policies, and the material denominator boundary beside the symbols.

DuPont Decomposition independently auditable worked example

The worked-example view separates synthetic input, author-derived output, invariant, and counterfactual.

DuPont Decomposition variant comparison

The comparison view prevents same-label measures from being treated as interchangeable.

DuPont Decomposition evidence lineage

The lineage view shows where entity, period, framework, unit, sign, and definition decisions enter.

AssetLearning purposeStatic fallback
Article heroQuestion and learning contractEmbedded SVG text
Formula anatomyUnits, policies, denominator gateSymbol table
Worked exampleIndependent arithmetic and invariantFixture JSON
Definition comparisonVariant selectionComparison table
Evidence lineageSource-to-output audit pathClaim ledger
Guided playgroundCanonical, boundary, comparison, and failure statesScenario ledger

Implementation walkthrough

The Python and TypeScript references validate the same JSON contract, retain components and reason codes, calculate at full precision, and are compared against shared expected output plus 427 scenario states.

The main implementation branches are:

  • revenue = 0 — Return not-meaningful, because Margin and turnover cannot both be formed.
  • average assets or equity <= 0 — Return not-meaningful, because Multiples become economically unstable.
  • identity gap != 0 beyond tolerance — Stop and audit mapping, because A reused amount, period, or scope diverged.

Neither reference silently fetches data, mutates caller-owned inputs outside the declared engine behavior, guesses hidden state, or substitutes a provider default. Shared JSON fixtures make value, null, state, and reason-code drift visible across languages.

Testing and validation

Definition tests compare every canonical field, reject malformed state, and exercise the material boundary. Family validation recomputes every playground state from the reference function. Independent arithmetic is recorded beside the fixture rather than inferred only from implementation output.

The audit must preserve these invariants:

  • When meaningful, DuPont ROE equals direct ROE within numerical tolerance.
  • Revenue and average assets cancel algebraically only when reused identically.
  • The identity does not explain causation or establish whether leverage is safe.
  • The output retains components, state, and a reason code rather than publishing an unexplained scalar.

Passing definition and parity checks proves that the implementation matches the selected contract. It does not prove production performance, universal applicability, or a later market outcome.

Failure modes and misuse

  • A correct ratio can still be distorted by acquisitions, disposals, seasonality, inflation, foreign exchange, restatements, or classification choices.
  • Cross-company comparison requires the same framework, consolidation scope, period length, mapping, and analytical definition.
  • Ratios summarize reported accounting amounts; they do not measure market value, forecast cash flows, or establish investment merit.

Debugging order

When a result looks surprising, inspect the state in this order:

  1. Confirm entity, period, framework, currency, and scale
  2. Trace every source fact and sign
  3. Recompute averages and bridges independently
  4. Check denominator state and selected variant
  5. Compare unrounded Python and TypeScript output

Evidence and historical boundary

Historical decision: not useful. A named issuer is not useful for the canonical arithmetic because filings mix company-specific labels, fiscal calendars, tax effects, lease policies, segments, and non-GAAP reconciliations. Primary filings are used to prove definition variability; controlled synthetic statements isolate the mechanism without implying a company judgment.

The primary sources are SEC financial-statement guide, SEC EDGAR XBRL APIs, IFRS 18. They support the source roles listed in the research ledger. They do not supply a redistributable filed/provider observation or support investment quality, solvency, valuation, peer superiority, forecast accuracy, profitability, covenant compliance, or investment advice.

Practical questions and review answers

What exactly does this package calculate?

Canonical three-factor DuPont: net income/revenue × revenue/average assets × average assets/average equity. Five-factor, parent-only, and sector variants are compared but excluded. The structured output includes components, state, and a reason code; the headline ratio is never the whole audit record.

Why can a filing platform, data vendor, spreadsheet, or company presentation disagree?

They may map different statement rows, fiscal periods, signs, tax or lease policies, cash definitions, balance averages, or non-GAAP adjustments. Compare the definition and evidence ledger before comparing numbers.

Can quarterly, trailing-twelve-month, and annual facts be mixed?

No. A flow numerator and every flow denominator must cover the declared period, while balance-sheet stocks must come from the matching boundary dates. Annualization is a separate, declared analytical transformation.

When should the output be null rather than zero or infinity?

At the material boundary: zero revenue or nonpositive average equity. Null means the package cannot answer its stated question under that denominator state; it is not a missing cosmetic value.

Does a negative, high, or improving result automatically mean good or bad?

No. Interpretation depends on sector, business model, accounting framework, period, definition, and the components that moved. The ratio can describe a state without establishing its cause or desirability.

Does passing the code and parity checks prove investment value?

No. It proves that Python, TypeScript, fixtures, visuals, and the playground implement the frozen definition. It does not establish forecasting ability, solvency, covenant compliance, valuation, peer superiority, or investment suitability.

Where can I inspect every scenario rather than one example?

Open the guided 427-state playground and the full scenario-results.json.

Summary and next topic

You can now carry the result from DuPont Decomposition into ROIC Calculation only after carrying forward the fact-mapping ledger and resetting method-specific numerator and denominator choices. The learning flow is: Common-Size Statements → DuPont Decomposition → ROIC Calculation. Carry the result forward only with its scope, clock, state, and evidence label.

DuPont Decomposition calculation flow

This flow identifies the selected calculation stages and the structured output.

Rendering system map…

Takeaway: The cancellation bridge is also a data-quality control: one mismatched revenue or asset amount breaks the identity.

ReferencesPrimary sources and evidence notes

Expand the source trail, evidence role, and limitations behind the engineering choices.

S1 — Beginners' Guide to Financial Statements

  • Organization or authors: U.S. Securities and Exchange Commission
  • Source type: Official regulator investor publication
  • Publication or effective date: 2007-02-05
  • Version: Current SEC web publication
  • URL or DOI: https://www.sec.gov/about/reports-publications/investor-publications/beginners-guide-financial-statements
  • Accessed: 2026-08-04
  • Jurisdiction: United States public-company reporting
  • Supports: Balance sheets describe a point in time, while income and cash-flow statements describe a period; notes are part of interpreting the reported amounts.
  • Limitations: It does not prescribe the package ratios, XBRL mapping, normalization choices, or cross-company conclusions.

S2 — EDGAR Application Programming Interfaces

  • Organization or authors: U.S. Securities and Exchange Commission
  • Source type: Official regulator technical documentation
  • Publication or effective date: 2025-04-08
  • Version: Company Facts, Company Concept, and Frames API documentation
  • URL or DOI: https://www.sec.gov/search-filings/edgar-application-programming-interfaces
  • Accessed: 2026-08-04
  • Jurisdiction: United States public filings
  • Supports: EDGAR exposes filed XBRL facts with taxonomy, unit, form, period, and filing context; company extensions and fiscal-calendar differences require explicit mapping.
  • Limitations: A matching tag does not by itself prove semantic comparability, adjustment policy, or the correct ratio denominator.

S3 — IFRS 18 Presentation and Disclosure in Financial Statements

  • Organization or authors: International Accounting Standards Board
  • Source type: Official accounting-standard overview
  • Publication or effective date: 2024-04
  • Version: Effective for annual periods beginning on or after 2027-01-01; early application permitted
  • URL or DOI: https://www.ifrs.org/issued-standards/list-of-standards/ifrs-18-presentation-and-disclosure-in-financial-statements/
  • Accessed: 2026-08-04
  • Jurisdiction: IFRS reporting
  • Supports: IFRS 18 replaces IAS 1, defines new profit-or-loss subtotals, addresses aggregation and disaggregation, and is effective in 2027 unless early applied.
  • Limitations: The ratios remain analytical constructions; framework transition dates and issuer adoption must be checked for each record.

Evidence boundary

The sources establish the exact rule, interface, protocol, or research context named above. They do not verify the repository-authored synthetic fixture, thresholds, empirical usefulness, execution probability, or profitability. Package-selected choices remain labeled as implementation choices wherever they are used.

statement-ratios.ts
/** Deterministic TypeScript reference calculations for D18-F01. */

type Inputs = Record<string, unknown>;
type Output = Record<string, unknown>;

function numberValue(name: string, value: unknown): number {
  if (typeof value !== "number" || !Number.isFinite(value)) throw new RangeError(`${name} must be a finite number`);
  return value;
}

function positive(name: string, value: unknown): number {
  const parsed = numberValue(name, value);
  if (parsed <= 0) throw new RangeError(`${name} must be positive`);
  return parsed;
}

function booleanValue(name: string, value: unknown): boolean {
  if (typeof value !== "boolean") throw new RangeError(`${name} must be boolean`);
  return value;
}

const average = (beginning: number, ending: number): number => (beginning + ending) / 2;
const clean = (value: number): number => {
  const rounded = Math.round((value + Number.EPSILON) * 1e12) / 1e12;
  return Object.is(rounded, -0) ? 0 : rounded;
};

export function dupontDecomposition(input: Inputs): Output {
  const income = numberValue("net_income", input.net_income);
  const revenue = numberValue("revenue", input.revenue);
  const averageAssets = average(numberValue("beginning_assets", input.beginning_assets), numberValue("ending_assets", input.ending_assets));
  const averageEquity = average(numberValue("beginning_equity", input.beginning_equity), numberValue("ending_equity", input.ending_equity));
  if (revenue === 0 || averageAssets <= 0 || averageEquity <= 0) {
    const reasons: string[] = [];
    if (revenue === 0) reasons.push("zero-revenue");
    if (averageAssets <= 0) reasons.push("nonpositive-average-assets");
    if (averageEquity <= 0) reasons.push("nonpositive-average-equity");
    return { net_profit_margin: null, asset_turnover: null, equity_multiplier: null, dupont_roe: null, direct_roe: null, identity_gap: null,
      average_assets: clean(averageAssets), average_equity: clean(averageEquity), state: "not-meaningful", reason: reasons.join("+") };
  }
  const margin = income / revenue;
  const turnover = revenue / averageAssets;
  const multiplier = averageAssets / averageEquity;
  const dupontRoe = margin * turnover * multiplier;
  const directRoe = income / averageEquity;
  return { net_profit_margin: clean(margin), asset_turnover: clean(turnover), equity_multiplier: clean(multiplier), dupont_roe: clean(dupontRoe),
    direct_roe: clean(directRoe), identity_gap: clean(dupontRoe - directRoe), average_assets: clean(averageAssets), average_equity: clean(averageEquity),
    state: income < 0 ? "loss" : "calculated", reason: "three-factor-identity-reconciles" };
}

export function roicCalculation(input: Inputs): Output {
  const profit = numberValue("operating_profit", input.operating_profit);
  const taxRate = numberValue("normalized_tax_rate", input.normalized_tax_rate);
  if (taxRate < 0 || taxRate > 1) throw new RangeError("normalized_tax_rate must be between 0 and 1");
  const capitalBegin = numberValue("beginning_operating_assets", input.beginning_operating_assets) - numberValue("beginning_operating_liabilities", input.beginning_operating_liabilities);
  const capitalEnd = numberValue("ending_operating_assets", input.ending_operating_assets) - numberValue("ending_operating_liabilities", input.ending_operating_liabilities);
  const averageCapital = average(capitalBegin, capitalEnd);
  const nopat = profit * (1 - taxRate);
  if (averageCapital <= 0) return { nopat: clean(nopat), beginning_invested_capital: clean(capitalBegin), ending_invested_capital: clean(capitalEnd),
    average_invested_capital: clean(averageCapital), roic: null, state: "not-meaningful", reason: "nonpositive-average-invested-capital" };
  return { nopat: clean(nopat), beginning_invested_capital: clean(capitalBegin), ending_invested_capital: clean(capitalEnd),
    average_invested_capital: clean(averageCapital), roic: clean(nopat / averageCapital), state: nopat < 0 ? "loss" : "calculated",
    reason: "package-operating-capital-definition" };
}

export function cashConversionCycle(input: Inputs): Output {
  const revenue = numberValue("revenue", input.revenue);
  const cogs = numberValue("cost_of_goods_sold", input.cost_of_goods_sold);
  const days = positive("day_count", input.day_count);
  const receivablesBegin = numberValue("beginning_receivables", input.beginning_receivables);
  const receivablesEnd = numberValue("ending_receivables", input.ending_receivables);
  const inventoryBegin = numberValue("beginning_inventory", input.beginning_inventory);
  const inventoryEnd = numberValue("ending_inventory", input.ending_inventory);
  const payablesBegin = numberValue("beginning_trade_payables", input.beginning_trade_payables);
  const payablesEnd = numberValue("ending_trade_payables", input.ending_trade_payables);
  if (Math.min(receivablesBegin, receivablesEnd, inventoryBegin, inventoryEnd, payablesBegin, payablesEnd) < 0) throw new RangeError("working-capital balances must be nonnegative");
  const receivables = average(receivablesBegin, receivablesEnd);
  const inventory = average(inventoryBegin, inventoryEnd);
  const payables = average(payablesBegin, payablesEnd);
  if (revenue <= 0 || cogs <= 0) return { average_receivables: clean(receivables), average_inventory: clean(inventory), average_trade_payables: clean(payables),
    days_sales_outstanding: null, days_inventory_outstanding: null, days_payables_outstanding: null, cash_conversion_cycle_days: null,
    state: "not-meaningful", reason: revenue <= 0 ? "nonpositive-revenue" : "nonpositive-cogs" };
  const dso = receivables / revenue * days;
  const dio = inventory / cogs * days;
  const dpo = payables / cogs * days;
  const ccc = dio + dso - dpo;
  return { average_receivables: clean(receivables), average_inventory: clean(inventory), average_trade_payables: clean(payables),
    days_sales_outstanding: clean(dso), days_inventory_outstanding: clean(dio), days_payables_outstanding: clean(dpo),
    cash_conversion_cycle_days: clean(ccc), state: ccc < 0 ? "negative-cycle" : "calculated", reason: "dio-plus-dso-minus-dpo" };
}

export function interestCoverageRatio(input: Inputs): Output {
  const ebit = numberValue("ebit", input.ebit);
  const interest = numberValue("gross_interest_expense", input.gross_interest_expense);
  if (interest <= 0) return { ebit: clean(ebit), gross_interest_expense: clean(interest), interest_coverage_ratio: null,
    coverage_shortfall: null, state: "not-meaningful", reason: "nonpositive-gross-interest-expense" };
  const ratio = ebit / interest;
  return { ebit: clean(ebit), gross_interest_expense: clean(interest), interest_coverage_ratio: clean(ratio),
    coverage_shortfall: clean(Math.max(0, interest - ebit)), state: ratio >= 1 ? "covered" : "not-covered",
    reason: "ebit-divided-by-gross-interest-expense" };
}

export function netDebtToEbitda(input: Inputs): Output {
  const income = numberValue("net_income", input.net_income);
  const interest = numberValue("interest_expense", input.interest_expense);
  const tax = numberValue("income_tax_expense", input.income_tax_expense);
  const da = numberValue("depreciation_and_amortization", input.depreciation_and_amortization);
  const currentDebt = numberValue("current_borrowings", input.current_borrowings);
  const noncurrentDebt = numberValue("noncurrent_borrowings", input.noncurrent_borrowings);
  const leases = numberValue("lease_liabilities", input.lease_liabilities);
  const cash = numberValue("cash_and_cash_equivalents", input.cash_and_cash_equivalents);
  const includeLeases = booleanValue("include_lease_liabilities", input.include_lease_liabilities);
  if (Math.min(interest, da, currentDebt, noncurrentDebt, leases, cash) < 0) throw new RangeError("debt, cash, interest, and D&A inputs must be nonnegative");
  const ebitda = income + interest + tax + da;
  const grossDebt = currentDebt + noncurrentDebt + (includeLeases ? leases : 0);
  const netDebt = grossDebt - cash;
  if (ebitda <= 0) return { ebitda: clean(ebitda), gross_debt: clean(grossDebt), net_debt: clean(netDebt), net_debt_to_ebitda: null,
    lease_policy: includeLeases ? "included" : "excluded", state: "not-meaningful", reason: "nonpositive-ebitda" };
  return { ebitda: clean(ebitda), gross_debt: clean(grossDebt), net_debt: clean(netDebt), net_debt_to_ebitda: clean(netDebt / ebitda),
    lease_policy: includeLeases ? "included" : "excluded", state: netDebt < 0 ? "net-cash" : "net-debt", reason: "net-debt-divided-by-ebitda" };
}

interface Item { label: string; value: number }
interface Period { period: string; items: Item[] }

export function commonSizeStatements(input: Inputs): Output {
  const statementType = input.statement_type;
  if (statementType !== "income" && statementType !== "balance") throw new RangeError("statement_type must be 'income' or 'balance'");
  const baseLabel = input.base_label;
  const focusLabel = input.focus_label;
  if (typeof baseLabel !== "string" || !baseLabel.trim()) throw new RangeError("base_label must be a nonempty string");
  if (typeof focusLabel !== "string" || !focusLabel.trim()) throw new RangeError("focus_label must be a nonempty string");
  if (!Array.isArray(input.periods) || input.periods.length < 2) throw new RangeError("periods must contain at least two period objects");
  let expectedLabels: string[] | null = null;
  let previousFocus: number | null = null;
  const normalizedPeriods = (input.periods as unknown[]).map((rawPeriod, periodIndex) => {
    if (!rawPeriod || typeof rawPeriod !== "object") throw new RangeError(`periods[${periodIndex}] must be an object`);
    const period = rawPeriod as Record<string, unknown>;
    if (typeof period.period !== "string" || !period.period.trim()) throw new RangeError(`periods[${periodIndex}].period must be a nonempty string`);
    if (!Array.isArray(period.items) || period.items.length < 4) throw new RangeError(`periods[${periodIndex}].items must contain at least four rows`);
    const seen = new Set<string>();
    const parsed = (period.items as unknown[]).map((rawItem, itemIndex): Item => {
      if (!rawItem || typeof rawItem !== "object") throw new RangeError(`periods[${periodIndex}].items[${itemIndex}] must be an object`);
      const item = rawItem as Record<string, unknown>;
      if (typeof item.label !== "string" || !item.label.trim() || seen.has(item.label)) throw new RangeError("statement item labels must be nonempty and unique within a period");
      seen.add(item.label);
      return { label: item.label, value: numberValue(`${period.period}.${item.label}`, item.value) };
    });
    const labels = parsed.map((item) => item.label);
    if (expectedLabels === null) expectedLabels = labels;
    else if (labels.join("\u0000") !== expectedLabels.join("\u0000")) throw new RangeError("every period must use the same ordered line-item labels");
    const values = Object.fromEntries(parsed.map((item) => [item.label, item.value]));
    if (!(baseLabel in values) || !(focusLabel in values)) throw new RangeError("base_label and focus_label must exist in every period");
    const base = values[baseLabel];
    if (base === 0 || (statementType === "balance" && base < 0)) throw new RangeError("the common-size base must be nonzero and positive for balance sheets");
    const focusPercentage = values[focusLabel] / base * 100;
    const result = { period: (period.period as string).trim(), base_value: clean(base), focus_percentage: clean(focusPercentage),
      focus_change_pp: previousFocus === null ? null : clean(focusPercentage - previousFocus),
      items: parsed.map((item) => ({ label: item.label, value: clean(item.value), common_size_pct: clean(item.value / base * 100) })) };
    previousFocus = focusPercentage;
    return result;
  });
  const latest = normalizedPeriods[normalizedPeriods.length - 1];
  return { statement_type: statementType, base_label: baseLabel, focus_label: focusLabel, period_count: normalizedPeriods.length,
    line_item_count: normalizedPeriods[0].items.length, focus_percentage: latest.focus_percentage, focus_change_pp: latest.focus_change_pp,
    periods: normalizedPeriods, state: "calculated", reason: "signed-line-items-divided-by-period-base" };
}

export function calculate(topicId: string, input: Inputs): Output {
  if (!input || typeof input !== "object" || Array.isArray(input)) throw new RangeError("inputs must be an object");
  switch (topicId) {
    case "D18-F01-A01": return dupontDecomposition(input);
    case "D18-F01-A02": return roicCalculation(input);
    case "D18-F01-A03": return cashConversionCycle(input);
    case "D18-F01-A04": return interestCoverageRatio(input);
    case "D18-F01-A05": return netDebtToEbitda(input);
    case "D18-F01-A06": return commonSizeStatements(input);
    default: throw new RangeError(`unsupported topic_id: ${topicId}`);
  }
}
Full-height labplaygroundOpen full screen