Spaces:
Sleeping
Sleeping
File size: 5,474 Bytes
bc308f6 | 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 | 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)
|