Download bsm_engine.py from Azofam/Altman: direct link, hf CLI and curl.
- Browser
- Download file 4.56 kB
-
https://huggingface.co/datasets/Azofam/Altman/resolve/main/bsm_engine.py
- Command line
-
hf download hf://datasets/Azofam/Altman/bsm_engine.py
-
curl -L -o bsm_engine.py https://huggingface.co/datasets/Azofam/Altman/resolve/main/bsm_engine.py
4.56 kB
| """ | |
| bsm_engine.py | |
| Subagent 3: Derivatives & Executive Compensation Pricing Engine | |
| Implements Black-Scholes-Merton (BSM) formula, Greeks (Delta, Vega, Gamma), | |
| and dividend protection simulation under leverage-induced volatility expansion. | |
| """ | |
| import numpy as np | |
| from scipy.stats import norm | |
| import pandas as pd | |
| def black_scholes_call(S, K, T, r, sigma, q=0.0): | |
| """ | |
| Standard Black-Scholes-Merton Call Option Pricing with Greeks | |
| """ | |
| if T <= 0: | |
| return max(0.0, S - K), 0.0, 0.0 | |
| d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) | |
| d2 = d1 - sigma * np.sqrt(T) | |
| call_price = S * np.exp(-q * T) * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2) | |
| delta = np.exp(-q * T) * norm.cdf(d1) | |
| vega = S * np.exp(-q * T) * norm.pdf(d1) * np.sqrt(T) | |
| gamma = (norm.pdf(d1) * np.exp(-q * T)) / (S * sigma * np.sqrt(T)) | |
| return call_price, delta, vega, gamma | |
| def simulate_dividend_impact(S_0, K_0, T, r, sigma_0, dividend, sigma_increase=0.10): | |
| """ | |
| Compares: | |
| 1. Baseline (T0): Value prior to distribution. | |
| 2. Scenario A (Unprotected): S -> S - D, K unchanged. | |
| 3. Scenario B (Protected + Volatility Expansion): K -> K - D, S -> S - D, sigma -> sigma + sigma_increase. | |
| """ | |
| val_pre, delta_pre, vega_pre, gamma_pre = black_scholes_call(S_0, K_0, T, r, sigma_0) | |
| # Scenario A: Unprotected (Stock drops by dividend, Strike unchanged) | |
| S_post = max(0.01, S_0 - dividend) | |
| val_unprotected, delta_unprot, vega_unprot, _ = black_scholes_call(S_post, K_0, T, r, sigma_0) | |
| # Protected baseline (Strike adjusted to K - D, but volatility unchanged) | |
| K_post = max(0.01, K_0 - dividend) | |
| val_protected_same_vol, _, _, _ = black_scholes_call(S_post, K_post, T, r, sigma_0) | |
| # Scenario B: Protected + Leverage-Induced Volatility Jump | |
| sigma_post = sigma_0 + sigma_increase | |
| val_protected_higher_vol, delta_prot, vega_prot, gamma_prot = black_scholes_call( | |
| S=S_post, | |
| K=K_post, | |
| T=T, | |
| r=r, | |
| sigma=sigma_post | |
| ) | |
| net_benefit_vs_unprotected = val_protected_higher_vol - val_unprotected | |
| net_benefit_vs_pre = val_protected_higher_vol - val_pre | |
| return { | |
| "S_0": S_0, | |
| "K_0": K_0, | |
| "Dividend": dividend, | |
| "S_Post": S_post, | |
| "K_Post": K_post, | |
| "T": T, | |
| "Risk_Free_Rate": r, | |
| "Sigma_0": sigma_0, | |
| "Sigma_Post": sigma_post, | |
| "Sigma_Increase": sigma_increase, | |
| "Value_Pre_Div": val_pre, | |
| "Value_Unprotected": val_unprotected, | |
| "Value_Protected_Same_Vol": val_protected_same_vol, | |
| "Value_Protected_Higher_Vol": val_protected_higher_vol, | |
| "Loss_If_Unprotected": val_unprotected - val_pre, | |
| "Gain_From_Protection_Clause": val_protected_same_vol - val_unprotected, | |
| "Gain_From_Leverage_Volatility": val_protected_higher_vol - val_protected_same_vol, | |
| "Net_Benefit_To_Executive": net_benefit_vs_unprotected, | |
| "Total_Value_Change_Post_Event": net_benefit_vs_pre, | |
| "Delta_Pre": delta_pre, | |
| "Delta_Post": delta_prot, | |
| "Vega_Pre": vega_pre, | |
| "Vega_Post": vega_prot | |
| } | |
| def run_sensitivity_grid(S_0, K_0, T, r, sigma_0, dividend): | |
| """ | |
| Runs multi-parameter grid across strike moneyness and volatility shocks (0.02 to 0.16). | |
| """ | |
| moneyness_factors = [0.8, 0.9, 1.0, 1.1, 1.2] | |
| vol_shocks = [0.02, 0.05, 0.08, 0.10, 0.12, 0.15] | |
| records = [] | |
| for m in moneyness_factors: | |
| curr_k = S_0 * m | |
| for v_shock in vol_shocks: | |
| res = simulate_dividend_impact(S_0, curr_k, T, r, sigma_0, dividend, sigma_increase=v_shock) | |
| records.append({ | |
| "Moneyness_K_over_S": m, | |
| "Strike_K": curr_k, | |
| "Vol_Shock": v_shock, | |
| "Val_Pre": res["Value_Pre_Div"], | |
| "Val_Unprot": res["Value_Unprotected"], | |
| "Val_Prot_Vol": res["Value_Protected_Higher_Vol"], | |
| "Net_Gain_NIS": res["Net_Benefit_To_Executive"], | |
| "Pct_Gain": (res["Value_Protected_Higher_Vol"] / res["Value_Unprotected"] - 1) * 100, | |
| "Vega": res["Vega_Post"] | |
| }) | |
| return pd.DataFrame(records) | |
| if __name__ == "__main__": | |
| 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) | |
| print("BSM Simulation Test:") | |
| for k, v in test_sim.items(): | |
| print(f" {k}: {v:.4f}" if isinstance(v, float) else f" {k}: {v}") | |