-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC: Add
example_heston_iv.py
(#403)
- Loading branch information
simaki
committed
Nov 23, 2021
1 parent
30750dd
commit 1b489f1
Showing
3 changed files
with
45 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sys | ||
from math import exp | ||
|
||
import matplotlib.pyplot as plt | ||
import torch | ||
|
||
sys.path.append("..") | ||
from pfhedge.instruments import EuropeanOption | ||
from pfhedge.instruments import HestonStock | ||
from pfhedge.nn import BlackScholes | ||
|
||
LOG_MONEYNESS_MIN = -0.1 | ||
LOG_MONEYNESS_MAX = 0.1 | ||
LOG_MONEYNESS_STEPS = 20 | ||
LOG_MONEYNESS_RANGE = torch.linspace( | ||
LOG_MONEYNESS_MIN, LOG_MONEYNESS_MAX, LOG_MONEYNESS_STEPS | ||
) | ||
|
||
|
||
def compute_iv(log_moneyness: float, rho: float) -> float: | ||
torch.manual_seed(42) | ||
|
||
d = EuropeanOption(HestonStock(rho=rho)) | ||
spot = exp(log_moneyness) * d.strike | ||
d.simulate(n_paths=int(1e5), init_state=(spot, d.ul().theta)) | ||
p = d.payoff().mean(0) | ||
|
||
return BlackScholes(d).implied_volatility(log_moneyness, d.maturity, p).item() | ||
|
||
|
||
def main(): | ||
plt.figure() | ||
for rho in [-0.7, 0.0, 0.7]: | ||
y = [compute_iv(s, rho) for s in LOG_MONEYNESS_RANGE] | ||
plt.plot(LOG_MONEYNESS_RANGE, y, label=f"rho={rho}") | ||
plt.xlabel("Log moneyness") | ||
plt.ylabel("Implied volatility") | ||
plt.legend() | ||
plt.savefig("output/heston-iv.png") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,2 @@ | ||
matplotlib | ||
tqdm |
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