docstr: Tests that the model reproduces implied volatility smile.
docstr_tok: ["Tests","that","the","model","reproduces","implied","volatility","smile","."]
code: def test_lv_correctness(
      self,
      dim,
      risk_free_rate,
      discount_factor_fn,
      num_time_steps,
      using_market_data,
      jit_compile,
      iv_start_index=0,
      flat_iv=False,
  ):
      """Tests that the model reproduces implied volatility smile."""
      dtype = tf.float64
      num_samples = 10000
      if flat_iv:
          (
              val_date,
              expiries,
              expiry_times,
              strikes,
              iv,
              spot,
          ) = build_tensors(dim)
      else:
          tensors = build_scaled_tensors(dim)
          val_date, expiries, expiry_times, strikes, iv, spot = tensors
  
      # Handle the cases where we have constant rates.
      if discount_factor_fn is None:
          r = tf.convert_to_tensor(risk_free_rate, dtype=dtype)
          discount_factor_fn = lambda t: tf.math.exp(-r * t)
  
      if using_market_data:
          lv = LocalVolatilityModel.from_market_data(
              dim=dim,
              valuation_date=val_date,
              expiry_dates=expiries,
              strikes=strikes,
              implied_volatilities=iv,
              spot=spot,
              discount_factor_fn=discount_factor_fn,
              dividend_yield=[0.0],
              dtype=dtype,
          )
      else:
          vs = build_volatility_surface(
              val_date, expiry_times, expiries, strikes, iv, dtype=dtype
          )
          lv = LocalVolatilityModel.from_volatility_surface(
              dim=dim,
              spot=spot,
              implied_volatility_surface=vs,
              discount_factor_fn=discount_factor_fn,
              dividend_yield=[0.0],
              dtype=dtype,
          )
  
      @tf.function(jit_compile=jit_compile)
      def _get_sample_paths():
          return lv.sample_paths(
              # Our test times are the same for each dim.
              times=expiry_times[0],
              num_samples=num_samples,
              initial_state=spot,
              num_time_steps=num_time_steps,
              random_type=tff.math.random.RandomType.STATELESS_ANTITHETIC,
              seed=[1, 2],
          )
  
      paths = self.evaluate(_get_sample_paths())
      sim_iv = self.evaluate(
          tf.function(_get_all_iv)(
              dim,
              expiry_times,
              strikes,
              spot,
              paths,
              iv,
              iv_start_index,
              discount_factor_fn,
              dtype,
          )
      )
      num_times = expiry_times[0].shape[0]
      for d in range(dim):
          for i in range(num_times):
              for j in range(iv_start_index, len(iv[0][0])):
                  self.assertAllClose(
                      sim_iv[d][i][j - iv_start_index],
                      iv[d][i][j],
                      atol=0.05,
                      rtol=0.005,
                  )
  
imports: import functools
  from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  import tf_quant_finance
  from tf_quant_finance.experimental.local_volatility import local_volatility_model
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 1 for calling the LocalVolatilityTest obj's underlying member method code with example usage: obj.test_lv_correctness(dim, risk_free_rate, discount_factor_fn, num_time_steps, using_market_data, jit_compile, iv_start_index, flat_iv) and returns: lv