# File: logic/financial_model.py def normalize_bool(value): if isinstance(value, str): value = value.strip().lower() return value in ["yes", "true", "1"] return bool(value) def estimate_financial_recovery(care_gaps_df, patient_data, base_rate, sdoh_keys): results = [] hcc_weights = { 'HCC18': 0.4, 'HCC85': 0.5, 'HCC19': 0.3 } for _, row in care_gaps_df.iterrows(): patient = patient_data[patient_data['patient_id'] == row['patient_id']].iloc[0] hccs = [h.strip() for h in str(patient.get('hcc_codes', '')).split(';') if h.strip() in hcc_weights] risk_score = sum(hcc_weights[h] for h in hccs) sdoh_count = sum(1 for key in sdoh_keys if normalize_bool(patient.get(key))) sdoh_modifier = 1 + 0.05 * sdoh_count # Each SDOH adds 5% to the opportunity care_gap_count = len(row['care_gaps']) expected_gain = base_rate * (risk_score * 0.02 * care_gap_count) * sdoh_modifier results.append({ 'patient_id': row['patient_id'], 'risk_score': round(risk_score, 2), 'projected_gain': round(expected_gain, 2), 'sdoh_flags': sdoh_count }) return pd.DataFrame(results)