aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/index.py9
-rw-r--r--python/analytics/option.py18
-rw-r--r--python/analytics/utils.py7
3 files changed, 17 insertions, 17 deletions
diff --git a/python/analytics/index.py b/python/analytics/index.py
index ab32fa89..8e3e9684 100644
--- a/python/analytics/index.py
+++ b/python/analytics/index.py
@@ -11,6 +11,7 @@ try:
from bbg_helpers import BBG_IP, retrieve_data, init_bbg_session
except ModuleNotFoundError:
pass
+from itertools import chain
from pandas.tseries.offsets import BDay
from pyisda.curve import SpreadCurve
from pyisda.date import previous_twentieth
@@ -386,7 +387,13 @@ class ForwardIndex:
self.index.ref = val
def __hash__(self):
- return hash(tuple(getattr(self, k) for k in ForwardIndex.__slots__[:-1]))
+ return hash(
+ tuple(
+ getattr(self, k)
+ for k in chain.from_iterable(c.__slots__ for c in type(self).mro()[:-1])
+ if not k.startswith("__")
+ )
+ )
def _update(self, *args):
self.df = self.index._yc.discount_factor(self.exercise_date_settle)
diff --git a/python/analytics/option.py b/python/analytics/option.py
index 1669803a..11664eeb 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -265,11 +265,6 @@ class BlackSwaption(ForwardIndex):
intrinsic = max(V, 0) if self.option_type == "payer" else max(-V, 0)
return self._direction * intrinsic * self.notional * self.index.factor
- def __hash__(self):
- return hash(
- (hash(super()), tuple(getattr(self, k) for k in BlackSwaption.__slots__))
- )
-
@property
def pv(self):
"""compute pv using black-scholes formula"""
@@ -515,17 +510,14 @@ class BlackSwaption(ForwardIndex):
class Swaption(BlackSwaption):
- __slots__ = ("_cache", "_Z", "_w")
+ __slots__ = ("__cache", "__Z", "__w")
def __init__(
self, index, exercise_date, strike, option_type="payer", direction="Long"
):
super().__init__(index, exercise_date, strike, option_type, direction)
- self._cache = {}
- self._Z, self._w = GHquad(30)
-
- def __hash__(self):
- return super().__hash__()
+ self.__cache = {}
+ self.__Z, self.__w = GHquad(30)
@property
@memoize
@@ -534,7 +526,7 @@ class Swaption(BlackSwaption):
if T == 0.0:
return self.notional * self.intrinsic_value * self.index.factor
sigmaT = self.sigma * math.sqrt(T)
- tilt = np.exp(-0.5 * sigmaT ** 2 + sigmaT * self._Z)
+ tilt = np.exp(-0.5 * sigmaT ** 2 + sigmaT * self.__Z)
ctx = init_context(
self.index._yc,
self.exercise_date,
@@ -547,7 +539,7 @@ class Swaption(BlackSwaption):
sigmaT,
0.01,
)
- args = (self.forward_pv, tilt, self._w, ctx)
+ args = (self.forward_pv, tilt, self.__w, ctx)
eta = 1.05
a = self.index.spread * 0.99
b = a * eta
diff --git a/python/analytics/utils.py b/python/analytics/utils.py
index c8e5901b..8c01d2ae 100644
--- a/python/analytics/utils.py
+++ b/python/analytics/utils.py
@@ -188,11 +188,12 @@ def memoize(f=None, *, hasher=lambda args: (hash(args),)):
def cached_f(*args, **kwargs):
self = args[0]
key = (f.__name__, *hasher(args))
- if key in self._cache:
- return self._cache[key]
+ cache = getattr(self, f"_{type(self).__name__}__cache")
+ if key in cache:
+ return cache[key]
else:
v = f(*args, **kwargs)
- self._cache[key] = v
+ cache[key] = v
return v
return cached_f