lr_values = [0.001, 0.0001, 0.00001] xi = tf.random_uniform((batch_size, d), minval=90., maxval=110., dtype=dtype) def phi(x, axis=1): return np.exp(-r * T) \ * tf.maximum(K - tf.reduce_min(x, axis=axis, keepdims=True), 0.) def mc_body(idx, p): _w = tf.matmul( tf.random_normal((mc_samples_ref * batch_size, d), stddev=np.sqrt(T / N), dtype=dtype), sigma) _w = tf.reshape(_w, (mc_samples_ref, batch_size, d)) _x = xi * tf.exp((r - c - 0.5 * (beta * sigma_norms)**2) * T + beta * _w) return idx + 1, p + tf.reduce_mean(phi(_x, 2), axis=0) x_sde = xi * tf.exp( (r - c - 0.5 * (beta * sigma_norms)**2) * T + beta * tf.matmul( tf.random_normal( (batch_size, d), stddev=np.sqrt(T / N), dtype=dtype), sigma)) _, u = tf.while_loop(lambda idx, p: idx < mc_rounds_ref, mc_body, (tf.constant(0), tf.zeros((batch_size, 1), dtype))) u_reference = u / tf.cast(mc_rounds_ref, tf.float32) kolmogorov_train_and_test(xi, x_sde, phi, u_reference, neurons, lr_boundaries, lr_values, train_steps, mc_rounds, mc_freq, 'example_bs_model_with_correlated_noise.csv', dtype)
neurons = [d + 100, d + 100, 1] train_steps = 750000 mc_rounds, mc_freq = 10, 25000 mc_samples_ref, mc_rounds_ref = 1024, 1024 lr_boundaries = [250001, 500001] lr_values = [0.001, 0.0001, 0.00001] xi = tf.random_uniform(shape=(batch_size, d), minval=90., maxval=110., dtype=dtype) def phi(x, axis=1): return np.exp(-r * T) * tf.maximum( tf.reduce_max(x, axis=axis, keep_dims=True) - K, 0.) def mc_body(idx, p): _x = xi * tf.exp((r - c - 0.5 * sigma**2) * T + sigma * tf.random_normal( (mc_samples_ref, batch_size, d), stddev=np.sqrt(T / N), dtype=dtype)) return idx + 1, p + tf.reduce_mean(phi(_x, 2), axis=0) x_sde = xi * tf.exp((r - c - 0.5 * sigma**2) * T + sigma * tf.random_normal( (mc_samples_ref, batch_size, d), stddev=np.sqrt(T / N), dtype=dtype)) _, u = tf.while_loop(lambda idx, p: idx < mc_rounds_ref, mc_body, (tf.constant(0), tf.zeros((batch_size, 1), dtype))) u_reference = u / tf.cast(mc_rounds_ref, tf.float32) kolmogorov_train_and_test(xi, x_sde, phi, u_reference, neurons, lr_boundaries, lr_values, train_steps, mc_rounds, mc_freq, 'example3_3.csv', dtype)
mc_samples_ref, mc_rounds_ref = 1024, 1024 lr_boundaries = [250001, 500001] lr_values = [0.001, 0.0001, 0.00001] xi = tf.random_uniform((batch_size, d), minval=90., maxval=110., dtype=dtype) def phi(x, axis=1): return np.exp(-r * T) \ * tf.maximum(tf.reduce_max(x, axis=axis, keepdims=True) - K, 0.) def mc_body(idx, p): _x = xi * tf.exp((r - c - 0.5 * sigma ** 2) * T + sigma * tf.random_normal((mc_samples_ref, batch_size, d), stddev=np.sqrt(T / N), dtype=dtype)) return idx + 1, p + tf.reduce_mean(phi(_x, 2), axis=0) x_sde = xi * tf.exp((r - c - 0.5 * sigma ** 2) * T + sigma * tf.random_normal((batch_size, d), stddev=np.sqrt(T / N), dtype=dtype)) _, u = tf.while_loop(lambda idx, p: idx < mc_rounds_ref, mc_body, (tf.constant(0), tf.zeros((batch_size, 1), dtype))) u_reference = u / tf.cast(mc_rounds_ref, tf.float32) kolmogorov_train_and_test(xi, x_sde, phi, u_reference, neurons, lr_boundaries, lr_values, train_steps, mc_rounds, mc_freq, 'example_geometric_brownian_motions.csv', dtype)
_sqrt_v) + sigma / 2. * dw_2) ** 2 + (kappa * theta - sigma ** 2 / 4. - kappa * v) * h, 0.) def mc_body(idx, p): _, _x, __v = tf.while_loop( lambda _idx, s, v: _idx < N, lambda _idx, s, v: sde_body(_idx, s, v, mc_samples_ref), loop_var_mc) return idx + 1, p + tf.reduce_mean(phi(_x, 2), axis=0) loop_var_mc = (tf.constant(0), tf.ones( (mc_samples_ref, batch_size, d / 2), dtype) * tf.log(S_0), tf.ones((mc_samples_ref, batch_size, d / 2), dtype) * V_0) loop_var = (tf.constant(0), tf.ones( (1, batch_size, d / 2), dtype) * tf.log(S_0), tf.ones((1, batch_size, d / 2), dtype) * V_0) _, x_sde, _v = tf.while_loop(lambda idx, s, v: idx < N, lambda idx, s, v: sde_body(idx, s, v, 1), loop_var) _, u = tf.while_loop(lambda idx, p: idx < mc_rounds_ref, mc_body, (tf.constant(0), tf.zeros((batch_size, 1), dtype))) u_reference = u / tf.cast(mc_rounds_ref, tf.float32) kolmogorov_train_and_test(xi, tf.squeeze(x_sde, axis=0), phi, u_reference, neurons, lr_boundaries, lr_values, train_steps, mc_rounds, mc_freq, 'example_heston_model.csv', dtype)
def sde_body(idx, s, samples): return tf.add(idx, 1), s \ + tf.cast(T / N * tf.sqrt(phi(mu(s), 2 if samples > 1 else 1)) <= 1., dtype) * mu(s) * T / N \ + beta * tf.random_normal((samples, batch_size, d), stddev=np.sqrt(T / N), dtype=dtype) def mc_body(idx, p): _, _x = tf.while_loop(lambda _idx, s: _idx < N, lambda _idx, s: sde_body(_idx, s, mc_samples_ref), loop_var_mc) return idx + 1, p + tf.reduce_mean(phi(_x, 2), axis=0) loop_var_mc = (tf.constant(0), tf.ones( (mc_samples_ref, batch_size, d), dtype) * xi) loop_var = (tf.constant(0), tf.ones((1, batch_size, d), dtype) * xi) _, x_sde = tf.while_loop(lambda idx, s: idx < N, lambda idx, s: sde_body(idx, s, 1), loop_var) _, u = tf.while_loop(lambda idx, p: idx < mc_rounds_ref, mc_body, (tf.constant(0), tf.zeros((batch_size, 1), dtype))) u_reference = u / tf.cast(mc_rounds_ref, tf.float32) kolmogorov_train_and_test(xi, tf.squeeze(x_sde, axis=0), phi, u_reference, neurons, lr_boundaries, lr_values, train_steps, mc_rounds, mc_freq, 'example_stochastic_lorenz_equation.csv', dtype)