feat(lead): score High Needs criteria and separate suspect moves

This commit is contained in:
kert
2026-10-31 22:01:50 -04:00
parent 080df238b3
commit 9bddb3ddf0
4 changed files with 268 additions and 0 deletions

80
src/lead/high_needs.py Normal file
View File

@@ -0,0 +1,80 @@
"""High Needs criteria for one quarter and for an alignment year.
The scores, the Kim index, the diagnosis flag, and the DME flag are
caller-supplied. Appendix B code lists, Kim weights, and the V28 software
are not in this repo, so this module does not invent them. The thresholds
are the ones named in the LEAD methodology for PY 2027.
A score of 3.0 is HN.4 only. A score of 0.35 is HN.5 only. The HN.6 bands
stop below those thresholds. Once any quarter meets a criterion, it applies
for the whole alignment year, and a criterion met in a prior year stays met.
"""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
AD_HIGH = 3.0
ESRD_HIGH = 0.35
AD_BAND_LOW = 2.0
ESRD_BAND_LOW = 0.24
UNPLANNED_ADMISSIONS = 2
SNF_DAYS = 45
@dataclass(frozen=True)
class QuarterFlags:
"""One quarter's lookback, already counted by the caller."""
quarter: str
hn1_diagnosis: bool = False
hn2_dme: bool = False
kim_index: float | None = None
kim_cutoff: float | None = None
ad_score: float | None = None
esrd_score: float | None = None
unplanned_admissions: int = 0
snf_days: int = 0
def criteria_met(flags: QuarterFlags) -> frozenset[str]:
"""Target ids this quarter meets. An empty set meets none."""
met: set[str] = set()
if flags.hn1_diagnosis:
met.add("HN.1")
if flags.hn2_dme:
met.add("HN.2")
if (
flags.kim_index is not None
and flags.kim_cutoff is not None
and flags.kim_index >= flags.kim_cutoff
):
met.add("HN.3")
if flags.ad_score is not None and flags.ad_score >= AD_HIGH:
met.add("HN.4")
if flags.esrd_score is not None and flags.esrd_score >= ESRD_HIGH:
met.add("HN.5")
admissions = flags.unplanned_admissions >= UNPLANNED_ADMISSIONS
ad_band = flags.ad_score is not None and AD_BAND_LOW <= flags.ad_score < AD_HIGH
esrd_band = (
flags.esrd_score is not None and ESRD_BAND_LOW <= flags.esrd_score < ESRD_HIGH
)
if admissions and (ad_band or esrd_band):
met.add("HN.6")
if flags.snf_days >= SNF_DAYS:
met.add("HN.7")
return frozenset(met)
def alignment_year(quarters: Sequence[QuarterFlags]) -> frozenset[str]:
"""Union of the criteria met in any quarter of the alignment year."""
met: set[str] = set()
for quarter in quarters:
met |= criteria_met(quarter)
return frozenset(met)
def persists(prior: frozenset[str], this_year: frozenset[str]) -> frozenset[str]:
"""High Needs status does not go away once a criterion has been met."""
return prior | this_year

50
src/lead/suspect.py Normal file
View File

@@ -0,0 +1,50 @@
"""A suspect is a status change, by coding or by care.
Coding makes a status the person already has visible to the score. Care
changes the underlying state. Demographic reweighting multiplies only the
coding delta. The two are never collapsed into one number.
"""
from __future__ import annotations
from dataclasses import dataclass
@dataclass(frozen=True)
class SuspectMove:
"""How far each mechanism moves the score."""
coding_delta: float
care_delta: float
coding_after_reweight: float
def suspect_move(
*,
before_status: str,
after_status: str,
score_before: float,
score_coding: float,
score_care: float,
weight_before: float,
weight_after: float,
) -> SuspectMove:
"""Return the coding delta, the care delta, and the reweighted coding delta.
Raises:
ValueError: the status does not change, the weights are equal, or
the starting weight is zero.
"""
if before_status == after_status:
raise ValueError("a suspect is a status change")
if weight_before == weight_after:
raise ValueError("reweight needs a different weight")
if weight_before == 0:
raise ValueError("weight_before must not be zero")
coding_delta = score_coding - score_before
care_delta = score_care - score_before
return SuspectMove(
coding_delta=coding_delta,
care_delta=care_delta,
coding_after_reweight=coding_delta * (weight_after / weight_before),
)

View File

