aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics')
-rw-r--r--python/analytics/black.py22
-rw-r--r--python/analytics/option.py3
2 files changed, 17 insertions, 8 deletions
diff --git a/python/analytics/black.py b/python/analytics/black.py
index ba9d8e96..94f91efb 100644
--- a/python/analytics/black.py
+++ b/python/analytics/black.py
@@ -3,13 +3,16 @@ from numba import jit, float64, boolean
from scipy.stats import norm
import math
+
def d1(F, K, sigma, T):
return (log(F / K) + sigma**2 * T / 2) / (sigma * math.sqrt(T))
+
def d2(F, K, sigma, T):
return d1(F, K, sigma, T) - sigma * math.sqrt(T)
-@jit(cache=True,nopython=True)
+
+@jit(cache=True, nopython=True)
def d12(F, K, sigma, T):
sigmaT = sigma * sqrt(T)
d1 = log(F / K) / sigmaT
@@ -18,27 +21,32 @@ def d12(F, K, sigma, T):
d2 -= 0.5 * sigmaT
return d1, d2
-@jit(float64(float64),cache=True,nopython=True)
+
+@jit(float64(float64), cache=True, nopython=True)
def cnd_erf(d):
+ """ 2 * Phi where Phi is the cdf of a Normal """
RSQRT2 = 0.7071067811865475
return 1 + erf(RSQRT2 * d)
-@jit(float64(float64,float64,float64,float64,boolean),cache=True,nopython=True)
+
+@jit(float64(float64, float64, float64, float64, boolean), cache=True, nopython=True)
def black(F, K, T, sigma, payer=True):
d1, d2 = d12(F, K, sigma, T)
if payer:
return 0.5 * (F * cnd_erf(d1) - K * cnd_erf(d2))
else:
- return 0.5 * (K * cnd_erf(-d2) - F * cnd_erf(-d1))
+ return 0.5 * (K * cnd_erf(-d2) - F * cnd_erf(-d1))
+
-@jit(float64(float64,float64,float64,float64),cache=True,nopython=True)
+@jit(float64(float64, float64, float64, float64), cache=True, nopython=True)
def Nx(F, K, sigma, T):
- return cnd_erf((log(F/K) - sigma**2 * T /2) / (sigma * sqrt(T))) /2
+ return cnd_erf((log(F/K) - sigma**2 * T / 2) / (sigma * sqrt(T))) / 2
+
def bachelier(F, K, T, sigma):
""" Bachelier formula for normal dynamics
need to multiply by discount factor
"""
- d1 = (F - K) / ( sigma * sqrt(T))
+ d1 = (F - K) / (sigma * sqrt(T))
return (0.5 * (F - K) * cnd_erf(d1) + sigma * sqrt(T) * norm.pdf(d1))
diff --git a/python/analytics/option.py b/python/analytics/option.py
index fbb84caf..5924fc56 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -86,7 +86,8 @@ class BlackSwaption(ForwardIndex):
if rec is None:
return ValueError("trade_id doesn't exist")
if index is None:
- index = CreditIndex(redcode=rec.security_id, maturity=rec.maturity, value_date=rec.trade_date)
+ index = CreditIndex(redcode=rec.security_id, maturity=rec.maturity,
+ value_date=rec.trade_date)
index.ref = rec.index_ref
instance = cls(index, rec.expiration_date, rec.strike, rec.option_type.lower(),
direction="Long" if rec.buysell else "Short")