Library/Fundamental Analysis and Valuation/Integrated Equity Scoring/Point-in-Time Stock-Scoring Input Assembly

D18-F09-A01 / Complete engineering topic

Point-in-Time Stock-Scoring Input Assembly

Assemble a reproducible stock-scoring input record without allowing later restatements, future filings, or mixed units to leak into an earlier decision.

Point-in-Time Stock-Scoring Input Assembly maps point-in-time fundamentals to an auditable stock-scoring decisionD18 / D18-F09

Assemble a reproducible stock-scoring input record without allowing later restatements, future filings, or mixed units to leak into an earlier decision.

The decision this tutorial makes visible

Point-in-Time Stock-Scoring Input Assembly matters because an integrated stock screen is only useful when every input, peer, model variant, weight, and abstention reason can be audited at the same knowledge timestamp.

The precise question is: Which original filing facts are eligible for a stock score at a declared knowledge cutoff?

A practitioner needs to know what the diagnostic does and does not justify. A builder needs a contract that can be reproduced from the same point-in-time inputs in Python, TypeScript, a visual, and a browser lab.

Intuition before notation

Assembly is a gate before scoring: a plausible value is not usable if its publication time, revision, period, currency, or scale is wrong.

The result depends on the declared algorithm scope, input clocks, units, equality and rounding policies, and unsupported-state treatment. Change one of those and the output represents a different decision even when its field name is unchanged.

Scope and nearby methods

The canonical assembler selects original, period-aligned facts whose availability date is on or before the knowledge cutoff and whose currency and scale match the requested contract.

VariantDefinitionBest useMain limitation
Original point-in-time assemblySelect original aligned facts known by a cutoffBacktests and dated research snapshotsRequires a complete fact ledger
Latest-restated assemblyUse the newest available restatementCurrent descriptive reportingNot valid for earlier decision simulation
Provider snapshotUse a provider's normalized latest recordOperational data productsNot interchangeable without the provider contract

What is sourced, selected, synthetic, and derived

RoleMaterial claimEvidenceBoundary
Sourced factThe named accounting, statistical, or historical model context is limited to the cited source role.SEC XBRL guideThe source does not validate the synthetic fixture or current calibration.
Author-derived calculationcompleteness = accepted_required_fields / required_fieldscanonical-input.json, expected-output.json, and independent arithmeticSynthetic teaching record under this package contract.
Implementation choiceWeights, caps, thresholds, peer rules, and abstention gates are explicit package choices.Frozen definition contract and data contractNot a universal rating, probability, or investment conclusion.

The authoritative sources support only the exact facts named in the claim ledger. They do not certify the synthetic numbers in this tutorial. The repository fixture is deliberately invented for auditability, and the displayed output is author-derived under the selected implementation choice.

Formula, symbols, and numerical policy

Plain text
completeness = accepted_required_fields / required_fields
SymbolMeaningUnitPolicy
Kknowledge cutoffdateavailability_at must be no later than K
Aaccepted field countcountOriginal aligned facts only
Rrequired field countcountNonempty contract list
  • Use full floating-point precision and round only for display.
  • Scores are bounded to 0–100 only where the input contract explicitly says so.
  • Reject missing, nonfinite, malformed, mixed-period, unsupported, and contradictory records rather than manufacturing defaults.
  • Keep the raw components, weights, thresholds, clock, and diagnostic state with every result.

Read the formula in the same order as the algorithm. Validate identity, ordering, units, and supported state first. Apply the selected equality and window 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 the cutoff, period, currency, scale, required fields, and fact list.
  2. Reject facts published after the cutoff or carrying the wrong period, revision, currency, or scale.
  3. Select the latest eligible original fact per required field.
  4. Report completeness and a ready/incomplete state before downstream scoring.

Production-minded operational checklist

  1. Freeze the knowledge cutoff and source ownership
  2. Resolve population and model applicability before calculating
  3. Retain components, weights, and evidence coverage beside the headline
  4. Abstain or route when the contract is not satisfied

Stop when a source clock, peer membership, model population, weight, or missing-data rule is unavailable; do not silently complete the score.

Worked synthetic example

The canonical fixture is synthetic teaching data, not an observed control event, filing fact, or portfolio decision. Its primary author-derived output, completeness, is four accepted fields, completeness = 1.000, and ready = true. The complete input and output are in datasets/canonical-input.json and datasets/expected-output.json.

