class_docstr: Tests for methods for the asian pricing module.
class_docstr_tok: ["Tests","for","methods","for","the","asian","pricing","module","."]
docstr: Tests that analytic calculator and monte carlo match closely.
docstr_tok: ["Tests","that","analytic","calculator","and","monte","carlo","match","closely","."]
code: def test_analytic_pricer_vs_monte_carlo(self):
      """Tests that analytic calculator and monte carlo match closely."""
      volatilities = np.array([0.1])
      spots = np.array([650.0, 650.0, 650.0, 650.0, 650.0])
      strikes = np.array([550.0, 600.0, 650.0, 680.0, 730.0])
      expiries = np.array([1.0])
      discount_rates = np.array([0.03])
      dividend_rates = np.array([0.01])
  
      sampling_times = np.array(
          [[0.5, 0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0, 1.0]]
      )
  
      computed_prices = tff.black_scholes.asian_option_price(
          volatilities=volatilities,
          strikes=strikes,
          expiries=expiries,
          spots=spots,
          discount_rates=discount_rates,
          dividend_rates=dividend_rates,
          sampling_times=sampling_times,
      )
  
      dtype = np.float64
      num_samples = 500000
      num_timesteps = 100
  
      dt = 1.0 / num_timesteps
      sigma = tf.constant(volatilities[0], dtype=dtype)
      spot = tf.constant(spots[0], dtype=dtype)
      sampling_times = tf.constant([0.5, 1.0], dtype=dtype)
      sampling_times = tf.constant([0.5, 1.0], dtype=dtype)
  
      def set_up_pricer(expiries, watch_params=False):
          def price_asian_options(strikes, spot, sigma):
              # Define drift and volatility functions.
              def drift_fn(t, x):
                  del t, x
                  return (
                      discount_rates - dividend_rates - 0.5 * sigma**2
                  )
  
              def vol_fn(t, x):
                  del t, x
                  return tf.reshape(sigma, [1, 1])
  
              process = tff.models.GenericItoProcess(
                  dim=1,
                  drift_fn=drift_fn,
                  volatility_fn=vol_fn,
                  dtype=dtype,
              )
              log_spot = tf.math.log(tf.reduce_mean(spot))
              if watch_params:
                  watch_params_list = [sigma]
              else:
                  watch_params_list = None
              paths = process.sample_paths(
                  sampling_times,
                  num_samples=num_samples,
                  initial_state=log_spot,
                  watch_params=watch_params_list,
                  random_type=tff.math.random.RandomType.PSEUDO_ANTITHETIC,
                  seed=43,
                  time_step=dt,
              )
              prices = tf.exp(
                  -tf.expand_dims(discount_rates * expiries, axis=-1)
              ) * tf.reduce_mean(
                  tf.nn.relu(
                      tf.math.exp(tf.reduce_mean(paths, [1])) - strikes
                  ),
                  [0],
              )
              return prices
  
          return price_asian_options
  
      price_asian_options = tf.function(set_up_pricer(expiries))
      mc_prices = price_asian_options(strikes, spot, sigma)
  
      self.assertAllClose(computed_prices, mc_prices[0], 1e-2)
  
file_path: tf-quant-finance/tf_quant_finance/black_scholes/asian_prices_test.py
imports: from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  import tf_quant_finance
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 4 for calling the AsianPriceTest obj's underlying member method code with example usage: obj.test_analytic_pricer_vs_monte_carlo() and returns: price_asian_options, prices, unknown, tf