-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssvi.py
More file actions
140 lines (122 loc) · 4.4 KB
/
Copy pathssvi.py
File metadata and controls
140 lines (122 loc) · 4.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
#!/usr/bin/env python3
"""
SSVI (surface SVI) utilities.
Implements the SSVI total variance:
w(k, theta) = 0.5 * theta * (1 + rho * phi(theta) * k +
sqrt((phi(theta) * k + rho)**2 + 1 - rho**2))
where k is log-moneyness and theta is total variance at the money.
"""
from __future__ import annotations
from typing import Dict
import numpy as np
from scipy.optimize import least_squares
def ssvi_total_variance(
k: np.ndarray,
theta: float | np.ndarray,
rho: float,
eta: float,
gamma: float,
) -> np.ndarray:
"""Compute SSVI total variance for log-moneyness k."""
theta_arr = np.asarray(theta, dtype=float)
if np.any(theta_arr <= 0.0):
return np.full_like(k, np.nan, dtype=float)
if not (-0.999 <= rho <= 0.999):
return np.full_like(k, np.nan, dtype=float)
if eta <= 0.0 or gamma <= 0.0:
return np.full_like(k, np.nan, dtype=float)
phi = eta / (theta_arr ** gamma)
term = phi * k + rho
return 0.5 * theta_arr * (1.0 + rho * phi * k + np.sqrt(term * term + 1.0 - rho * rho))
def fit_ssvi_theta(
k: np.ndarray,
w: np.ndarray,
rho: float,
eta: float,
gamma: float,
) -> Dict[str, float]:
"""Fit theta for fixed rho/eta/gamma via least squares."""
thetas = np.linspace(1e-4, 2.0, 400)
best = None
for theta in thetas:
w_fit = ssvi_total_variance(k, theta, rho, eta, gamma)
if np.any(~np.isfinite(w_fit)):
continue
rmse = np.sqrt(np.mean((w_fit - w) ** 2))
if best is None or rmse < best["rmse"]:
best = {"theta": theta, "rmse": rmse}
if best is None:
return {"theta": float("nan"), "rmse": float("nan")}
return best
def fit_ssvi(
k: np.ndarray,
w: np.ndarray,
theta0: float | None = None,
rho0: float = -0.2,
eta0: float = 0.5,
gamma0: float = 0.5,
) -> Dict[str, float]:
"""Fit theta, rho, eta, gamma by least squares."""
k = np.asarray(k, dtype=float)
w = np.asarray(w, dtype=float)
if theta0 is None:
theta0 = float(np.nanmedian(w)) if np.isfinite(np.nanmedian(w)) else 0.1
if theta0 <= 0:
theta0 = 0.1
def loss(params: np.ndarray) -> np.ndarray:
theta, rho, eta, gamma = params
w_fit = ssvi_total_variance(k, theta, rho, eta, gamma)
return w_fit - w
x0 = np.array([theta0, rho0, eta0, gamma0], dtype=float)
lower = np.array([1e-6, -0.999, 1e-6, 0.01], dtype=float)
upper = np.array([10.0, 0.999, 10.0, 5.0], dtype=float)
res = least_squares(loss, x0, bounds=(lower, upper), max_nfev=5000)
theta, rho, eta, gamma = res.x
rmse = float(np.sqrt(np.mean(res.fun ** 2))) if res.fun.size else float("nan")
return {"theta": theta, "rho": rho, "eta": eta, "gamma": gamma, "rmse": rmse}
def essvi_total_variance(
k: np.ndarray,
t: np.ndarray,
a: float,
b: float,
c: float,
rho: float,
eta: float,
gamma: float,
) -> np.ndarray:
"""Compute eSSVI total variance for log-moneyness k and tenor t."""
t = np.asarray(t, dtype=float)
theta = a + b * np.power(t, c)
if np.any(theta <= 0.0):
return np.full_like(k, np.nan, dtype=float)
return ssvi_total_variance(k, theta, rho, eta, gamma)
def fit_essvi(
k: np.ndarray,
t: np.ndarray,
w: np.ndarray,
a0: float | None = None,
b0: float = 0.1,
c0: float = 0.5,
rho0: float = -0.2,
eta0: float = 0.5,
gamma0: float = 0.5,
) -> Dict[str, float]:
"""Fit eSSVI parameters with constant rho/eta/gamma."""
k = np.asarray(k, dtype=float)
t = np.asarray(t, dtype=float)
w = np.asarray(w, dtype=float)
if a0 is None:
a0 = float(np.nanmedian(w)) if np.isfinite(np.nanmedian(w)) else 0.1
if a0 <= 0:
a0 = 0.1
def loss(params: np.ndarray) -> np.ndarray:
a, b, c, rho, eta, gamma = params
w_fit = essvi_total_variance(k, t, a, b, c, rho, eta, gamma)
return w_fit - w
x0 = np.array([a0, b0, c0, rho0, eta0, gamma0], dtype=float)
lower = np.array([1e-6, 0.0, 0.05, -0.999, 1e-6, 0.01], dtype=float)
upper = np.array([10.0, 10.0, 5.0, 0.999, 10.0, 5.0], dtype=float)
res = least_squares(loss, x0, bounds=(lower, upper), max_nfev=8000)
a, b, c, rho, eta, gamma = res.x
rmse = float(np.sqrt(np.mean(res.fun ** 2))) if res.fun.size else float("nan")
return {"a": a, "b": b, "c": c, "rho": rho, "eta": eta, "gamma": gamma, "rmse": rmse}