Skip to content

Commit

Permalink
helper strings
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanoCodes committed Sep 17, 2024
1 parent c7be753 commit 0618793
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
24 changes: 20 additions & 4 deletions fypy/pricing/fourier/CarrMadanEuropeanPricer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@ class CarrMadanEuropeanPricer(StrikesPricer):
def __init__(
self, model: FourierModel, alpha: float = 0.75, eta: float = 0.1, N: int = 2**9
):
"""Carr-Madan method for Pricing European options under a Fourier model (i.e. using ChF)
Args:
model (FourierModel): FourierModel, model to price under
alpha (float, optional): Defaults to 0.75.
eta (float, optional): Defaults to 0.1.
N (int, optional): Defaults to 2**9.
"""
self._model = model
self._alpha = alpha # contour shift param (see Lee for recommendations)
self._alpha = alpha
self._eta = eta
self._N = N
self._logS0 = np.log(self._model.spot())

def price(self, T: float, K: float, is_call: bool):
def price(self, T: float, K: float, is_call: bool) -> float:
"""
Price a single strike of European option
:param T: float, time to maturity
:param K: float, strike of option
:param is_call: bool, indicator of if strike is call (true) or put (false)
:return: float, price of option
"""
lam = 2 * np.pi / (self._N * self._eta)
b = self._N * lam / 2

Expand All @@ -41,9 +57,9 @@ def price(self, T: float, K: float, is_call: bool):

xp = [ku[istrike], ku[istrike + 1]]
yp = [Cku[istrike], Cku[istrike + 1]]
price = np.interp(logK, xp, yp)
price = float(np.interp(logK, xp, yp))
if not is_call:
price = price - (self._model.forwardCurve(T) * disc - K * disc)
price = price - float((self._model.forwardCurve(T) * disc - K * disc))
return price

def _chf(self, T: float, xi: np.ndarray):
Expand Down
22 changes: 19 additions & 3 deletions fypy/pricing/fourier/HilbertEuropeanPricer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,32 @@ def __init__(
N: int = 2**9,
Nh: int = 2**5,
):
"""Hilbert method for Pricing European options under a Fourier model (i.e. using ChF)
Args:
model (FourierModel): FourierModel, model to price under
alpha (float, optional): Defaults to 0.75.
eta (float, optional): Defaults to 0.1.
N (int, optional): Defaults to 2**9.
Nh (int, optional): Defaults to 2**9.
"""
self._model = model
self._alpha = alpha # contour shift param (see Lee for recommendations)
self._alpha = alpha
self._eta = eta
self._N = N
self._Nh = Nh
self._h = 2 * np.pi / Nh
self._logS0 = np.log(self._model.spot())

def price(self, T: float, K: float, is_call: bool):
def price(self, T: float, K: float, is_call: bool) -> float:
"""
Price a single strike of European option
:param T: float, time to maturity
:param K: float, strike of option
:param is_call: bool, indicator of if strike is call (true) or put (false)
:return: float, price of option
"""
gridL = np.arange(-int(self._N / 2), 0)
gridR = -gridL[::-1]
H = (
Expand All @@ -41,7 +58,6 @@ def price(self, T: float, K: float, is_call: bool):
return price

def _g(self, xi: np.ndarray, T: float, K: float):
# return self._model.chf(T, xi) * np.exp(1j * self._logS0 * xi)
return np.exp(-1j * xi * np.log(K / self._model.spot())) * (
self._model.spot() * self._model.chf(T, xi - 1j)
- K * self._model.chf(T, xi)
Expand Down

0 comments on commit 0618793

Please sign in to comment.