Spaces:
Sleeping
Sleeping
Download src/event_simulation.py from frigate-global/financial-risk-api: direct link, hf CLI and curl.
- Browser
- Download file 5.47 kB
-
https://huggingface.co/spaces/frigate-global/financial-risk-api/resolve/main/src/event_simulation.py
- Command line
-
hf download hf://spaces/frigate-global/financial-risk-api/src/event_simulation.py
-
curl -L -o event_simulation.py https://huggingface.co/spaces/frigate-global/financial-risk-api/resolve/main/src/event_simulation.py
5.47 kB
| import random | |
| import datetime | |
| import numpy as np | |
| class FinancialShockEngine: | |
| """Simulates realistic, multi-stage event-driven financial shocks and behavioral adaptations. | |
| Shock types: | |
| - layoff (job loss, zero income, delayed job search) | |
| - income_cut (underemployment, 30% salary reduction) | |
| - illness (health expense spike + temporary wage reduction) | |
| - macro_inflation (10% increase in utilities, mortgage, groceries) | |
| - housing_shock (sudden rent increase or repair cost) | |
| - debt_cascade (failed payment triggers penalty fee and high borrowing costs) | |
| """ | |
| def __init__(self, random_state=42): | |
| self.random_state = random_state | |
| random.seed(random_state) | |
| def simulate_layoff(self, monthly_income, monthly_expenses, seq_len_days=180): | |
| """Simulates job loss with multi-stage deterioration, delayed recovery, and spending adaptation. | |
| Timeline: | |
| - Day 0: Layoff occurs. Income drops to 0. | |
| - Day 15: Discretionary spend drops by 45% (behavioural adaptation). | |
| - Day 45: Cash depletion forces taking a $1000 cash advance (emergency borrowing). | |
| - Day 90: Finds a new job. Income resumes at 90% of original. | |
| """ | |
| daily_income = monthly_income / 30.0 | |
| daily_expenses = monthly_expenses / 30.0 | |
| # Original spending split: 30% fixed, 40% essential (groceries, utilities), 30% discretionary | |
| discretionary_daily = daily_expenses * 0.30 | |
| fixed_daily = daily_expenses * 0.70 | |
| balances = [1000.0] # start balance | |
| income_stream = [] | |
| expense_stream = [] | |
| shock_flags = [] | |
| for d in range(seq_len_days): | |
| # Phase 1: Income | |
| if d < 0: # baseline | |
| inc = daily_income | |
| elif d < 90: # unemployed phase | |
| inc = 0.0 | |
| else: # re-employed | |
| inc = daily_income * 0.90 | |
| # Phase 2: Expense with behavioral adaptation | |
| if d < 15: | |
| exp = daily_expenses | |
| elif d < 90: | |
| # Discretionary spend contracted by 50% | |
| exp = fixed_daily + discretionary_daily * 0.50 | |
| else: | |
| # Slowly normalize expenses | |
| exp = fixed_daily + discretionary_daily * 0.80 | |
| # Phase 3: Emergency borrowing cascade | |
| borrowing = 0.0 | |
| if d == 45 and balances[-1] < 200.0: | |
| borrowing = 1000.0 # cash advance emergency event | |
| exp += 50.0 # transaction fee | |
| balance_change = inc - exp + borrowing | |
| new_bal = balances[-1] + balance_change | |
| balances.append(new_bal) | |
| income_stream.append(inc) | |
| expense_stream.append(exp) | |
| shock_flags.append(1.0 if (0 <= d < 90) else 0.0) | |
| return np.array(balances[1:]), np.array(income_stream), np.array(expense_stream), np.array(shock_flags) | |
| def simulate_illness(self, monthly_income, monthly_expenses, seq_len_days=180): | |
| """Simulates medical shock: large expense spike + temporary wage reduction. | |
| Timeline: | |
| - Day 10: Illness strike. Sudden $1500 expense spike (hospital fee). | |
| - Day 10-40: Sick leave. Income reduced by 50%. | |
| - Day 40+: Returns to work. Normal income. Expenses return to normal. | |
| """ | |
| daily_income = monthly_income / 30.0 | |
| daily_expenses = monthly_expenses / 30.0 | |
| balances = [2000.0] | |
| income_stream = [] | |
| expense_stream = [] | |
| shock_flags = [] | |
| for d in range(seq_len_days): | |
| # Income | |
| if 10 <= d < 40: | |
| inc = daily_income * 0.50 # wage loss | |
| else: | |
| inc = daily_income | |
| # Expense | |
| exp = daily_expenses | |
| if d == 10: | |
| exp += 1500.0 # medical bill spike | |
| balance_change = inc - exp | |
| new_bal = balances[-1] + balance_change | |
| balances.append(new_bal) | |
| income_stream.append(inc) | |
| expense_stream.append(exp) | |
| shock_flags.append(1.0 if (10 <= d < 40) else 0.0) | |
| return np.array(balances[1:]), np.array(income_stream), np.array(expense_stream), np.array(shock_flags) | |
| def simulate_macro_inflation(self, monthly_income, monthly_expenses, seq_len_days=180): | |
| """Simulates inflationary pressures: constant 15% increase in expenses, zero behavioral adaptation delay.""" | |
| daily_income = monthly_income / 30.0 | |
| daily_expenses = monthly_expenses / 30.0 | |
| balances = [1500.0] | |
| income_stream = [] | |
| expense_stream = [] | |
| shock_flags = [] | |
| for d in range(seq_len_days): | |
| inc = daily_income | |
| # Constantly higher expenses | |
| exp = daily_expenses * 1.15 | |
| balance_change = inc - exp | |
| new_bal = balances[-1] + balance_change | |
| balances.append(new_bal) | |
| income_stream.append(inc) | |
| expense_stream.append(exp) | |
| shock_flags.append(1.0) | |
| return np.array(balances[1:]), np.array(income_stream), np.array(expense_stream), np.array(shock_flags) | |