def test_gbm_euler_step_nd_output_changes_with_key(self):
        drift = np.asarray([0.1, 0.3, -0.05], dtype=np.float32)
        vol_matrix = 0.2 * np.asarray(
            [[1.5, 0.2, 0.3], [0.2, 1.1, -0.1], [0.3, -0.1, 0.8]],
            dtype=np.float32)
        t = 0.3
        dt = 0.01
        num_samples = 8
        num_dims = drift.shape[0]
        key_0 = 42
        key_1 = 77

        states = tf.ones([num_samples, num_dims])
        next_states_0 = dynamics.gbm_euler_step_nd(states,
                                                   drift,
                                                   vol_matrix,
                                                   t,
                                                   dt,
                                                   key=key_0)
        next_states_1 = dynamics.gbm_euler_step_nd(states,
                                                   drift,
                                                   vol_matrix,
                                                   t,
                                                   dt,
                                                   key=key_1)

        with self.session() as session:
            next_states_0_eval, next_states_1_eval = session.run(
                (next_states_0, next_states_1))

        self.assertEqual(next_states_0_eval.shape, (num_samples, num_dims))
        self.assertEqual(next_states_1_eval.shape, (num_samples, num_dims))

        # The step is a bijection w.r.t. dw_t, all terms should be different.
        self.assertAllDistinct(next_states_0_eval, next_states_1_eval)
    def test_gbm_euler_step_nd_is_deterministic(self):
        drift = np.asarray([0.1, 0.3, -0.05], dtype=np.float32)
        vol_matrix = 0.2 * np.asarray(
            [[1.5, 0.2, 0.3], [0.2, 1.1, -0.1], [0.3, -0.1, 0.8]],
            dtype=np.float32)
        t = 0.3
        dt = 0.01
        num_samples = 8
        num_dims = drift.shape[0]
        key = 42

        states = tf.ones([num_samples, num_dims])
        eps_t = contrib_stateless.stateless_random_normal(
            shape=[num_samples, num_dims], seed=[key, int(t / dt)])

        next_states = dynamics.gbm_euler_step_nd(
            states, drift, vol_matrix, t, dt, random_normal_op=lambda: eps_t)
        next_states_bis = dynamics.gbm_euler_step_nd(states,
                                                     drift,
                                                     vol_matrix,
                                                     t,
                                                     dt,
                                                     key=key)

        with self.session() as session:
            next_states_eval, next_states_bis_eval = session.run(
                (next_states, next_states_bis))

        self.assertEqual(next_states_eval.shape, (num_samples, num_dims))
        self.assertEqual(next_states_bis_eval.shape, (num_samples, num_dims))

        self.assertAllClose(next_states_eval, next_states_bis_eval)
  def test_gbm_euler_step_nd_expects_static_shape(self):
    drift = np.asarray([0.1, 0.3, -0.05], dtype=np.float32)
    vol_matrix = 0.2 * np.asarray(
        [[1.5, 0.2, 0.3], [0.2, 1.1, -0.1], [0.3, -0.1, 0.8]], dtype=np.float32)
    t = 0.0
    dt = 0.01
    num_dims = drift.shape[0]

    states = tf.placeholder(dtype=tf.float32, shape=[None, num_dims])

    with self.assertRaises(ValueError):
      dynamics.gbm_euler_step_nd(states, drift, vol_matrix, t, dt)
    def test_gbm_euler_step_nd_output_is_correct(self):
        np.random.seed(0)
        drift = np.asarray([0.1, 0.3, -0.05], dtype=np.float32)
        vol_matrix = 0.2 * np.asarray(
            [[1.5, 0.2, 0.3], [0.2, 1.1, -0.1], [0.3, -0.1, 0.8]],
            dtype=np.float32)
        t = 0.0
        dt = 0.01
        num_samples = 8
        num_dims = drift.shape[0]

        states = tf.ones([num_samples, num_dims])
        eps_t = np.ndarray.astype(
            np.random.normal(size=[num_samples, num_dims]), dtype=np.float32)

        next_states = dynamics.gbm_euler_step_nd(
            states, drift, vol_matrix, t, dt, random_normal_op=lambda: eps_t)

        with self.session() as session:
            next_states_eval = session.run(next_states)

        self.assertEqual(next_states_eval.shape, (num_samples, num_dims))

        for i in range(num_samples):
            self.assertAllClose(
                next_states_eval[i],
                np.ones([num_dims], dtype=np.float32) *
                (1.0 + drift * dt +
                 np.matmul(vol_matrix, eps_t[i] * np.sqrt(dt))))
示例#5
0
 def _dynamics_op(s, t, dt):
     return dynamics.gbm_euler_step_nd(s, drift, vol_matrix, t, dt)