docstr: Tests finite difference valuation for 2-factor model.
docstr_tok: ["Tests","finite","difference","valuation","for","2-factor","model","."]
code: def test_correctness_2_factor_fd(
      self, time_step, num_time_steps, use_xla
  ):
      """Tests finite difference valuation for 2-factor model."""
      # 1y x 1y swaption with quarterly payments.
      dtype = tf.float64
      error_tol = 1e-3
  
      expiries = np.array([1.002739726])
      fixed_leg_payment_times = np.array(
          [1.249315068, 1.498630137, 1.750684932, 2.002739726]
      )
      fixed_leg_daycount_fractions = np.array(
          [0.2465753425, 0.2493150685, 0.2520547945, 0.2520547945]
      )
      fixed_leg_coupon = 0.011 * np.ones_like(fixed_leg_payment_times)
      zero_rate_fn = lambda x: 0.01 * tf.ones_like(x, dtype=dtype)
  
      mean_reversion = [0.03, 0.15]
      volatility = [0.01, 0.015]
  
      def _fn():
          price = tff.models.hjm.swaption_price(
              expiries=expiries,
              fixed_leg_payment_times=fixed_leg_payment_times,
              fixed_leg_daycount_fractions=fixed_leg_daycount_fractions,
              fixed_leg_coupon=fixed_leg_coupon,
              reference_rate_fn=zero_rate_fn,
              notional=100.0,
              num_hjm_factors=2,
              mean_reversion=mean_reversion,
              volatility=volatility,
              valuation_method=tff.models.ValuationMethod.FINITE_DIFFERENCE,
              time_step_finite_difference=time_step,
              num_time_steps_finite_difference=num_time_steps,
              num_grid_points_finite_difference=251,
              time_step=0.1,
              dtype=dtype,
          )
          return price
  
      if use_xla:
          price = self.evaluate(tf.function(_fn, jit_compile=True)())
      else:
          price = self.evaluate(_fn())
      quantlib_price = 0.5900860719515227
      self.assertAllClose(
          price, [quantlib_price], rtol=error_tol, atol=error_tol
      )
  
file_path: tf-quant-finance/tf_quant_finance/models/hjm/swaption_pricing_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 10 for calling the HJMSwaptionTest obj's underlying member method code with example usage: obj.test_correctness_2_factor_fd(time_step, num_time_steps, use_xla) and returns: price