From 06187930c5e3bf02c1d88dda8afd307ff43dff4a Mon Sep 17 00:00:00 2001 From: GaetanoCodes Date: Tue, 17 Sep 2024 21:34:37 +0200 Subject: [PATCH] helper strings --- .../fourier/CarrMadanEuropeanPricer.py | 24 +++++++++++++++---- fypy/pricing/fourier/HilbertEuropeanPricer.py | 22 ++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/fypy/pricing/fourier/CarrMadanEuropeanPricer.py b/fypy/pricing/fourier/CarrMadanEuropeanPricer.py index 69239b3..0ae7ccf 100644 --- a/fypy/pricing/fourier/CarrMadanEuropeanPricer.py +++ b/fypy/pricing/fourier/CarrMadanEuropeanPricer.py @@ -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 @@ -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): diff --git a/fypy/pricing/fourier/HilbertEuropeanPricer.py b/fypy/pricing/fourier/HilbertEuropeanPricer.py index 3beffd7..5dbeb26 100644 --- a/fypy/pricing/fourier/HilbertEuropeanPricer.py +++ b/fypy/pricing/fourier/HilbertEuropeanPricer.py @@ -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 = ( @@ -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)