def test_gbm_euler_step_running_max_bridge_changes_with_key(self): drift = 0.2 vol = 0.1 t = 0.2 dt = 0.01 num_samples = 8 key_0 = 74 key_1 = 75 states_and_max = [tf.ones([num_samples])] * 2 next_states_and_max_0 = dynamics.gbm_euler_step_running_max( states_and_max, drift, vol, t, dt, key=key_0, simulate_bridge=True) next_states_and_max_1 = dynamics.gbm_euler_step_running_max( states_and_max, drift, vol, t, dt, key=key_1, simulate_bridge=True) with self.session() as session: next_states_and_max_0_eval, next_states_and_max_1_eval = session.run( (next_states_and_max_0, next_states_and_max_1)) next_states_0_eval, next_max_0_eval = next_states_and_max_0_eval next_states_1_eval, next_max_1_eval = next_states_and_max_1_eval self.assertEqual(next_states_0_eval.shape, (num_samples, )) self.assertEqual(next_states_1_eval.shape, (num_samples, )) self.assertEqual(next_max_0_eval.shape, (num_samples, )) self.assertEqual(next_max_1_eval.shape, (num_samples, )) # The step is a bijection w.r.t. dw_t, all terms should be different. # However there is no such guarantee for the running maxima. self.assertAllDistinct(next_states_0_eval, next_states_1_eval)
def test_gbm_euler_step_running_max_expects_static_shape_both_members(self): drift = 0.2 vol = 0.1 t = 0.2 dt = 0.01 states_and_max = [ tf.placeholder(dtype=tf.float32, shape=[None]), tf.placeholder(dtype=tf.float32, shape=[None])] with self.assertRaises(ValueError): dynamics.gbm_euler_step_running_max(states_and_max, drift, vol, t, dt)
def test_gbm_euler_step_running_max_bridge_is_deterministic(self): drift = 0.2 vol = 0.1 t = 0.2 dt = 0.01 num_samples = 8 key = 1337 states_and_max = [tf.ones([num_samples])] * 2 eps_t = contrib_stateless.stateless_random_normal( shape=[num_samples], seed=[2 * key, int(t / dt)]) u_t = contrib_stateless.stateless_random_uniform( shape=[num_samples], seed=[2 * key + 1, int(t / dt)]) next_states_and_max = dynamics.gbm_euler_step_running_max( states_and_max, drift, vol, t, dt, simulate_bridge=True, random_normal_op=lambda: eps_t, random_uniform_op=lambda: u_t) next_states_and_max_bis = dynamics.gbm_euler_step_running_max( states_and_max, drift, vol, t, dt, simulate_bridge=True, key=key) with self.session() as session: next_states_and_max_eval, next_states_and_max_bis_eval = session.run( (next_states_and_max, next_states_and_max_bis)) next_states_eval, next_max_eval = next_states_and_max_eval next_states_bis_eval, next_max_bis_eval = next_states_and_max_bis_eval self.assertEqual(next_states_eval.shape, (num_samples, )) self.assertEqual(next_states_bis_eval.shape, (num_samples, )) self.assertEqual(next_max_eval.shape, (num_samples, )) self.assertEqual(next_max_bis_eval.shape, (num_samples, )) self.assertAllClose(next_states_eval, next_states_bis_eval) self.assertAllClose(next_max_eval, next_max_bis_eval)
def test_gbm_euler_step_running_max_bridge_output_is_correct(self): np.random.seed(0) drift = 0.2 vol = 0.1 t = 0.0 dt = 0.01 num_samples = 8 initial_states = np.ones([num_samples], dtype=np.float32) states_and_max = [tf.constant(initial_states)] * 2 eps_t = np.ndarray.astype(np.random.normal(size=[num_samples]), dtype=np.float32) u_t = np.ndarray.astype(np.random.uniform(size=[num_samples]), dtype=np.float32) (next_states, next_max) = dynamics.gbm_euler_step_running_max( states_and_max, drift, vol, t, dt, simulate_bridge=True, random_normal_op=lambda: eps_t, random_uniform_op=lambda: u_t) with self.session() as session: (next_states_eval, next_max_eval) = session.run( (next_states, next_max)) self.assertEqual(next_states_eval.shape, (num_samples, )) self.assertEqual(next_max_eval.shape, (num_samples, )) expected_next_states = initial_states * (1.0 + drift * dt + vol * eps_t * np.sqrt(dt)) expected_bridge_max = 0.5 * ( initial_states + expected_next_states + np.sqrt((initial_states - expected_next_states)**2 - 2.0 * dt * (vol * initial_states)**2 * np.log(u_t))) expected_next_max = np.maximum(expected_next_states, expected_bridge_max) # Here the maximum discrepancy is 1.17e-7 due to differences in # numerical implementations between tf and np so we set delta to 1.2e-7. self.assertAllClose(next_states_eval, expected_next_states, atol=1.2e-7) self.assertAllClose(next_max_eval, expected_next_max, atol=1.2e-7)