aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/tranche_basket.py5
-rw-r--r--python/analytics/utils.py28
2 files changed, 19 insertions, 14 deletions
diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py
index 5cd152f3..c55a3bd3 100644
--- a/python/analytics/tranche_basket.py
+++ b/python/analytics/tranche_basket.py
@@ -18,6 +18,8 @@ import numpy as np
class DualCorrTranche():
+ _cache = LRU(64)
+
def __init__(self, index_type: str, series: int, tenor: str, *,
attach: float, detach: float, corr_attach: float,
corr_detach: float, tranche_running: float,
@@ -46,7 +48,6 @@ class DualCorrTranche():
self.cs = credit_schedule(value_date, None,
1., self._index.yc, self._index.maturities[0])
self._accrued = cds_accrued(value_date, tranche_running * 1e-4)
- self._cache = LRU(64)
self._ignore_hash = set(['_Z', '_w', 'cs', '_cache', '_ignore_hash'])
def _default_prob(self):
@@ -97,7 +98,7 @@ class DualCorrTranche():
self.cs = credit_schedule(d, None, 1., self._index.yc, self._index.maturities[0])
self._accrued = cds_accrued(d, self.tranche_running * 1e-4)
- @memoize
+ @memoize(lambda args: (hash(args[0]._index), *args[1:]))
def tranche_legs(self, K, rho):
if K == 0.:
return 0., 0.
diff --git a/python/analytics/utils.py b/python/analytics/utils.py
index fab94397..0100f5bc 100644
--- a/python/analytics/utils.py
+++ b/python/analytics/utils.py
@@ -102,15 +102,19 @@ def build_table(rows, format_strings, row_format):
return r
-def memoize(f):
- @wraps(f)
- def cached_f(*args, **kwargs):
- obj = args[0]
- key = (f.__name__, hash(args))
- if key in obj._cache:
- return obj._cache[key]
- else:
- v = f(*args, **kwargs)
- obj._cache[key] = v
- return v
- return cached_f
+def memoize(hasher=lambda args: (hash(args),)):
+
+ def memoize_(f):
+ @wraps(f)
+ def cached_f(*args, **kwargs):
+ self = args[0]
+ key = (f.__name__, *hasher(args))
+ if key in self._cache:
+ return self._cache[key]
+ else:
+ v = f(*args, **kwargs)
+ self._cache[key] = v
+ return v
+ return cached_f
+
+ return memoize_