""" page5_optimisation.py โ 10% Bandwidth Optimisation, Efficiency Metrics, Baseline Comparison """ import streamlit as st import plotly.graph_objects as go import pandas as pd import numpy as np from styles import (inject_global_css, page_header, metric_row, WCO_GOLD, WCO_BLUE, WCO_GREEN, WCO_RED, WCO_CARD_BG, WCO_BORDER, WCO_MUTED) from simulation_engine import RISK_AREAS def efficiency_comparison_chart(hybrid: dict, baseline: dict): categories = ["Selection Rate", "Detection Rate", "Precision", "Efficiency Index"] b_vals = [ baseline["selection_rate"], baseline["detection_rate"], baseline["precision"], baseline["efficiency_index"], ] h_vals = [ hybrid["selection_rate"], hybrid["detection_rate"], hybrid["precision"], hybrid["efficiency_index"], ] fig = go.Figure() fig.add_trace(go.Bar(name="๐ Static Baseline (DATE only)", x=categories, y=b_vals, marker_color=WCO_RED, opacity=0.80, text=[f"{v:.3f}" for v in b_vals], textposition="outside", textfont=dict(color="#D0DCF0"))) fig.add_trace(go.Bar(name="๐ No-CelH Hybrid Model", x=categories, y=h_vals, marker_color=WCO_GREEN, opacity=0.85, text=[f"{v:.3f}" for v in h_vals], textposition="outside", textfont=dict(color="#D0DCF0"))) fig.update_layout( paper_bgcolor="#070E1C", plot_bgcolor="#0B1220", barmode="group", font=dict(family="IBM Plex Sans", color="#D0DCF0", size=12), height=400, title=dict(text="Efficiency Metrics: Static Baseline vs Hybrid Self-Learning Model", font=dict(color=WCO_GOLD, size=15, family="Playfair Display"), x=0.5), xaxis=dict(gridcolor="#1E3A6E"), yaxis=dict(gridcolor="#1E3A6E", title="Metric Value", range=[0, 1.1]), legend=dict(bgcolor="#0F1C35", bordercolor=WCO_BORDER, font=dict(size=12)), margin=dict(l=55, r=20, t=60, b=50), ) return fig def bandwidth_optimisation_chart(df): """Show how risk score thresholds map to channel decisions.""" n = len(df) thresholds = np.linspace(0.1, 0.9, 30) rows = [] for t in thresholds: selected = df[df["fraud_score"] >= t] n_sel = len(selected) n_fraud = (selected["is_illicit"] == 1).sum() all_fraud = (df["is_illicit"] == 1).sum() sel_rate = n_sel / n if n else 0 det_rate = n_fraud / all_fraud if all_fraud else 0 precision = n_fraud / n_sel if n_sel else 0 eff = det_rate / sel_rate if sel_rate > 0 else 0 rows.append(dict(threshold=round(t,2), sel_rate=sel_rate, det_rate=det_rate, precision=precision, efficiency=eff)) opt_df = pd.DataFrame(rows) fig = go.Figure() fig.add_trace(go.Scatter(x=opt_df["threshold"], y=opt_df["sel_rate"], name="Selection Rate", line=dict(color=WCO_BLUE, width=2))) fig.add_trace(go.Scatter(x=opt_df["threshold"], y=opt_df["det_rate"], name="Detection Rate", line=dict(color=WCO_GREEN, width=2))) fig.add_trace(go.Scatter(x=opt_df["threshold"], y=opt_df["precision"], name="Precision", line=dict(color=WCO_GOLD, width=2))) fig.add_trace(go.Scatter(x=opt_df["threshold"], y=opt_df["efficiency"].clip(0,2), name="Efficiency Index", line=dict(color=WCO_RED, width=2.5, dash="dot"))) # 10% bandwidth marker fig.add_vline(x=opt_df.iloc[(opt_df["sel_rate"] - 0.10).abs().argsort().iloc[0]]["threshold"], line=dict(color=WCO_GOLD, dash="dash", width=1.5), annotation_text="10% Bandwidth โ", annotation_font_color=WCO_GOLD) fig.update_layout( paper_bgcolor="#070E1C", plot_bgcolor="#0B1220", font=dict(family="IBM Plex Sans", color="#D0DCF0", size=11), height=380, title=dict(text="Bandwidth Optimisation Curve โ Risk Score Threshold Analysis", font=dict(color=WCO_GOLD, size=14, family="Playfair Display"), x=0.5), xaxis=dict(title="Risk Score Threshold", gridcolor="#1E3A6E"), yaxis=dict(title="Rate / Index", gridcolor="#1E3A6E", range=[0, 2.1]), legend=dict(bgcolor="#0F1C35", bordercolor=WCO_BORDER), margin=dict(l=55, r=20, t=55, b=45), ) return fig def exploration_ratio_chart(): """Simulate efficiency at different exploration ratios (from paper Fig.11 inspired).""" ratios = [0.01, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.40, 0.50] # Simulated efficiency values inspired by Kim et al. (2022) paper findings eff_country_m = [0.65, 0.66, 0.64, 0.62, 0.60, 0.58, 0.55, 0.52, 0.47, 0.41] eff_country_t = [0.30, 0.35, 0.42, 0.57, 0.63, 0.67, 0.71, 0.74, 0.72, 0.68] eff_custom = [0.55, 0.58, 0.63, 0.70, 0.72, 0.71, 0.69, 0.68, 0.63, 0.55] fig = go.Figure() fig.add_trace(go.Scatter(x=[r*100 for r in ratios], y=eff_country_m, name="Low-drift trade (best@1%)", line=dict(color=WCO_BLUE, width=2), mode="lines+markers", marker=dict(size=7))) fig.add_trace(go.Scatter(x=[r*100 for r in ratios], y=eff_country_t, name="High-drift trade (best@30%)", line=dict(color=WCO_GREEN, width=2), mode="lines+markers", marker=dict(size=7))) fig.add_trace(go.Scatter(x=[r*100 for r in ratios], y=eff_custom, name="This simulation", line=dict(color=WCO_GOLD, width=2.5, dash="dot"), mode="lines+markers", marker=dict(size=9, symbol="star"))) fig.add_vline(x=10, line=dict(color=WCO_GOLD, dash="dash", width=1.5), annotation_text="Default 10% โ", annotation_font_color=WCO_GOLD) fig.update_layout( paper_bgcolor="#070E1C", plot_bgcolor="#0B1220", font=dict(family="IBM Plex Sans", color="#D0DCF0", size=11), height=360, title=dict(text="Efficiency Index vs Exploration Ratio ฮต (inspired by Kim et al. 2022)", font=dict(color=WCO_GOLD, size=14, family="Playfair Display"), x=0.5), xaxis=dict(title="Exploration Ratio ฮต (%)", gridcolor="#1E3A6E"), yaxis=dict(title="Norm-Rev@10% (Efficiency)", gridcolor="#1E3A6E", range=[0.25, 0.85]), legend=dict(bgcolor="#0F1C35", bordercolor=WCO_BORDER), margin=dict(l=55, r=20, t=55, b=45), ) return fig def risk_score_threshold_table(df): """Show how top-scored bills materialise into Red/Yellow/Green.""" bins = pd.cut(df["fraud_score"], bins=[0,.2,.4,.6,.8,1.0], labels=["0-20%","20-40%","40-60%","60-80%","80-100%"]) result = df.groupby(bins, observed=True).agg( Bills=("bill_id","count"), Red=("channel", lambda x: (x=="RED").sum()), Yellow=("channel", lambda x: (x=="YELLOW").sum()), Green=("channel", lambda x: (x=="GREEN").sum()), Fraud_Detected=("inspection_outcome", lambda x: (x=="FRAUD_DETECTED").sum()), Avg_Revenue=("detected_revenue","mean"), Illicit_Count=("is_illicit","sum"), ).reset_index() result.columns = ["Risk Score Band","Bills","RED","YELLOW","GREEN", "Fraud Detected","Avg Revenue ($)","True Illicit"] result["Avg Revenue ($)"] = result["Avg Revenue ($)"].round(2) result["Detection Rate (%)"] = ( 100 * result["Fraud Detected"] / result["Bills"].replace(0,1) ).round(1) return result def show(): inject_global_css() page_header("๐", "Bandwidth Optimisation & Efficiency Report", "10% INTERDICTION BANDWIDTH ยท ACCURACY ยท ROI ยท HYBRID vs STATIC COMPARISON") if "sim_df" not in st.session_state: st.markdown("""
| Model | Strategy | Selection Rate | Detection Rate | Efficiency Index | Verdict |
|---|