All four synthetic facts were filed before 2025-03-31 with the requested period, USD millions scale, and original revision, so completeness is 1.0 and the record is ready.

Counterfactual checkpoint

One-driver integration stress. Move one declared input or rule boundary while holding the remaining synthetic record fixed. The output changes because the visible component or gate changes, not through an unexplained hidden adjustment.

The structured result retains state and diagnostics in addition to the primary number. That makes the calculation independently reviewable and prevents a incomplete, rejected, or abstained score state 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 filing bundleStep 30 · canonical fixtureDriver: all required facts. All required facts are aligned and available. Predict first: Will the assembled record be ready?ready100.0% complete4 accepted · 0 rejected1
Cutoff equalityStep 30 · canonical fixtureDriver: revenue.available_at. Move one fact exactly onto the knowledge boundary. Predict first: Does availability exactly on the cutoff remain eligible?ready100.0% complete4 accepted · 0 rejected1
Late filingStep 30 · comparison focusDriver: revenue.available_at. Push revenue after the cutoff. Predict first: Will one future fact make the record incomplete?incomplete75.0% complete3 accepted · 1 rejected1
Restated factStep 30 · comparison focusDriver: net_income.revision. Mark one fact as revised and observe the rejection ledger. Predict first: Does a revised fact replace the original?incomplete75.0% complete3 accepted · 1 rejected1
Wrong periodStep 30 · comparison focusDriver: total_assets.period_end. Shift one fact to a different period. Predict first: Will a period mismatch be rejected?incomplete75.0% complete3 accepted · 1 rejected1
Wrong scaleStep 30 · comparison focusDriver: operating_cash_flow.scale. Change one fact from millions to thousands. Predict first: Can a unit mismatch pass unnoticed?incomplete75.0% complete3 accepted · 1 rejected1
Missing required fieldStep 30 · comparison focusDriver: facts. Remove one required fact from the assembled bundle. Predict first: What happens when one required field disappears?incomplete75.0% complete3 accepted · 1 rejected1

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

Point-in-Time Stock-Scoring Input Assembly 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.

Implementation walkthrough

The Python and TypeScript references begin with the same validation contract, reject malformed and unsupported state before calculation, preserve declared ordering and rounding policies, and return structured diagnostics rather than one context-free number.

The main implementation branches are:

  • all required facts accepted — ready, because downstream score inputs are complete.
  • one or more required facts rejected — incomplete, because the score should route or abstain.
  • mixed currency, scale, period, or revision — reject the fact, because alignment is part of the contract.

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:

  • completeness equals accepted_fields divided by required_fields.
  • A fact after the knowledge cutoff cannot be accepted.
  • A ready record has no rejected required field.
  • Retain the method identifier, source clock, intermediate components, and final diagnostic beside the headline.

Passing the tests proves the frozen assembly, routing, arithmetic, and parity contract. It does not validate live-market performance or a current issuer decision.

Failure modes and misuse

  • A transparent composite remains dependent on the source models, population, weights, peer set, and accounting mapping.
  • A high or low score is a research-screen state, not a rating, audit conclusion, fraud finding, default forecast, or investment recommendation.
  • Implementation fidelity does not establish current-population calibration, causality, predictive accuracy, or profitability.

Debugging order

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

  1. Confirm the as-of and revision clock.
  2. Confirm the eligible population and selected model variant.
  3. Confirm units, signs, peer direction, weights, caps, and thresholds.
  4. Reconcile every component and abstention reason before interpreting the headline.

Evidence and historical boundary

Historical decision: not useful. A named issuer case is not useful for the canonical orchestration arithmetic without a reproducible point-in-time filing bundle, peer-membership snapshot, model-population eligibility decision, adjustment basis, and redistribution permission. The family therefore uses clearly labeled synthetic records and cites the original model papers for definition history.

The primary sources are SEC XBRL guide, SEC statements guide, IFRS framework. They support the source roles listed in the research ledger, not a redistributable historical observation, a private participant decision, production conformance certification, execution-quality result, profitability claim, or prediction claim.

Summary and next topic

You can now calculate, audit, and bound Point-in-Time Stock-Scoring Input Assembly before continuing to Stock-Scoring Peer Cohort Resolver. The learning flow is: Point-in-Time Financial Inputs → Point-in-Time Stock-Scoring Input Assembly → Stock-Scoring Peer Cohort Resolver. Carry the result forward only with its scope, clock, state, and evidence label.

Level 2 learning check

This additive check keeps the original calculation and example unchanged. Use the studio in four passes:

