Skip to content

Commit dea4c7a

Browse files
authored
TESTS PERF add black scholes test (#499)
* TESTS PERF add black scholes test * TESTS PERF avoid warming up
1 parent 43209ef commit dea4c7a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import math
2+
import numpy
3+
import pytest
4+
5+
from tests.tests_perf.data_generator import *
6+
from tests.tests_perf.test_perf_base import DPNPTestPerfBase
7+
8+
9+
SEED = 7777777
10+
SL, SH = 10.0, 50.0
11+
KL, KH = 10.0, 50.0
12+
TL, TH = 1.0, 2.0
13+
RISK_FREE = 0.1
14+
VOLATILITY = 0.2
15+
16+
17+
def math_erf(x):
18+
result = numpy.empty(x.shape, dtype=x.dtype)
19+
for i in range(result.size):
20+
result[i] = math.erf(x[i])
21+
22+
return result
23+
24+
25+
# module 'numpy' has no attribute 'erf'
26+
numpy.erf = math_erf
27+
28+
29+
def gen_data(low, high, size):
30+
return numpy.random.uniform(low, high, size)
31+
32+
33+
def black_scholes_put(lib, S, K, T, r, sigma):
34+
d1 = (lib.log(S/K) + (r + sigma*sigma/2.)*T) / sigma*lib.sqrt(T)
35+
d2 = d1 - sigma * lib.sqrt(T)
36+
37+
cdf_d1 = (1 + lib.erf(d1 / lib.sqrt(2))) / 2
38+
cdf_d2 = (1 + lib.erf(d2 / lib.sqrt(2))) / 2
39+
40+
bs_call = S*cdf_d1 - K*lib.exp(-r*T)*cdf_d2
41+
42+
return K*lib.exp(-r*T) - S + bs_call
43+
44+
45+
class TestBlackScholes(DPNPTestPerfBase):
46+
47+
@pytest.mark.parametrize("dtype", [numpy.float64])
48+
@pytest.mark.parametrize("size", [1024, 2048, 4096, 8192])
49+
def test_bs_put(self, lib, dtype, size):
50+
numpy.random.seed(SEED)
51+
S = gen_data(SL, SH, size)
52+
K = gen_data(KL, KH, size)
53+
T = gen_data(TL, TH, size)
54+
55+
self.dpnp_benchmark("bs_put", lib, dtype, size,
56+
lib, S, K, T, RISK_FREE, VOLATILITY,
57+
custom_fptr=black_scholes_put)

0 commit comments

Comments
 (0)