def operator_truediv2(size): a = Array(size, 'double') b = Array(size, 'double') for i in range(size): a[i] = nb_types.double(i+10) b[i] = nb_types.double(i+3) return operator.truediv(a, b)
def operator_ifloordiv2(size): a = Array(size, 'double') b = Array(size, 'double') for i in range(size): a[i] = nb_types.double(i+10) b[i] = nb_types.double(i+3) operator.ifloordiv(a, b) return a
def next_double(self): sig = types.double(types.CPointer(types.uint64)) @cfunc(sig) def next_double(st): bit_gen_state = carray(st, (2, ), dtype=np.uint64) return (np.uint64(splitmix_next(bit_gen_state)) >> np.uint64(11)) / 9007199254740992.0 # Ensure a reference is held self._next_double = next_double return next_double
def make_integrand(): global _integrand import numba import numba.types as nt import scipy @numba.cfunc(nt.double(nt.intc, nt.CPointer(nt.double)), nopython=True, nogil=True) def _fn(n, xx): if n != 4: return np.nan y = xx[0] k = xx[1] sigma2 = xx[2] z = xx[3] return y ** (k/2 - 1) * np.exp(-(k * y + (z-y)**2 / sigma2) / 2) _integrand = scipy.LowLevelCallable(_fn.ctypes)
def _computeParameters(self): self.theta = sp.sympify(self.theta) self.b = sp.sympify(self.b) self.delta = sp.sympify(self.delta) self.epsilon = sp.sympify(self.epsilon) self.eosDisplayName = self.eosDisplayName self.eosInfo = self.eosInfo self._PsymbolicFromVT = R_IG * self.T / (self.V - self.b) - self.theta / ( (self.V - self.b) * (self.V ** 2 + self.delta * self.V + self.epsilon) ) self._ZsymbolicFromVT = self.V / (self.V - self.b) - ( self.V * self.theta / (R_IG * self.T) ) / (self.V ** 2 + self.V * self.delta + self.epsilon) self._dZdTsymbolicFromVT = sp.diff(self._ZsymbolicFromVT, self.T) self._numf_ZfromVT = njit()( sp.lambdify([self.V, self.T], self._ZsymbolicFromVT, modules="numpy") ) self._numf_dZdTfromVT = njit()( sp.lambdify([self.V, self.T], self._dZdTsymbolicFromVT, modules="numpy") ) self._Bl = self.b * self.P / (R_IG * self.T) self._deltal = self.delta * self.P / (R_IG * self.T) self._thetal = self.theta * self.P / (R_IG * self.T) ** 2 self._epsilonl = self.epsilon * (self.P / (R_IG * self.T)) ** 2 # coefficients Z**3 + a0*Z**2 + a1*Z + a2 = 0 self._a0 = self._deltal - self._Bl - 1 self._a1 = self._thetal + self._epsilonl - self._deltal * (self._Bl + 1) self._a2 = -(self._epsilonl * (self._Bl + 1) + self._thetal * self._Bl) self._numf_a0 = njit()(sp.lambdify([self.P, self.T], self._a0, modules="numpy")) self._numf_a1 = njit()(sp.lambdify([self.P, self.T], self._a1, modules="numpy")) self._numf_a2 = njit()(sp.lambdify([self.P, self.T], self._a2, modules="numpy")) self.tmp_cfunc = None self.tmp_cfunc2 = None c_sig = types.double(types.intc, types.CPointer(types.double)) exec( "self.tmp_cfunc = lambda n, data: {:s}".format( str((1 - self._ZsymbolicFromVT) / self.V) .replace("V", "data[0]") .replace("T", "data[1]") .replace("Abs", "np.abs") .replace("sign", "np.sign") ) ) qf = cfunc(c_sig)(self.tmp_cfunc) self._qnf = LowLevelCallable(qf.ctypes) exec( "self.tmp_cfunc2 = lambda n, data: {:s}".format( str(self.T * self._dZdTsymbolicFromVT / self.V) .replace("V", "data[0]") .replace("T", "data[1]") .replace("Abs", "np.abs") .replace("sign", "np.sign") ) ) tf = cfunc(c_sig)(self.tmp_cfunc2) self._numf_UR = LowLevelCallable(tf.ctypes)
def issue109(size): a = Array(5, 'double') for i in range(5): a[i] = nb_types.double(i) return a
x_max = 5 #chi=uf.chi(x) def func(x): return x**2 print(sp.quad(lambda x: x * func(x), x_min, x_max)) x = np.linspace(x_min, x_max, 10) func = func(x) print(sp.trapz(x * func, x)) c_sig = types.double(types.intc, types.CPointer(types.double)) @cfunc(c_sig) def f(n_args, x): x1 = x[0] func = x[1] return x1 * func print(sp.nquad(LowLevelCallable(f.ctypes), [[0, 25], [0, 5]], full_output=True)) import numpy as np import scipy.integrate as si import numba
Undiscounted value of the Put price under the LV-Sabr model by Bang (2018). Def (5) """ extra_args = (forward_rate, time_to_maturity, alpha, nu, rho, mu, beta_low, beta_high, eps, eps_s, f_low, f_high, lambda_, Psi, K_h) return quad(CDF_underlying, a=-np.inf, b=strike, epsabs=1e-6, epsrel=1e-6, args=extra_args, limit=100) #### Extra function for checking integration is correct lv_sigma_csignature = types.double(types.intc, types.CPointer(types.double)) @cfunc(lv_sigma_csignature, nopython=True) def lv_sigma_cfunc(n, xx): in_array = carray(xx, (n, )) u, beta_low, beta_high, eps, eps_s, f_low, f_high, lambda_, Psi, K_h, F0 = in_array sigma_argument = min(max(u, f_low), f_high) heta_1 = eps * math.log(1 + math.exp(sigma_argument / eps)) heta_2 = eps_s * math.log(1 + math.exp((sigma_argument - K_h) / eps_s)) beta_factor = math.tanh(lambda_ * (sigma_argument - F0)) beta = 0.5 * (beta_high + beta_low) - 0.5 * (beta_high - beta_low) * beta_factor return 1.0 / (math.pow(heta_1, beta) + Psi * heta_2)