PassLearner questionEvidence to inspect
Predictwhich fact makes the record incompleteThe active scenario prompt and driver
InspectWhat changed first?accepted/rejected fact ledger
ReconcileCan the visible intermediate explain the headline?The component, route, peer, confidence, or migration ledger
BoundIs the result safe to interpret?the knowledge cutoff and exact period/currency/scale match and the evidence clock

The output is a research-screen state, not a rating, audit conclusion, default forecast, fraud finding, or investment recommendation. Continue to Stock-Scoring Peer Cohort Resolver only after retaining the scope, clock, state, and evidence label.

Integration visual atlas

Integration pipeline anatomy

Gate and interpretation ceiling

Point-in-time evidence clock

Variant and misuse boundaries

Use the atlas to follow Assemble → Resolve → Normalize → Explain, then inspect the evidence clock and boundary map before interpreting the headline.

Use the integration studio

  1. Read the prediction prompt and name the expected direction before moving the driver.
  2. Compare the scenario base with the current state and changed-input summary.
  3. Reconcile the visible intermediate (accepted/rejected fact ledger) to the headline.
  4. Apply the boundary and evidence clock: the knowledge cutoff and exact period/currency/scale match.

Point-in-Time Stock-Scoring Input Assembly calculation flow

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

Rendering system map…

Takeaway: The score cannot be more point-in-time than its least-available required input.

ReferencesPrimary sources and evidence notes

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

S1 — EDGAR XBRL Guide

  • Organization or authors: U.S. Securities and Exchange Commission
  • Source type: Official filing specification
  • Publication or effective date: 2026-06-29
  • Version: Source edition or current web page
  • URL or DOI: https://www.sec.gov/file/xbrl-guide
  • Accessed: 2026-08-09
  • Jurisdiction: U.S. EDGAR filings
  • Supports: XBRL facts carry period and unit information that must be mapped before score assembly.
  • Limitations: A filing fact's presence does not by itself prove the package's economic mapping, availability cutoff, or model eligibility.

S2 — Beginners' Guide to Financial Statements

  • Organization or authors: U.S. Securities and Exchange Commission
  • Source type: Official regulator publication
  • Publication or effective date: 2014-01-12
  • Version: Source edition or current web page
  • URL or DOI: https://www.sec.gov/about/reports-publications/beginners-guide-financial-statements
  • Accessed: 2026-08-09
  • Jurisdiction: United States public-company reporting
  • Supports: Balance sheets show stocks at a fixed point while income and cash-flow statements describe periods; the statements must be read together.
  • Limitations: It does not prescribe this family's scores, weights, peer rules, or current-company conclusion.

S3 — Conceptual Framework for Financial Reporting

Evidence boundary

Primary sources establish statement context, statistical conventions, or the historical source-model boundary. They do not certify the repository-authored weights, synthetic universe, current calibration, or issuer conclusion.

d18f09-core.ts
/** Canonical TypeScript parity implementation for D18-F09. */

type RecordLike = Record<string, any>;
const finite = (value: any, name: string): number => {
  if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError(`${name} must be a finite number`);
  return value;
};
const positive = (data: RecordLike, name: string): number => {
  const value = finite(data[name], name);
  if (value <= 0) throw new RangeError(`${name} must be positive`);
  return value;
};
const nonnegative = (data: RecordLike, name: string): number => {
  const value = finite(data[name], name);
  if (value < 0) throw new RangeError(`${name} must be nonnegative`);
  return value;
};
const isoDate = (value: any, name: string): string => {
  if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) throw new TypeError(`${name} must be YYYY-MM-DD`);
  return value;
};
const clamp = (value: number, low = 0, high = 100): number => Math.max(low, Math.min(high, value));
const score = (data: RecordLike, name: string): number => {
  const value = finite(data[name], name);
  if (value < 0 || value > 100) throw new RangeError(`${name} must be between 0 and 100`);
  return value;
};
const band = (value: number): string => value >= 75 ? "strong" : value >= 50 ? "watch" : "weak";

function assemble(data: RecordLike): RecordLike {
  const cutoff = isoDate(data.knowledge_cutoff, "knowledge_cutoff");
  const periodEnd = isoDate(data.period_end, "period_end");
  if (typeof data.currency !== "string" || data.currency.length === 0) throw new TypeError("currency must be nonempty text");
  if (typeof data.scale !== "string" || data.scale.length === 0) throw new TypeError("scale must be nonempty text");
  if (!Array.isArray(data.required_fields) || data.required_fields.length === 0 || data.required_fields.some((x: any) => typeof x !== "string" || x.length === 0)) throw new TypeError("required_fields must be a nonempty string list");
  if (!Array.isArray(data.facts)) throw new TypeError("facts must be a list");
  const accepted: RecordLike = {};
  const rejected: RecordLike[] = [];
  for (const field of data.required_fields as string[]) {
    const candidates = (data.facts as any[]).filter((fact: any) => fact && fact.field === field && typeof fact.value === "number" && Number.isFinite(fact.value) && typeof fact.available_at === "string" && typeof fact.period_end === "string" && fact.available_at <= cutoff && fact.period_end === periodEnd && fact.currency === data.currency && fact.scale === data.scale && fact.revision === "original");
    if (candidates.length) {
      candidates.sort((a: any, b: any) => a.available_at.localeCompare(b.available_at));
      accepted[field] = candidates[candidates.length - 1].value;
    } else rejected.push({ field, reason: "no original, aligned fact available by the knowledge cutoff" });
  }
  const completeness = Object.keys(accepted).length / data.required_fields.length;
  return { state: completeness === 1 ? "ready" : "incomplete", method: "point-in-time-filing-fact-assembly", as_of: data.knowledge_cutoff, accepted_fields: Object.keys(accepted).sort(), selected_values: accepted, rejected_fields: rejected, completeness, ready: completeness === 1, clock_policy: "availability_at <= knowledge_cutoff; original revision; exact period/currency/scale" };
}

function cohort(data: RecordLike): RecordLike {
  const asOf = isoDate(data.as_of, "as_of");
  if (typeof data.target_id !== "string" || typeof data.target_sector !== "string" || typeof data.target_country !== "string") throw new TypeError("target identifiers and cohort dimensions must be text");
  const minCap = nonnegative(data, "min_market_cap");
  const minPeers = Math.trunc(positive(data, "min_peers"));
  if (!Array.isArray(data.universe) || data.universe.length === 0) throw new TypeError("universe must be a nonempty list");
  const target = (data.universe as any[]).find((row: any) => row && row.id === data.target_id);
  if (!target) throw new RangeError("target_id must occur in universe");
  const targetCap = positive(target, "market_cap");
  const eligible: any[] = [];
  const exclusions: RecordLike[] = [];
  for (const row of data.universe as any[]) {
    if (!row || row.id === data.target_id) continue;
    let reason: string | null = null;
    let cap = 0;
    try { cap = positive(row, "market_cap"); isoDate(row.available_at, "universe.available_at"); } catch { reason = "invalid market-cap or availability fact"; }
    if (!reason && row.available_at > asOf) reason = "not available at as-of date";
    else if (!reason && row.listed !== true) reason = "not listed under the selected scope";
    else if (!reason && row.sector !== data.target_sector) reason = "sector mismatch";
    else if (!reason && row.country !== data.target_country) reason = "country mismatch";
    else if (!reason && cap < minCap) reason = "below market-cap floor";
    if (reason) exclusions.push({ id: String(row?.id ?? "?"), reason }); else eligible.push(row);
  }
  eligible.sort((a, b) => Math.abs(a.market_cap - targetCap) - Math.abs(b.market_cap - targetCap) || String(a.id).localeCompare(String(b.id)));
  const ids = eligible.map(row => String(row.id));
  return { state: ids.length >= minPeers ? "resolved" : "abstain", method: "point-in-time-sector-country-market-cap-cohort", as_of: data.as_of, eligible_ids: ids, cohort_count: ids.length, minimum_required: minPeers, coverage: ids.length / minPeers, exclusions, tie_break: "absolute market-cap distance, then stable entity id" };
}

