Skip to content

Commit ede62b4

Browse files
committed
Add 3d vol plot
1 parent ecb3ff9 commit ede62b4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

notebooks/applications/volatility_surface.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ df
9494
The plot function is enabled only if [plotly](https://plotly.com/python/) is installed
9595

9696
```{code-cell} ipython3
97-
vs.plot()
97+
vs.plot().update_layout(height=500, title="BTC Volatility Surface", xaxis_title=r"$\frac{1}{T}$")
9898
```
9999

100100
The `moneyness_ttm` is defined as
@@ -103,9 +103,11 @@ The `moneyness_ttm` is defined as
103103
\frac{1}{\sqrt{T}} \ln{\frac{K}{F}}
104104
\end{equation}
105105

106-
where $T$ is the time-to-maturity
106+
where $T$ is the time-to-maturity.
107107

108-
+++
108+
```{code-cell} ipython3
109+
vs.plot3d().update_layout(height=800, title="BTC Volatility Surface")
110+
```
109111

110112
## Model Calibration
111113

@@ -116,7 +118,7 @@ from quantflow.options.calibration import HestonCalibration, OptionPricer
116118
from quantflow.sp.heston import Heston
117119
118120
pricer = OptionPricer(Heston.create(vol=0.5))
119-
cal = HestonCalibration(pricer=pricer, vol_surface=vs)
121+
cal = HestonCalibration(pricer=pricer, vol_surface=vs, moneyness_weight=-0)
120122
len(cal.options)
121123
```
122124

@@ -142,7 +144,7 @@ pricer.reset()
142144
```
143145

144146
```{code-cell} ipython3
145-
cal.plot(index=5, max_moneyness_ttm=1)
147+
cal.plot(index=1, max_moneyness_ttm=1)
146148
```
147149

148150
## Serialization

quantflow/options/surface.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,16 @@ def plot(
493493
df = self.options_df(index=index, select=select)
494494
return plot.plot_vol_surface(df, **kwargs)
495495

496+
def plot3d(
497+
self,
498+
*,
499+
select: OptionSelection = OptionSelection.best,
500+
**kwargs: Any,
501+
) -> Any:
502+
"""Plot the volatility surface"""
503+
df = self.options_df(select=select)
504+
return plot.plot_vol_surface_3d(df, **kwargs)
505+
496506

497507
@dataclass
498508
class VolCrossSectionLoader(Generic[S]):

quantflow/utils/plot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def plot_vol_surface(
108108
return fig
109109

110110

111+
def plot_vol_surface_3d(
112+
df: pd.DataFrame,
113+
*,
114+
marker_size: int = 10,
115+
series: str = "implied_vol",
116+
**kwargs: Any
117+
) -> Any:
118+
check_plotly()
119+
return px.scatter_3d(df, x="moneyness_ttm", y="ttm", z=series, color="side")
120+
121+
111122
def plot_vol_cross(
112123
data: pd.DataFrame,
113124
data2: pd.DataFrame | None = None,

0 commit comments

Comments
 (0)