All checks were successful
CI / lint (push) Successful in 44s
CI / notebooks-smoke (push) Successful in 1m33s
Deploy / changes (push) Successful in 14s
CI / test (push) Successful in 3m9s
Infra CI / zotero (push) Successful in 14s
Infra CI / notebooks (push) Successful in 55s
Infra CI / api (push) Successful in 1m9s
Infra CI / docs (push) Successful in 1m23s
Infra CI / llm (push) Successful in 1m1s
Infra CI / mc (push) Successful in 16s
Infra CI / bcda (push) Successful in 1m43s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Package Supply Chain / pkg-supply-chain (push) Successful in 1m30s
Deploy / api (push) Successful in 1m17s
Deploy / llm (push) Successful in 1m18s
Deploy / mc (push) Has been skipped
Deploy / bcda (push) Successful in 1m42s
Deploy / report (push) Successful in 1m36s
Ports the public CMS "LEAD Model PY 2027 Alignment and Financial Methodology Paper" v1.2 (bib E4PEU3HV) as generic engines: parameters come from the paper, everything CMS computes per ACO or per year comes from the caller. No participant data, no organization-specific rules. - lead.params: Table 5.2.1 discounts, 3% Quality Withhold, 2% retention incentive, Box 5.3.3 Stop-Loss bands, Tables 5.4.1/5.4.3 corridors, 2% sequestration, base-year weights, Table 3.5.1 ACPT guardrails, risk-score growth caps, REA/PSA shares and caps, voluntary guardrail and 500 minimum, Table 2.6.1 base-year alignment minimums. - lead.settlement: Final Financial Settlement, Table 5.6.1 line by line (settle(SettlementInputs) -> SettlementLines). - lead.stop_loss: Box 5.3.3 charge, banded payout, neutrality factor, application to PY expenditure. - lead.monies_owed: Table 5.5.1 Total Monies Owed, APO reconciliation. - lead.timing: run-out end, settlement-basis rule, settlement-stage inputs. - lead.benchmark: §3 formulas — risk standardization, two-/three-way trend, base-year weights and sufficiency, voluntary guardrail, regional FFS, spending designation, REA, PSA, risk-score cap, hybrid alignment dollars, category blend. - lead.express: the same as narwhals Exprs; express.settlement.settle settles a frame of scenarios on any backend. Rounding: every dollar line rounds to the cent (half away from zero) as it is produced; PBPMs, rates and factors never do. It is the only rule under which Tables 3.3.3, 3.5.3.2, 3.5.4, 5.5.1 and 5.6.1 all reproduce (3.5.4 line 24 sums cent-rounded cells; 3.3.3 carries unrounded PBPMs). The Expr cents snaps its ÷100 with round(2): polars 2.0 divides a column by a literal as a reciprocal multiply (k × 0.01), one ulp off k/100 for ~15% of values. Missing inputs stay missing (None/null or a refused input model); the only zeros are the paper's own (Professional "N/A" discount, no Stop-Loss election, sequestration on losses, ineligible REA/PSA). Paper inconsistencies recorded in tests: Table 5.5.1 line 2 is a cent below Table 5.6.1 line 28 (.43 vs .44); Box 3.3.2 prints 1.104 then says 1.103; §3.5.1 prose widens the ACPT lower bound a year later than Table 3.5.1 (the table is followed). Tests: 283 in tests/lead — every published line to the cent, Expr builders on polars and DuckDB, bit-for-bit scalar/frame parity over 160 scenarios; 100% of src/lead.
90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
"""Beneficiary-grain Stop-Loss (Box 5.3.3) as narwhals expressions.
|
||
|
||
One row per beneficiary: Predicted Expenditure, Residual Expenditure, the
|
||
distance to the Attachment Point, the banded payout and the band reached.
|
||
Scalar twins: ``lead.stop_loss``.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) §5.3.3 / Box 5.3.3.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import narwhals as nw
|
||
|
||
from lead.express import IntoExpr, cents, col
|
||
from lead.params import STOP_LOSS_BANDS
|
||
from lead.stop_loss import BELOW_ATTACHMENT_POINT
|
||
|
||
|
||
def predicted_expenditure(
|
||
regional_expenditure_pbpm: IntoExpr, risk_score: IntoExpr, months_aligned: IntoExpr
|
||
) -> nw.Expr:
|
||
"""Predicted Expenditure = regional expenditure PBPM × risk score ×
|
||
months of alignment in the PY.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) Box 5.3.3 (Stop-Loss
|
||
Residual Expenditure).
|
||
"""
|
||
return cents(col(regional_expenditure_pbpm) * col(risk_score) * col(months_aligned))
|
||
|
||
|
||
def residual_expenditure(actual_spend: IntoExpr, predicted_spend: IntoExpr) -> nw.Expr:
|
||
"""Residual Expenditure = PY expenditure − Predicted Expenditure.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) §5.3.3 / Box 5.3.3.
|
||
"""
|
||
return cents(col(actual_spend) - col(predicted_spend))
|
||
|
||
|
||
def distance_to_attachment_point(
|
||
attachment_point: IntoExpr, residual: IntoExpr
|
||
) -> nw.Expr:
|
||
"""Attachment Point − Residual: positive is headroom before Stop-Loss
|
||
attaches, negative is how far past it the beneficiary is.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) §5.3.3 (protection begins
|
||
once the Residual passes the Attachment Point).
|
||
"""
|
||
return cents(col(attachment_point) - col(residual))
|
||
|
||
|
||
def stop_loss_payout(residual: IntoExpr, attachment_point: IntoExpr) -> nw.Expr:
|
||
"""Banded payout on the Residual above the Attachment Point: 80 percent
|
||
up to 200 percent of it, 100 percent beyond; null if either is null.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) Box 5.3.3 (Stop-Loss
|
||
Payment Schedule; illustration: $400,000 residual, $150,000 AP → $220,000).
|
||
"""
|
||
amount, point = col(residual), col(attachment_point)
|
||
paid = []
|
||
for band in STOP_LOSS_BANDS:
|
||
capped = (
|
||
amount
|
||
if band.end_multiple is None
|
||
else nw.min_horizontal(amount, point * band.end_multiple)
|
||
)
|
||
exposure = (capped - point * band.start_multiple).clip(lower_bound=0.0)
|
||
paid.append(cents(exposure * band.payout_rate))
|
||
total = paid[0]
|
||
for band_payout in paid[1:]:
|
||
total = total + band_payout
|
||
return nw.when(~amount.is_null() & ~point.is_null()).then(cents(total))
|
||
|
||
|
||
def band_status(residual: IntoExpr, attachment_point: IntoExpr) -> nw.Expr:
|
||
"""The band the Residual reaches: ``band_2``, ``band_1`` or
|
||
``below_attachment_point``; null if either input is null.
|
||
|
||
Source: LEAD PY2027 methodology (bib E4PEU3HV) Box 5.3.3 (Stop-Loss
|
||
Payment Schedule).
|
||
"""
|
||
amount, point = col(residual), col(attachment_point)
|
||
status = nw.when(amount.is_null() | point.is_null()).then(
|
||
nw.lit(None, dtype=nw.String())
|
||
)
|
||
for band in reversed(STOP_LOSS_BANDS):
|
||
status = status.when(amount > point * band.start_multiple).then(
|
||
nw.lit(f"band_{band.band}")
|
||
)
|
||
return status.otherwise(nw.lit(BELOW_ATTACHMENT_POINT))
|