function normalize(data: RecordLike): RecordLike {
  if (!Array.isArray(data.metrics) || data.metrics.length === 0) throw new TypeError("metrics must be a nonempty list");
  const minPeers = Math.trunc(positive(data, "min_peers"));
  const results: RecordLike[] = [];
  for (const metric of data.metrics as any[]) {
    if (!metric || typeof metric.name !== "string" || !Array.isArray(metric.peers)) throw new TypeError("each metric needs name and peers");
    const target = finite(metric.target, "target");
    const peers = metric.peers.filter((value: any) => typeof value === "number" && Number.isFinite(value)) as number[];
    if (peers.length < minPeers) throw new RangeError(`${metric.name} has fewer than min_peers observations`);
    const equal = peers.filter(value => Math.abs(value - target) <= 1e-12).length;
    const less = peers.filter(value => value < target - 1e-12).length;
    const percentile = 100 * (less + 0.5 * equal) / peers.length;
    if (typeof metric.higher_is_better !== "boolean") throw new TypeError(`${metric.name}.higher_is_better must be boolean`);
    const normalized = metric.higher_is_better ? percentile : 100 - percentile;
    results.push({ name: metric.name, target, peer_count: peers.length, percentile, higher_is_better: metric.higher_is_better, normalized_score: normalized });
  }
  const aggregate = results.reduce((sum, row) => sum + row.normalized_score, 0) / results.length;
  return { state: "calculated", method: "midrank-empirical-percentile-with-direction", metrics: results, aggregate_score: aggregate, metric_count: results.length, invariant: "higher-is-better reverses the percentile only; peer values are not z-scored" };
}

const modelRules: Record<string, { population: string; eligible: (target: RecordLike) => boolean; variant: string }> = {
  altman_z_original: { population: "public industrial manufacturer", eligible: t => t.is_public === true && t.sector === "industrial" && Number(t.market_cap) > 0, variant: "Use D18-F04-A01 original public-manufacturer coefficients" },
  piotroski_f: { population: "non-financial issuer with two annual periods", eligible: t => !["bank", "insurance", "reit", "utility"].includes(t.sector) && Number(t.annual_periods) >= 2, variant: "Use D18-F04-A02 nine-signal contract" },
  beneish_m: { population: "non-financial issuer with two annual periods", eligible: t => !["bank", "insurance", "reit", "utility"].includes(t.sector) && Number(t.annual_periods) >= 2, variant: "Use D18-F04-A03 eight-index contract" },
  ohlson_o: { population: "industrial public research screen", eligible: t => t.is_public === true && Number(t.annual_periods) >= 2, variant: "Use D18-F04-A05 Model 1 convention" },
  dividend_safety: { population: "issuer with declared dividend and cash-flow facts", eligible: t => t.dividends_known === true && Number(t.annual_periods) >= 1, variant: "Use D18-F09-A07 payout contract" },
  balance_sheet_resilience: { population: "issuer with aligned balance-sheet and coverage facts", eligible: t => Number(t.total_assets) > 0 && ["US-GAAP", "IFRS"].includes(t.framework), variant: "Use D18-F09-A08 resilience contract" },
};

function route(data: RecordLike): RecordLike {
  if (!data.target || typeof data.target !== "object" || !Array.isArray(data.requested_models) || data.requested_models.length === 0) throw new TypeError("target and requested_models are required");
  const routes: RecordLike[] = [];
  for (const model of data.requested_models as any[]) {
    if (typeof model !== "string") throw new TypeError("requested model names must be text");
    const rule = modelRules[model];
    if (!rule) { routes.push({ model, status: "unsupported", variant: null, reason: "no frozen rule for this model label" }); continue; }
    const eligible = rule.eligible(data.target);
    routes.push({ model, status: eligible ? "eligible" : "reroute", variant: eligible ? rule.variant : null, population: rule.population, reason: eligible ? "all required scope facts pass" : `target does not meet ${rule.population} contract` });
  }
  const eligibleCount = routes.filter(row => row.status === "eligible").length;
  return { state: eligibleCount ? "routed" : "abstain", method: "explicit-model-applicability-router", routes, eligible_count: eligibleCount, requested_count: routes.length, coverage: eligibleCount / routes.length };
}

function weighted(data: RecordLike, names: string[], weights: number[], output: string, method: string): RecordLike {
  const values = names.map(name => score(data, name));
  const components: RecordLike = {}, weightMap: RecordLike = {}, contributions: RecordLike = {};
  names.forEach((name, i) => { components[name] = values[i]; weightMap[name] = weights[i]; contributions[name] = values[i] * weights[i]; });
  const total = Object.values(contributions).reduce((sum: number, value: any) => sum + value, 0);
  return { state: "calculated", method, components, weights: weightMap, contributions, [output]: total, band: band(total), coverage: 1 };
}
const health = (data: RecordLike): RecordLike => weighted(data, ["profitability_score", "cash_flow_score", "liquidity_score", "leverage_score"], [0.3, 0.3, 0.2, 0.2], "financial_health_score", "accounting-financial-health-weighted-composite");
const earnings = (data: RecordLike): RecordLike => weighted(data, ["accrual_quality_score", "cash_conversion_score", "revenue_quality_score", "manipulation_safety_score"], [0.3, 0.25, 0.25, 0.2], "earnings_quality_score", "earnings-quality-weighted-composite");

