def black_scholes(price, strike, t, rate, vol, call, put): mr = -rate sig_sig_two = vol * vol * 2 P = price S = strike T = t a = np.log(P / S) b = T * mr z = T * sig_sig_two c = 0.25 * z y = 1./np.sqrt(z) w1 = (a - b + c) * y w2 = (a - b - c) * y d1 = 0.5 + 0.5 * np.erf(w1) d2 = 0.5 + 0.5 * np.erf(w2) Se = np.exp(b) * S r = P * d1 - Se * d2 call[:] = r # temporary `r` is necessary for faster `put` computation put[:] = r - P + Se
def test_erf_fallback(): a = numpy.linspace(2.0, 3.0, num=10) expected = numpy.empty_like(a) for idx, val in enumerate(a): expected[idx] = math.erf(val) result = dpnp.erf(a) numpy.testing.assert_array_equal(result, expected)
def test_strides_erf(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] result = dpnp.erf(dpb) expected = numpy.empty_like(b) for idx, val in enumerate(b): expected[idx] = math.erf(val) numpy.testing.assert_allclose(result, expected)
def black_scholes(price, strike, t, mrs, vol_vol_twos, quarters, ones, halfs, call, put): P = price S = strike T = t a = np.log(P / S) b = T * mrs z = T * vol_vol_twos c = quarters * z y = ones / np.sqrt(z) w1 = (a - b + c) * y w2 = (a - b - c) * y d1 = halfs + halfs * np.erf(w1) d2 = halfs + halfs * np.erf(w2) Se = np.exp(b) * S r = P * d1 - Se * d2 call[:] = r # temporary `r` is necessary for faster `put` computation put[:] = r - P + Se