-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxsynthetic_svi.py
More file actions
388 lines (354 loc) · 13.4 KB
/
Copy pathxsynthetic_svi.py
File metadata and controls
388 lines (354 loc) · 13.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
"""
Generate synthetic European option prices from SVI and optionally fit SVI back.
"""
from __future__ import annotations
import math
import sys
from datetime import date, timedelta
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from black_scholes import bs_price, implied_vol
from option_utils import normalize_expiry, parse_date
from svi import fit_svi, svi_total_variance
from scipy.optimize import least_squares
def _parse_multi(argv: list[str], flag: str) -> list[str]:
"""Collect a list of values following a flag."""
if flag not in argv:
return []
idx = argv.index(flag) + 1
vals = []
while idx < len(argv) and not argv[idx].startswith("--"):
vals.append(argv[idx])
idx += 1
return vals
def _as_expiries(as_of: str | None, tenor_days: list[int], expiries: list[str]) -> list[str]:
"""Return a list of expiry dates (YYYY-MM-DD), rolling weekends forward."""
if expiries:
return [normalize_expiry(e) for e in expiries]
t0 = parse_date(as_of) if as_of else date.today()
out = []
for d in tenor_days:
exp = t0 + timedelta(days=d)
if exp.weekday() == 5:
exp = exp + timedelta(days=2)
elif exp.weekday() == 6:
exp = exp + timedelta(days=1)
out.append(exp.isoformat())
return out
def _rate_from_df(dfactor: float, t: float) -> float:
"""Return continuous rate from a discount factor."""
if t <= 0.0:
return 0.0
return -math.log(dfactor) / t
def _generate_for_expiry(
exp: str,
t: float,
forward: float,
dfactor: float,
a: float,
b: float,
rho: float,
m: float,
sigma: float,
strikes: np.ndarray,
opt_types: list[str],
noise: float,
spread: float,
symbol: str,
) -> pd.DataFrame:
"""Return synthetic options for one expiry."""
rate = _rate_from_df(dfactor, t)
spot = forward * dfactor
q = 0.0
rows = []
for k in strikes:
k_log = math.log(k / forward)
w = svi_total_variance(np.array([k_log]), a, b, rho, m, sigma)[0]
if w <= 0.0:
continue
iv = math.sqrt(w / t)
for opt in opt_types:
price = bs_price(opt, spot, k, rate, t, iv, q)
if noise > 0.0:
price = price + np.random.normal(0.0, noise)
price = max(price, 0.0)
bid = max(price - spread / 2.0, 0.0)
ask = price + spread / 2.0
rows.append(
{
"contractSymbol": f"{symbol}{exp.replace('-', '')}{opt.upper()}{k:.2f}",
"expiration": exp,
"strike": float(k),
"option_type": "call" if opt == "c" else "put",
"bid": float(bid),
"ask": float(ask),
"lastPrice": float(price),
"volume": 1,
"openInterest": 1,
}
)
return pd.DataFrame(rows)
def _fit_and_report(df: pd.DataFrame, expiries: list[str], forward: float, dfactor: float, a: float,
b: float, rho: float, m: float, sigma: float, as_of: str | None,
fit_space: str) -> list[dict]:
"""Fit SVI per expiry and print a summary."""
t0 = parse_date(as_of) if as_of else date.today()
rows = []
for exp in expiries:
work = df[df["expiration"] == exp].copy()
if work.empty:
continue
t1 = parse_date(exp)
if t1 == t0:
t0_eff = t0 - timedelta(days=1)
else:
t0_eff = t0
t = (t1 - t0_eff).days / 365.25
if t <= 0:
continue
rate = _rate_from_df(dfactor, t)
spot = forward * dfactor
q = 0.0
work["price"] = (work["bid"] + work["ask"]) / 2.0
strikes = work["strike"].values.astype(float)
prices = work["price"].values.astype(float)
opt_types = work["option_type"].values.astype(str)
mask = np.isfinite(strikes) & np.isfinite(prices) & (prices >= 0.0)
strikes = strikes[mask]
prices = prices[mask]
opt_types = opt_types[mask]
if strikes.size == 0:
continue
if fit_space == "vol":
def _iv(row: pd.Series) -> float:
opt = "c" if row["option_type"] == "call" else "p"
return implied_vol(opt, row["price"], spot, row["strike"], rate, t, q, model="european")
work["iv"] = work.apply(_iv, axis=1)
work = work[np.isfinite(work["iv"]) & (work["iv"] > 0.0)]
if work.empty:
continue
strikes = work["strike"].values.astype(float)
ivs = work["iv"].values.astype(float)
k = np.log(strikes / forward)
w = (ivs ** 2) * t
mask = np.isfinite(k) & np.isfinite(w) & (w > 0.0)
k = k[mask]
w = w[mask]
if k.size == 0:
continue
params = fit_svi(k, w)
else:
k = np.log(strikes / forward)
def resid(params: np.ndarray) -> np.ndarray:
a_fit, b_fit, rho_fit, m_fit, sigma_fit = params
w_fit = svi_total_variance(k, a_fit, b_fit, rho_fit, m_fit, sigma_fit)
w_fit = np.maximum(w_fit, 0.0)
iv_fit = np.sqrt(w_fit / t)
model_prices = []
for kk, iv, opt in zip(strikes, iv_fit, opt_types):
opt_code = "c" if opt == "call" else "p"
model_prices.append(bs_price(opt_code, spot, kk, rate, t, iv, q))
return np.asarray(model_prices) - prices
x0 = np.array([a, b, rho, m, sigma], dtype=float)
lower = np.array([0.0, 0.0, -0.999, -10.0, 1e-6], dtype=float)
upper = np.array([10.0, 10.0, 0.999, 10.0, 5.0], dtype=float)
res = least_squares(resid, x0, bounds=(lower, upper), max_nfev=5000)
a_fit, b_fit, rho_fit, m_fit, sigma_fit = res.x
params = {
"a": a_fit,
"b": b_fit,
"rho": rho_fit,
"m": m_fit,
"sigma": sigma_fit,
"rmse": float(np.sqrt(np.mean(res.fun ** 2))) if res.fun.size else float("nan"),
}
rows.append(
{
"expiry": exp,
"a_fit": params["a"],
"b_fit": params["b"],
"rho_fit": params["rho"],
"m_fit": params["m"],
"sigma_fit": params["sigma"],
"rmse": params["rmse"],
"a_true": a,
"b_true": b,
"rho_true": rho,
"m_true": m,
"sigma_true": sigma,
}
)
if rows:
print("\nSVI fit summary:")
print(pd.DataFrame(rows).to_string(index=False))
return rows
def main(argv: list[str]) -> None:
"""CLI entrypoint."""
if len(argv) < 2:
print("usage: python xsynthetic_svi.py --outfile out.csv [--fit] [--plot [file.png]]")
print(" [--as-of YYYY-MM-DD] [--expiry YYYYMMDD ... | --tenor-days D ...]")
print(" [--fwd F] [--df DF] [--rate R] [--fwd-range L:U] [--n-strikes N]")
print(" [--a A] [--b B] [--rho RHO] [--m M] [--sigma S]")
print(" [--fit-vol | --fit-price]")
print(" [--noise N ...] [--spread S] [--both-sides] [--symbol SYM]")
sys.exit(1)
as_of = None
outfile = None
do_fit = "--fit" in argv
fit_space = "price"
plot = "--plot" in argv
plot_file = None
fwd = 100.0
dfactor = None
rate = 0.0
fwd_range = (0.8, 1.2)
n_strikes = 41
a = 0.01
b = 0.2
rho = -0.3
m = 0.0
sigma = 0.2
noise_vals = [0.0]
spread = 0.02
both_sides = "--both-sides" in argv
symbol = "SYN"
if "--fit-vol" in argv:
fit_space = "vol"
if "--fit-price" in argv:
fit_space = "price"
if "--as-of" in argv:
as_of = normalize_expiry(argv[argv.index("--as-of") + 1])
if "--outfile" in argv:
outfile = argv[argv.index("--outfile") + 1]
if "--plot" in argv:
pidx = argv.index("--plot")
if pidx + 1 < len(argv) and not argv[pidx + 1].startswith("--"):
plot_file = argv[pidx + 1]
if "--fwd" in argv:
fwd = float(argv[argv.index("--fwd") + 1])
if "--df" in argv:
dfactor = float(argv[argv.index("--df") + 1])
if "--rate" in argv:
rate = float(argv[argv.index("--rate") + 1])
if "--fwd-range" in argv:
spec = argv[argv.index("--fwd-range") + 1]
left, right = spec.split(":", 1)
fwd_range = (float(left), float(right))
if "--n-strikes" in argv:
n_strikes = int(argv[argv.index("--n-strikes") + 1])
if "--a" in argv:
a = float(argv[argv.index("--a") + 1])
if "--b" in argv:
b = float(argv[argv.index("--b") + 1])
if "--rho" in argv:
rho = float(argv[argv.index("--rho") + 1])
if "--m" in argv:
m = float(argv[argv.index("--m") + 1])
if "--sigma" in argv:
sigma = float(argv[argv.index("--sigma") + 1])
if "--noise" in argv:
vals = _parse_multi(argv, "--noise")
noise_vals = [float(v) for v in vals] if vals else [0.0]
if "--spread" in argv:
spread = float(argv[argv.index("--spread") + 1])
if "--symbol" in argv:
symbol = argv[argv.index("--symbol") + 1]
expiries = _parse_multi(argv, "--expiry")
tenor_days = _parse_multi(argv, "--tenor-days")
tenor_days = [int(d) for d in tenor_days] if tenor_days else [30]
expiry_list = _as_expiries(as_of, tenor_days, expiries)
if dfactor is None:
if rate != 0.0:
t0 = parse_date(as_of) if as_of else date.today()
dfactor = math.exp(-rate * (tenor_days[0] / 365.25))
else:
dfactor = 1.0
opt_types = ["c", "p"] if both_sides else ["c"]
if not outfile and not do_fit:
print("error: provide --outfile and/or --fit", file=sys.stderr)
sys.exit(1)
fit_results = {}
expiry_grids = {}
for exp in expiry_list:
t1 = parse_date(exp)
t0 = parse_date(as_of) if as_of else date.today()
if t1 == t0:
t0_eff = t0 - timedelta(days=1)
else:
t0_eff = t0
t = (t1 - t0_eff).days / 365.25
if t <= 0:
continue
k_min = fwd * fwd_range[0]
k_max = fwd * fwd_range[1]
expiry_grids[exp] = {
"t": t,
"strikes": np.linspace(k_min, k_max, n_strikes),
}
for noise in noise_vals:
frames = []
for exp in expiry_list:
grid = expiry_grids.get(exp)
if grid is None:
continue
t = grid["t"]
strikes = grid["strikes"]
frames.append(
_generate_for_expiry(
exp, t, fwd, dfactor, a, b, rho, m, sigma, strikes, opt_types, noise, spread, symbol
)
)
if not frames:
continue
out_df = pd.concat(frames, ignore_index=True)
if outfile:
if len(noise_vals) > 1:
base, ext = outfile.rsplit(".", 1) if "." in outfile else (outfile, "csv")
out_path = f"{base}_noise{noise:.4f}.{ext}"
else:
out_path = outfile
out_df.to_csv(out_path, index=False)
print(f"\nWrote {len(out_df)} rows to {out_path}")
if do_fit:
print(f"\nFitting SVI for noise={noise:.6f}")
fit_rows = _fit_and_report(out_df, expiry_list, fwd, dfactor, a, b, rho, m, sigma, as_of, fit_space)
fit_results[noise] = fit_rows
if plot and do_fit and expiry_grids and fit_results:
multi_exp = len(expiry_grids) > 1
for exp, grid in expiry_grids.items():
t = grid["t"]
strikes = grid["strikes"]
k = np.log(strikes / fwd)
w_true = svi_total_variance(k, a, b, rho, m, sigma)
iv_true = np.sqrt(np.maximum(w_true, 0.0) / t) * 100.0
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(strikes, iv_true, linestyle="--", color="black", label="true")
for noise, rows in fit_results.items():
row = next((r for r in rows if r["expiry"] == exp), None)
if row is None:
continue
w_fit = svi_total_variance(k, row["a_fit"], row["b_fit"], row["rho_fit"],
row["m_fit"], row["sigma_fit"])
iv_fit = np.sqrt(np.maximum(w_fit, 0.0) / t) * 100.0
ax.plot(strikes, iv_fit, label=f"fit noise={noise:.4f}")
tenor_days = int(round(t * 365.25))
ax.set_title(f"SVI true vs fit ({symbol}, expiry {exp}, T={tenor_days}d)")
ax.set_xlabel("strike")
ax.set_ylabel("implied vol (pct)")
ax.legend(fontsize="small")
ax.grid(True, alpha=0.3)
plt.tight_layout()
if plot_file and not multi_exp:
fig.savefig(plot_file, dpi=150)
print(f"Wrote plot to {plot_file}")
elif plot_file and multi_exp:
base, ext = plot_file.rsplit(".", 1) if "." in plot_file else (plot_file, "png")
out_path = f"{base}_{exp}.{ext}"
fig.savefig(out_path, dpi=150)
print(f"Wrote plot to {out_path}")
else:
plt.show()
if __name__ == "__main__":
main(sys.argv)