-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickstart.py
More file actions
154 lines (133 loc) · 4.21 KB
/
Copy pathquickstart.py
File metadata and controls
154 lines (133 loc) · 4.21 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""
Minimal Python walkthrough for pyquant_pricer bindings.
Run with:
python -m python.examples.quickstart
"""
from __future__ import annotations
import importlib.util
import subprocess
import sys
from pathlib import Path
import numpy as np
import pyquant_pricer as qp
def price_vanilla() -> None:
spot = 100.0
strike = 105.0
rate = 0.02
dividend = 0.01
vol = 0.25
expiry = 0.5
call = qp.bs_call(spot, strike, rate, dividend, vol, expiry)
delta = qp.bs_delta_call(spot, strike, rate, dividend, vol, expiry)
print(f"Black–Scholes call: {call:.4f} (delta {delta:.4f})")
def price_barrier() -> None:
barrier = qp.BarrierSpec()
barrier.type = qp.BarrierType.DownOut
barrier.B = 95.0
barrier.rebate = 0.0
spot = 100.0
strike = 100.0
rate = 0.03
dividend = 0.0
vol = 0.2
expiry = 1.0
price = qp.barrier_bs(
qp.OptionType.Call, barrier, spot, strike, rate, dividend, vol, expiry
)
print(f"Down-and-out call (analytic RR): {price:.4f}")
def heston_helpers() -> None:
params = qp.HestonParams()
params.kappa = 1.5
params.theta = 0.04
params.sigma = 0.5
params.rho = -0.5
params.v0 = 0.04
market = qp.HestonMarket()
market.spot = 100.0
market.strike = 100.0
market.rate = 0.01
market.dividend = 0.0
market.time = 1.0
iv = qp.heston_implied_vol(market, params)
phi = qp.heston_characteristic_fn(1.0, market, params)
print(
f"Heston analytic call IV: {iv:.4%} | phi(1) = {phi.real:.4f} + {phi.imag:.4f}i"
)
def price_heston_batch() -> None:
"""Price an analytic Heston batch; no simulation or market data is used."""
markets = np.array(
[
[100.0, 90.0, 0.015, 0.005, 0.5],
[100.0, 100.0, 0.015, 0.005, 1.0],
[100.0, 110.0, 0.015, 0.005, 2.0],
],
dtype=np.float64,
)
params = np.array([[1.5, 0.04, 0.6, -0.45, 0.04]])
metrics = qp.heston_call_metrics_batch(markets, params)
print(f"Heston analytic batch: [call_price, implied_vol] {metrics.tolist()}")
def portfolio_risk_and_stress() -> None:
"""Value and stress a mixed long/short call-put portfolio."""
positions = np.array(
[
[1, 120, 100, 95, 0.03, 0.01, 0.22, 90 / 365],
[-1, -80, 100, 105, 0.03, 0.01, 0.25, 90 / 365],
[1, 50, 100, 110, 0.03, 0.01, 0.28, 180 / 365],
],
dtype=np.float64,
)
risk = qp.bs_portfolio_risk(positions)
totals = dict(zip(risk["total_columns"], risk["portfolio_totals"]))
shocks = np.array(
[[0, 0, 0, 0, 0], [-0.10, 0.08, 0.01, 0, 1 / 365]],
dtype=np.float64,
)
pnl = qp.bs_portfolio_scenarios(positions, shocks, detail=False)["portfolio_pnl"]
print(f"Portfolio risk: {totals}")
print(f"Exact scenario P&L: {pnl.tolist()}")
def maybe_run_heston(repo_root: Path) -> None:
optional_modules = ("matplotlib", "pandas", "scipy")
missing = [
name for name in optional_modules if importlib.util.find_spec(name) is None
]
if missing:
print(
"Optional Heston calibration dependencies are unavailable "
f"({', '.join(missing)}); skipping calibration demo."
)
return
samples_dir = repo_root / "data" / "samples"
normalized_dir = repo_root / "data" / "normalized"
candidates = list(samples_dir.glob("spx_*.csv")) + list(
normalized_dir.glob("spy_*.csv")
)
if not candidates:
print("No normalized surfaces found; skipping Heston demo.")
return
surface = sorted(candidates)[0]
cmd = [
sys.executable,
str(repo_root / "scripts" / "calibrate_heston.py"),
"--input",
str(surface),
"--fast",
"--metric",
"price",
"--seed",
"19",
"--retries",
"3",
]
print(f"Running Heston FAST calibration on {surface.name} ...")
subprocess.run(cmd, cwd=repo_root, check=True)
def main() -> None:
repo_root = Path(__file__).resolve().parents[2]
price_vanilla()
price_barrier()
heston_helpers()
price_heston_batch()
portfolio_risk_and_stress()
maybe_run_heston(repo_root)
if __name__ == "__main__":
main()