def get_paths(p0: float, sigma0: float, t: float, theta: float, w: float, k: float, no_paths: int, no_time_steps: int, seed: int): paths = np.empty(shape=(no_paths, no_time_steps)) v_t = np.empty(shape=(no_paths, no_time_steps)) paths[:, 0] = p0 v_t[:, 0] = sigma0 * sigma0 t_i_s = np.linspace(0.0, t, no_time_steps) np.random.rand(seed) nu = np.sqrt(2.0 * k * theta) for i in range(0, no_paths): u_s = np.random.rand(no_time_steps) u_sigma = np.random.rand(no_time_steps) for j in range(1, no_time_steps): delta_time = (t_i_s[j] - t_i_s[j - 1]) z_sigma_i = ndtri(u_sigma[j]) z_s_i = ndtri(u_s[j]) exp_t = np.exp(-theta * delta_time) v_t[i, j] = v_t[i, j - 1] * exp_t + w * (1.0 - exp_t) + \ nu * np.sqrt(0.5 * ((1.0 - exp_t * exp_t) / theta)) * v_t[i, j - 1] * z_sigma_i paths[i, j] = paths[i, j - 1] + np.sqrt( v_t[i, j - 1]) * np.sqrt(delta_time) * z_s_i return paths, v_t, t_i_s
def norm_inv(mu, sigma, z): size_z = len(z) normal_narray = np.empty(size_z) for i in range(0, size_z): normal_narray[i] = ndtri(z[i]) return normal_narray
def get_variance(k: float, theta: float, epsilon: float, phi_switch_level: float, t_i_1: float, t_i: float, v_t_i_1: ndarray, u_i: ndarray, no_paths: int): # no_paths = len(v_t_i_1) paths = np.zeros(no_paths) for i in range(0, no_paths): s_2_i = HestonTools.v_t_conditional_variance(k, theta, epsilon, v_t_i_1[i], t_i_1, t_i) m_i = HestonTools.v_t_conditional_mean(k, theta, v_t_i_1[i], t_i_1, t_i) phi = s_2_i / (m_i * m_i) if phi < phi_switch_level: parameters = HestonTools.matching_qe_moments_qg(m_i, s_2_i) z_i = ndtri(u_i[i]) paths[i] = parameters[1] * np.power(parameters[0] + z_i, 2.0) else: parameters = HestonTools.matching_qe_moments_exp(m_i, s_2_i) paths[i] = HestonTools.inv_exp_heston(parameters[0], parameters[1], u_i[i]) return paths
def test_ndtri(): assert_almost_equal(ndtri(0.6), 0.253347103136)