@@ -0,0 +1,76 @@
"""High Needs criteria from caller-supplied quarter flags."""
from __future__ import annotations
from lead.high_needs import QuarterFlags, alignment_year, criteria_met, persists
def test_ad_score_at_3_is_hn4_and_not_hn6():
met = criteria_met(QuarterFlags(quarter="2027Q1", ad_score=3.0))
assert met == frozenset({"HN.4"})
def test_ad_band_with_two_admissions_is_hn6():
met = criteria_met(
QuarterFlags(quarter="2027Q1", ad_score=2.0, unplanned_admissions=2)
)
assert met == frozenset({"HN.6"})
def test_ad_band_with_one_admission_meets_nothing():
met = criteria_met(
QuarterFlags(quarter="2027Q1", ad_score=2.0, unplanned_admissions=1)
)
assert met == frozenset()
def test_ad_below_the_band_meets_nothing():
met = criteria_met(
QuarterFlags(quarter="2027Q1", ad_score=1.999, unplanned_admissions=2)
)
assert met == frozenset()
def test_esrd_score_at_0_35_is_hn5_and_not_hn6():
met = criteria_met(QuarterFlags(quarter="2027Q1", esrd_score=0.35))
assert "HN.5" in met
assert "HN.6" not in met
def test_esrd_band_with_two_admissions_is_hn6():
met = criteria_met(
QuarterFlags(quarter="2027Q1", esrd_score=0.24, unplanned_admissions=2)
)
assert met == frozenset({"HN.6"})
def test_forty_five_snf_days_meet_hn7():
assert "HN.7" in criteria_met(QuarterFlags(quarter="2027Q1", snf_days=45))
assert "HN.7" not in criteria_met(QuarterFlags(quarter="2027Q1", snf_days=44))
def test_diagnosis_and_dme_are_separate_criteria():
assert criteria_met(
QuarterFlags(quarter="2027Q1", hn1_diagnosis=True)
) == frozenset({"HN.1"})
assert criteria_met(QuarterFlags(quarter="2027Q1", hn2_dme=True)) == frozenset(
{"HN.2"}
)
def test_kim_needs_both_the_index_and_the_cutoff():
assert "HN.3" in criteria_met(
QuarterFlags(quarter="2027Q1", kim_index=0.2, kim_cutoff=0.2)
)
assert "HN.3" not in criteria_met(QuarterFlags(quarter="2027Q1", kim_index=0.2))
assert "HN.3" not in criteria_met(QuarterFlags(quarter="2027Q1", kim_cutoff=0.2))
def test_one_quarter_sets_the_alignment_year():
quiet = QuarterFlags(quarter="2027Q1")
met = QuarterFlags(quarter="2027Q2", snf_days=45)
assert alignment_year((quiet, met, quiet, quiet)) == frozenset({"HN.7"})
def test_a_met_criterion_never_goes_away():
assert persists(frozenset({"HN.1"}), frozenset()) == frozenset({"HN.1"})

View File

@@ -0,0 +1,62 @@
"""A suspect is a status change. Coding and care stay separate numbers."""
from __future__ import annotations
import pytest
from lead.suspect import SuspectMove, suspect_move
def test_coding_and_care_stay_apart_and_reweight_shrinks_only_coding():
move = suspect_move(
before_status="standard",
after_status="high_needs",
score_before=1.0,
score_coding=1.2,
score_care=1.5,
weight_before=1.0,
weight_after=0.5,
)
assert isinstance(move, SuspectMove)
assert move.coding_delta == pytest.approx(0.2)
assert move.care_delta == pytest.approx(0.5)
assert move.coding_after_reweight == pytest.approx(0.1)
def test_the_same_status_is_not_a_suspect():
with pytest.raises(ValueError):
suspect_move(
before_status="high_needs",
after_status="high_needs",
score_before=1.0,
score_coding=1.2,
score_care=1.5,
weight_before=1.0,
weight_after=0.5,
)
def test_equal_weights_raise():
with pytest.raises(ValueError):
suspect_move(
before_status="standard",
after_status="high_needs",
score_before=1.0,
score_coding=1.2,
score_care=1.5,
weight_before=1.0,
weight_after=1.0,
)
def test_a_zero_starting_weight_raises():
with pytest.raises(ValueError):
suspect_move(
before_status="standard",
after_status="high_needs",
score_before=1.0,
score_coding=1.2,
score_care=1.5,
weight_before=0.0,
weight_after=0.5,
)