""" Sheet 8: Consolidated macro dashboard. Aggregate time series, key ratios, stress flags from workbook and Monte Carlo. """ from typing import Any, Optional import pandas as pd from fqbm.state import FQBMState from fqbm.sheets.monte_carlo import run_n_simulations, ShockSpec def dashboard_aggregate( state: FQBMState, mc_runs: int = 0, shock_spec: Optional[ShockSpec] = None, ) -> dict[str, Any]: """ Build dashboard: state snapshot, key ratios, optional MC summary. """ rwa = state.Loans cr = (state.E_b / rwa) if rwa > 0 else 0 ratios = { "capital_ratio": cr, "reserves_to_deposits": (state.R / state.Deposits) if state.Deposits else 0, "loans_to_deposits": (state.Loans / state.Deposits) if state.Deposits else 0, } snapshot = { "B": state.B, "R": state.R, "C": state.C, "Loans": state.Loans, "Deposits": state.Deposits, "E_cb": state.E_cb, "E_b": state.E_b, "S": state.S, "Spread": state.Spread, "O": state.O, "L_cb": getattr(state, "L_cb", 0), } out = {"state": snapshot, "ratios": ratios} # Part VII: shadow banking leverage ratio (bank) try: from fqbm.sheets.shadow_banking import leverage_ratio total_assets_bank = state.Loans out["ratios"]["leverage_ratio_bank"] = leverage_ratio(total_assets_bank, state.E_b) except Exception: pass if mc_runs > 0: df = run_n_simulations(mc_runs, state, shock_spec) out["mc_summary"] = { "p_insolvency": df["insolvent"].mean(), "p_reserve_breach": df["reserve_breach"].mean(), "inflation_mean": df["inflation"].mean(), "inflation_std": df["inflation"].std(), "p_debt_unsustainable": 1 - df["debt_sustainable"].mean(), } return out