forked from IntelPython/BlackScholes_bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs_erf_numba_numpy.py
45 lines (33 loc) · 878 Bytes
/
bs_erf_numba_numpy.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
44
45
# Copyright (C) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
import base_bs_erf
import numba as nb
import numpy as np
from numpy import log, exp, sqrt
from math import erf
# Numba does know erf function from numpy or scipy
@nb.vectorize(nopython=True)
def nberf(x):
return erf(x)
@nb.jit(nopython=True, parallel=True)
def black_scholes( nopt, price, strike, t, rate, vol, call, put ):
mr = -rate
sig_sig_two = vol * vol * 2
P = price
S = strike
T = t
a = log(P / S)
b = T * mr
z = T * sig_sig_two
c = 0.25 * z
y = 1./sqrt(z)
w1 = (a - b + c) * y
w2 = (a - b - c) * y
d1 = 0.5 + 0.5 * nberf(w1)
d2 = 0.5 + 0.5 * nberf(w2)
Se = exp(b) * S
r = P * d1 - Se * d2
call[:] = r # temporary `r` is necessary for faster `put` computation
put[:] = r - P + Se
base_bs_erf.run("Numba@jit-numpy", black_scholes, nparr=True, pass_args=True)