docstr: Test the example in the docstring.
  
  In this example, we are calibrating a SABR model using the lognormal
  volatility approximation for implied volatility, and we explicitly fix the
  beta's ourselves.
docstr_tok: ["Test","the","example","in","the","docstring",".","In","this","example",",","we","are","calibrating","a","SABR","model","using","the","lognormal","volatility","approximation","for","implied","volatility",",","and","we","explicitly","fix","the","beta","'s","ourselves","."]
code: def test_calibration_docstring(self):
      """Test the example in the docstring.
  
      In this example, we are calibrating a SABR model using the lognormal
      volatility approximation for implied volatility, and we explicitly fix the
      beta's ourselves.
      """
      dtype = np.float64
  
      observed_prices = np.array(
          [
              [
                  20.09689284,
                  10.91953054,
                  4.25012702,
                  1.11561839,
                  0.20815853,
              ],
              [
                  3.34813209,
                  6.03578711,
                  10.2874194,
                  16.26824328,
                  23.73850935,
              ],
          ],
          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
      is_call_options = np.array([[True], [False]])
  
      beta = np.array([0.5, 0.5], dtype=dtype)
  
      models, is_converged, _ = tff.models.sabr.calibration(
          prices=observed_prices,
          strikes=strikes,
          expiries=expiries,
          forwards=forwards,
          is_call_options=is_call_options,
          beta=beta,
          calibrate_beta=False,
          volvol=np.array([1.0, 1.0], dtype=dtype),
          volvol_lower_bound=0.0,
          volvol_upper_bound=10.0,
          rho=np.array([0.0, 0.0], dtype=dtype),
          rho_lower_bound=-0.75,
          rho_upper_bound=0.75,
          maximum_iterations=1000,
      )
  
      [
          calibrated_alpha,
          calibrated_beta,
          calibrated_volvol,
          calibrated_rho,
          is_converged,
      ] = self.evaluate(
          [
              models.alpha,
              models.beta,
              models.volvol,
              models.rho,
              is_converged,
          ]
      )
  
      with self.subTest("AllConverged"):
          self.assertTrue(all(is_converged))
      with self.subTest("AlphaRecovered"):
          self.assertAllClose(
              calibrated_alpha, [1.5, 2.5], atol=2e-3, rtol=2e-3
          )
      with self.subTest("BetaRecovered"):
          self.assertAllClose(
              calibrated_beta, [0.5, 0.5], atol=2e-3, rtol=2e-3
          )
      with self.subTest("VolVolRecovered"):
          self.assertAllClose(
              calibrated_volvol, [0.33, 0.66], atol=2e-2, rtol=5e-2
          )
      with self.subTest("RhoRecovered"):
          self.assertAllClose(
              calibrated_rho, [0.1, -0.1], atol=1e-2, rtol=5e-2
          )
  
file_path: tf-quant-finance/tf_quant_finance/models/sabr/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_calibration_docstring() without return types