thisisbbln commited on
Commit
aabd2be
·
verified ·
1 Parent(s): 43ba2e4

Add bsm_engine.py

Browse files
Files changed (1) hide show
  1. bsm_engine.py +114 -0
bsm_engine.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ bsm_engine.py
3
+ Subagent 3: Derivatives & Executive Compensation Pricing Engine
4
+ Implements Black-Scholes-Merton (BSM) formula, Greeks (Delta, Vega, Gamma),
5
+ and dividend protection simulation under leverage-induced volatility expansion.
6
+ """
7
+
8
+ import numpy as np
9
+ from scipy.stats import norm
10
+ import pandas as pd
11
+
12
+ def black_scholes_call(S, K, T, r, sigma, q=0.0):
13
+ """
14
+ Standard Black-Scholes-Merton Call Option Pricing with Greeks
15
+ """
16
+ if T <= 0:
17
+ return max(0.0, S - K), 0.0, 0.0
18
+
19
+ d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
20
+ d2 = d1 - sigma * np.sqrt(T)
21
+
22
+ call_price = S * np.exp(-q * T) * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
23
+ delta = np.exp(-q * T) * norm.cdf(d1)
24
+ vega = S * np.exp(-q * T) * norm.pdf(d1) * np.sqrt(T)
25
+ gamma = (norm.pdf(d1) * np.exp(-q * T)) / (S * sigma * np.sqrt(T))
26
+
27
+ return call_price, delta, vega, gamma
28
+
29
+ def simulate_dividend_impact(S_0, K_0, T, r, sigma_0, dividend, sigma_increase=0.10):
30
+ """
31
+ Compares:
32
+ 1. Baseline (T0): Value prior to distribution.
33
+ 2. Scenario A (Unprotected): S -> S - D, K unchanged.
34
+ 3. Scenario B (Protected + Volatility Expansion): K -> K - D, S -> S - D, sigma -> sigma + sigma_increase.
35
+ """
36
+ val_pre, delta_pre, vega_pre, gamma_pre = black_scholes_call(S_0, K_0, T, r, sigma_0)
37
+
38
+ # Scenario A: Unprotected (Stock drops by dividend, Strike unchanged)
39
+ S_post = max(0.01, S_0 - dividend)
40
+ val_unprotected, delta_unprot, vega_unprot, _ = black_scholes_call(S_post, K_0, T, r, sigma_0)
41
+
42
+ # Protected baseline (Strike adjusted to K - D, but volatility unchanged)
43
+ K_post = max(0.01, K_0 - dividend)
44
+ val_protected_same_vol, _, _, _ = black_scholes_call(S_post, K_post, T, r, sigma_0)
45
+
46
+ # Scenario B: Protected + Leverage-Induced Volatility Jump
47
+ sigma_post = sigma_0 + sigma_increase
48
+ val_protected_higher_vol, delta_prot, vega_prot, gamma_prot = black_scholes_call(
49
+ S=S_post,
50
+ K=K_post,
51
+ T=T,
52
+ r=r,
53
+ sigma=sigma_post
54
+ )
55
+
56
+ net_benefit_vs_unprotected = val_protected_higher_vol - val_unprotected
57
+ net_benefit_vs_pre = val_protected_higher_vol - val_pre
58
+
59
+ return {
60
+ "S_0": S_0,
61
+ "K_0": K_0,
62
+ "Dividend": dividend,
63
+ "S_Post": S_post,
64
+ "K_Post": K_post,
65
+ "T": T,
66
+ "Risk_Free_Rate": r,
67
+ "Sigma_0": sigma_0,
68
+ "Sigma_Post": sigma_post,
69
+ "Sigma_Increase": sigma_increase,
70
+ "Value_Pre_Div": val_pre,
71
+ "Value_Unprotected": val_unprotected,
72
+ "Value_Protected_Same_Vol": val_protected_same_vol,
73
+ "Value_Protected_Higher_Vol": val_protected_higher_vol,
74
+ "Loss_If_Unprotected": val_unprotected - val_pre,
75
+ "Gain_From_Protection_Clause": val_protected_same_vol - val_unprotected,
76
+ "Gain_From_Leverage_Volatility": val_protected_higher_vol - val_protected_same_vol,
77
+ "Net_Benefit_To_Executive": net_benefit_vs_unprotected,
78
+ "Total_Value_Change_Post_Event": net_benefit_vs_pre,
79
+ "Delta_Pre": delta_pre,
80
+ "Delta_Post": delta_prot,
81
+ "Vega_Pre": vega_pre,
82
+ "Vega_Post": vega_prot
83
+ }
84
+
85
+ def run_sensitivity_grid(S_0, K_0, T, r, sigma_0, dividend):
86
+ """
87
+ Runs multi-parameter grid across strike moneyness and volatility shocks (0.02 to 0.16).
88
+ """
89
+ moneyness_factors = [0.8, 0.9, 1.0, 1.1, 1.2]
90
+ vol_shocks = [0.02, 0.05, 0.08, 0.10, 0.12, 0.15]
91
+
92
+ records = []
93
+ for m in moneyness_factors:
94
+ curr_k = S_0 * m
95
+ for v_shock in vol_shocks:
96
+ res = simulate_dividend_impact(S_0, curr_k, T, r, sigma_0, dividend, sigma_increase=v_shock)
97
+ records.append({
98
+ "Moneyness_K_over_S": m,
99
+ "Strike_K": curr_k,
100
+ "Vol_Shock": v_shock,
101
+ "Val_Pre": res["Value_Pre_Div"],
102
+ "Val_Unprot": res["Value_Unprotected"],
103
+ "Val_Prot_Vol": res["Value_Protected_Higher_Vol"],
104
+ "Net_Gain_NIS": res["Net_Benefit_To_Executive"],
105
+ "Pct_Gain": (res["Value_Protected_Higher_Vol"] / res["Value_Unprotected"] - 1) * 100,
106
+ "Vega": res["Vega_Post"]
107
+ })
108
+ return pd.DataFrame(records)
109
+
110
+ if __name__ == "__main__":
111
+ test_sim = simulate_dividend_impact(S_0=100.0, K_0=100.0, T=3.0, r=0.04, sigma_0=0.30, dividend=15.0, sigma_increase=0.10)
112
+ print("BSM Simulation Test:")
113
+ for k, v in test_sim.items():
114
+ print(f" {k}: {v:.4f}" if isinstance(v, float) else f" {k}: {v}")