File size: 4,521 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
import numpy as np
import pandas as pd

BEHAVIOURAL_FEATURE_NAMES = [
    "liquidity_stress_curve",
    "behavioural_adaptation_index",
    "recovery_velocity_score",
    "financial_resilience_score",
    "expense_rigidity_index",
    "behavioural_deterioration_velocity",
    "runway_decay_trajectory",
    "overdraft_persistence_score",
    "financial_shock_recovery_curve",
    "cashflow_stability_persistence"
]

def add_behavioural_features(df):
    """Calculates and appends 10 core behavioural intelligence features to the DataFrame."""
    df_out = df.copy()
    
    # Check if empty
    if df_out.empty:
        for col in BEHAVIOURAL_FEATURE_NAMES:
            df_out[col] = pd.Series(dtype=float)
        return df_out
        
    # Helper series with defaults if columns don't exist
    def get_col(name, default_val=0.0):
        if name in df_out.columns:
            return df_out[name].fillna(default_val)
        return pd.Series(default_val, index=df_out.index)

    runway_months = get_col("runway_months", 1.0)
    liquidity_decay_rate = get_col("liquidity_decay_rate", 0.0)
    overdraft_days_30d = get_col("overdraft_days_30d", 0.0)
    expense_acceleration_instability = get_col("expense_acceleration_instability", 1.0)
    discretionary_spend_ratio = get_col("discretionary_spend_ratio", 0.3)
    recovery_half_life = get_col("recovery_half_life", 15.0)
    savings_rate = get_col("savings_rate", 0.0)
    credit_score = get_col("credit_score", 600.0)
    runway_deterioration_slope = get_col("runway_deterioration_slope", 0.0)
    liquidity_collapse_velocity = get_col("liquidity_collapse_velocity", 0.0)
    recent_failed_payments_30d = get_col("recent_failed_payments_30d", 0.0)
    overdraft_persistence_ratio = get_col("overdraft_persistence_ratio", 0.0)
    emergency_depletion_velocity = get_col("emergency_depletion_velocity", 0.0)
    income_volatility = get_col("income_volatility", 0.1)
    expense_volatility = get_col("expense_volatility", 0.2)

    # 1. Liquidity Stress Curve: Probability of checking balance falling < 0 in 30 days
    # Higher decay, higher overdraft history, lower runway = higher stress probability
    df_out["liquidity_stress_curve"] = 1.0 / (1.0 + np.exp(
        3.0 - 0.5 * runway_months + 10.0 * liquidity_decay_rate + 0.1 * overdraft_days_30d
    ))

    # 2. Behavioural Adaptation Index: Ratio of discretionary spend contraction under stress
    # Higher instability * discretionary ratio implies poor adaptation/rigid structure (index closer to 0)
    df_out["behavioural_adaptation_index"] = np.clip(1.0 - expense_acceleration_instability * discretionary_spend_ratio, 0.0, 1.0)

    # 3. Recovery Velocity Score: Rebound speed of account balance
    df_out["recovery_velocity_score"] = 1.0 / (1.0 + recovery_half_life / 30.0)

    # 4. Financial Resilience Score: Composite score (0-100)
    # Reuses base savings rate, savings runway, and normalized credit score
    norm_credit = credit_score.map(lambda c: 0.0 if pd.isna(c) or c == 0 else c)
    df_out["financial_resilience_score"] = np.clip(
        savings_rate * 40.0 + (runway_months / 6.0) * 30.0 + (norm_credit / 850.0) * 30.0, 
        0.0, 100.0
    )

    # 5. Expense Rigidity Index: Fixed expenses / net income
    # Fixed expenses roughly = 1.0 - discretionary spend ratio
    df_out["expense_rigidity_index"] = 1.0 - discretionary_spend_ratio

    # 6. Behavioural Deterioration Velocity: Transition speed toward financial distress
    df_out["behavioural_deterioration_velocity"] = np.clip(
        runway_deterioration_slope * 0.4 + liquidity_collapse_velocity * 0.4 + recent_failed_payments_30d * 0.2,
        0.0, 1.0
    )

    # 7. Runway Decay Trajectory: Burn rate depletion curve projection (months left under deterioration)
    df_out["runway_decay_trajectory"] = np.clip(runway_months * (1.0 - runway_deterioration_slope), 0.0, 24.0)

    # 8. Overdraft Persistence Score: Overdraft usage ratio
    df_out["overdraft_persistence_score"] = overdraft_persistence_ratio

    # 9. Financial Shock Recovery Curve: Probability of recovery from an emergency depletion
    df_out["financial_shock_recovery_curve"] = np.clip(
        1.0 - emergency_depletion_velocity * (1.0 - recovery_half_life / 30.0),
        0.0, 1.0
    )

    # 10. Cashflow Stability Persistence: Normalized consistency of cash flow streams
    df_out["cashflow_stability_persistence"] = np.clip(
        1.0 - (income_volatility + expense_volatility) / 2.0,
        0.0, 1.0
    )
    
    return df_out