function dividend(data: RecordLike): RecordLike {
  const dividends = positive(data, "dividends_paid");
  const fcf = finite(data.free_cash_flow, "free_cash_flow");
  const netIncome = finite(data.net_income, "net_income");
  const interestCoverage = nonnegative(data, "interest_coverage");
  const cash = nonnegative(data, "cash_and_equivalents");
  const components = { free_cash_flow_coverage: clamp(fcf / dividends / 2 * 100), earnings_coverage: clamp(netIncome / dividends / 2 * 100), interest_coverage: clamp(interestCoverage / 10 * 100), cash_buffer: clamp(cash / dividends / 4 * 100) };
  const weights = { free_cash_flow_coverage: 0.35, earnings_coverage: 0.25, interest_coverage: 0.2, cash_buffer: 0.2 };
  const contributions: RecordLike = {}; Object.keys(components).forEach(key => { contributions[key] = (components as any)[key] * (weights as any)[key]; });
  const value = Object.values(contributions).reduce((sum: number, item: any) => sum + item, 0);
  return { state: "calculated", method: "coverage-and-liquidity-dividend-safety", components, weights, contributions, dividend_safety_score: value, band: band(value), coverage_policy: "coverage is capped at two times and cash buffer at four times" };
}

function resilience(data: RecordLike): RecordLike {
  const currentAssets = positive(data, "current_assets"), currentLiabilities = positive(data, "current_liabilities"), totalDebt = positive(data, "total_debt"), ebitda = positive(data, "ebitda"), interest = positive(data, "interest_expense"), cash = nonnegative(data, "cash_and_equivalents"), due = nonnegative(data, "debt_due_12m");
  if (due > totalDebt) throw new RangeError("debt_due_12m cannot exceed total_debt");
  const components = { liquidity: clamp(currentAssets / currentLiabilities / 2 * 100), net_leverage: clamp((1 - (totalDebt - cash) / (4 * ebitda)) * 100), interest_coverage: clamp(ebitda / interest / 10 * 100), maturity_headroom: clamp((1 - due / totalDebt) * 100) };
  const weights = { liquidity: 0.3, net_leverage: 0.3, interest_coverage: 0.25, maturity_headroom: 0.15 };
  const contributions: RecordLike = {}; Object.keys(components).forEach(key => { contributions[key] = (components as any)[key] * (weights as any)[key]; });
  const value = Object.values(contributions).reduce((sum: number, item: any) => sum + item, 0);
  return { state: "calculated", method: "liquidity-leverage-coverage-maturity-resilience", components, weights, contributions, balance_sheet_resilience_score: value, band: band(value) };
}

function ensemble(data: RecordLike): RecordLike {
  if (!Array.isArray(data.models) || data.models.length === 0) throw new TypeError("models must be a nonempty list");
  const eligible: [string, number, number][] = [];
  for (const model of data.models as any[]) {
    if (!model || typeof model !== "object") throw new TypeError("each model must be an object");
    if (model.eligible !== true) continue;
    const probability = finite(model.distress_probability, "distress_probability"), weight = positive(model, "weight");
    if (probability < 0 || probability > 1) throw new RangeError("distress_probability must be between zero and one");
    eligible.push([String(model.name ?? "model"), probability, weight]);
  }
  if (!eligible.length) throw new RangeError("at least one eligible model is required");
  const weightSum = eligible.reduce((sum, row) => sum + row[2], 0);
  const probability = eligible.reduce((sum, row) => sum + row[1] * row[2], 0) / weightSum;
  const variance = eligible.reduce((sum, row) => sum + row[2] * (row[1] - probability) ** 2, 0) / weightSum;
  const values = eligible.map(row => row[1]), dispersion = Math.max(...values) - Math.min(...values);
  return { state: "calculated", method: "weighted-distress-probability-ensemble", eligible_models: eligible.map(row => ({ name: row[0], probability: row[1], weight: row[2] })), model_count: eligible.length, distress_probability: probability, weighted_stddev: Math.sqrt(variance), disagreement_range: dispersion, agreement: 1 - dispersion, band: probability >= 0.66 ? "high-review" : probability >= 0.33 ? "watch" : "lower-review", calibration_boundary: "weighted aggregation preserves supplied probabilities; it does not recalibrate them" };
}

