Last active
August 19, 2023 19:51
-
-
Save AbdealiLoKo/c06045e94867f59ac1ed19cc41fe812b to your computer and use it in GitHub Desktop.
JIT In Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import math | |
import numpy as np | |
# Taken from numba.tests.test_blackscholes | |
N = 16384 | |
RISKFREE = 0.02 | |
VOLATILITY = 0.30 | |
A1 = 0.31938153 | |
A2 = -0.356563782 | |
A3 = 1.781477937 | |
A4 = -1.821255978 | |
A5 = 1.330274429 | |
RSQRT2PI = 0.39894228040143267793994605993438 | |
def cnd(d: float): | |
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
def blackscholes(seed: int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import math | |
import numpy as np | |
# Taken from numba.tests.test_blackscholes | |
N = 16384 | |
RISKFREE = 0.02 | |
VOLATILITY = 0.30 | |
A1 = 0.31938153 | |
A2 = -0.356563782 | |
A3 = 1.781477937 | |
A4 = -1.821255978 | |
A5 = 1.330274429 | |
RSQRT2PI = 0.39894228040143267793994605993438 | |
def cnd(d: float): | |
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
def blackscholes(seed: int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) | |
####################### | |
# Benchmark | |
import time | |
start = time.perf_counter() | |
blackscholes(1) | |
duration = time.perf_counter() - start | |
print('First run:', duration * 1000, 'ms') | |
start = time.perf_counter() | |
blackscholes(2) | |
duration = time.perf_counter() - start | |
print('Second run:', duration * 1000, 'ms') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import cython | |
from cython.cimports.libc import math | |
import numpy as np | |
# Taken from numba.tests.test_blackscholes | |
N: cython.int = 16384 | |
RISKFREE: cython.double = 0.02 | |
VOLATILITY: cython.double = 0.30 | |
A1: cython.double = 0.31938153 | |
A2: cython.double = -0.356563782 | |
A3: cython.double = 1.781477937 | |
A4: cython.double = -1.821255978 | |
A5: cython.double = 1.330274429 | |
RSQRT2PI: cython.double = 0.39894228040143267793994605993438 | |
def cnd(d: cython.double): | |
K: cython.double = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val: cython.double = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
def blackscholes(seed: cython.int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) | |
####################### | |
# Benchmark | |
import time | |
start = time.perf_counter() | |
blackscholes(1) | |
duration = time.perf_counter() - start | |
print('First run:', duration * 1000, 'ms') | |
start = time.perf_counter() | |
blackscholes(2) | |
duration = time.perf_counter() - start | |
print('Second run:', duration * 1000, 'ms') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import math | |
import numpy as np | |
# Taken from numba.tests.test_blackscholes | |
N = 16384 | |
RISKFREE = 0.02 | |
VOLATILITY = 0.30 | |
A1 = 0.31938153 | |
A2 = -0.356563782 | |
A3 = 1.781477937 | |
A4 = -1.821255978 | |
A5 = 1.330274429 | |
RSQRT2PI = 0.39894228040143267793994605993438 | |
def cnd(d: float): | |
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
def blackscholes(seed: int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) | |
####################### | |
# Benchmark | |
import time | |
start = time.perf_counter() | |
blackscholes(1) | |
duration = time.perf_counter() - start | |
print('First run:', duration * 1000, 'ms') | |
start = time.perf_counter() | |
blackscholes(2) | |
duration = time.perf_counter() - start | |
print('Second run:', duration * 1000, 'ms') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import math | |
import numpy as np | |
from numba import jit | |
# Taken from numba.tests.test_blackscholes | |
N = 16384 | |
RISKFREE = 0.02 | |
VOLATILITY = 0.30 | |
A1 = 0.31938153 | |
A2 = -0.356563782 | |
A3 = 1.781477937 | |
A4 = -1.821255978 | |
A5 = 1.330274429 | |
RSQRT2PI = 0.39894228040143267793994605993438 | |
@jit(nopython=True) | |
def cnd(d: float): | |
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
@jit(nopython=True) | |
def blackscholes(seed: int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark an implementation of the Black–Scholes model. | |
""" | |
import math | |
import numpy as np | |
import pyston_lite | |
pyston_lite.enable() | |
# Taken from numba.tests.test_blackscholes | |
N = 16384 | |
RISKFREE = 0.02 | |
VOLATILITY = 0.30 | |
A1 = 0.31938153 | |
A2 = -0.356563782 | |
A3 = 1.781477937 | |
A4 = -1.821255978 | |
A5 = 1.330274429 | |
RSQRT2PI = 0.39894228040143267793994605993438 | |
def cnd(d: float): | |
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d)) | |
ret_val = (RSQRT2PI * math.exp(-0.5 * d * d) * | |
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))) | |
if d > 0: | |
ret_val = 1.0 - ret_val | |
return ret_val | |
def blackscholes(seed: int): | |
R = RISKFREE | |
V = VOLATILITY | |
np.random.seed(seed) | |
S = np.random.uniform(5.0, 30.0, N) | |
X = np.random.uniform(1.0, 100.0, N) | |
T = np.random.uniform(0.25, 10.0, N) | |
callResult = np.zeros(N) | |
putResult = np.zeros(N) | |
for i in range(len(S)): | |
sqrtT = math.sqrt(T[i]) | |
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT) | |
d2 = d1 - V * sqrtT | |
cndd1 = cnd(d1) | |
cndd2 = cnd(d2) | |
expRT = math.exp((-1. * R) * T[i]) | |
callResult[i] = (S[i] * cndd1 - X[i] * expRT * cndd2) | |
putResult[i] = (X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment