Library/Fundamental Analysis and Valuation/Statement Ratios/ROIC Calculation

D18-F01-A02 / Complete engineering topic

ROIC Calculation

Build an explicit NOPAT and average-invested-capital bridge, then refuse false comparability across other ROIC conventions.

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

Build an explicit NOPAT and average-invested-capital bridge, then refuse false comparability across other ROIC conventions.

The decision this tutorial makes visible

ROIC can connect operations to capital deployment, but companies and datasets often disagree about cash, goodwill, leases, tax, and averaging.

The precise question is: How much after-tax operating profit did the selected operating capital base generate?

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

Pair an operating numerator with an operating capital denominator. The ratio is not meaningful when average invested capital is nonpositive.

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

Package ROIC = operating profit × (1 − normalized tax rate) / average(operating assets − non-interest-bearing operating liabilities). Financing-source and issuer variants are comparisons.

VariantDefinitionBest useMain limitation
Operating-capital packageNOPAT / average(OA − OL)Operating performance analysisMapping judgment is material
Financing-source bridgeNOPAT / average(debt + equity − non-operating cash)Cross-check under clean mappingGoodwill, leases, pensions, and cash policies differ
Issuer-defined ROICFiled numerator and capital reconciliationUnderstanding management's measureNot automatically peer-comparable

What is sourced, selected, synthetic, and derived

RoleMaterial claimEvidenceBoundary
Sourced factAfter-tax operating income should be paired with operating assets or invested capital, with cash excluded under the cited return-on-capital convention.S1 and supporting sourcesIt is an analytical convention rather than GAAP/IFRS and does not remove mapping judgment.
Implementation choicePackage ROIC = operating profit × (1 − normalized tax rate) / average(operating assets − non-interest-bearing operating liabilities). Financing-source and issuer variants are comparisons.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 operating profit 180 at a 25% normalized tax rate gives NOPAT 135. Operating capital moves from 650 to 730, so average capital is 690 and ROIC is 19.5652%.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
NOPAT = Operating Profit × (1 − t); IC = Operating Assets − Operating Liabilities; ROIC = NOPAT / average(IC_begin, IC_end)
SymbolMeaningUnitPolicy
OPOperating profitcurrency/periodMapped package subtotal
tNormalized tax ratedecimal0 ≤ t ≤ 1
OAOperating assetscurrency at dateExcludes mapped financing/non-operating assets
OLNon-interest-bearing operating liabilitiescurrency at dateSubtracted from operating assets
  • 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. Map operating assets and non-interest-bearing operating liabilities.
  2. Calculate NOPAT using the declared normalized tax rate.
  3. Average beginning and ending operating capital.
  4. Return ROIC or a nonpositive-capital diagnostic.

Production-minded operational checklist

  1. Freeze operating subtotal
  2. Document tax normalization
  3. Map every operating asset/liability
  4. Reconcile to financing-source capital

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, roic, is 0.195652173913. The complete input and output are in datasets/canonical-input.json and datasets/expected-output.json.

Synthetic operating profit 180 at a 25% normalized tax rate gives NOPAT 135. Operating capital moves from 650 to 730, so average capital is 690 and ROIC is 19.5652%.

Counterfactual checkpoint

Same NOPAT, different capital. Hold operating profit and tax constant while expanding mapped operating capital. The output changes because The denominator records the resources tied up in operations.

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 operating-profit sweepStep 30 · canonical fixtureMove operating profit through the canonical NOPAT.calculated19.57%calculated · package-operating-capital-definition1
Tax-policy sensitivityStep 30 · canonical fixtureChange the normalized tax rate.calculated19.57%calculated · package-operating-capital-definition1
Operating-asset expansionStep 30 · comparison focusIncrease operating assets while profit stays fixed.calculated18.24%calculated · package-operating-capital-definition1
Operating-liability fundingStep 30 · comparison focusIncrease non-interest-bearing operating liabilities.calculated21.60%calculated · package-operating-capital-definition1
Operating lossStep 30 · comparison focusMove NOPAT through zero.loss-7.61%loss · package-operating-capital-definition2
Zero-capital boundaryStep 30 · comparison focusCross nonpositive average invested capital.calculated60.00%calculated · package-operating-capital-definition2
Beginning/end capital asymmetryStep 30 · comparison focusChange the ending capital balance and averaging effect.calculated18.24%calculated · package-operating-capital-definition1

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