function conflict(data: RecordLike): RecordLike {
  if (!Array.isArray(data.components) || data.components.length === 0) throw new TypeError("components must be a nonempty list");
  const groupCap = finite(data.group_cap, "group_cap"), threshold = finite(data.conflict_threshold, "conflict_threshold");
  if (groupCap <= 0 || groupCap > 1 || threshold <= 0 || threshold > 100) throw new RangeError("group_cap must be in (0,1] and conflict_threshold in (0,100]");
  const groups = new Map<string, RecordLike[]>(); let rawWeight = 0, rawNumerator = 0;
  for (const item of data.components as any[]) {
    if (!item || typeof item.evidence_group !== "string") throw new TypeError("each component needs an evidence_group");
    const value = score(item, "score"), weight = positive(item, "weight"), group = item.evidence_group;
    if (!groups.has(group)) groups.set(group, []);
    groups.get(group)!.push({ name: String(item.name ?? "component"), score: value, weight }); rawWeight += weight; rawNumerator += value * weight;
  }
  const adjusted: RecordLike[] = [], conflicts: string[] = []; let denominator = 0, numerator = 0;
  [...groups.entries()].sort((a, b) => a[0].localeCompare(b[0])).forEach(([group, items]) => {
    const weight = items.reduce((sum, item) => sum + item.weight, 0), mean = items.reduce((sum, item) => sum + item.score * item.weight, 0) / weight, values = items.map(item => item.score), range = Math.max(...values) - Math.min(...values), isConflict = range >= threshold, capped = Math.min(weight, groupCap);
    if (isConflict) conflicts.push(group);
    adjusted.push({ group, raw_weight: weight, capped_weight: capped, mean_score: mean, range, conflict: isConflict }); denominator += capped; numerator += capped * mean;
  });
  const resolved = numerator / denominator, raw = rawNumerator / rawWeight;
  return { state: conflicts.length ? "conflict-detected" : "resolved", method: "evidence-group-cap-and-conflict-resolver", raw_score: raw, resolved_score: resolved, double_counting_adjustment: resolved - raw, groups: adjusted, conflict_groups: conflicts, conflict_count: conflicts.length, group_cap: groupCap, conflict_threshold: threshold };
}

const overall = (data: RecordLike): RecordLike => weighted(data, ["financial_health_score", "earnings_quality_score", "dividend_safety_score", "balance_sheet_resilience_score", "distress_safety_score", "valuation_score"], [0.24, 0.18, 0.14, 0.18, 0.16, 0.10], "overall_stock_score", "explainable-six-pillar-stock-score");

function confidence(data: RecordLike): RecordLike {
  const baseScore = score(data, "base_score"), required = Math.trunc(positive(data, "required_components")), available = Math.trunc(nonnegative(data, "available_components")), maxConflicts = Math.trunc(positive(data, "max_conflicts")), conflicts = Math.trunc(nonnegative(data, "conflict_count")), minimum = Math.trunc(positive(data, "minimum_components")), staleDays = nonnegative(data, "stale_days");
  if (available > required || conflicts > maxConflicts) throw new RangeError("available components or conflicts exceed their declared maxima");
  const coverage = available / required, missingPenalty = 1 - coverage, stalenessPenalty = Math.min(staleDays / 365, 1) * 0.2, conflictPenalty = Math.min(conflicts / maxConflicts, 1) * 0.2, confidenceValue = coverage * (1 - stalenessPenalty) * (1 - conflictPenalty), adjusted = clamp(baseScore - 15 * missingPenalty - 10 * conflictPenalty), abstain = available < minimum || confidenceValue < 0.6;
  return { state: abstain ? "abstain" : "usable-with-confidence", method: "coverage-staleness-conflict-confidence-gate", base_score: baseScore, coverage, missing_penalty: missingPenalty, staleness_penalty: stalenessPenalty, conflict_penalty: conflictPenalty, confidence: confidenceValue, adjusted_score: adjusted, abstain, reason: abstain ? "minimum component or confidence gate failed" : "coverage and confidence gates passed" };
}

