-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
321 lines (269 loc) · 16.1 KB
/
Copy pathstreamlit_app.py
File metadata and controls
321 lines (269 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Streamlit demo app — Motor Vehicle Claims ML Pipeline.
Run with:
uv run streamlit run streamlit_app.py
Improvements over v1:
- Preset scenario buttons (Low Risk / Young Driver / Senior Driver / High Risk)
- Severity model → E[severity | claim] and expected loss (pure premium proxy)
- Portfolio distribution chart: where does this policy rank?
- 4-metric summary row including expected loss
- Session-state-backed form so presets populate inputs immediately
"""
import sys
import warnings
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import streamlit as st
warnings.filterwarnings("ignore")
sys.path.insert(0, str(Path(__file__).parent / "src"))
st.set_page_config(
page_title="Motor Claims ML",
page_icon="🚗",
layout="wide",
initial_sidebar_state="collapsed",
)
from claims.data import load_fremtpl2, build_claims_dataset, claims_only
from claims.features import (
CATEGORICAL_FEATURES,
ENG_NUMERIC_FEATURES,
engineer_features,
)
from claims.process.severity import catboost_severity
ALL_ENG = ENG_NUMERIC_FEATURES + CATEGORICAL_FEATURES
# ─── Preset policy profiles ──────────────────────────────────────────────────
PRESETS: dict[str, dict] = {
"🟢 Low Risk": {"driv_age": 45, "veh_age": 8, "bonus_malus": 80, "exposure": 1.0, "density": 250},
"🧑 Young Driver": {"driv_age": 22, "veh_age": 2, "bonus_malus": 115, "exposure": 0.5, "density": 3000},
"👴 Senior Driver": {"driv_age": 76, "veh_age": 6, "bonus_malus": 95, "exposure": 0.8, "density": 400},
"🔴 High Risk": {"driv_age": 24, "veh_age": 1, "bonus_malus": 150, "exposure": 1.0, "density": 9000},
}
_NUMERIC_DEFAULTS = {"driv_age": 35, "veh_age": 5, "bonus_malus": 100, "exposure": 0.5, "density": 500}
# ─── Cached model training (runs once; ~4 min on first load) ─────────────────
@st.cache_resource(show_spinner="Training models on freMTPL2 (one-time setup ~4 min)…")
def _train_models():
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
from claims.classification.calibration import calibrate_beta
freq, sev = load_fremtpl2()
df = build_claims_dataset(freq, sev)
df = engineer_features(df)
# ── Classification: strict 4-way split ───────────────────────────────────
df_pool, _ = train_test_split(df, test_size=0.10, stratify=df["HasClaim"], random_state=42)
df_train, df_temp = train_test_split(df_pool, test_size=0.333, stratify=df_pool["HasClaim"], random_state=42)
df_early, df_cal = train_test_split(df_temp, test_size=0.50, stratify=df_temp["HasClaim"], random_state=42)
from claims.config import load_config
cfg = load_config()
# Cast categoricals to str (category dtype causes CatBoostError with column names)
def _cast_cats(Xdf):
Xdf = Xdf.copy()
for _c in CATEGORICAL_FEATURES:
Xdf[_c] = Xdf[_c].astype(str)
return Xdf
X_tr = _cast_cats(df_train[ALL_ENG])
X_early = _cast_cats(df_early[ALL_ENG])
X_cal = _cast_cats(df_cal[ALL_ENG])
cb = CatBoostClassifier(
iterations=cfg.catboost.iterations,
learning_rate=cfg.catboost.learning_rate,
depth=cfg.catboost.depth,
l2_leaf_reg=cfg.catboost.l2_leaf_reg,
auto_class_weights="Balanced", eval_metric="AUC",
early_stopping_rounds=40, random_seed=42, verbose=0,
)
cb.fit(
X_tr, df_train["HasClaim"].values,
eval_set=(X_early, df_early["HasClaim"].values),
cat_features=CATEGORICAL_FEATURES, verbose=False,
)
beta_cal = calibrate_beta(cb.predict_proba(X_cal)[:, 1], df_cal["HasClaim"].values)
# ── Severity model: CatBoost on log1p(AvgSeverity) ───────────────────────
df_sev = claims_only(df).dropna(subset=["AvgSeverity"])
df_sev = df_sev[df_sev["AvgSeverity"] > 0].reset_index(drop=True)
sev_tr, sev_te = train_test_split(df_sev, test_size=0.2, random_state=42)
y_log_tr = np.log1p(sev_tr["AvgSeverity"].values)
y_log_te = np.log1p(sev_te["AvgSeverity"].values)
X_sev_tr = _cast_cats(sev_tr[ALL_ENG])
X_sev_te = _cast_cats(sev_te[ALL_ENG])
cb_sev = catboost_severity()
cb_sev.fit(
X_sev_tr, y_log_tr,
eval_set=(X_sev_te, y_log_te),
cat_features=CATEGORICAL_FEATURES, verbose=False,
)
# ── Portfolio sample for distribution chart (5k calibrated probs) ────────
sample = df.sample(5_000, random_state=42)
s_raw = cb.predict_proba(_cast_cats(sample[ALL_ENG]))[:, 1]
portfolio_probs = np.clip(beta_cal.predict(s_raw.reshape(-1, 1)), 0, 1).astype(float)
cat_opts = {
col: sorted(df[col].astype(str).dropna().unique().tolist())
for col in CATEGORICAL_FEATURES
}
portfolio_stats = {
"n_policies": len(df),
"claim_rate": float(df["HasClaim"].mean()),
"avg_severity": float(df["AvgSeverity"].dropna().mean()),
"avg_exposure": float(pd.to_numeric(df["Exposure"], errors="coerce").mean()),
}
return cb, beta_cal, cb_sev, cat_opts, portfolio_stats, portfolio_probs
cb_model, beta_cal, cb_sev, cat_opts, stats, portfolio_probs = _train_models()
# ─── Helpers ──────────────────────────────────────────────────────────────────
def _stp_lane(p: float) -> str:
if p < 0.10: return "auto-settle"
if p >= 0.40: return "investigate"
return "review"
def _portfolio_chart(cal_prob: float) -> plt.Figure:
"""Histogram of 5k portfolio calibrated probs with this policy marked."""
fig, ax = plt.subplots(figsize=(7, 2.6))
ax.hist(portfolio_probs, bins=60, color="#4472C4", alpha=0.75, edgecolor="white", density=True)
ax.axvline(cal_prob, color="#ED7D31", lw=2.5, linestyle="--", label=f"This policy p={cal_prob:.3f}")
pct = float((portfolio_probs <= cal_prob).mean()) * 100
ymax = ax.get_ylim()[1]
ax.text(cal_prob + 0.002, ymax * 0.82, f"{pct:.0f}th\npct", color="#ED7D31", fontsize=9, fontweight="bold")
ax.set_xlabel("Calibrated Claim Probability", fontsize=9)
ax.set_ylabel("Density", fontsize=9)
ax.set_title("Portfolio Distribution — Where does this policy sit?", fontsize=10, fontweight="bold")
ax.legend(fontsize=9)
fig.tight_layout()
return fig
# ─── Header ───────────────────────────────────────────────────────────────────
st.title("🚗 Motor Vehicle Claims — ML Decision Support")
st.caption(
"French Motor TPL (freMTPL2) · 678k policies · "
"CatBoost (Optuna-tuned: AUC=0.71, Gini=0.42) · Beta calibration · SHAP explainability"
)
tab_predict, tab_portfolio = st.tabs(["🔮 Predict", "📊 Portfolio Overview"])
# ─── Tab: Portfolio Overview ──────────────────────────────────────────────────
with tab_portfolio:
c1, c2, c3, c4 = st.columns(4)
c1.metric("Total Policies", f"{stats['n_policies']:,}")
c2.metric("Claim Rate", f"{stats['claim_rate']:.2%}")
c3.metric("Avg Claim Severity", f"€{stats['avg_severity']:,.0f}")
c4.metric("Avg Exposure", f"{stats['avg_exposure']:.2f} years")
st.markdown("---")
fig_dir = Path(__file__).parent / "reports" / "figures"
for fname, caption in [
("01_eda_overview.png", "EDA — Claim distribution & feature overview"),
("02_classification.png", "Classification — Beta & Venn-ABERS calibration, STP routing"),
("03_fraud_detection.png", "Fraud Detection — Anomaly scoring + cost-sensitive CatBoost"),
("04_process_analytics.png", "Process Analytics — Lorenz curve, severity models, P50/P75/P95 reserves"),
("05_summary.png", "Results Summary — CatBoost feature importance & metrics"),
("06_shap_beeswarm.png", "SHAP Beeswarm — Global feature impact (500 test samples)"),
("06_shap_waterfall.png", "SHAP Waterfall — Highest-risk prediction breakdown"),
("07_gains_lift_chart.png", "Gains & Lift — Top-decile lift: 3.1× | Top-20% captures 45% of claims"),
("08_decile_analysis.png", "Decile Analysis — Full 10-decile breakdown with cumulative capture rate"),
("09_temporal_validation.png", "Temporal Validation — Walk-forward AUC vs random split baseline"),
("10_fraud_gains_lift.png", "Fraud Gains & Lift — Cost-sensitive model lift by decile"),
("11_optuna_comparison.png", "Optuna Tuning — ΔAUC=+0.0025, ΔGini=+0.0051 vs stock defaults"),
]:
fpath = fig_dir / fname
if fpath.exists():
st.image(str(fpath), caption=caption, width="stretch")
st.markdown("")
else:
st.info(f"📁 `{fname}` not found — run `uv run python reports/make_figures.py` first.")
# ─── Tab: Predict ─────────────────────────────────────────────────────────────
with tab_predict:
st.subheader("Single-Policy Risk Assessment")
# ── Initialise session-state defaults (first run only) ───────────────────
for k, v in _NUMERIC_DEFAULTS.items():
st.session_state.setdefault(k, v)
# ── Preset buttons ────────────────────────────────────────────────────────
st.markdown("**Quick scenario presets**")
preset_cols = st.columns(len(PRESETS))
for col, (name, vals) in zip(preset_cols, PRESETS.items()):
if col.button(name, width="stretch"):
for k, v in vals.items():
st.session_state[k] = v
st.rerun()
st.markdown("---")
col_form, col_result = st.columns([1, 2], gap="large")
with col_form:
st.markdown("**Driver & Vehicle**")
driv_age = st.number_input("Driver Age", min_value=18, max_value=100, step=1, key="driv_age")
veh_age = st.number_input("Vehicle Age (years)", min_value=0, max_value=30, step=1, key="veh_age")
bonus_malus = st.number_input("BonusMalus (100 = neutral)", min_value=50, max_value=350, step=1, key="bonus_malus")
exposure = st.slider( "Exposure (policy years)", 0.0, 1.0, step=0.01, key="exposure")
density = st.number_input("Area Density (pop/km²)", min_value=1, max_value=30000, step=100, key="density")
st.markdown("**Categorical**")
veh_brand = st.selectbox("Vehicle Brand", cat_opts["VehBrand"], key="veh_brand")
veh_gas = st.selectbox("Fuel Type", cat_opts["VehGas"], key="veh_gas")
area = st.selectbox("Area Code", cat_opts["Area"], key="area")
region = st.selectbox("Region", cat_opts["Region"], key="region")
veh_power = st.selectbox("Vehicle Power", cat_opts["VehPower"], key="veh_power")
predict_btn = st.button("🔮 Predict Risk", type="primary", width="stretch")
with col_result:
if predict_btn:
row = pd.DataFrame([{
"VehAge": veh_age, "DrivAge": driv_age, "BonusMalus": bonus_malus,
"Density": density, "Exposure": exposure,
"VehBrand": veh_brand, "VehGas": veh_gas,
"Area": area, "Region": region, "VehPower": veh_power,
}])
row_eng = engineer_features(row)
row_X = row_eng[ALL_ENG].copy()
for _c in CATEGORICAL_FEATURES:
row_X[_c] = row_X[_c].astype(str)
raw_prob = float(cb_model.predict_proba(row_X)[:, 1][0])
cal_prob = float(np.clip(beta_cal.predict(np.array([[raw_prob]])), 0, 1)[0])
sev_est = float(np.expm1(cb_sev.predict(row_X))[0])
exp_loss = cal_prob * sev_est
lane = _stp_lane(cal_prob)
# ── 4 key metrics ─────────────────────────────────────────────────
m1, m2, m3, m4 = st.columns(4)
m1.metric("Raw P(claim)", f"{raw_prob:.3f}")
m2.metric("Calibrated P (Beta)", f"{cal_prob:.3f}")
m3.metric("E[Severity | claim]", f"€{sev_est:,.0f}",
help="Expected claim amount given a claim occurs")
m4.metric("Expected Loss", f"€{exp_loss:,.0f}",
help="P(claim) × E[severity | claim] — pure premium proxy")
# ── STP decision banner ───────────────────────────────────────────
lane_cfg = {
"auto-settle": ("✅", "success", "Auto-settle — Instant payout, no adjuster needed."),
"review": ("⚠️", "warning", "Review — Manual adjuster assigned."),
"investigate": ("🚨", "error", "Investigate — Detailed review required before settlement."),
}
icon, alert_fn, msg = lane_cfg[lane]
getattr(st, alert_fn)(f"{icon} **{lane.title()}** ({cal_prob:.1%}) — {msg}")
progress_val = min(cal_prob / 0.30, 1.0)
st.progress(
progress_val,
text=f"Risk level: {cal_prob:.1%} (portfolio mean: {stats['claim_rate']:.1%})",
)
# ── Portfolio context ─────────────────────────────────────────────
st.markdown("---")
pct_rank = float((portfolio_probs <= cal_prob).mean()) * 100
st.markdown(
f"**Portfolio Context** — riskier than **{pct_rank:.0f}%** of the sampled portfolio"
)
portfolio_fig = _portfolio_chart(cal_prob)
st.pyplot(portfolio_fig, width="stretch")
plt.close(portfolio_fig)
# ── SHAP waterfall ────────────────────────────────────────────────
st.markdown("---")
st.markdown("**SHAP Feature Contributions**")
st.caption("Positive SHAP value = feature raises claim probability | Negative = lowers it")
try:
import shap
shap_expl = shap.TreeExplainer(cb_model)
sv = shap_expl(row_X)
plt.close("all") # ensure SHAP starts with a clean figure
shap.plots.waterfall(sv[0], max_display=12, show=False)
st.pyplot(plt.gcf(), width="stretch")
plt.close()
except Exception as e:
st.info(f"SHAP plot unavailable: {e}")
else:
st.info(
"👈 Pick a **preset** or configure the policy on the left, then click **Predict Risk**.\n\n"
"**What this app shows:**\n"
"- **Calibrated P(claim)** — Beta-calibrated probability (ECE < 0.001 on test set)\n"
"- **E[Severity | claim]** — Expected claim amount (CatBoost on log-transformed target)\n"
"- **Expected Loss** — P × E[sev] = actuarial pure premium proxy\n"
"- **STP Lane** — Auto-settle / Review / Investigate routing decision\n"
"- **Portfolio rank** — Where this policy sits vs 5k sampled policies\n"
"- **SHAP waterfall** — Which features drove this specific prediction\n\n"
"**STP thresholds:** `p < 0.10` → Auto-settle · `0.10–0.40` → Review · `p ≥ 0.40` → Investigate"
)