ROIC Calculation 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

ROIC Calculation learning promise

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

ROIC Calculation formula anatomy

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

ROIC Calculation independently auditable worked example

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

ROIC Calculation variant comparison

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

ROIC Calculation 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:

  • tax rate outside [0,1] — Reject, because The normalized rate contract is malformed.
  • average invested capital <= 0 — Return not-meaningful, because The ratio is unstable or sign-reversing.
  • operating and financing bridges disagree — Retain a reconciliation gap, because A mapping choice is missing.

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:

  • NOPAT uses the same operating-profit and tax convention across periods.
  • Average invested capital equals the average of beginning and ending operating capital.
  • ROIC is null—not infinite or precise—when average invested capital is nonpositive.
  • 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 Damodaran financial-measure definitions, SEC non-GAAP C&DIs, Caterpillar filed ROIC reconciliation, SEC EDGAR XBRL APIs. 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?

Package ROIC = operating profit × (1 − normalized tax rate) / average(operating assets − non-interest-bearing operating liabilities). Financing-source and issuer variants are comparisons. 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: average invested capital equals zero. 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 ROIC Calculation into Cash Conversion Cycle only after carrying forward the fact-mapping ledger and resetting method-specific numerator and denominator choices. The learning flow is: DuPont Decomposition → ROIC Calculation → Cash Conversion Cycle. Carry the result forward only with its scope, clock, state, and evidence label.

ROIC Calculation calculation flow

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

Rendering system map…

Takeaway: ROIC is only comparable after its operating-profit, tax, cash, lease, goodwill, liability, and averaging policies match.

ReferencesPrimary sources and evidence notes

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

S1 — Financial Measures and Ratios

  • Organization or authors: Aswath Damodaran, New York University Stern School of Business
  • Source type: Authoritative academic methodology
  • Publication or effective date: current
  • Version: Accessed 2026-08-04
  • URL or DOI: https://pages.stern.nyu.edu/~adamodar/New_Home_Page/definitions.html
  • Accessed: 2026-08-04
  • Jurisdiction: General corporate-finance analysis
  • Supports: After-tax operating income should be paired with operating assets or invested capital, with cash excluded under the cited return-on-capital convention.
  • Limitations: It is an analytical convention rather than GAAP/IFRS and does not remove mapping judgment.

S2 — Non-GAAP Financial Measures: Compliance and Disclosure Interpretations

  • Organization or authors: SEC Division of Corporation Finance
  • Source type: Official regulator staff guidance
  • Publication or effective date: 2022-12-13
  • Version: Questions 100, 102, and 103
  • URL or DOI: https://www.sec.gov/rules-regulations/staff-guidance/corporation-finance-interpretations/non-gaap-financial-measures
  • Accessed: 2026-08-04
  • Jurisdiction: United States securities disclosure
  • Supports: Non-GAAP measures can mislead when labels or adjustments obscure their construction; SEC staff describes EBIT and EBITDA from GAAP net income and expects reconciliation and comparable-GAAP prominence.
  • Limitations: It governs issuer disclosure; it does not make one analytical ROIC, net-debt, or leverage convention universal.

S3 — Reconciliation of Non-GAAP Financial Measures — Return on Invested Capital

  • Organization or authors: Caterpillar Inc.
  • Source type: Filed entity exhibit hosted by SEC EDGAR
  • Publication or effective date: 2005
  • Version: SEC accession 0000018230-05-000689, Exhibit 99.2
  • URL or DOI: https://www.sec.gov/Archives/edgar/data/18230/000001823005000689/ex99-2.htm
  • Accessed: 2026-08-04
  • Jurisdiction: United States issuer disclosure
  • Supports: The filing defines ROIC as NOPAT divided by average invested capital and states that similarly titled measures may not be comparable.
  • Limitations: It validates variability and disclosure boundaries, not this package's synthetic numbers or a universal formula.

S4 — 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.

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