function screen(data: RecordLike): RecordLike {
  if (!Array.isArray(data.universe) || data.universe.length === 0) throw new TypeError("universe must be a nonempty list");
  const floor = score(data, "screen_floor"), minConfidence = finite(data.min_confidence, "min_confidence"), minLiquidity = nonnegative(data, "min_liquidity");
  if (minConfidence < 0 || minConfidence > 1) throw new RangeError("min_confidence must be between zero and one");
  const selected: RecordLike[] = [], excluded: RecordLike[] = [];
  for (const row of data.universe as any[]) {
    if (!row || typeof row.id !== "string") throw new TypeError("each universe row needs an id");
    const rowScore = score(row, "score"), confidenceValue = finite(row.confidence, "confidence"), liquidity = nonnegative(row, "liquidity");
    if (confidenceValue < 0 || confidenceValue > 1) throw new RangeError("universe confidence must be between zero and one");
    const reasons: string[] = []; if (row.eligible !== true) reasons.push("not eligible"); if (rowScore < floor) reasons.push("below score floor"); if (confidenceValue < minConfidence) reasons.push("below confidence floor"); if (liquidity < minLiquidity) reasons.push("below liquidity floor");
    const candidate: RecordLike = { id: row.id, score: rowScore, confidence: confidenceValue, liquidity, sector: row.sector ?? "unspecified" };
    if (reasons.length) excluded.push({ ...candidate, reasons }); else selected.push(candidate);
  }
  selected.sort((a, b) => b.score - a.score || b.confidence - a.confidence || a.id.localeCompare(b.id));
  const ranked = selected.map((row, index) => ({ rank: index + 1, ...row }));
  return { state: selected.length ? "ranked" : "abstain", method: "eligibility-confidence-score-floor-ranking", ranked, screened_count: ranked.length, universe_count: data.universe.length, coverage: ranked.length / data.universe.length, score_floor: floor, confidence_floor: minConfidence, liquidity_floor: minLiquidity, excluded, tie_break: "score descending, confidence descending, stable id ascending" };
}

function history(data: RecordLike): RecordLike {
  if (!Array.isArray(data.history) || data.history.length < 2) throw new RangeError("history needs at least two points");
  const rows = (data.history as any[]).map(row => { if (!row || typeof row.components !== "object") throw new TypeError("each history row needs components"); return { date: isoDate(row.as_of, "history.as_of"), row }; });
  for (let index = 0; index + 1 < rows.length; index += 1) if (rows[index].date >= rows[index + 1].date) throw new RangeError("history must be strictly chronological");
  const previous = rows[rows.length - 2].row, latest = rows[rows.length - 1].row, previousScore = score(previous, "overall_score"), latestScore = score(latest, "overall_score");
  const keys = [...new Set([...Object.keys(previous.components), ...Object.keys(latest.components)])].sort();
  const deltas: RecordLike = {}; for (const key of keys) deltas[key] = score(latest.components, key) - score(previous.components, key);
  const topDriver = keys.reduce((best, key) => Math.abs(deltas[key]) > Math.abs(deltas[best]) || (Math.abs(deltas[key]) === Math.abs(deltas[best]) && key > best) ? key : best, keys[0]);
  const delta = latestScore - previousScore, priorBand = String(previous.band), latestBand = String(latest.band), migration = priorBand === latestBand ? "unchanged" : `${priorBand} -> ${latestBand}`, order: Record<string, number> = { abstain: 0, watch: 1, eligible: 2, strong: 3 };
  return { state: delta > 1e-12 ? "improved" : delta < -1e-12 ? "deteriorated" : "unchanged", method: "point-in-time-score-history-change-attribution", previous_as_of: previous.as_of, latest_as_of: latest.as_of, previous_score: previousScore, latest_score: latestScore, score_change: delta, prior_band: priorBand, latest_band: latestBand, migration, migration_direction: (order[latestBand] ?? 0) - (order[priorBand] ?? 0), component_deltas: deltas, top_driver: topDriver, clock_policy: "only knowledge-available snapshots are comparable" };
}

export function calculate(topicId: string, data: RecordLike): RecordLike {
  const functions: Record<string, (input: RecordLike) => RecordLike> = { "D18-F09-A01": assemble, "D18-F09-A02": cohort, "D18-F09-A03": normalize, "D18-F09-A04": route, "D18-F09-A05": health, "D18-F09-A06": earnings, "D18-F09-A07": dividend, "D18-F09-A08": resilience, "D18-F09-A09": ensemble, "D18-F09-A10": conflict, "D18-F09-A11": overall, "D18-F09-A12": confidence, "D18-F09-A13": screen, "D18-F09-A14": history };
  if (!functions[topicId]) throw new RangeError(`unsupported topic id: ${topicId}`);
  return functions[topicId](data);
}
Full-height labplaygroundOpen full screen