def build_data_alerts_html(tn_ratio, n_fragile):
data_alerts_html = ""
if tn_ratio is not None and tn_ratio < 10:
sev_c = "#f85149" if tn_ratio < 5 else "#d29922"
sev_l = "Critical" if tn_ratio < 5 else "Warning"
data_alerts_html += (
f'
'
f'⚠ Spurious Correlation {sev_l} — '
f'T/N={tn_ratio:.1f}x. Need ≥10× observations vs assets.
'
)
if n_fragile > 0:
data_alerts_html += (
f''
f'⚠ Fragile Allocations — {n_fragile} asset(s) had '
f'weight swings >15 pp under ±10% return noise. Stability penalty applied.
'
)
return data_alerts_html
def build_warnings_html(diags):
warn_html = ""
if diags:
items = "".join(f'{d}' for d in diags)
warn_html = (
''
'
⚠ Behavioral Diagnostics
'
f'
'
)
return warn_html
def build_constraint_diag_html(model_info):
relax_log = model_info.get("relaxation_log", [])
binding_cons = model_info.get("display_constraints", [])
_cd_inner = ""
if binding_cons:
_bc_items = "".join(f'{c}' for c in binding_cons)
_cd_inner += (
'Binding Constraints
'
''
'These rules actively limited the optimizer — relaxing them may improve returns:
'
f''
)
if relax_log:
if any("Universe too small" in r for r in relax_log):
_rl_summary = "The universe was too small to simultaneously satisfy constraints (e.g. beta, sector limits). Solved with maximum flexibility."
_rl_items = f'{relax_log[0]}'
elif len(relax_log) > 3:
_rl_summary = "Severe constraint conflict detected. The optimizer had to run through multiple relaxation stages (dropping beta, sector, and turnover limits) to find a mathematically viable solution."
_rl_items = f'Skipped {len(relax_log)} constraint layers to guarantee a valid portfolio.'
else:
_rl_summary = "Initial constraints were infeasible — the engine auto-relaxed these rules to find a solution:"
_rl_items = "".join(f'{r}' for r in relax_log)
_cd_inner += (
'Constraint Relaxation History
'
f'{_rl_summary}
'
f''
)
elif not binding_cons:
_cd_inner = (
'All Constraints Satisfied
'
''
'No binding constraints or relaxations required. The optimizer found a clean solution.
'
)
return (
''
'
'
'Constraint Diagnostics
'
f'{_cd_inner}
'
)