-
Notifications
You must be signed in to change notification settings - Fork 7
/
bs_erf_numexpr.py
44 lines (31 loc) · 966 Bytes
/
bs_erf_numexpr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (C) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
import base_bs_erf
import numexpr as ne
from base_bs_erf import numpy_ver
def black_scholes ( nopt, price, strike, t, rate, vol ):
mr = -rate
sig_sig_two = vol * vol * 2
P = price
S = strike
T = t
a = ne.evaluate("log(P / S) ")
b = ne.evaluate("T * mr ")
z = ne.evaluate("T * sig_sig_two ")
c = ne.evaluate("0.25 * z ")
y = ne.evaluate("1/sqrt(z) ")
w1 = ne.evaluate("(a - b + c) * y ")
w2 = ne.evaluate("(a - b - c) * y ")
d1 = ne.evaluate("0.5 + 0.5 * erf(w1) ")
d2 = ne.evaluate("0.5 + 0.5 * erf(w2) ")
Se = ne.evaluate("exp(b) * S ")
call = ne.evaluate("P * d1 - Se * d2 ")
put = ne.evaluate("call - P + Se ")
return call, put
ne.set_num_threads(ne.detect_number_of_cores())
ne.set_vml_accuracy_mode('high')
if "invsqrt" in numpy_ver: # XXX: find a better way
base_bs_erf.run("Numexpr", black_scholes)
else:
print("Skipping current environment")