Loading...
Loading...
Pricing completo de opciones europeas y americanas. 9 metodos: Black-Scholes, Binomial CRR, Trinomial, Monte Carlo (antithetic) + Longstaff-Schwartz, Bjerksund-Stensland 2002 / BAW (American closed-form), Heston 1993 (vol estocastica, sonrisa via Fourier), Bates 1996 (Heston + Merton jumps, crash risk), greeks (BS), implied vol, P(ITM) y P(Profit). Disenado para backtesting: cada funcion es flat Python vectorizado con numpy (sin abstracciones), usa math.erfc (no scipy). BS 2.4 us/op, BS2 3.6 us, Heston 400 us, Binomial N=500 5.6 ms. CLI con 15 modos mas validate y bench. Time complexity O(1) para todos los closed-form.
npx skill4agent add gauss314/skills option-pricingreferences/REFERENCE.md# 1. Black-Scholes (europea)
py scripts/option_pricing.py bs --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
# 2. Binomial CRR (europea o americana)
py scripts/option_pricing.py binomial --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 --style american
# 3. Trinomial (europea o americana)
py scripts/option_pricing.py trinomial --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
# 4. Monte Carlo (europea, antithetic variates)
py scripts/option_pricing.py mc --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 --paths 200000
# 5. Longstaff-Schwartz (americana via MC)
py scripts/option_pricing.py lsm --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 \
--style american --paths 100000 --steps 50
# 6. Barone-Adesi-Whaley (americana closed-form)
py scripts/option_pricing.py bs2 --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 --q 0.04 \
--style american
# 7. Greeks analiticos (BS)
py scripts/option_pricing.py greeks --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
# 8. Implied volatility
py scripts/option_pricing.py iv --S 100 --K 100 --T 0.25 --r 0.05 --price 4.62
# 9. P(ITM) y P(Profit) bajo medida risk-neutral Q
py scripts/option_pricing.py pitm --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
py scripts/option_pricing.py pitm --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 --premium 4.62
# 10. Superficie de precios across strikes
py scripts/option_pricing.py surface --S 100 --T 0.25 --r 0.05 --sigma 0.20 \
--K-min 80 --K-max 120 --K-step 5
# 11. Heston 1993 (vol estocastica, sonrisa via Fourier)
py scripts/option_pricing.py heston --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 \
--v0 0.04 --kappa 2.0 --theta 0.04 --sigma_v 0.3 --rho -0.5
# 12. Bates 1996 (Heston + Merton jumps, captura crash risk)
py scripts/option_pricing.py bates --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20 \
--v0 0.04 --kappa 2.0 --theta 0.04 --sigma_v 0.3 --rho -0.5 \
--lam 1.0 --mu_J -0.05 --sigma_J 0.10
# 13. Comparar todos los metodos aplicables
py scripts/option_pricing.py all --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
# Validar contra casos de assets/validation_cases.json
py scripts/option_pricing.py validate
# Benchmark de todos los metodos
py scripts/option_pricing.py bench --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20skills/option-pricing/
├── SKILL.md # Este archivo (guia rapida)
├── references/
│ └── REFERENCE.md # Teoria completa de los 5 metodos
├── assets/
│ ├── defaults.json # Parametros default para el CLI
│ └── validation_cases.json # Casos de test (Hull Examples + extra)
└── scripts/
└── option_pricing.py # CLI con 15 modos + validate + bench| Flag | Default | Descripcion |
|---|---|---|
| 100.0 | Spot price del subyacente |
| 100.0 | Strike |
| 0.25 | Tiempo a maturity en anos (0.25 = 3 meses) |
| 0.05 | Tasa libre de riesgo (continua anual) |
| 0.0 | Dividend yield continuo anual |
| 0.20 | Volatilidad anualizada |
| call | call o put |
| european | european o american |
| 500 | Pasos del tree / pasos temporales de LSM |
| 100000 | Paths de Monte Carlo |
| 42 | Seed para reproducibilidad |
| True | Activar variates antitetic (MC) |
| False | Output en JSON en vez de tabla |
assets/defaults.jsonS=100, K=100, T=0.25, r=0.05, q=0, sigma=0.20time.perf_counter()| Metodo | Time complexity | us/op (medido) | ops/sec | Estilo | Error tipico |
|---|---|---|---|---|---|
Black-Scholes | O(1) | 2.4 us | 419k/s | European | 0 (closed) |
P(ITM) | O(1) | 1.1 us | 908k/s | Ambos | 0 (closed N(d2)) |
Greeks (BS) | O(1) | 3.9 us | 257k/s | European | 0 (closed) |
BS2/BAW | O(1) | 3.6 us | 276k/s | American | <1% vs binomial N=2000 |
IV solve | O(log(1/eps) * 1 opcion) | 82 us | 12k/s | Ambos | 1e-7 |
Heston | O(N_GL) ~ O(1) | 398 us | 2.5k/s | European | <0.1% |
Binomial N=500 | O(N^2) | 5.6 ms | 178/s | Ambos | ~0.5% |
Bates | O(15 * N_GL) | 6.2 ms | 160/s | European | <0.5% |
MC paths=10k | O(paths) | 1.3 ms | 788/s | European | ~1% (stderr) |
Trinomial N=500 | O(N^2) | 9.4 ms | 107/s | Ambos | ~0.3% |
MC paths=100k | O(paths) | 5.6 ms | 177/s | European | ~0.1% (stderr) |
Binomial N=2000 | O(N^2) | 31 ms | 32/s | Ambos | ~0.1% |
LSM paths=10k | O(paths * steps) | ~150 ms | ~7/s | American | ~1-2% |
bsbs2hestonbinomial --steps 2000hestonbatesbs2binomiallsmbinomialtrinomialmclsmbs2q >= rivsigma_implpitmN(d2)N(-d2)N(d2)N(-d2)N(d2')K +/- premiumP(Profit) > X%r - qr# Para cada dia historico:
# 1. Obtener IV actual (e.g. de yahoo-finance) y precio de mercado
# 2. Calcular precio teorico con IV_historica
# 3. Comparar contra precio de mercado
# Precio teorico con IV_historica
py scripts/option_pricing.py bs --S 580 --K 580 --T 0.08 --r 0.05 --sigma 0.18
# -> 12.34
# Precio teorico con IV_actual (subestimada por el mercado)
py scripts/option_pricing.py bs --S 580 --K 580 --T 0.08 --r 0.05 --sigma 0.22
# -> 15.67
# P&L esperado = (market - teorico) = 15.67 - 12.34 = +3.33 (long vol paga)# Para cada strike/expiry:
py scripts/option_pricing.py iv --S 580 --K 590 --T 0.08 --r 0.05 --price 8.50
# -> 0.2145 (la IV implicita de esa opcion)py scripts/option_pricing.py greeks --S 580 --K 580 --T 0.08 --r 0.05 --sigma 0.20 --json
# Devuelve {delta: 0.512, gamma: 0.018, vega: 0.85, theta: -0.12, rho: 0.31}
# Comprar 1000 opc + short 512 acciones = delta neutral# Opcion sobre indice con yield alto (e.g. SPX con div yield ~1.5%)
py scripts/option_pricing.py bs2 --S 5800 --K 5800 --T 0.25 --r 0.05 --q 0.015 \
--sigma 0.18 --type call --style american
# -> ~123.45 (vs BS europea ~120.10, premium de early exercise = $3.35)all--style# Ejemplo 1: American put (Hull 21.1)
py scripts/option_pricing.py all --S 50 --K 50 --T 0.4167 --r 0.10 --sigma 0.40 \
--type put --style american
# Output (9 rows: BS, Binomial, Trinomial, MC, LSM, BS2, P(ITM), P(Profit), Greeks):
# +---------------------------+------------------+-----------------------+
# | Method | Config | Price / Value |
# +---------------------------+------------------+-----------------------+
# | Black-Scholes | closed-form O(1) | 4.076101 | (europea ref)
# | Binomial CRR | N=500 | 4.283160 |
# | Trinomial | N=500 | 4.283429 |
# | Monte Carlo | paths=100000 | 4.077892 +/- 0.0254 | (europea)
# | Longstaff-Schwartz | paths=100000 | 4.258337 |
# | BS2/BAW (closed-form) | O(1) | 4.283766 |
# | P(ITM) | N(d2) bajo Q | 0.4871 |
# | P(Profit) vs BS price | premio=4.0761 | 0.3588 |
# | Greeks (delta/gamma/vega) | BS closed-form | d=-0.3857 g=+0.0296 |
# +---------------------------+------------------+-----------------------+
# Ejemplo 2: European call (incluye Heston y Bates, NO incluye LSM/BS2)
py scripts/option_pricing.py all --S 100 --K 100 --T 0.25 --r 0.05 --sigma 0.20
# Output (9 rows: BS, Binomial, Trinomial, MC, Heston, Bates, P(ITM), P(Profit), Greeks):
# +---------------------------+----------------------------------------+-----------------------+
# | Method | Config | Price / Value |
# +---------------------------+----------------------------------------+-----------------------+
# | Black-Scholes | closed-form O(1) | 4.614997 |
# | Binomial CRR | N=500 | 4.613001 |
# | Trinomial | N=500 | 4.613999 |
# | Monte Carlo | paths=100000 | 4.620907 +/- 0.0295 |
# | Heston 1993 | v0=s2, k=2, th=s2, sv=0.3, rho=-0.5 | 4.577095 |
# | Bates 1996 | Heston + lam=1, mu_J=-0.05, sig_J=0.10 | 4.924756 |
# | P(ITM) | N(d2) bajo Q | 0.5299 |
# | P(Profit) vs BS price | premio=4.6150 | 0.3534 |
# | Greeks (delta/gamma/vega) | BS closed-form | d=+0.5695 g=+0.0393 |
# +---------------------------+----------------------------------------+-----------------------+all--style american--style europeanallv0=sigma^2, kappa=2, theta=v0, sigma_v=0.3, rho=-0.5, lambda=1, mu_J=-0.05, sigma_J=0.10hestonbatesqnumpy.random.Generatordefault_rngnp.vectorize(bs_price)numbaT, r, q, sigma, Kvalidateassets/validation_cases.jsonpy scripts/option_pricing.py validate
# === Black-Scholes European ===
# [OK] Hull 9th ed Example 15.6 (ATM call): got 4.7594, ref 4.7594
# [OK] ... (5/5 pass)
# === American (Binomial N=2000) ===
# [OK] Hull 9th ed Example 21.1: got 4.2841, ref 4.2841
# [OK] ... (4/4 pass)
# === Put-Call Parity (BS) ===
# [OK] C - P = 4.8770575499, S*exp(-qT) - K*exp(-rT) = 4.8770575499
# 0 failure(s)assets/validation_cases.jsonfrom scripts.option_pricing import bs_price, binomial_price, mc_european_price, lsm_price, bs2_american_price, bs_greeks, implied_vol
# Pricing
c = bs_price(100, 100, 0.25, 0.05, 0.0, 0.20, "call") # 4.615
p = binomial_price(100, 100, 0.25, 0.05, 0.0, 0.20, 500, "put", "european")
# Greeks (solo BS)
g = bs_greeks(100, 100, 0.25, 0.05, 0.0, 0.20, "call")
# -> {"delta": 0.569, "gamma": 0.039, "vega": 19.64, "theta": -10.47, "rho": 13.08}
# Implied vol
iv = implied_vol(4.62, 100, 100, 0.25, 0.05, 0.0, "call", "european")
# -> 0.2003np.vectorize