docstr: Test calibration function with dynamically shaped inputs.
docstr_tok: ["Test","calibration","function","with","dynamically","shaped","inputs","."]
code: def test_dynamic_shapes(self):
      """Test calibration function with dynamically shaped inputs."""
      dtype = np.float64
  
      observed_prices = np.array(
          [
              [
                  29.45783648,
                  24.15306768,
                  19.73346361,
                  16.08792179,
                  13.10198113,
              ],
              [
                  16.02509124,
                  21.51061818,
                  27.61195796,
                  34.24527161,
                  41.33562669,
              ],
          ],
          dtype=dtype,
      )
  
      strikes = np.array(
          [
              [80.0, 90.0, 100.0, 110.0, 120.0],
              [80.0, 90.0, 100.0, 110.0, 120.0],
          ],
          dtype=dtype,
      )
  
      expiries = np.array([[0.5], [1.0]], dtype=dtype)
      forwards = [[100.0], [100.0]]
      is_call_options = np.array([[True], [False]])
  
      volvol = np.array([0.75], dtype=dtype)
      initial_variance = np.array([0.25], dtype=dtype)
      mean_reversion = np.array([0.4], dtype=dtype)
      theta = np.array([0.9], dtype=dtype)
  
      @tf.function(
          input_signature=[
              tf.TensorSpec([None, None], dtype=dtype),
              tf.TensorSpec([None, None], dtype=dtype),
              tf.TensorSpec([None, None], dtype=dtype),
              tf.TensorSpec([None, None], dtype=dtype),
              tf.TensorSpec([None, None], dtype=tf.bool),
              tf.TensorSpec([None], dtype=dtype),
              tf.TensorSpec([None], dtype=dtype),
              tf.TensorSpec([None], dtype=dtype),
              tf.TensorSpec([None], dtype=dtype),
          ]
      )
      def fn(
          observed_prices,
          strikes,
          expiries,
          forwards,
          is_call_options,
          initial_variance,
          mean_reversion,
          theta,
          volvol,
      ):
          models, is_converged, _ = tff.models.heston.calibration(
              prices=observed_prices,
              strikes=strikes,
              expiries=expiries,
              forwards=forwards,
              is_call_options=is_call_options,
              mean_reversion=mean_reversion,
              initial_variance=initial_variance,
              theta=theta,
              volvol=volvol,
              rho=np.array(-0.5, dtype=dtype),
              optimizer_fn=tff.math.optimizer.bfgs_minimize,
              maximum_iterations=100,
          )
          return models, is_converged
  
      models, is_converged = fn(
          observed_prices,
          strikes,
          expiries,
          forwards,
          is_call_options,
          initial_variance,
          mean_reversion,
          theta,
          volvol,
      )
      [
          calibrated_initial_variance,
          calibrated_mean_reversion,
          calibrated_volvol,
          calibrated_rho,
          calibrated_theta,
          is_converged,
      ] = self.evaluate(
          [
              models.initial_variance[:, tf.newaxis],
              models.mean_reversion[:, tf.newaxis],
              models.volvol[:, tf.newaxis],
              models.rho[:, tf.newaxis],
              models.theta[:, tf.newaxis],
              is_converged,
          ]
      )
      with self.subTest("AllConverged"):
          self.assertTrue(all(is_converged))
      calibrated_prices = (
          tff.models.heston.approximations.european_option_price(
              variances=calibrated_initial_variance,
              strikes=strikes,
              expiries=expiries,
              forwards=forwards,
              is_call_options=is_call_options,
              mean_reversion=calibrated_mean_reversion,
              theta=calibrated_theta,
              volvol=calibrated_volvol,
              rho=calibrated_rho,
              dtype=dtype,
          )
      )
      with self.subTest("PricesRecovered"):
          self.assertAllClose(
              calibrated_prices, observed_prices, atol=1e-1, rtol=1e-3
          )
  
file_path: tf-quant-finance/tf_quant_finance/models/heston/approximations/calibration_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 1 for calling the CalibrationTest obj's underlying member method code with example usage: obj.test_dynamic_shapes() and returns: models,  is_converged