-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ENH: Add SVI model: `svi_variance` and `SVIVariance` (#406) (#410) * ENH: Add Sobol quasirandom engine (#430) (#431) (#478) * ENH: Support `BSEuropeanBinary` for put (#434) (#438) * ENH: Enabling customizing tqdm progress bar (#446) * ENH: Add antithetic sampling of randn (close #449) (#450) * DOC: Add an example of hedging variance swap using options (#426) (#435) * DOC: Add `autogreek.theta` to documentation (#429) * DOC: Add citation to Heston model (#447) * DOC: Add an example of sticky strike and sticky delta (#479) * TEST: Add tests for BSEuropean put (#439) * TEST: Add tests for identity between vega and gamma (#441) * MAINT: Update codecov action (#442)
- Loading branch information
simaki
authored
Feb 8, 2022
1 parent
bc4ae30
commit c342b52
Showing
33 changed files
with
741 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,8 @@ Vega | |
---- | ||
|
||
.. autofunction:: vega | ||
|
||
Theta | ||
----- | ||
|
||
.. autofunction:: theta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,4 @@ Other Functions | |
npdf | ||
d1 | ||
d2 | ||
svi_variance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,4 @@ Other Modules | |
:template: classtemplate.rst | ||
|
||
nn.Naked | ||
nn.SVIVariance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
|
||
import matplotlib.pyplot as plt | ||
import torch | ||
|
||
sys.path.append("..") | ||
|
||
from pfhedge.instruments import BrownianStock | ||
from pfhedge.instruments import EuropeanOption | ||
from pfhedge.nn import BlackScholes | ||
|
||
if __name__ == "__main__": | ||
options_list = [] | ||
strikes_list = [] | ||
for call in (True, False): | ||
for strike in torch.arange(70, 180, 10): | ||
option = EuropeanOption(BrownianStock(), call=call, strike=strike) | ||
options_list.append(option) | ||
strikes_list.append(strike) | ||
|
||
spot = torch.linspace(50, 200, 100) | ||
t = options_list[0].maturity | ||
v = options_list[0].ul().sigma | ||
|
||
plt.figure() | ||
total_vega = torch.zeros_like(spot) | ||
for option, strike in zip(options_list, strikes_list): | ||
lm = (spot / strike).log() | ||
vega = BlackScholes(option).vega(lm, t, v) / (strike**2) | ||
total_vega += vega | ||
if option.call: | ||
# 2 is for call and put | ||
plt.plot(spot.numpy(), 2 * vega.numpy()) | ||
plt.plot(spot.numpy(), total_vega.numpy(), color="k", lw=2) | ||
plt.savefig("./output/options-vega.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import sys | ||
|
||
import matplotlib.pyplot as plt | ||
import torch | ||
|
||
sys.path.append("..") | ||
|
||
import pfhedge.autogreek as autogreek | ||
from pfhedge.nn import BSEuropeanOption | ||
from pfhedge.nn.functional import svi_variance | ||
|
||
if __name__ == "__main__": | ||
a, b, rho, m, sigma = 0.02, 0.10, -0.40, 0.00, 0.20 | ||
k = torch.linspace(-0.10, 0.10, 100) | ||
v = svi_variance(k, a=a, b=b, rho=rho, m=m, sigma=sigma) | ||
|
||
plt.figure() | ||
plt.plot(k.numpy(), v.numpy()) | ||
plt.xlabel("Log strike") | ||
plt.xlabel("Variance") | ||
plt.savefig("output/svi_variance.pdf") | ||
print("Saved figure to output/svi_variance.pdf") | ||
|
||
bs = BSEuropeanOption() | ||
t = torch.full_like(k, 0.1) | ||
delta_sticky_strike = bs.delta(-k, t, v.sqrt()) | ||
|
||
def price_sticky_delta(log_moneyness): | ||
v = svi_variance(-log_moneyness, a=a, b=b, rho=rho, m=m, sigma=sigma) | ||
return bs.price(log_moneyness, t, v.sqrt()) | ||
|
||
log_moneyness = -k.requires_grad_() | ||
delta_sticky_delta = autogreek.delta( | ||
price_sticky_delta, log_moneyness=log_moneyness | ||
) | ||
k.detach_() | ||
|
||
plt.figure() | ||
plt.plot(-k.numpy(), delta_sticky_strike.numpy(), label="Delta for sticky strike") | ||
plt.plot(-k.numpy(), delta_sticky_delta.numpy(), label="Delta for sticky delta") | ||
plt.xlabel("Log strike") | ||
plt.xlabel("Delta") | ||
plt.legend() | ||
plt.savefig("output/svi_delta.pdf") | ||
print("Saved figure to output/svi_delta.pdf") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from torch import Tensor | ||
from torch.nn import Module | ||
|
||
from pfhedge._utils.typing import TensorOrScalar | ||
from pfhedge.nn.functional import svi_variance | ||
|
||
|
||
class SVIVariance(Module): | ||
r"""Returns total variance in the SVI model. | ||
The total variance for log strike :math:`k = \log(K / S)`, | ||
where :math:`K` and :math:`S` are strike and spot, reads: | ||
.. math:: | ||
w = a + b \left[ \rho (k - m) + \sqrt{(k - m)^2 + \sigma^2} \right] . | ||
References: | ||
- Jim Gatheral and Antoine Jacquier, | ||
Arbitrage-free SVI volatility surfaces. | ||
[arXiv:`1204.0646 <https://arxiv.org/abs/1204.0646>`_ [q-fin.PR]] | ||
Args: | ||
a (torch.Tensor or float): The parameter :math:`a`. | ||
b (torch.Tensor or float): The parameter :math:`b`. | ||
rho (torch.Tensor or float): The parameter :math:`\rho`. | ||
m (torch.Tensor or float): The parameter :math:`m`. | ||
sigma (torch.Tensor or float): The parameter :math:`\sigma`. | ||
Examples: | ||
>>> import torch | ||
>>> | ||
>>> a, b, rho, m, sigma = 0.03, 0.10, 0.10, 0.00, 0.10 | ||
>>> module = SVIVariance(a, b, rho, m, sigma) | ||
>>> input = torch.tensor([-0.10, -0.01, 0.00, 0.01, 0.10]) | ||
>>> module(input) | ||
tensor([0.0431, 0.0399, 0.0400, 0.0401, 0.0451]) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
a: TensorOrScalar, | ||
b: TensorOrScalar, | ||
rho: TensorOrScalar, | ||
m: TensorOrScalar, | ||
sigma: TensorOrScalar, | ||
) -> None: | ||
super().__init__() | ||
self.a = a | ||
self.b = b | ||
self.rho = rho | ||
self.m = m | ||
self.sigma = sigma | ||
|
||
def forward(self, input: Tensor) -> Tensor: | ||
return svi_variance( | ||
input, a=self.a, b=self.b, rho=self.rho, m=self.m, sigma=self.sigma | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.