docstr: Demonstrates the instability of Hagan West for extreme cases.
docstr_tok: ["Demonstrates","the","instability","of","Hagan","West","for","extreme","cases","."]
code: def test_unstable(self, dtype):
      """Demonstrates the instability of Hagan West for extreme cases."""
      cashflows = [
          # 1 year bond with 5% three monthly coupon.
          np.array([12.5, 12.5, 12.5, 1012.5], dtype=dtype),
          # 2 year bond with 6% semi-annual coupon.
          np.array([30, 30, 30, 1030], dtype=dtype),
          # 3 year bond with 8% semi-annual coupon.
          np.array([40, 40, 40, 40, 40, 1040], dtype=dtype),
          # 4 year bond with 3% semi-annual coupon.
          np.array([15, 15, 15, 15, 15, 15, 15, 1015], dtype=dtype),
      ]
      cashflow_times = [
          np.array([0.25, 0.5, 0.75, 1.0], dtype=dtype),
          np.array([0.5, 1.0, 1.5, 2.0], dtype=dtype),
          np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0], dtype=dtype),
          np.array(
              [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0], dtype=dtype
          ),
      ]
      # Computed with discount rates of [5.0, 4.75, 4.53333333, 4.775]
      # which are 100 times the values in the previous test case.
      pvs = np.array(
          [
              11.561316110080888,
              2.6491572753698067,
              3.4340789041846866,
              1.28732090544209,
          ],
          dtype=dtype,
      )
      true_discount_rates = np.array(
          [5.0, 4.75, 4.53333333, 4.775], dtype=dtype
      )
      # Check failure with default initial rates.
      results_default = self.evaluate(
          bond_curve.bond_curve(
              cashflows,
              cashflow_times,
              pvs,
              maximum_iterations=100,
              validate_args=True,
              dtype=dtype,
          )
      )
      self.assertFalse(results_default.converged)
      self.assertTrue(results_default.failed)
      self.assertFalse(np.isnan(results_default.discount_rates[0]))
      self.assertTrue(np.isnan(results_default.discount_rates[1]))
  
      # It even fails if we underestimate the result even marginally.
      # However the behaviour is different if we start above the true values.
      # See next test.
      results_close = self.evaluate(
          bond_curve.bond_curve(
              cashflows,
              cashflow_times,
              pvs,
              initial_discount_rates=true_discount_rates * 0.9999,
              maximum_iterations=100,
          )
      )
      with self.subTest("Converged"):
          self.assertFalse(results_close.converged)
      with self.subTest("Failed"):
          self.assertTrue(results_close.failed)
      with self.subTest("DiscountRates"):
          self.assertFalse(np.isnan(results_close.discount_rates[0]))
          self.assertFalse(np.isnan(results_close.discount_rates[1]))
          self.assertTrue(np.isnan(results_close.discount_rates[2]))
  
imports: import math
  from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  from tensorflow.python.framework import test_util
  from tf_quant_finance.rates.hagan_west import bond_curve
  from tf_quant_finance.rates.hagan_west import monotone_convex
label_desc: Use image node_id 5 for calling the BondCurveTest obj's underlying member method code with example usage: obj.test_unstable(dtype) without return types